File: /usr/src/linux/drivers/sbus/audio/cs4231.c

1     /* $Id: cs4231.c,v 1.46 2001/05/21 01:25:22 davem Exp $
2      * drivers/sbus/audio/cs4231.c
3      *
4      * Copyright 1996, 1997, 1998, 1999 Derrick J Brashear (shadow@andrew.cmu.edu)
5      * The 4231/ebus support was written by David Miller, who didn't bother
6      * crediting himself here, so I will.
7      *
8      * Based on the AMD7930 driver:
9      * Copyright 1996 Thomas K. Dyas (tdyas@noc.rutgers.edu)
10      *
11      * This is the lowlevel driver for the CS4231 audio chip found on some
12      * sun4m and sun4u machines.
13      * 
14      * This was culled from the Crystal docs on the 4231a, and the addendum they
15      * faxed me on the 4231.
16      * The APC DMA controller support unfortunately is not documented. Thanks, Sun.
17      */
18     
19     #include <linux/config.h>
20     #include <linux/module.h>
21     #include <linux/kernel.h>
22     #include <linux/sched.h>
23     #include <linux/errno.h>
24     #include <linux/interrupt.h>
25     #include <linux/slab.h>
26     #include <linux/init.h>
27     #include <linux/delay.h>
28     #include <linux/soundcard.h>
29     #include <linux/version.h>
30     #include <linux/ioport.h>
31     #include <asm/openprom.h>
32     #include <asm/oplib.h>
33     #include <asm/system.h>
34     #include <asm/irq.h>
35     #include <asm/io.h>
36     #include <asm/pgtable.h>
37     #include <asm/sbus.h>
38     #ifdef CONFIG_PCI
39     #define EB4231_SUPPORT
40     #include <asm/ebus.h>
41     #include <asm/pbm.h>
42     #endif
43     
44     #include <asm/audioio.h>
45     #include "cs4231.h"
46     
47     #undef __CS4231_DEBUG
48     #undef __CS4231_TRACE
49     #define __CS4231_ERROR
50     #ifdef __CS4231_ERROR
51     #define eprintk(x) printk x
52     #else
53     #define eprintk(x)
54     #endif
55     #ifdef __CS4231_TRACE
56     #define tprintk(x) printk x
57     #else
58     #define tprintk(x)
59     #endif
60     #ifdef __CS4231_DEBUG
61     #define dprintk(x) printk x
62     #else
63     #define dprintk(x)
64     #endif
65     
66     #define MAX_DRIVERS 1
67     static struct sparcaudio_driver drivers[MAX_DRIVERS];
68     static int num_drivers;
69     
70     static int cs4231_record_gain(struct sparcaudio_driver *drv, int value, 
71                                   unsigned char balance);
72     static int cs4231_play_gain(struct sparcaudio_driver *drv, int value, 
73                                 unsigned char balance);
74     static void cs4231_ready(struct sparcaudio_driver *drv);
75     static void cs4231_playintr(struct sparcaudio_driver *drv, int);
76     static int cs4231_recintr(struct sparcaudio_driver *drv);
77     static int cs4231_output_muted(struct sparcaudio_driver *drv, int value);
78     static void cs4231_pollinput(struct sparcaudio_driver *drv);
79     static int cs4231_length_to_samplecount(struct audio_prinfo *thisdir, 
80                                             unsigned int length);
81     static void cs4231_getsamplecount(struct sparcaudio_driver *drv, 
82                                       unsigned int length, unsigned int value);
83     #ifdef EB4231_SUPPORT
84     static void eb4231_pollinput(struct sparcaudio_driver *drv);
85     #endif
86     
87     /* Serveral shorthands save typing... */
88     #define CHIP_READY() \
89     do { udelay(100); cs4231_ready(drv); udelay(1000); } while(0)
90     #define WRITE_IAR(__VAL) \
91     	CS4231_WRITE8(cs4231_chip, cs4231_chip->regs + IAR, __VAL)
92     #define WRITE_IDR(__VAL) \
93     	CS4231_WRITE8(cs4231_chip, cs4231_chip->regs + IDR, __VAL)
94     #define READ_IAR() \
95     	CS4231_READ8(cs4231_chip, cs4231_chip->regs + IAR)
96     #define READ_IDR() \
97     	CS4231_READ8(cs4231_chip, cs4231_chip->regs + IDR)
98     
99     /* Enable cs4231 interrupts atomically. */
100     static void cs4231_enable_interrupts(struct sparcaudio_driver *drv)
101     {
102             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
103             unsigned long flags;
104     
105             tprintk(("enabling interrupts\n"));
106             save_flags(flags);
107             cli();
108             if ((cs4231_chip->status & CS_STATUS_INTS_ON) == 0) {
109                     WRITE_IAR(0xa);
110                     WRITE_IDR(INTR_ON);
111                     cs4231_chip->status |= CS_STATUS_INTS_ON;
112             }
113             restore_flags(flags);
114     }
115     
116     /* Disable cs4231 interrupts atomically. */
117     static void cs4231_disable_interrupts(struct sparcaudio_driver *drv)
118     {
119             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
120             unsigned long flags;
121     
122             tprintk(("disabling interrupts\n"));
123             save_flags(flags);
124             cli();
125             if ((cs4231_chip->status & CS_STATUS_INTS_ON) != 0) {
126                     WRITE_IAR(0xa);
127                     WRITE_IDR(INTR_OFF);
128                     cs4231_chip->status &= ~CS_STATUS_INTS_ON;
129             }
130             restore_flags(flags);
131     }
132     
133     static void cs4231_enable_play(struct sparcaudio_driver *drv)
134     {
135             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
136             unsigned long flags;
137     
138             tprintk(("enabling play\n"));
139             save_flags(flags);
140             cli();
141             WRITE_IAR(0x9);
142             WRITE_IDR(READ_IDR() | PEN_ENABLE);
143             restore_flags(flags);
144     }
145     
146     static void cs4231_disable_play(struct sparcaudio_driver *drv)
147     {
148             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
149             unsigned long flags;
150     
151             tprintk(("disabling play\n"));
152             save_flags(flags);
153             cli();
154             WRITE_IAR(0x9);
155             WRITE_IDR(READ_IDR() & PEN_DISABLE);
156             restore_flags(flags);
157     }
158     
159     static void cs4231_enable_rec(struct sparcaudio_driver *drv)
160     {
161             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
162             unsigned long flags;
163     
164             tprintk(("enabling rec\n"));
165             save_flags(flags);
166             cli();
167             WRITE_IAR(0x9);
168             WRITE_IDR(READ_IDR() | CEN_ENABLE);
169             restore_flags(flags);
170     }
171     
172     static void cs4231_disable_rec(struct sparcaudio_driver *drv)
173     {
174             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
175             unsigned long flags;
176     
177             tprintk(("disabling rec\n"));
178             save_flags(flags);
179             cli();
180             WRITE_IAR(0x9);
181             WRITE_IDR(READ_IDR() & CEN_DISABLE);
182             restore_flags(flags);
183     }
184     
185     static struct cs4231_rates {
186             int speed, bits;
187     } cs4231_rate_table[] = {
188             { 5512, CS4231_DFR_5512 },
189             { 6615, CS4231_DFR_6615 },
190             { 8000, CS4231_DFR_8000 },
191             { 9600, CS4231_DFR_9600 },
192             { 11025, CS4231_DFR_11025 },
193             { 16000, CS4231_DFR_16000 },
194             { 18900, CS4231_DFR_18900 },
195             { 22050, CS4231_DFR_22050 },
196             { 27429, CS4231_DFR_27429 },
197             { 32000, CS4231_DFR_32000 },
198             { 33075, CS4231_DFR_33075 },
199             { 37800, CS4231_DFR_37800 },
200             { 44100, CS4231_DFR_44100 },
201             { 48000, CS4231_DFR_48000 }
202     };
203     
204     #define NUM_RATES	(sizeof(cs4231_rate_table) / sizeof(struct cs4231_rates))
205     
206     static int cs4231_rate_to_bits(struct sparcaudio_driver *drv, int *value)
207     {
208             struct cs4231_rates *p = &cs4231_rate_table[0];
209             int i, wanted = *value;
210     
211             /* We try to be nice and approximate what the user asks for. */
212             if (wanted < 5512)
213                     wanted = 5512;
214             if (wanted > 48000)
215                     wanted = 48000;
216     
217             for (i = 0; i < NUM_RATES; i++, p++) {
218                     /* Exact match? */
219                     if (wanted == p->speed)
220                             break;
221     
222                     /* If we're inbetween two entries, and neither is exact,
223                      * pick the closest one.
224                      */
225                     if (wanted == p[1].speed)
226                             continue;
227                     if (wanted > p->speed && wanted < p[1].speed) {
228                             int diff1, diff2;
229     
230                             diff1 = wanted - p->speed;
231                             diff2 = p[1].speed - wanted;
232                             if (diff2 < diff1)
233                                     p++;
234                             break;
235                     }
236             }
237             *value = p->speed;
238             return p->bits;
239     }
240     
241     static int cs4231_encoding_to_bits(struct sparcaudio_driver *drv, int value)
242     {
243             int set_bits;
244       
245             switch (value) {
246             case AUDIO_ENCODING_ULAW:
247                     set_bits = CS4231_DFR_ULAW;
248                     break;
249             case AUDIO_ENCODING_ALAW:
250                     set_bits = CS4231_DFR_ALAW;
251                     break;
252             case AUDIO_ENCODING_DVI:
253                     set_bits = CS4231_DFR_ADPCM;
254                     break;
255             case AUDIO_ENCODING_LINEARLE:
256                     set_bits = CS4231_DFR_LINEARLE;
257                     break;
258             case AUDIO_ENCODING_LINEAR:
259                     set_bits = CS4231_DFR_LINEARBE;
260                     break;
261             case AUDIO_ENCODING_LINEAR8:
262                     set_bits = CS4231_DFR_LINEAR8;
263                     break;
264             default:
265                     set_bits = -EINVAL;
266                     break;
267             };
268       
269             return set_bits;
270     }
271     
272     static int cs4231_set_output_encoding(struct sparcaudio_driver *drv, int value)
273     {
274             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
275             int tmp_bits, set_bits;
276     
277             tprintk(("output encoding %d\n", value));
278             if (value != 0) {
279                     set_bits = cs4231_encoding_to_bits(drv, value);
280                     if (set_bits >= 0) {
281                             READ_IDR();
282                             READ_IDR();
283                             WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x8);
284                             tmp_bits = READ_IDR();
285                             WRITE_IDR(CHANGE_ENCODING(tmp_bits, set_bits));
286                             READ_IDR();
287                             READ_IDR();
288                             CHIP_READY();
289                             cs4231_chip->perchip_info.play.encoding = value;
290                             return 0;
291                     }
292             }
293             dprintk(("output enc failed\n"));
294             return -EINVAL;
295     }
296     
297     static int cs4231_get_output_encoding(struct sparcaudio_driver *drv)
298     {
299           struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
300           return cs4231_chip->perchip_info.play.encoding;
301     }
302     
303     static int cs4231_set_input_encoding(struct sparcaudio_driver *drv, int value)
304     {
305             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
306             int tmp_bits, set_bits;
307     
308             tprintk(("input encoding %d\n", value));
309             if (value != 0) {
310                     set_bits = cs4231_encoding_to_bits(drv, value);
311                     if (set_bits >= 0) {
312                             READ_IDR();
313                             READ_IDR();
314                             WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x1c);
315                             tmp_bits = READ_IDR();
316                             WRITE_IDR(CHANGE_ENCODING(tmp_bits, set_bits));
317                             READ_IDR();
318                             READ_IDR();
319                             CHIP_READY();
320     
321                             cs4231_chip->perchip_info.record.encoding = value;
322                             return 0;
323                     }
324             }
325             dprintk(("input enc failed\n"));
326             return -EINVAL;
327     }
328     
329     static int cs4231_get_input_encoding(struct sparcaudio_driver *drv)
330     {
331           struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
332           return cs4231_chip->perchip_info.record.encoding;
333     }
334     
335     static int cs4231_set_output_rate(struct sparcaudio_driver *drv, int value)
336     {
337             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
338             int tmp_bits, set_bits;
339     
340             tprintk(("output rate %d\n", value));
341             if (value != 0) {
342                     set_bits = cs4231_rate_to_bits(drv, &value);
343                     if (set_bits >= 0) {
344                             READ_IDR();
345                             READ_IDR();
346                             WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x8);
347                             tmp_bits = READ_IDR();
348                             WRITE_IDR(CHANGE_DFR(tmp_bits, set_bits));
349                             READ_IDR();
350                             READ_IDR();
351                             CHIP_READY();
352     
353                             cs4231_chip->perchip_info.play.sample_rate = value;
354                             tprintk(("tmp_bits[%02x] set_bits[%02x] CHANGE_DFR[%02x]\n",
355                                      tmp_bits, set_bits, CHANGE_DFR(tmp_bits, set_bits)));
356                             return 0;
357                     }
358             }
359             dprintk(("output rate failed\n"));
360             return -EINVAL;
361     }
362     
363     static int cs4231_get_output_rate(struct sparcaudio_driver *drv)
364     {
365           struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
366           return cs4231_chip->perchip_info.play.sample_rate;
367     }
368     
369     static int cs4231_set_input_rate(struct sparcaudio_driver *drv, int value)
370     {
371             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
372             int tmp_bits, set_bits;
373     
374             tprintk(("input rate %d\n", value));
375             if (value != 0) {
376                     set_bits = cs4231_rate_to_bits(drv, &value);
377                     if (set_bits >= 0) {
378                             READ_IDR();
379                             READ_IDR();
380                             WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x1c);
381                             tmp_bits = READ_IDR();
382                             WRITE_IDR(CHANGE_DFR(tmp_bits, set_bits));
383                             READ_IDR();
384                             READ_IDR();
385                             CHIP_READY();
386     
387                             cs4231_chip->perchip_info.record.sample_rate = value;
388                             return 0;
389                     }
390             }
391             dprintk(("input rate failed\n"));
392             return -EINVAL;
393     }
394     
395     static int cs4231_get_input_rate(struct sparcaudio_driver *drv)
396     {
397           struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
398           return cs4231_chip->perchip_info.record.sample_rate;
399     }
400     
401     /* Generically we support 4 channels. This hardware does 2 */
402     static int cs4231_set_input_channels(struct sparcaudio_driver *drv, int value)
403     {
404             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
405             int tmp_bits;
406     
407             tprintk(("input channels %d\n", value));
408             WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x1c);
409             tmp_bits = READ_IDR();
410             switch (value) {
411             case 1:
412                     WRITE_IDR(CS4231_MONO_ON(tmp_bits));
413                     break;
414             case 2:
415                     WRITE_IDR(CS4231_STEREO_ON(tmp_bits));
416                     break;
417             default:
418                     dprintk(("input chan failed\n"));
419                     return -EINVAL;
420             };
421             CHIP_READY();
422     
423             cs4231_chip->perchip_info.record.channels = value;
424             return 0;
425     }
426     
427     static int cs4231_get_input_channels(struct sparcaudio_driver *drv)
428     {
429           struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
430           return cs4231_chip->perchip_info.record.channels;
431     }
432     
433     /* Generically we support 4 channels. This hardware does 2 */
434     static int cs4231_set_output_channels(struct sparcaudio_driver *drv, int value)
435     {
436             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
437             int tmp_bits;
438     
439             tprintk(("output channels %d\n", value));
440             WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x8);
441             tmp_bits = READ_IDR();
442             switch (value) {
443             case 1:
444                     WRITE_IDR(CS4231_MONO_ON(tmp_bits));
445                     break;
446             case 2:
447                     WRITE_IDR(CS4231_STEREO_ON(tmp_bits));
448                     break;
449             default:
450                     dprintk(("output chan failed\n"));
451                     return -EINVAL;
452             };
453             CHIP_READY();
454         
455     	cs4231_chip->perchip_info.play.channels = value;
456             return 0;
457     }
458     
459     static int cs4231_get_output_channels(struct sparcaudio_driver *drv)
460     {
461           struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
462           return cs4231_chip->perchip_info.play.channels;
463     }
464     
465     static int cs4231_get_input_precision(struct sparcaudio_driver *drv)
466     {
467           struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
468           return cs4231_chip->perchip_info.record.precision;
469     }
470     
471     static int cs4231_get_output_precision(struct sparcaudio_driver *drv)
472     {
473           struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
474           return cs4231_chip->perchip_info.play.precision;
475     }
476     
477     static int cs4231_set_input_precision(struct sparcaudio_driver *drv, int val)
478     {
479           struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
480     
481           cs4231_chip->perchip_info.record.precision = val;
482     
483           return cs4231_chip->perchip_info.record.precision;
484     }
485     
486     static int cs4231_set_output_precision(struct sparcaudio_driver *drv, int val)
487     {
488           struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private; 
489     
490           cs4231_chip->perchip_info.play.precision = val;
491     
492           return cs4231_chip->perchip_info.play.precision;
493     }
494     
495     /* Wait until the auto calibration process has finished */
496     static void cs4231_ready(struct sparcaudio_driver *drv) 
497     {
498             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
499             unsigned int x;
500     
501             WRITE_IAR(IAR_AUTOCAL_END);
502             x = 0;
503             do {
504                     if (READ_IDR() != IAR_NOT_READY)
505                             break;
506                     x++;
507             } while (x <= CS_TIMEOUT);
508     
509             WRITE_IAR(0x0b);
510             x = 0;
511             do {
512                     if (READ_IDR() != AUTOCAL_IN_PROGRESS)
513                             break;
514                     x++;
515             } while (x <= CS_TIMEOUT);
516     }
517     
518     /* Set output mute */
519     static int cs4231_output_muted(struct sparcaudio_driver *drv, int value)
520     {
521             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
522             tprintk(("in cs4231_output_muted: %d\n", value));
523             if (!value) {
524                     WRITE_IAR(0x7);
525                     WRITE_IDR(READ_IDR() & OUTCR_UNMUTE);
526                     WRITE_IAR(0x6);
527                     WRITE_IDR(READ_IDR() & OUTCR_UNMUTE);
528                     cs4231_chip->perchip_info.output_muted = 0;
529             } else {
530                     WRITE_IAR(0x7);
531                     WRITE_IDR(READ_IDR() | OUTCR_MUTE);
532                     WRITE_IAR(0x6);
533                     WRITE_IDR(READ_IDR() | OUTCR_MUTE);
534                     cs4231_chip->perchip_info.output_muted = 1;
535             }
536             return 0;
537     }
538     
539     static int cs4231_get_output_muted(struct sparcaudio_driver *drv)
540     {
541             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
542             return cs4231_chip->perchip_info.output_muted;
543     }
544     
545     static int cs4231_get_formats(struct sparcaudio_driver *drv)
546     {
547             return (AFMT_MU_LAW | AFMT_A_LAW |
548                     AFMT_U8 | AFMT_IMA_ADPCM | 
549                     AFMT_S16_LE | AFMT_S16_BE);
550     }
551     
552     static int cs4231_get_output_ports(struct sparcaudio_driver *drv)
553     {
554             return (AUDIO_LINE_OUT | AUDIO_SPEAKER | AUDIO_HEADPHONE);
555     }
556     
557     static int cs4231_get_input_ports(struct sparcaudio_driver *drv)
558     {
559             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
560     
561             /* This apparently applies only to APC ultras, not ebus ultras */
562             if (cs4231_chip->status & CS_STATUS_IS_ULTRA)
563                     return (AUDIO_LINE_IN | AUDIO_MICROPHONE | AUDIO_ANALOG_LOOPBACK);
564             else
565                     return (AUDIO_INTERNAL_CD_IN | AUDIO_LINE_IN |
566                             AUDIO_MICROPHONE | AUDIO_ANALOG_LOOPBACK);
567     }
568     
569     /* Set chip "output" port */
570     static int cs4231_set_output_port(struct sparcaudio_driver *drv, int value)
571     {
572             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
573             int retval = 0;
574     
575             tprintk(("output port: %d\n", value));
576             /* Aaaaaah! It's all coming so fast! Turn it all off, then selectively
577              * enable things.
578              */
579             WRITE_IAR(0x1a);
580             WRITE_IDR(READ_IDR() | MONO_IOCR_MUTE);
581             WRITE_IAR(0x0a);
582             WRITE_IDR(READ_IDR() | PINCR_LINE_MUTE);
583             WRITE_IDR(READ_IDR() | PINCR_HDPH_MUTE);
584     
585             if (value & AUDIO_SPEAKER) {
586                     WRITE_IAR(0x1a);
587                     WRITE_IDR(READ_IDR() & ~MONO_IOCR_MUTE);
588                     retval |= AUDIO_SPEAKER;
589             }
590     
591             if (value & AUDIO_HEADPHONE) {
592                     WRITE_IAR(0x0a);
593                     WRITE_IDR(READ_IDR() & ~PINCR_HDPH_MUTE);
594                     retval |= AUDIO_HEADPHONE;
595             }
596     
597             if (value & AUDIO_LINE_OUT) {
598                     WRITE_IAR(0x0a);
599                     WRITE_IDR(READ_IDR() & ~PINCR_LINE_MUTE);
600                     retval |= AUDIO_LINE_OUT;
601             }
602       
603             cs4231_chip->perchip_info.play.port = retval;
604     
605             return (retval);
606     }
607     
608     static int cs4231_get_output_port(struct sparcaudio_driver *drv)
609     {
610             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
611             return cs4231_chip->perchip_info.play.port;
612     }
613     
614     /* Set chip "input" port */
615     static int cs4231_set_input_port(struct sparcaudio_driver *drv, int value)
616     {
617             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
618             int retval = 0;
619     
620             tprintk(("input port: %d\n", value));
621     
622             /* You can have one and only one. This is probably wrong, but
623              * appears to be how SunOS is doing it. Should be able to mix.
624              * More work to be done. CD input mixable, analog loopback may be.
625              */
626     
627             /* Ultra systems do not support AUDIO_INTERNAL_CD_IN */
628             /* This apparently applies only to APC ultras, not ebus ultras */
629             if (!(cs4231_chip->status & CS_STATUS_IS_ULTRA)) {
630                     if (value & AUDIO_INTERNAL_CD_IN) {
631                             WRITE_IAR(0x1);
632                             WRITE_IDR(CDROM_ENABLE(READ_IDR()));
633                             WRITE_IAR(0x0);
634                             WRITE_IDR(CDROM_ENABLE(READ_IDR()));
635                             retval = AUDIO_INTERNAL_CD_IN;
636                     }
637             }
638             if ((value & AUDIO_LINE_IN)) {
639                     WRITE_IAR(0x1);
640                     WRITE_IDR(LINE_ENABLE(READ_IDR()));
641                     WRITE_IAR(0x0);
642                     WRITE_IDR(LINE_ENABLE(READ_IDR()));
643                     retval = AUDIO_LINE_IN;
644             } else if (value & AUDIO_MICROPHONE) {
645                     WRITE_IAR(0x1);
646                     WRITE_IDR(MIC_ENABLE(READ_IDR()));
647                     WRITE_IAR(0x0);
648                     WRITE_IDR(MIC_ENABLE(READ_IDR()));
649                     retval = AUDIO_MICROPHONE;
650             } else if (value & AUDIO_ANALOG_LOOPBACK) {
651                     WRITE_IAR(0x1);
652                     WRITE_IDR(OUTPUTLOOP_ENABLE(READ_IDR()));
653                     WRITE_IAR(0x0);
654                     WRITE_IDR(OUTPUTLOOP_ENABLE(READ_IDR()));
655                     retval = AUDIO_ANALOG_LOOPBACK;
656             }
657     
658             cs4231_chip->perchip_info.record.port = retval;
659     
660             return retval;
661     }
662     
663     static int cs4231_get_input_port(struct sparcaudio_driver *drv)
664     {
665             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
666             return cs4231_chip->perchip_info.record.port;
667     }
668     
669     /* Set chip "monitor" gain */
670     static int cs4231_set_monitor_volume(struct sparcaudio_driver *drv, int value)
671     {
672             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
673             int a = 0;
674     
675             tprintk(("monitor gain: %d\n", value));
676     
677             /* This interpolation really sucks. The question is, be compatible 
678              * with ScumOS/Sloaris or not?
679              */
680             a = CS4231_MON_MAX_ATEN - (value * (CS4231_MON_MAX_ATEN + 1) / 
681                                        (AUDIO_MAX_GAIN + 1));
682     
683             WRITE_IAR(0x0d);
684             if (a >= CS4231_MON_MAX_ATEN) 
685                     WRITE_IDR(LOOPB_OFF);
686             else 
687                     WRITE_IDR((a << 2) | LOOPB_ON);
688     
689             if (value == AUDIO_MAX_GAIN) 
690                     cs4231_chip->perchip_info.monitor_gain = AUDIO_MAX_GAIN;
691             else 
692                     cs4231_chip->perchip_info.monitor_gain =
693                             ((CS4231_MAX_DEV_ATEN - a) * 
694                              (AUDIO_MAX_GAIN + 1) / 
695                              (CS4231_MAX_DEV_ATEN + 1));
696     
697             return 0;
698     }
699     
700     static int cs4231_get_monitor_volume(struct sparcaudio_driver *drv)
701     {
702             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
703     
704             return (int) cs4231_chip->perchip_info.monitor_gain;
705     }
706     
707     static int cs4231_get_output_error(struct sparcaudio_driver *drv)
708     {
709             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
710     
711             return (int) cs4231_chip->perchip_info.play.error;
712     }
713     
714     static int cs4231_get_input_error(struct sparcaudio_driver *drv)
715     {
716             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
717     
718             return (int) cs4231_chip->perchip_info.record.error;
719     }
720     
721     #ifdef EB4231_SUPPORT
722     static int eb4231_get_output_samples(struct sparcaudio_driver *drv)
723     {
724             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
725             u32 dbcr = readl(cs4231_chip->eb2p + EBDMA_COUNT);
726             int count =
727                     cs4231_length_to_samplecount(&cs4231_chip->perchip_info.play, dbcr);
728     
729             return (cs4231_chip->perchip_info.play.samples - 
730                     ((count > cs4231_chip->perchip_info.play.samples) 
731                      ? 0 : count));
732     }
733     
734     static int eb4231_get_input_samples(struct sparcaudio_driver *drv)
735     {
736             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
737             u32 dbcr = readl(cs4231_chip->eb2c + EBDMA_COUNT);
738             int count =
739                     cs4231_length_to_samplecount(&cs4231_chip->perchip_info.record, dbcr);
740     
741             return (cs4231_chip->perchip_info.record.samples - 
742                     ((count > cs4231_chip->perchip_info.record.samples) ?
743                     0 : count));
744     }
745     #endif
746     
747     static int cs4231_get_output_samples(struct sparcaudio_driver *drv)
748     {
749             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
750             u32 dmapc = sbus_readl(cs4231_chip->regs + APCPC);
751             int count = 
752               cs4231_length_to_samplecount(&cs4231_chip->perchip_info.play, dmapc);
753     
754             return (cs4231_chip->perchip_info.play.samples - 
755                     ((count > cs4231_chip->perchip_info.play.samples) 
756                      ? 0 : count));
757     }
758     
759     static int cs4231_get_input_samples(struct sparcaudio_driver *drv)
760     {
761             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
762             u32 dmacc = sbus_readl(cs4231_chip->regs + APCCC);
763             int count = 
764               cs4231_length_to_samplecount(&cs4231_chip->perchip_info.record, dmacc);
765     
766             return (cs4231_chip->perchip_info.record.samples - 
767                     ((count > cs4231_chip->perchip_info.record.samples) ?
768                     0 : count));
769     }
770     
771     static int cs4231_get_output_pause(struct sparcaudio_driver *drv)
772     {
773             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
774     
775             return (int) cs4231_chip->perchip_info.play.pause;
776     }
777     
778     static int cs4231_get_input_pause(struct sparcaudio_driver *drv)
779     {
780             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
781     
782             return (int) cs4231_chip->perchip_info.record.pause;
783     }
784     
785     /* But for play/record we have these cheesy jacket routines because of 
786      * how this crap gets set.
787      */
788     static int cs4231_set_input_volume(struct sparcaudio_driver *drv, int value)
789     {
790             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
791     
792     	cs4231_record_gain(drv, value, 
793                                cs4231_chip->perchip_info.record.balance);
794     	
795             return 0;
796     }
797     
798     static int cs4231_get_input_volume(struct sparcaudio_driver *drv)
799     {
800             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
801     
802             return (int) cs4231_chip->perchip_info.record.gain;
803     }
804     
805     static int cs4231_set_output_volume(struct sparcaudio_driver *drv, int value)
806     {
807             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
808     
809     	cs4231_play_gain(drv, value, cs4231_chip->perchip_info.play.balance);
810     	
811             return 0;
812     }
813     
814     static int cs4231_get_output_volume(struct sparcaudio_driver *drv)
815     {
816             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
817     
818             return cs4231_chip->perchip_info.play.gain;
819     }
820     
821     /* Likewise for balance */
822     static int cs4231_set_input_balance(struct sparcaudio_driver *drv, int value)
823     {
824             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
825     
826     	cs4231_chip->perchip_info.record.balance = value;
827     	cs4231_record_gain(drv, cs4231_chip->perchip_info.record.gain, 
828                                cs4231_chip->perchip_info.record.balance);
829     	
830             return 0;
831     }
832     
833     static int cs4231_get_input_balance(struct sparcaudio_driver *drv)
834     {
835             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
836     
837             return (int) cs4231_chip->perchip_info.record.balance;
838     }
839     
840     static int cs4231_set_output_balance(struct sparcaudio_driver *drv, int value)
841     {
842             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
843     
844     	cs4231_chip->perchip_info.play.balance = value;
845     	cs4231_play_gain(drv, cs4231_chip->perchip_info.play.gain, 
846                              cs4231_chip->perchip_info.play.balance);
847     	
848             return 0;
849     }
850     
851     static int cs4231_get_output_balance(struct sparcaudio_driver *drv)
852     {
853             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
854     
855             return (int) cs4231_chip->perchip_info.play.balance;
856     }
857     
858     /* Set chip record gain */
859     static int cs4231_record_gain(struct sparcaudio_driver *drv, int value,
860                                   unsigned char balance)
861     {
862             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
863             int tmp = 0, r, l, r_adj, l_adj;
864             unsigned char old_gain;
865     
866             r = l = value;
867     
868             if (balance < AUDIO_MID_BALANCE) {
869                     r = (int) (value -
870                                ((AUDIO_MID_BALANCE - balance) << AUDIO_BALANCE_SHIFT));
871     
872                     if (r < 0)
873                             r = 0;
874             } else if (balance > AUDIO_MID_BALANCE) {
875                     l = (int) (value -
876                                ((balance - AUDIO_MID_BALANCE) << AUDIO_BALANCE_SHIFT));
877     
878                     if (l < 0)
879                             l = 0;
880             }
881     
882             l_adj = l * (CS4231_MAX_GAIN + 1) / (AUDIO_MAX_GAIN + 1);
883             r_adj = r * (CS4231_MAX_GAIN + 1) / (AUDIO_MAX_GAIN + 1);
884       
885             WRITE_IAR(0x0);
886             old_gain = READ_IDR();
887             WRITE_IDR(RECGAIN_SET(old_gain, l_adj));
888             WRITE_IAR(0x1);
889             old_gain = READ_IDR();
890             WRITE_IDR(RECGAIN_SET(old_gain, r_adj));
891       
892             if (l == value) {
893                     (l == 0) ? (tmp = 0) : (tmp = ((l_adj + 1) * AUDIO_MAX_GAIN) / 
894                                             (CS4231_MAX_GAIN + 1));
895             } else if (r == value) {
896                     (r == 0) ? (tmp = 0) : (tmp = ((r_adj + 1) * AUDIO_MAX_GAIN) / 
897                                             (CS4231_MAX_GAIN + 1));
898             }
899             cs4231_chip->perchip_info.record.gain = tmp;
900             return 0;
901     }
902     
903     /* Set chip play gain */
904     static int cs4231_play_gain(struct sparcaudio_driver *drv, int value,
905                                 unsigned char balance)
906     {
907             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
908             int tmp = 0, r, l, r_adj, l_adj;
909             unsigned char old_gain;
910     
911             tprintk(("in play_gain: %d %c\n", value, balance));
912             r = l = value;
913             if (balance < AUDIO_MID_BALANCE) {
914                     r = (int) (value -
915                                ((AUDIO_MID_BALANCE - balance) << AUDIO_BALANCE_SHIFT));
916     
917                     if (r < 0)
918                             r = 0;
919             } else if (balance > AUDIO_MID_BALANCE) {
920                     l = (int) (value -
921                                ((balance - AUDIO_MID_BALANCE) << AUDIO_BALANCE_SHIFT));
922     
923                     if (l < 0)
924                             l = 0;
925             }
926     
927             (l == 0) ? (l_adj = CS4231_MAX_DEV_ATEN) : (l_adj = CS4231_MAX_ATEN - 
928                                                         (l * (CS4231_MAX_ATEN + 1) / 
929                                                          (AUDIO_MAX_GAIN + 1)));
930             (r == 0) ? (r_adj = CS4231_MAX_DEV_ATEN) : (r_adj = CS4231_MAX_ATEN -
931                                                         (r * (CS4231_MAX_ATEN + 1) /
932                                                          (AUDIO_MAX_GAIN + 1)));
933       
934             WRITE_IAR(0x6);
935             old_gain = READ_IDR();
936             WRITE_IDR(GAIN_SET(old_gain, l_adj));
937             WRITE_IAR(0x7);
938             old_gain = READ_IDR();
939             WRITE_IDR(GAIN_SET(old_gain, r_adj));
940       
941             if ((value == 0) || (value == AUDIO_MAX_GAIN)) {
942                     tmp = value;
943             } else {
944                     if (value == l) {
945                             tmp = ((CS4231_MAX_ATEN - l_adj) * (AUDIO_MAX_GAIN + 1) / 
946                                    (CS4231_MAX_ATEN + 1));
947                     } else if (value == r) {
948                             tmp = ((CS4231_MAX_ATEN - r_adj) * (AUDIO_MAX_GAIN + 1) / 
949                                    (CS4231_MAX_ATEN + 1));
950                     }
951             }
952             cs4231_chip->perchip_info.play.gain = tmp;
953     
954             return 0;
955     }
956     
957     /* Reset the audio chip to a sane state. */
958     static void cs4231_chip_reset(struct sparcaudio_driver *drv)
959     {
960             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
961             unsigned char vers;
962     
963             tprintk(("in cs4231_chip_reset\n"));
964     
965             if (cs4231_chip->status & CS_STATUS_IS_EBUS) {
966     #ifdef EB4231_SUPPORT
967                     writel(EBUS_DCSR_RESET, cs4231_chip->eb2p + EBDMA_CSR);
968                     writel(EBUS_DCSR_RESET, cs4231_chip->eb2c + EBDMA_CSR);
969                     writel(EBUS_DCSR_BURST_SZ_16, cs4231_chip->eb2p + EBDMA_CSR);
970                     writel(EBUS_DCSR_BURST_SZ_16, cs4231_chip->eb2c + EBDMA_CSR);
971     #endif
972             } else {
973                     u32 tmp;
974     
975                     sbus_writel(APC_CHIP_RESET, cs4231_chip->regs + APCCSR);
976                     sbus_writel(0x00, cs4231_chip->regs + APCCSR);
977                     tmp = sbus_readl(cs4231_chip->regs + APCCSR);
978                     tmp |= APC_CDC_RESET;
979                     sbus_writel(tmp, cs4231_chip->regs + APCCSR);
980       
981                     udelay(20);
982       
983                     tmp = sbus_readl(cs4231_chip->regs + APCCSR);
984                     tmp &= ~(APC_CDC_RESET);
985                     sbus_writel(tmp, cs4231_chip->regs + APCCSR);
986             }
987     
988             WRITE_IAR(READ_IAR() | IAR_AUTOCAL_BEGIN);
989             CHIP_READY();
990         
991             WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x0c);
992             WRITE_IDR(MISC_IR_MODE2);
993     
994             /* This is the equivalent of DEFAULT_DATA_FMAT */
995             cs4231_set_input_encoding(drv, AUDIO_ENCODING_ULAW);
996             cs4231_set_input_rate(drv, CS4231_RATE);
997             cs4231_set_input_channels(drv, CS4231_CHANNELS);
998             cs4231_set_input_precision(drv, CS4231_PRECISION);
999     
1000             cs4231_set_output_encoding(drv, AUDIO_ENCODING_ULAW);
1001             cs4231_set_output_rate(drv, CS4231_RATE);
1002             cs4231_set_output_channels(drv, CS4231_CHANNELS);
1003             cs4231_set_output_precision(drv, CS4231_PRECISION);
1004     
1005             WRITE_IAR(0x19);
1006     
1007             /* see what we can turn on */
1008             vers = READ_IDR();
1009             if (vers & CS4231A) {
1010                     tprintk(("This is a CS4231A\n"));
1011                     cs4231_chip->status |= CS_STATUS_REV_A;
1012             } else {
1013                     cs4231_chip->status &= ~CS_STATUS_REV_A;
1014             }
1015       
1016             WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x10);
1017             WRITE_IDR(OLB_ENABLE);
1018       
1019             WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x11);
1020             if (cs4231_chip->status & CS_STATUS_REV_A)
1021                     WRITE_IDR(HPF_ON | XTALE_ON);
1022             else
1023                     WRITE_IDR(HPF_ON);
1024       
1025             WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x1a);
1026             WRITE_IDR(0x00);
1027       
1028             /* Now set things up for defaults */
1029             cs4231_set_input_balance(drv, AUDIO_MID_BALANCE);
1030             cs4231_set_output_balance(drv, AUDIO_MID_BALANCE);
1031     
1032             cs4231_set_input_volume(drv, CS4231_DEFAULT_RECGAIN);
1033             cs4231_set_output_volume(drv, CS4231_DEFAULT_PLAYGAIN);
1034     
1035             cs4231_set_input_port(drv, AUDIO_MICROPHONE);
1036             cs4231_set_output_port(drv, AUDIO_SPEAKER);
1037     
1038             cs4231_set_monitor_volume(drv, LOOPB_OFF);
1039       
1040             WRITE_IAR(IAR_AUTOCAL_END);
1041       
1042             cs4231_ready(drv);
1043       
1044             WRITE_IAR(IAR_AUTOCAL_BEGIN | 0x09);
1045             WRITE_IDR(READ_IDR() & ACAL_DISABLE);
1046             WRITE_IAR(IAR_AUTOCAL_END);
1047       
1048             cs4231_ready(drv);
1049     
1050             cs4231_output_muted(drv, 0);
1051     
1052             cs4231_chip->recording_count = 0;
1053             cs4231_chip->input_next_dma_handle = 0;
1054             cs4231_chip->input_dma_handle = 0;
1055             cs4231_chip->input_next_dma_size = 0;
1056             cs4231_chip->input_dma_size = 0;
1057     
1058             cs4231_chip->playing_count = 0;
1059             cs4231_chip->output_next_dma_handle = 0;
1060             cs4231_chip->output_dma_handle = 0;
1061             cs4231_chip->output_next_dma_size = 0;
1062             cs4231_chip->output_dma_size = 0;
1063     }
1064     
1065     static int
1066     cs4231_length_to_samplecount(struct audio_prinfo *thisdir, unsigned int length)
1067     {
1068             unsigned int count;
1069     
1070             if (thisdir->channels == 2)
1071                     count = (length / 2);
1072             else 
1073                     count = length;
1074       
1075             if (thisdir->encoding == AUDIO_ENCODING_LINEAR)
1076                     count = (count / 2);
1077             else if (thisdir->encoding == AUDIO_ENCODING_DVI)
1078                     count = (count / 4);
1079       
1080             return count;
1081     }
1082     
1083     #ifdef EB4231_SUPPORT
1084     static void eb4231_getsamplecount(struct sparcaudio_driver *drv,
1085                                       unsigned int length,
1086                                       unsigned int direction)
1087     {
1088             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1089             struct audio_prinfo *thisdir;
1090             unsigned int count, curcount, nextcount, dbcr;
1091     
1092             if(direction == 1) {
1093                     thisdir = &cs4231_chip->perchip_info.record;
1094                     dbcr = readl(cs4231_chip->eb2c + EBDMA_COUNT);
1095                     nextcount = cs4231_chip->input_next_dma_size;
1096             } else {
1097                     thisdir = &cs4231_chip->perchip_info.play;
1098                     dbcr = readl(cs4231_chip->eb2p + EBDMA_COUNT);
1099                     nextcount = cs4231_chip->output_next_dma_size;
1100             }
1101             curcount = cs4231_length_to_samplecount(thisdir, dbcr);
1102             count = thisdir->samples;
1103             length = cs4231_length_to_samplecount(thisdir, length);
1104             /* normalize for where we are. */
1105             thisdir->samples = ((count - nextcount) + (length - curcount));
1106     }
1107     #endif
1108     
1109     static void cs4231_getsamplecount(struct sparcaudio_driver *drv,
1110                                       unsigned int length,
1111                                       unsigned int direction)
1112     {
1113             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1114             struct audio_prinfo *thisdir;
1115             unsigned int count, nextcount, curcount;
1116             u32 tmp;
1117     
1118             if (direction == 1) {
1119                     /* record */ 
1120                     thisdir = &cs4231_chip->perchip_info.record;
1121                     tmp = sbus_readl(cs4231_chip->regs + APCCC);
1122                     curcount = cs4231_length_to_samplecount(thisdir, tmp);
1123                     tmp = sbus_readl(cs4231_chip->regs + APCCNC);
1124                     nextcount = cs4231_length_to_samplecount(thisdir, tmp);
1125             } else {
1126                     /* play */
1127                     thisdir = &cs4231_chip->perchip_info.play;
1128                     tmp = sbus_readl(cs4231_chip->regs + APCPC);
1129                     curcount = cs4231_length_to_samplecount(thisdir, tmp);
1130                     tmp = sbus_readl(cs4231_chip->regs + APCPNC);
1131                     nextcount = cs4231_length_to_samplecount(thisdir, tmp);
1132             }
1133             count = thisdir->samples;
1134             length = cs4231_length_to_samplecount(thisdir, length);
1135     
1136             /* normalize for where we are. */
1137             thisdir->samples = ((count - nextcount) + (length - curcount));
1138     }
1139     
1140     static int cs4231_open(struct inode * inode, struct file * file, struct sparcaudio_driver *drv)
1141     {	
1142             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1143     
1144             /* Set the default audio parameters if not already in use. */
1145             if (file->f_mode & FMODE_WRITE) {
1146                     if (!(drv->flags & SDF_OPEN_WRITE) && 
1147                         (cs4231_chip->perchip_info.play.active == 0)) {
1148                             cs4231_chip->perchip_info.play.open = 1;
1149                             cs4231_chip->perchip_info.play.samples =
1150                                     cs4231_chip->perchip_info.play.error = 0;
1151                     }
1152             }
1153     
1154             if (file->f_mode & FMODE_READ) {
1155                     if (!(drv->flags & SDF_OPEN_READ) && 
1156                         (cs4231_chip->perchip_info.record.active == 0)) {
1157                             cs4231_chip->perchip_info.record.open = 1;
1158                             cs4231_chip->perchip_info.record.samples = 
1159                                     cs4231_chip->perchip_info.record.error = 0;
1160                     }
1161             }  
1162     
1163             cs4231_ready(drv);
1164             CHIP_READY();
1165         
1166             MOD_INC_USE_COUNT;
1167       
1168             return 0;
1169     }
1170     
1171     static void cs4231_release(struct inode * inode, struct file * file, struct sparcaudio_driver *drv)
1172     {
1173     	struct cs4231_chip *cs4231_chip = (struct cs4231_chip *)drv->private;
1174     	void (*dma_unmap_single)(struct sbus_dev *, dma_addr_t, size_t, int) = sbus_unmap_single;
1175     
1176     #ifdef EB4231_SUPPORT
1177     	if (cs4231_chip->status & CS_STATUS_IS_EBUS)
1178     		dma_unmap_single = (void (*)(struct sbus_dev *, dma_addr_t, size_t, int)) pci_unmap_single;
1179     #endif
1180             /* zero out any info about what data we have as well */
1181             if (file->f_mode & FMODE_READ) {
1182                     /* stop capture here or midlevel? */
1183                     cs4231_chip->perchip_info.record.open = 0;
1184                     if (cs4231_chip->input_dma_handle) {
1185     			dma_unmap_single(drv->dev,
1186     					 cs4231_chip->input_dma_handle,
1187     					 cs4231_chip->input_dma_size,
1188     					 SBUS_DMA_FROMDEVICE);
1189                             cs4231_chip->input_dma_handle = 0;
1190                             cs4231_chip->input_dma_size = 0;
1191                     }
1192                     if (cs4231_chip->input_next_dma_handle) {
1193     			dma_unmap_single(drv->dev,
1194     					 cs4231_chip->input_next_dma_handle,
1195     					 cs4231_chip->input_next_dma_size,
1196     					 SBUS_DMA_FROMDEVICE);
1197                             cs4231_chip->input_next_dma_handle = 0;
1198                             cs4231_chip->input_next_dma_size = 0;
1199                     }
1200             }
1201     
1202             if (file->f_mode & FMODE_WRITE) {
1203                     cs4231_chip->perchip_info.play.active =
1204                             cs4231_chip->perchip_info.play.open = 0;
1205                     if (cs4231_chip->output_dma_handle) {
1206     			dma_unmap_single(drv->dev,
1207     					 cs4231_chip->output_dma_handle,
1208     					 cs4231_chip->output_dma_size,
1209     					 SBUS_DMA_TODEVICE);
1210                             cs4231_chip->output_dma_handle = 0;
1211                             cs4231_chip->output_dma_size = 0;
1212                     }
1213                     if (cs4231_chip->output_next_dma_handle) {
1214     			dma_unmap_single(drv->dev,
1215     					 cs4231_chip->output_next_dma_handle,
1216     					 cs4231_chip->output_next_dma_size,
1217     					 SBUS_DMA_TODEVICE);
1218                             cs4231_chip->output_next_dma_handle = 0;
1219                             cs4231_chip->output_next_dma_size = 0;
1220                     }
1221             }
1222     
1223             if (!cs4231_chip->perchip_info.play.open && 
1224                 !cs4231_chip->perchip_info.record.open && 
1225                 (cs4231_chip->status & CS_STATUS_INIT_ON_CLOSE)) {
1226                     cs4231_chip_reset(drv);
1227                     cs4231_chip->status &= ~CS_STATUS_INIT_ON_CLOSE;
1228             }
1229     
1230             MOD_DEC_USE_COUNT;
1231     }
1232     
1233     static void cs4231_playintr(struct sparcaudio_driver *drv, int push)
1234     {
1235             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1236             int status = 0;
1237     
1238             if (!push) {
1239                     if (!cs4231_chip->perchip_info.play.active) {
1240                             sbus_writel(cs4231_chip->output_next_dma_handle,
1241                                         cs4231_chip->regs + APCPNVA);
1242                             sbus_writel(cs4231_chip->output_next_dma_size,
1243                                         cs4231_chip->regs + APCPNC);
1244                     }
1245                     sparcaudio_output_done(drv, 0);
1246                     return;
1247             }
1248     
1249             if (cs4231_chip->playlen == 0 && cs4231_chip->output_size > 0) 
1250                     cs4231_chip->playlen = cs4231_chip->output_size;
1251     
1252             if (cs4231_chip->output_dma_handle) {
1253                     sbus_unmap_single(drv->dev,
1254                                       cs4231_chip->output_dma_handle,
1255                                       cs4231_chip->output_dma_size,
1256                                       SBUS_DMA_TODEVICE);
1257                     cs4231_chip->output_dma_handle = 0;
1258                     cs4231_chip->output_dma_size = 0;
1259                     cs4231_chip->playing_count--;
1260                     status++;
1261             }
1262     
1263             if (cs4231_chip->output_next_dma_handle) {
1264                     cs4231_chip->output_dma_handle = cs4231_chip->output_next_dma_handle;
1265                     cs4231_chip->output_dma_size = cs4231_chip->output_next_dma_size;
1266                     cs4231_chip->output_next_dma_size = 0;
1267                     cs4231_chip->output_next_dma_handle = 0;
1268             }
1269     
1270             if ((cs4231_chip->output_ptr && cs4231_chip->output_size > 0) && 
1271                 !(cs4231_chip->perchip_info.play.pause)) {
1272                     cs4231_chip->output_next_dma_handle =
1273                             sbus_map_single(drv->dev,
1274                                             (char *)cs4231_chip->output_ptr,
1275                                             cs4231_chip->output_size,
1276                                             SBUS_DMA_TODEVICE);
1277                     cs4231_chip->output_next_dma_size = cs4231_chip->output_size;
1278                     sbus_writel(cs4231_chip->output_next_dma_handle,
1279                                 cs4231_chip->regs + APCPNVA);
1280                     sbus_writel(cs4231_chip->output_next_dma_size,
1281                                 cs4231_chip->regs + APCPNC);
1282                     cs4231_chip->output_size = 0;
1283                     cs4231_chip->output_ptr = NULL;
1284                     cs4231_chip->playing_count++;
1285                     status += 2;
1286             } else {
1287                     sbus_writel(0, cs4231_chip->regs + APCPNVA);
1288                     sbus_writel(0, cs4231_chip->regs + APCPNC);
1289             }
1290     
1291             sparcaudio_output_done(drv, status);
1292     }
1293     
1294     #ifdef EB4231_SUPPORT
1295     static void eb4231_playintr(struct sparcaudio_driver *drv)
1296     {
1297             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1298             int status = 0;
1299     
1300             if (cs4231_chip->playlen == 0 && cs4231_chip->output_size > 0) 
1301                     cs4231_chip->playlen = cs4231_chip->output_size;
1302     
1303             if (cs4231_chip->output_dma_handle) {
1304     		pci_unmap_single((struct pci_dev *)drv->dev,
1305     				 cs4231_chip->output_dma_handle,
1306     				 cs4231_chip->output_dma_size,
1307                                      PCI_DMA_TODEVICE);
1308                     cs4231_chip->output_dma_handle = 0;
1309                     cs4231_chip->output_dma_size = 0;
1310                     cs4231_chip->playing_count--;
1311                     status++;
1312             }
1313     
1314             if(cs4231_chip->output_next_dma_handle) {
1315                     cs4231_chip->output_dma_handle = cs4231_chip->output_next_dma_handle;
1316                     cs4231_chip->output_dma_size = cs4231_chip->output_next_dma_size;
1317                     cs4231_chip->output_next_dma_handle = 0;
1318                     cs4231_chip->output_next_dma_size = 0;
1319             }
1320     
1321             if ((cs4231_chip->output_ptr && cs4231_chip->output_size > 0) && 
1322                 !(cs4231_chip->perchip_info.play.pause)) {
1323                     cs4231_chip->output_next_dma_handle =
1324     			pci_map_single((struct pci_dev *)drv->dev,
1325     				       (char *)cs4231_chip->output_ptr,
1326     				       cs4231_chip->output_size,
1327                                            PCI_DMA_TODEVICE);
1328                     cs4231_chip->output_next_dma_size = cs4231_chip->output_size;
1329     
1330                     writel(cs4231_chip->output_next_dma_size,
1331                            cs4231_chip->eb2p + EBDMA_COUNT);
1332                     writel(cs4231_chip->output_next_dma_handle,
1333                            cs4231_chip->eb2p + EBDMA_ADDR);
1334                     cs4231_chip->output_size = 0;
1335                     cs4231_chip->output_ptr = NULL;
1336                     cs4231_chip->playing_count++;
1337                     status += 2;
1338             }
1339     
1340             sparcaudio_output_done(drv, status);
1341     }
1342     #endif
1343     
1344     static void cs4231_recclear(int fmt, char *dmabuf, int length)
1345     {
1346             switch (fmt) {
1347             case AUDIO_ENCODING_LINEAR:
1348                     memset(dmabuf, 0x00, length);
1349                     break;
1350             case AUDIO_ENCODING_ALAW:
1351                     memset(dmabuf, 0xd5, length);
1352                     break;
1353             case AUDIO_ENCODING_ULAW:
1354                     memset(dmabuf, 0xff, length);
1355                     break;
1356             }
1357     }
1358     
1359     static int cs4231_recintr(struct sparcaudio_driver *drv)
1360     {
1361             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1362             int status = 0;
1363     
1364             if (cs4231_chip->perchip_info.record.active == 0) {
1365                     dprintk(("going inactive\n"));
1366                     cs4231_pollinput(drv);
1367                     cs4231_disable_rec(drv);    
1368             } 
1369     
1370             if (cs4231_chip->input_dma_handle) {
1371                     sbus_unmap_single(drv->dev,
1372                                       cs4231_chip->input_dma_handle,
1373                                       cs4231_chip->input_dma_size,
1374                                       SBUS_DMA_FROMDEVICE);
1375                     cs4231_chip->input_dma_handle = 0;
1376                     cs4231_chip->input_dma_size = 0;
1377                     cs4231_chip->recording_count--;
1378                     status++;
1379             }
1380     
1381             if (cs4231_chip->input_next_dma_handle) {
1382                     cs4231_chip->input_dma_handle = cs4231_chip->input_next_dma_handle;
1383                     cs4231_chip->input_dma_size = cs4231_chip->input_next_dma_size;
1384                     cs4231_chip->input_next_dma_size = 0;
1385                     cs4231_chip->input_next_dma_handle = 0;
1386             }
1387     
1388             if ((cs4231_chip->input_ptr && cs4231_chip->input_size > 0) && 
1389                 !(cs4231_chip->perchip_info.record.pause)) {
1390                     cs4231_recclear(cs4231_chip->perchip_info.record.encoding, 
1391                                     (char *)cs4231_chip->input_ptr,
1392                                     cs4231_chip->input_size);
1393                     cs4231_chip->input_next_dma_handle =
1394                             sbus_map_single(drv->dev,
1395                                             (char *)cs4231_chip->input_ptr,
1396                                             cs4231_chip->input_size,
1397                                             SBUS_DMA_FROMDEVICE);
1398                     cs4231_chip->input_next_dma_size = cs4231_chip->input_size;
1399                     sbus_writel(cs4231_chip->input_next_dma_handle,
1400                                 cs4231_chip->regs + APCCNVA);
1401                     sbus_writel(cs4231_chip->input_next_dma_size,
1402                                 cs4231_chip->regs + APCCNC);
1403                     cs4231_chip->input_size = 0;
1404                     cs4231_chip->input_ptr = NULL;
1405                     cs4231_chip->recording_count++;
1406                     status += 2;
1407             } else {
1408                     sbus_writel(0, cs4231_chip->regs + APCCNVA);
1409                     sbus_writel(0, cs4231_chip->regs + APCCNC);
1410             }
1411     
1412             sparcaudio_input_done(drv, status);
1413             return 1;
1414     }
1415     
1416     #ifdef EB4231_SUPPORT
1417     static int eb4231_recintr(struct sparcaudio_driver *drv)
1418     {
1419             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1420             int status = 0;
1421     
1422             if (cs4231_chip->perchip_info.record.active == 0) {
1423                     dprintk(("going inactive\n"));
1424                     eb4231_pollinput(drv);
1425                     cs4231_disable_rec(drv);    
1426             } 
1427     
1428             if (cs4231_chip->input_dma_handle) {
1429     		pci_unmap_single((struct pci_dev *)drv->dev,
1430     				 cs4231_chip->input_dma_handle,
1431     				 cs4231_chip->input_dma_size,
1432                                      PCI_DMA_FROMDEVICE);
1433                     cs4231_chip->input_dma_handle = 0;
1434                     cs4231_chip->input_dma_size = 0;
1435                     cs4231_chip->recording_count--;
1436                     status++;
1437             }
1438     
1439             if (cs4231_chip->input_next_dma_handle) {
1440                     cs4231_chip->input_dma_handle = cs4231_chip->input_next_dma_handle;
1441                     cs4231_chip->input_dma_size = cs4231_chip->input_next_dma_size;
1442                     cs4231_chip->input_next_dma_size = 0;
1443                     cs4231_chip->input_next_dma_handle = 0;
1444             }
1445     
1446             if ((cs4231_chip->input_ptr && cs4231_chip->input_size > 0) && 
1447                 !(cs4231_chip->perchip_info.record.pause)) {
1448                     cs4231_recclear(cs4231_chip->perchip_info.record.encoding, 
1449                                     (char *)cs4231_chip->input_ptr,
1450                                     cs4231_chip->input_size);
1451     
1452                     cs4231_chip->input_next_dma_handle =
1453     			pci_map_single((struct pci_dev *)drv->dev,
1454     				       (char *)cs4231_chip->input_ptr,
1455     				       cs4231_chip->input_size,
1456                                            PCI_DMA_FROMDEVICE);
1457                     cs4231_chip->input_next_dma_size = cs4231_chip->input_size;
1458     
1459                     writel(cs4231_chip->input_next_dma_size,
1460                            cs4231_chip->eb2c + EBDMA_COUNT);
1461                     writel(cs4231_chip->input_next_dma_handle,
1462                            cs4231_chip->eb2c + EBDMA_ADDR);
1463     
1464                     cs4231_chip->input_size = 0;
1465                     cs4231_chip->input_ptr = NULL;
1466                     cs4231_chip->recording_count++;
1467                     status += 2;
1468             }
1469     
1470             sparcaudio_input_done(drv, status);
1471             return 1;
1472     }
1473     #endif
1474     
1475     #ifdef EB4231_SUPPORT
1476     static void eb4231_start_output(struct sparcaudio_driver *drv, __u8 * buffer,
1477                                     unsigned long count)
1478     {
1479             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1480             u32 dcsr;
1481     
1482             cs4231_chip->output_ptr = buffer;
1483             cs4231_chip->output_size = count;
1484     
1485             if (cs4231_chip->perchip_info.play.active || 
1486                 (cs4231_chip->perchip_info.play.pause))
1487                     return;
1488     
1489             cs4231_ready(drv);
1490     
1491             cs4231_chip->perchip_info.play.active = 1;
1492             cs4231_chip->playing_count = 0;
1493     
1494             dcsr = readl(cs4231_chip->eb2p + EBDMA_CSR);
1495             if (!(dcsr & EBUS_DCSR_EN_DMA)) {
1496                     writel(EBUS_DCSR_RESET, cs4231_chip->eb2p + EBDMA_CSR);
1497                     writel(EBUS_DCSR_BURST_SZ_16, cs4231_chip->eb2p + EBDMA_CSR);
1498     
1499                     eb4231_playintr(drv);
1500     
1501                     writel(EBUS_DCSR_BURST_SZ_16 |
1502                            (EBUS_DCSR_EN_DMA | EBUS_DCSR_INT_EN |
1503                             EBUS_DCSR_EN_CNT | EBUS_DCSR_EN_NEXT),
1504                            cs4231_chip->eb2p + EBDMA_CSR);
1505     
1506                     cs4231_enable_play(drv);
1507     
1508                     cs4231_ready(drv);
1509             } else {
1510                     eb4231_playintr(drv);
1511             }
1512     }
1513     #endif
1514     
1515     static void cs4231_start_output(struct sparcaudio_driver *drv, __u8 * buffer,
1516                                     unsigned long count)
1517     {
1518             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1519             u32 csr;
1520     
1521             tprintk(("in 4231 start output\n"));
1522             cs4231_chip->output_ptr = buffer;
1523             cs4231_chip->output_size = count;
1524     
1525             if (cs4231_chip->perchip_info.play.active || 
1526                 (cs4231_chip->perchip_info.play.pause))
1527                     return;
1528     
1529             cs4231_ready(drv);
1530     
1531             cs4231_chip->perchip_info.play.active = 1;
1532             cs4231_chip->playing_count = 0;
1533     
1534             csr = sbus_readl(cs4231_chip->regs + APCCSR);
1535             if ((csr & APC_PPAUSE) || !(csr & APC_PDMA_READY)) {
1536                     u32 pnva;
1537     
1538                     csr &= ~APC_XINT_PLAY;
1539                     sbus_writel(csr, cs4231_chip->regs + APCCSR);
1540                     csr &= ~APC_PPAUSE;
1541                     sbus_writel(csr, cs4231_chip->regs + APCCSR);
1542         
1543                     pnva = sbus_readl(cs4231_chip->regs + APCPNVA);
1544                     cs4231_playintr(drv, (pnva == 0) ? 1 : 0);
1545     
1546                     csr |= APC_PLAY_SETUP;
1547                     sbus_writel(csr, cs4231_chip->regs + APCCSR);
1548                     cs4231_enable_play(drv);
1549     
1550                     cs4231_ready(drv);
1551             }
1552     }
1553     
1554     #ifdef EB4231_SUPPORT
1555     static void eb4231_stop_output(struct sparcaudio_driver *drv)
1556     {
1557             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1558             u32 dcsr;
1559     
1560             dprintk(("eb4231_stop_output: dcsr 0x%x dacr 0x%x dbcr %d\n",
1561                      readl(cs4231_chip->eb2p + EBDMA_CSR),
1562                      readl(cs4231_chip->eb2p + EBDMA_ADDR),
1563                      readl(cs4231_chip->eb2p + EBDMA_COUNT)));
1564     
1565             cs4231_chip->output_ptr = NULL;
1566             cs4231_chip->output_size = 0;
1567     
1568             if (cs4231_chip->output_dma_handle) {
1569     		pci_unmap_single((struct pci_dev *)drv->dev,
1570     				 cs4231_chip->output_dma_handle,
1571     				 cs4231_chip->output_dma_size,
1572                                      PCI_DMA_TODEVICE);
1573                     cs4231_chip->output_dma_handle = 0;
1574                     cs4231_chip->output_dma_size = 0;
1575             }
1576     
1577             if (cs4231_chip->output_next_dma_handle) {
1578     		pci_unmap_single((struct pci_dev *)drv->dev,
1579     				 cs4231_chip->output_next_dma_handle,
1580     				 cs4231_chip->output_next_dma_size,
1581                                      PCI_DMA_TODEVICE);
1582                     cs4231_chip->output_next_dma_handle = 0;
1583                     cs4231_chip->output_next_dma_size = 0;
1584             }
1585             dcsr = readl(cs4231_chip->eb2p + EBDMA_CSR);
1586             if(dcsr & EBUS_DCSR_EN_DMA)
1587                     writel(dcsr & ~EBUS_DCSR_EN_DMA,
1588                            cs4231_chip->eb2p + EBDMA_CSR);
1589     
1590             /* Else subsequent speed setting changes are ignored by the chip. */
1591             cs4231_disable_play(drv);
1592     }
1593     #endif
1594     
1595     static void cs4231_stop_output(struct sparcaudio_driver *drv)
1596     {
1597             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1598     
1599             tprintk(("in cs4231_stop_output\n"));
1600             cs4231_chip->output_ptr = NULL;
1601             cs4231_chip->output_size = 0;
1602     
1603             if (cs4231_chip->output_dma_handle) {
1604                     sbus_unmap_single(drv->dev,
1605                                       cs4231_chip->output_dma_handle,
1606                                       cs4231_chip->output_dma_size,
1607                                       SBUS_DMA_TODEVICE);
1608                     cs4231_chip->output_dma_handle = 0;
1609                     cs4231_chip->output_dma_size = 0;
1610             }
1611     
1612             if (cs4231_chip->output_next_dma_handle) {
1613                     sbus_unmap_single(drv->dev,
1614                                       cs4231_chip->output_next_dma_handle,
1615                                       cs4231_chip->output_next_dma_size,
1616                                       SBUS_DMA_TODEVICE);
1617                     cs4231_chip->output_next_dma_handle = 0;
1618                     cs4231_chip->output_next_dma_size = 0;
1619             }
1620     #if 0 /* Not safe without shutting off the DMA controller as well. -DaveM */
1621             /* Else subsequent speed setting changes are ignored by the chip. */
1622             cs4231_disable_play(drv);
1623     #endif
1624     }
1625     
1626     #ifdef EB4231_SUPPORT
1627     static void eb4231_pollinput(struct sparcaudio_driver *drv)
1628     {
1629             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1630             int x;
1631             u32 dcsr;
1632     
1633             x = 0;
1634             do {
1635                     dcsr = readl(cs4231_chip->eb2c + EBDMA_CSR);
1636                     if (dcsr & EBUS_DCSR_TC)
1637                             break;
1638                     x++;
1639             } while (x <= CS_TIMEOUT);
1640     
1641             writel(dcsr | EBUS_DCSR_TC,
1642                    cs4231_chip->eb2c + EBDMA_CSR);
1643     }
1644     #endif
1645     
1646     static void cs4231_pollinput(struct sparcaudio_driver *drv)
1647     {
1648             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1649             int x;
1650             u32 csr;
1651     
1652             x = 0;
1653             do {
1654                     csr = sbus_readl(cs4231_chip->regs + APCCSR);
1655                     if (csr & APC_XINT_COVF)
1656                             break;
1657                     x++;
1658             } while (x <= CS_TIMEOUT);
1659     
1660             sbus_writel(csr | APC_XINT_CEMP,
1661                         cs4231_chip->regs + APCCSR);
1662     }
1663     
1664     static void cs4231_start_input(struct sparcaudio_driver *drv, __u8 * buffer, 
1665                                    unsigned long count)
1666     {
1667             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1668             u32 csr;
1669     
1670             cs4231_chip->input_ptr = buffer;
1671             cs4231_chip->input_size = count;
1672     
1673             if (cs4231_chip->perchip_info.record.active || 
1674                 (cs4231_chip->perchip_info.record.pause))
1675                     return;
1676     
1677             cs4231_ready(drv);
1678     
1679             cs4231_chip->perchip_info.record.active = 1;
1680             cs4231_chip->recording_count = 0;
1681     
1682             csr = sbus_readl(cs4231_chip->regs + APCCSR);
1683             if ((csr & APC_CPAUSE) || !(csr & APC_CDMA_READY)) {
1684                     csr &= ~APC_XINT_CAPT;
1685                     sbus_writel(csr, cs4231_chip->regs + APCCSR);
1686                     csr &= ~APC_CPAUSE;
1687                     sbus_writel(csr, cs4231_chip->regs + APCCSR);
1688     
1689                     cs4231_recintr(drv);
1690     
1691                     csr |= APC_CAPT_SETUP;
1692                     sbus_writel(csr, cs4231_chip->regs + APCCSR);
1693     
1694                     cs4231_enable_rec(drv);
1695                     cs4231_ready(drv);
1696             } else {
1697                     cs4231_recintr(drv);
1698             }
1699     }
1700     
1701     static void cs4231_stop_input(struct sparcaudio_driver *drv)
1702     {
1703             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1704             u32 csr;
1705     
1706             cs4231_chip->perchip_info.record.active = 0;
1707     
1708             csr = sbus_readl(cs4231_chip->regs + APCCSR);
1709             csr |= APC_CPAUSE;
1710             sbus_writel(csr, cs4231_chip->regs + APCCSR);
1711     
1712             cs4231_chip->input_ptr = NULL;
1713             cs4231_chip->input_size = 0;
1714     
1715             if (cs4231_chip->input_dma_handle) {
1716                     sbus_unmap_single(drv->dev,
1717                                       cs4231_chip->input_dma_handle,
1718                                       cs4231_chip->input_dma_size,
1719                                       SBUS_DMA_FROMDEVICE);
1720                     cs4231_chip->input_dma_handle = 0;
1721                     cs4231_chip->input_dma_size = 0;
1722             }
1723     
1724             if (cs4231_chip->input_next_dma_handle) {
1725                     sbus_unmap_single(drv->dev,
1726                                       cs4231_chip->input_next_dma_handle,
1727                                       cs4231_chip->input_next_dma_size,
1728                                       SBUS_DMA_FROMDEVICE);
1729                     cs4231_chip->input_next_dma_handle = 0;
1730                     cs4231_chip->input_next_dma_size = 0;
1731             }
1732     
1733             cs4231_pollinput(drv);
1734     }
1735     
1736     #ifdef EB4231_SUPPORT
1737     static void eb4231_start_input(struct sparcaudio_driver *drv, __u8 * buffer, 
1738                                    unsigned long count)
1739     {
1740             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1741             u32 dcsr;
1742     
1743             cs4231_chip->input_ptr = buffer;
1744             cs4231_chip->input_size = count;
1745     
1746             if (cs4231_chip->perchip_info.record.active || 
1747                 (cs4231_chip->perchip_info.record.pause))
1748                     return;
1749     
1750             cs4231_ready(drv);
1751             cs4231_chip->perchip_info.record.active = 1;
1752             cs4231_chip->recording_count = 0;
1753     
1754             dcsr = readl(cs4231_chip->eb2c + EBDMA_CSR);
1755             if (!(dcsr & EBUS_DCSR_EN_DMA)) {
1756                     writel(EBUS_DCSR_RESET, cs4231_chip->eb2c + EBDMA_CSR);
1757                     writel(EBUS_DCSR_BURST_SZ_16, cs4231_chip->eb2c + EBDMA_CSR);
1758     
1759                     eb4231_recintr(drv);
1760     
1761                     writel(EBUS_DCSR_BURST_SZ_16 |
1762                            (EBUS_DCSR_EN_DMA | EBUS_DCSR_INT_EN |
1763                             EBUS_DCSR_EN_CNT | EBUS_DCSR_EN_NEXT),
1764                            cs4231_chip->eb2c + EBDMA_CSR);
1765     
1766                     cs4231_enable_rec(drv);
1767                     cs4231_ready(drv);
1768             } else {
1769                     eb4231_recintr(drv);
1770             }
1771     }
1772     
1773     static void eb4231_stop_input(struct sparcaudio_driver *drv)
1774     {
1775             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1776             u32 dcsr;
1777     
1778             cs4231_chip->perchip_info.record.active = 0;
1779     
1780             cs4231_chip->input_ptr = NULL;
1781             cs4231_chip->input_size = 0;
1782     
1783             if (cs4231_chip->input_dma_handle) {
1784     		pci_unmap_single((struct pci_dev *)drv->dev,
1785     				 cs4231_chip->input_dma_handle,
1786     				 cs4231_chip->input_dma_size,
1787                                      PCI_DMA_FROMDEVICE);
1788                     cs4231_chip->input_dma_handle = 0;
1789                     cs4231_chip->input_dma_size = 0;
1790             }
1791     
1792             if (cs4231_chip->input_next_dma_handle) {
1793     		pci_unmap_single((struct pci_dev *)drv->dev,
1794     				 cs4231_chip->input_next_dma_handle,
1795     				 cs4231_chip->input_next_dma_size,
1796                                      PCI_DMA_FROMDEVICE);
1797                     cs4231_chip->input_next_dma_handle = 0;
1798                     cs4231_chip->input_next_dma_size = 0;
1799             }
1800     
1801             dcsr = readl(cs4231_chip->eb2c + EBDMA_CSR);
1802             if (dcsr & EBUS_DCSR_EN_DMA)
1803                     writel(dcsr & ~EBUS_DCSR_EN_DMA, cs4231_chip->eb2c + EBDMA_CSR);
1804     
1805             cs4231_disable_rec(drv);
1806     }
1807     #endif
1808     
1809     static int cs4231_set_output_pause(struct sparcaudio_driver *drv, int value)
1810     {
1811             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1812     
1813             cs4231_chip->perchip_info.play.pause = value;
1814     
1815             if (!value)
1816                     sparcaudio_output_done(drv, 0);
1817     
1818             return value;
1819     }
1820     
1821     static int cs4231_set_output_error(struct sparcaudio_driver *drv, int value)
1822     {
1823             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1824             int i;
1825       
1826             i = cs4231_chip->perchip_info.play.error;
1827             cs4231_chip->perchip_info.play.error = value;
1828       
1829             return i;
1830     }
1831     
1832     static int cs4231_set_input_error(struct sparcaudio_driver *drv, int value)
1833     {
1834             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1835             int i;
1836       
1837             i = cs4231_chip->perchip_info.record.error;
1838             cs4231_chip->perchip_info.record.error = value;
1839       
1840             return i;
1841     }
1842     
1843     static int cs4231_set_output_samples(struct sparcaudio_driver *drv, int value)
1844     {
1845             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *)drv->private;
1846             int i;
1847       
1848             i = cs4231_chip->perchip_info.play.samples;
1849             cs4231_chip->perchip_info.play.samples = value;
1850       
1851             return i;
1852     }
1853     
1854     static int cs4231_set_input_samples(struct sparcaudio_driver *drv, int value)
1855     {
1856             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1857             int i;
1858       
1859             i = cs4231_chip->perchip_info.record.samples;
1860             cs4231_chip->perchip_info.record.samples = value;
1861     
1862             return i;
1863     }
1864     
1865     static int cs4231_set_input_pause(struct sparcaudio_driver *drv, int value)
1866     {
1867             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1868     
1869             cs4231_chip->perchip_info.record.pause = value;
1870     	
1871             if (value)
1872                     cs4231_stop_input(drv);
1873     	
1874             return value;
1875     }
1876     
1877     static void cs4231_audio_getdev(struct sparcaudio_driver *drv,
1878                                      audio_device_t * audinfo)
1879     {
1880             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1881     
1882             strncpy(audinfo->name, "SUNW,CS4231", sizeof(audinfo->name) - 1);
1883             /* versions */
1884             /* a: SPARCstation 4/5          b: Ultra 1/2 (electron)       */
1885             /* c: Ultra 1/2 PCI? (positron) d: ppc                        */
1886             /* e: x86                       f: Ultra Enterprise? (tazmo)  */
1887             /* g: Ultra 30? (quark)         h: Ultra 5/10? (darwin)       */
1888             /* apparently Ultra 1, Ultra 2 don't have internal CD input */
1889             if (cs4231_chip->status & CS_STATUS_IS_ULTRA)
1890                     strncpy(audinfo->version, "b", sizeof(audinfo->version) - 1);
1891             else
1892                     strncpy(audinfo->version, "a", sizeof(audinfo->version) - 1);
1893             strncpy(audinfo->config, "onboard1", sizeof(audinfo->config) - 1);
1894     }
1895     
1896     
1897     static int cs4231_audio_getdev_sunos(struct sparcaudio_driver *drv)
1898     {
1899             return AUDIO_DEV_CS4231;
1900     }
1901     
1902     static void cs4231_loopback(struct sparcaudio_driver *drv, unsigned int value)
1903     {
1904             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1905     
1906             WRITE_IAR(0x0d);
1907             WRITE_IDR(value ? LOOPB_ON : 0);
1908     }
1909     
1910     static int cs4231_ioctl(struct inode * inode, struct file * file,
1911     			unsigned int cmd, unsigned long arg, 
1912     			struct sparcaudio_driver *drv)
1913     {
1914             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1915             int retval = 0;
1916       
1917             switch (cmd) {
1918             case AUDIO_DIAG_LOOPBACK:
1919                     cs4231_chip->status |= CS_STATUS_INIT_ON_CLOSE;
1920                     cs4231_loopback(drv, (unsigned int)arg);
1921                     break;
1922             default:
1923                     retval = -EINVAL;
1924             };
1925     
1926             return retval;
1927     }
1928     
1929     #ifdef EB4231_SUPPORT
1930     /* ebus audio capture interrupt handler. */
1931     void eb4231_cinterrupt(int irq, void *dev_id, struct pt_regs *regs)
1932     {
1933             struct sparcaudio_driver *drv = (struct sparcaudio_driver *) dev_id;
1934             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1935             u32 dummy;
1936       
1937             /* Clear the interrupt. */
1938             dummy = readl(cs4231_chip->eb2c + EBDMA_CSR);
1939             writel(dummy, cs4231_chip->eb2c + EBDMA_CSR);
1940     
1941             if ((dummy & EBUS_DCSR_TC) != 0
1942                 /*&& (dummy & EBUS_DCSR_A_LOADED) != 0*/) {
1943                     cs4231_chip->perchip_info.record.samples += 
1944                         cs4231_length_to_samplecount(&(cs4231_chip->perchip_info.record), 
1945                                                      cs4231_chip->reclen);
1946                     eb4231_recintr(drv);
1947             }
1948     
1949             if ((dummy & EBUS_DCSR_A_LOADED) == 0) {
1950                     cs4231_chip->perchip_info.record.active = 0;
1951                     eb4231_recintr(drv);
1952                     eb4231_getsamplecount(drv, cs4231_chip->reclen, 1);
1953             }
1954     }
1955     
1956     /* ebus audio play interrupt handler. */
1957     void eb4231_pinterrupt(int irq, void *dev_id, struct pt_regs *regs)
1958     {
1959             struct sparcaudio_driver *drv = (struct sparcaudio_driver *) dev_id;
1960             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1961             u32 dummy;
1962       
1963             /* Clear the interrupt.  Bleh, when not using the next-address
1964              * feature, TC can only be cleared by a reset.
1965              */
1966             dummy = readl(cs4231_chip->eb2p + EBDMA_CSR);
1967             writel(dummy, cs4231_chip->eb2p + EBDMA_CSR);
1968     
1969             /* If we get a terminal count and address loaded condition,
1970              * this means the DNAR was copied into DACR.
1971              */
1972             if((dummy & EBUS_DCSR_TC) != 0
1973                /*&& (dummy & EBUS_DCSR_A_LOADED) != 0*/) {
1974                     cs4231_chip->perchip_info.play.samples += 
1975                             cs4231_length_to_samplecount(&(cs4231_chip->perchip_info.play), 
1976                                                          cs4231_chip->playlen); 
1977                     eb4231_playintr(drv);
1978             }
1979     
1980             if((dummy & EBUS_DCSR_A_LOADED) == 0) {
1981                     cs4231_chip->perchip_info.play.active = 0;
1982                     eb4231_playintr(drv);
1983                     eb4231_getsamplecount(drv, cs4231_chip->playlen, 0);
1984             }
1985     }
1986     #endif
1987     
1988     /* Audio interrupt handler. */
1989     void cs4231_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1990     {
1991             struct sparcaudio_driver *drv = (struct sparcaudio_driver *) dev_id;
1992             struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
1993             __u32 dummy;
1994       
1995             dprintk(("in cs4231_interrupt\n"));
1996     
1997             /* Clear the interrupt. */
1998             dummy = sbus_readl(cs4231_chip->regs + APCCSR);
1999             sbus_writel(dummy, cs4231_chip->regs + APCCSR);
2000     
2001             /* now go through and figure out what gets to claim the interrupt 
2002              * if anything since we may be doing shared interrupts 
2003              */
2004             if (dummy & APC_PLAY_INT) {
2005                     if (dummy & APC_XINT_PNVA) {
2006                             cs4231_chip->perchip_info.play.samples += 
2007                              cs4231_length_to_samplecount(&(cs4231_chip->perchip_info.play), 
2008                                                           cs4231_chip->playlen); 
2009                             if (!(dummy & APC_XINT_EMPT))
2010                                     cs4231_playintr(drv, 1);
2011                     } 
2012                     /* Any other conditions we need worry about? */
2013             }
2014     
2015             if (dummy & APC_CAPT_INT) {
2016                     if (dummy & APC_XINT_CNVA) {
2017                       cs4231_chip->perchip_info.record.samples += 
2018                         cs4231_length_to_samplecount(&(cs4231_chip->perchip_info.record), 
2019                                                      cs4231_chip->reclen);
2020                       cs4231_recintr(drv);
2021                     }
2022                     /* Any other conditions we need worry about? */
2023             }
2024     
2025       
2026             if (dummy & APC_XINT_CEMP) {
2027                     if (cs4231_chip->perchip_info.record.active == 0) {
2028                             /* Fix me */
2029                             cs4231_chip->perchip_info.record.active = 0;
2030                             cs4231_chip->perchip_info.record.error = 1;
2031                             cs4231_recintr(drv);
2032                     }
2033             }
2034     
2035             if (dummy & APC_XINT_EMPT) {
2036                     if (!cs4231_chip->output_next_dma_handle) {
2037                             u32 csr = sbus_readl(cs4231_chip->regs + APCCSR);
2038     
2039                             csr |= APC_PPAUSE;
2040                             sbus_writel(csr, cs4231_chip->regs + APCCSR);
2041                             cs4231_disable_play(drv);
2042                             cs4231_chip->perchip_info.play.error = 1;
2043                     }
2044                     cs4231_chip->perchip_info.play.active = 0;
2045                     cs4231_playintr(drv, 0);
2046     
2047                     cs4231_getsamplecount(drv, cs4231_chip->playlen, 0);
2048             }
2049     
2050             if (dummy & APC_GENL_INT) {
2051                     /* If we get here we must be sharing an interrupt, but I haven't code 
2052                      * to handle this right now.
2053                      */
2054             }
2055     }
2056     
2057     static struct sparcaudio_operations cs4231_ops = {
2058     	cs4231_open,
2059     	cs4231_release,
2060     	cs4231_ioctl,
2061     	cs4231_start_output,
2062     	cs4231_stop_output,
2063     	cs4231_start_input,
2064             cs4231_stop_input,
2065     	cs4231_audio_getdev,
2066             cs4231_set_output_volume,
2067             cs4231_get_output_volume,
2068             cs4231_set_input_volume,
2069             cs4231_get_input_volume,
2070             cs4231_set_monitor_volume,
2071             cs4231_get_monitor_volume,
2072     	cs4231_set_output_balance,
2073     	cs4231_get_output_balance,
2074             cs4231_set_input_balance,
2075             cs4231_get_input_balance,
2076             cs4231_set_output_channels,
2077             cs4231_get_output_channels,
2078             cs4231_set_input_channels,
2079             cs4231_get_input_channels,
2080             cs4231_set_output_precision,
2081             cs4231_get_output_precision,
2082             cs4231_set_input_precision,
2083             cs4231_get_input_precision,
2084             cs4231_set_output_port,
2085             cs4231_get_output_port,
2086             cs4231_set_input_port,
2087             cs4231_get_input_port,
2088             cs4231_set_output_encoding,
2089             cs4231_get_output_encoding,
2090             cs4231_set_input_encoding,
2091             cs4231_get_input_encoding,
2092             cs4231_set_output_rate,
2093             cs4231_get_output_rate,
2094             cs4231_set_input_rate,
2095     	cs4231_get_input_rate,
2096     	cs4231_audio_getdev_sunos,
2097     	cs4231_get_output_ports,
2098     	cs4231_get_input_ports,
2099     	cs4231_output_muted,
2100     	cs4231_get_output_muted,
2101     	cs4231_set_output_pause,
2102     	cs4231_get_output_pause,
2103     	cs4231_set_input_pause,
2104     	cs4231_get_input_pause,
2105     	cs4231_set_output_samples,
2106     	cs4231_get_output_samples,
2107     	cs4231_set_input_samples,
2108     	cs4231_get_input_samples,
2109     	cs4231_set_output_error,
2110     	cs4231_get_output_error,
2111     	cs4231_set_input_error,
2112     	cs4231_get_input_error,
2113             cs4231_get_formats,
2114     };
2115     
2116     #ifdef EB4231_SUPPORT
2117     static struct sparcaudio_operations eb4231_ops = {
2118     	cs4231_open,
2119     	cs4231_release,
2120     	cs4231_ioctl,
2121     	eb4231_start_output,
2122     	eb4231_stop_output,
2123     	eb4231_start_input,
2124             eb4231_stop_input,
2125     	cs4231_audio_getdev,
2126             cs4231_set_output_volume,
2127             cs4231_get_output_volume,
2128             cs4231_set_input_volume,
2129             cs4231_get_input_volume,
2130             cs4231_set_monitor_volume,
2131             cs4231_get_monitor_volume,
2132     	cs4231_set_output_balance,
2133     	cs4231_get_output_balance,
2134             cs4231_set_input_balance,
2135             cs4231_get_input_balance,
2136             cs4231_set_output_channels,
2137             cs4231_get_output_channels,
2138             cs4231_set_input_channels,
2139             cs4231_get_input_channels,
2140             cs4231_set_output_precision,
2141             cs4231_get_output_precision,
2142             cs4231_set_input_precision,
2143             cs4231_get_input_precision,
2144             cs4231_set_output_port,
2145             cs4231_get_output_port,
2146             cs4231_set_input_port,
2147             cs4231_get_input_port,
2148             cs4231_set_output_encoding,
2149             cs4231_get_output_encoding,
2150             cs4231_set_input_encoding,
2151             cs4231_get_input_encoding,
2152             cs4231_set_output_rate,
2153             cs4231_get_output_rate,
2154             cs4231_set_input_rate,
2155     	cs4231_get_input_rate,
2156     	cs4231_audio_getdev_sunos,
2157     	cs4231_get_output_ports,
2158     	cs4231_get_input_ports,
2159     	cs4231_output_muted,
2160     	cs4231_get_output_muted,
2161     	cs4231_set_output_pause,
2162     	cs4231_get_output_pause,
2163     	cs4231_set_input_pause,
2164     	cs4231_get_input_pause,
2165     	cs4231_set_output_samples,
2166     	eb4231_get_output_samples,
2167     	cs4231_set_input_samples,
2168     	eb4231_get_input_samples,
2169     	cs4231_set_output_error,
2170     	cs4231_get_output_error,
2171     	cs4231_set_input_error,
2172     	cs4231_get_input_error,
2173             cs4231_get_formats,
2174     };
2175     #endif
2176     
2177     /* Attach to an cs4231 chip given its PROM node. */
2178     static int cs4231_attach(struct sparcaudio_driver *drv, 
2179     			 struct sbus_dev *sdev)
2180     {
2181             struct cs4231_chip *cs4231_chip;
2182             int err;
2183     
2184             /* Allocate our private information structure. */
2185             drv->private = kmalloc(sizeof(struct cs4231_chip), GFP_KERNEL);
2186             if (drv->private == NULL)
2187                     return -ENOMEM;
2188     
2189             /* Point at the information structure and initialize it. */
2190             drv->ops = &cs4231_ops;
2191             cs4231_chip = (struct cs4231_chip *) drv->private;
2192             cs4231_chip->input_ptr = cs4231_chip->output_ptr = NULL;
2193             cs4231_chip->input_size = cs4231_chip->output_size = 0;
2194             cs4231_chip->status = 0;
2195     
2196             drv->dev = sdev;
2197     
2198             /* Map the registers into memory. */
2199             cs4231_chip->regs_size = sdev->reg_addrs[0].reg_size;
2200             cs4231_chip->regs = sbus_ioremap(&sdev->resource[0], 0,
2201                                              sdev->reg_addrs[0].reg_size,
2202                                              "cs4231");
2203     
2204             if (!cs4231_chip->regs) {
2205                     printk(KERN_ERR "cs4231: could not remap registers\n");
2206                     kfree(drv->private);
2207                     return -EIO;
2208             }
2209     
2210             /* Attach the interrupt handler to the audio interrupt. */
2211             cs4231_chip->irq = sdev->irqs[0];
2212             request_irq(cs4231_chip->irq, cs4231_interrupt, SA_SHIRQ, "cs4231", drv);
2213             enable_irq(cs4231_chip->irq);
2214     
2215             cs4231_chip->nirqs = 1;
2216             cs4231_enable_interrupts(drv);
2217     
2218             /* Reset the audio chip. */
2219             cs4231_chip_reset(drv);
2220     
2221             /* Register ourselves with the midlevel audio driver. */
2222             err = register_sparcaudio_driver(drv, 1);
2223     
2224             if (err < 0) {
2225                     printk(KERN_ERR "cs4231: unable to register\n");
2226                     cs4231_disable_interrupts(drv);
2227                     disable_irq(cs4231_chip->irq);
2228                     free_irq(cs4231_chip->irq, drv);
2229                     sbus_iounmap(cs4231_chip->regs, cs4231_chip->regs_size);
2230                     kfree(drv->private);
2231                     return -EIO;
2232             }
2233     
2234             cs4231_chip->perchip_info.play.active = 
2235                     cs4231_chip->perchip_info.play.pause = 0;
2236     
2237             cs4231_chip->perchip_info.record.active = 
2238                     cs4231_chip->perchip_info.record.pause = 0;
2239     
2240             cs4231_chip->perchip_info.play.avail_ports = (AUDIO_HEADPHONE |
2241                                                           AUDIO_SPEAKER |
2242                                                           AUDIO_LINE_OUT);
2243     
2244             cs4231_chip->perchip_info.record.avail_ports = (AUDIO_INTERNAL_CD_IN |
2245                                                             AUDIO_LINE_IN | 
2246                                                             AUDIO_MICROPHONE |
2247                                                             AUDIO_ANALOG_LOOPBACK);
2248     
2249             /* Announce the hardware to the user. */
2250             printk(KERN_INFO "audio%d: cs4231%c at %lx irq %s\n",
2251                    drv->index, (cs4231_chip->status & CS_STATUS_REV_A) ? 'a' : ' ', 
2252                    cs4231_chip->regs, __irq_itoa(cs4231_chip->irq));
2253       
2254             /* Success! */
2255             return 0;
2256     }
2257     
2258     #ifdef EB4231_SUPPORT
2259     /* Attach to an cs4231 chip given its PROM node. */
2260     static int eb4231_attach(struct sparcaudio_driver *drv, 
2261     			 struct linux_ebus_device *edev)
2262     {
2263             struct cs4231_chip *cs4231_chip;
2264             int len, err, nregs;
2265             struct linux_prom_registers regs[4];
2266     
2267             /* Allocate our private information structure. */
2268             drv->private = kmalloc(sizeof(struct cs4231_chip), GFP_KERNEL);
2269             if (drv->private == NULL)
2270                     return -ENOMEM;
2271     
2272             /* Point at the information structure and initialize it. */
2273             drv->ops = &eb4231_ops;
2274             cs4231_chip = (struct cs4231_chip *) drv->private;
2275             cs4231_chip->input_ptr = cs4231_chip->output_ptr = NULL;
2276             cs4231_chip->input_size = cs4231_chip->output_size = 0;
2277             cs4231_chip->status = 0;
2278     
2279             drv->dev = (struct sbus_dev *)edev->bus->self;
2280     
2281             len = prom_getproperty(edev->prom_node, "reg", (void *)regs, sizeof(regs));
2282             if ((len % sizeof(regs[0])) != 0) {
2283                     printk("eb4231: Strange reg property size %d\n", len);
2284                     return -ENODEV;
2285             }
2286     
2287             nregs = len / sizeof(regs[0]);
2288             cs4231_chip->regs = (unsigned long)ioremap(edev->resource[0].start, 0x10);
2289             cs4231_chip->eb2p = (unsigned long)ioremap(edev->resource[1].start, 0x10);
2290             cs4231_chip->eb2c = (unsigned long)ioremap(edev->resource[2].start, 0x10);
2291     
2292             cs4231_chip->status |= CS_STATUS_IS_EBUS;
2293     
2294             /* Attach the interrupt handler to the audio interrupt. */
2295             cs4231_chip->irq = edev->irqs[0];
2296             cs4231_chip->irq2 = edev->irqs[1];
2297     
2298             if(request_irq(cs4231_chip->irq, eb4231_cinterrupt, SA_SHIRQ, "cs4231", drv) ||
2299                request_irq(cs4231_chip->irq2, eb4231_pinterrupt, SA_SHIRQ, "cs4231", drv))
2300                     goto bail;
2301     
2302             cs4231_chip->nirqs = 2;
2303             cs4231_enable_interrupts(drv);
2304     
2305             /* Reset the audio chip. */
2306             cs4231_chip_reset(drv);
2307     
2308             /* Register ourselves with the midlevel audio driver. */
2309             err = register_sparcaudio_driver(drv, 1);
2310     
2311             if (err < 0) {
2312             bail:
2313                     printk(KERN_ERR "cs4231: unable to register\n");
2314                     cs4231_disable_interrupts(drv);
2315                     disable_irq(cs4231_chip->irq);
2316                     free_irq(cs4231_chip->irq, drv);
2317                     disable_irq(cs4231_chip->irq2);
2318                     free_irq(cs4231_chip->irq2, drv);
2319                     kfree(drv->private);
2320                     return -EIO;
2321             }
2322     
2323             cs4231_chip->perchip_info.play.active = 
2324                     cs4231_chip->perchip_info.play.pause = 0;
2325     
2326             cs4231_chip->perchip_info.record.active = 
2327                     cs4231_chip->perchip_info.record.pause = 0;
2328     
2329             cs4231_chip->perchip_info.play.avail_ports = (AUDIO_HEADPHONE |
2330                                                           AUDIO_SPEAKER |
2331                                                           AUDIO_LINE_OUT);
2332     
2333             cs4231_chip->perchip_info.record.avail_ports = (AUDIO_INTERNAL_CD_IN |
2334                                                             AUDIO_LINE_IN | 
2335                                                             AUDIO_MICROPHONE |
2336                                                             AUDIO_ANALOG_LOOPBACK);
2337     
2338             /* Announce the hardware to the user. */
2339             printk(KERN_INFO "audio%d: cs4231%c(eb2) at %lx irq %s\n",
2340                    drv->index, (cs4231_chip->status & CS_STATUS_REV_A) ? 'a' : ' ', 
2341                    cs4231_chip->regs, __irq_itoa(cs4231_chip->irq));
2342       
2343             /* Success! */
2344             return 0;
2345     }
2346     #endif
2347     
2348     #ifdef EB4231_SUPPORT
2349     static int __init ebus_cs4231_p(struct linux_ebus_device *edev)
2350     {
2351             if (!strcmp(edev->prom_name, "SUNW,CS4231"))
2352                     return 1;
2353             if (!strcmp(edev->prom_name, "audio")) {
2354                     char compat[16];
2355     
2356                     prom_getstring(edev->prom_node, "compatible",
2357                                    compat, sizeof(compat));
2358                     compat[15] = '\0';
2359                     if (!strcmp(compat, "SUNW,CS4231"))
2360                             return 1;
2361             }
2362     
2363             return 0;
2364     }
2365     #endif
2366     
2367     /* Detach from an cs4231 chip given the device structure. */
2368     static void __exit cs4231_detach(struct sparcaudio_driver *drv)
2369     {
2370     	struct cs4231_chip *cs4231_chip = (struct cs4231_chip *) drv->private;
2371     
2372     	cs4231_disable_interrupts(drv);
2373     	unregister_sparcaudio_driver(drv, 1);
2374     	disable_irq(cs4231_chip->irq);
2375     	free_irq(cs4231_chip->irq, drv);
2376     	if (!(cs4231_chip->status & CS_STATUS_IS_EBUS)) {
2377     		sbus_iounmap(cs4231_chip->regs, cs4231_chip->regs_size);
2378     	} else {
2379     #ifdef EB4231_SUPPORT
2380     		iounmap(cs4231_chip->regs);
2381     		iounmap(cs4231_chip->eb2p);
2382     		iounmap(cs4231_chip->eb2c);
2383     		disable_irq(cs4231_chip->irq2);
2384     		free_irq(cs4231_chip->irq2, drv);
2385     #endif
2386     	}
2387     	kfree(drv->private);
2388     }
2389     
2390     
2391     /* Probe for the cs4231 chip and then attach the driver. */
2392     static int __init cs4231_init(void)
2393     {
2394             struct sbus_bus *sbus;
2395             struct sbus_dev *sdev;
2396     #ifdef EB4231_SUPPORT
2397             struct linux_ebus *ebus;
2398             struct linux_ebus_device *edev;
2399     #endif
2400     
2401             num_drivers = 0;
2402       
2403             /* Probe each SBUS for cs4231 chips. */
2404             for_all_sbusdev(sdev, sbus) {
2405                     if (!strcmp(sdev->prom_name, "SUNW,CS4231")) {
2406                             /* Don't go over the max number of drivers. */
2407                             if (num_drivers >= MAX_DRIVERS)
2408                                     continue;
2409           
2410                             if (cs4231_attach(&drivers[num_drivers], sdev) == 0)
2411                                     num_drivers++;
2412                     }
2413             }
2414       
2415     #ifdef EB4231_SUPPORT
2416             for_each_ebus(ebus) {
2417                     for_each_ebusdev(edev, ebus) {
2418                             if (ebus_cs4231_p(edev)) {
2419                                     /* Don't go over the max number of drivers. */
2420                                     if (num_drivers >= MAX_DRIVERS)
2421                                             continue;
2422           
2423                                     if (eb4231_attach(&drivers[num_drivers], edev) == 0)
2424                                             num_drivers++;
2425                             }
2426                     }
2427             }
2428     #endif
2429     
2430             /* Only return success if we found some cs4231 chips. */
2431             return (num_drivers > 0) ? 0 : -EIO;
2432     }
2433     
2434     static void __exit cs4231_exit(void)
2435     {
2436             register int i;
2437     
2438             for (i = 0; i < num_drivers; i++) {
2439                     cs4231_detach(&drivers[i]);
2440                     num_drivers--;
2441             }
2442     }
2443     
2444     module_init(cs4231_init);
2445     module_exit(cs4231_exit);
2446     /*
2447      * Overrides for Emacs so that we follow Linus's tabbing style.
2448      * Emacs will notice this stuff at the end of the file and automatically
2449      * adjust the settings for this buffer only.  This must remain at the end
2450      * of the file.
2451      * ---------------------------------------------------------------------------
2452      * Local variables:
2453      * c-indent-level: 4
2454      * c-brace-imaginary-offset: 0
2455      * c-brace-offset: -4
2456      * c-argdecl-indent: 4
2457      * c-label-offset: -4
2458      * c-continued-statement-offset: 4
2459      * c-continued-brace-offset: 0
2460      * indent-tabs-mode: nil
2461      * tab-width: 8
2462      * End:
2463      */
2464