4 * The low level driver for the AD1848/CS4248 codec chip which
5 * is used for example in the MS Sound System.
7 * The CS4231 which is used in the GUS MAX and some other cards is
8 * upwards compatible with AD1848 and this driver is able to drive it.
10 * CS4231A and AD1845 are upward compatible with CS4231. However
11 * the new features of these chips are different.
13 * CS4232 is a PnP audio chip which contains a CS4231A (and SB, MPU).
14 * CS4232A is an improved version of CS4232.
18 * Copyright (C) by Hannu Savolainen 1993-1997
20 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
21 * Version 2 (June 1991). See the "COPYING" file distributed with this software
25 * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
26 * general sleep/wakeup clean up.
27 * Alan Cox : reformatted. Fixed SMP bugs. Moved to kernel alloc/free
28 * of irqs. Use dev_id.
29 * Christoph Hellwig : adapted to module_init/module_exit
30 * Aki Laukkanen : added power management support
31 * Arnaldo C. de Melo : added missing restore_flags in ad1848_resume
32 * Miguel Freitas : added ISA PnP support
33 * Alan Cox : Added CS4236->4239 identification
34 * Daniel T. Cobra : Alernate config/mixer for later chips
35 * Alan Cox : Merged chip idents and config code
38 * APM save restore assist code on IBM thinkpad
41 * Tested. Believed fully functional.
44 #include <linux/init.h>
45 #include <linux/interrupt.h>
46 #include <linux/module.h>
47 #include <linux/stddef.h>
48 #include <linux/slab.h>
49 #include <linux/isapnp.h>
50 #include <linux/pnp.h>
51 #include <linux/spinlock.h>
53 #include "sound_config.h"
56 #include "ad1848_mixer.h"
64 int dual_dma
; /* 1, when two DMA channels allocated */
66 unsigned char MCE_bit
;
67 unsigned char saved_regs
[64]; /* Includes extended register space */
71 int record_dev
, playback_dev
;
77 char *chip_name
, *name
;
86 #define MD_4235 8 /* Crystal Audio CS4235 */
87 #define MD_1845_SSCAPE 9 /* Ensoniq Soundscape PNP*/
88 #define MD_4236 10 /* 4236 and higher */
89 #define MD_42xB 11 /* CS 42xB */
90 #define MD_4239 12 /* CS4239 */
92 /* Mixer parameters */
94 int supported_devices
, orig_devices
;
95 int supported_rec_devices
, orig_rec_devices
;
97 short mixer_reroute
[32];
99 volatile unsigned long timer_ticks
;
102 mixer_ents
*mix_devices
;
103 int mixer_output_port
;
106 typedef struct ad1848_port_info
110 unsigned char speed_bits
;
113 unsigned char format_bits
;
117 static struct address_info cfg
;
118 static int nr_ad1848_devs
;
120 static bool deskpro_xl
;
121 static bool deskpro_m
;
122 static bool soundpro
;
124 #ifndef EXCLUDE_TIMERS
125 static int timer_installed
= -1;
130 static int ad_format_mask
[13 /*devc->model */ ] =
133 AFMT_U8
| AFMT_S16_LE
| AFMT_MU_LAW
| AFMT_A_LAW
,
134 AFMT_U8
| AFMT_S16_LE
| AFMT_MU_LAW
| AFMT_A_LAW
| AFMT_S16_BE
| AFMT_IMA_ADPCM
,
135 AFMT_U8
| AFMT_S16_LE
| AFMT_MU_LAW
| AFMT_A_LAW
| AFMT_S16_BE
| AFMT_IMA_ADPCM
,
136 AFMT_U8
| AFMT_S16_LE
| AFMT_MU_LAW
| AFMT_A_LAW
, /* AD1845 */
137 AFMT_U8
| AFMT_S16_LE
| AFMT_MU_LAW
| AFMT_A_LAW
| AFMT_S16_BE
| AFMT_IMA_ADPCM
,
138 AFMT_U8
| AFMT_S16_LE
| AFMT_MU_LAW
| AFMT_A_LAW
| AFMT_S16_BE
| AFMT_IMA_ADPCM
,
139 AFMT_U8
| AFMT_S16_LE
| AFMT_MU_LAW
| AFMT_A_LAW
| AFMT_S16_BE
| AFMT_IMA_ADPCM
,
140 AFMT_U8
| AFMT_S16_LE
/* CS4235 */,
141 AFMT_U8
| AFMT_S16_LE
| AFMT_MU_LAW
| AFMT_A_LAW
/* Ensoniq Soundscape*/,
142 AFMT_U8
| AFMT_S16_LE
| AFMT_MU_LAW
| AFMT_A_LAW
| AFMT_S16_BE
| AFMT_IMA_ADPCM
,
143 AFMT_U8
| AFMT_S16_LE
| AFMT_MU_LAW
| AFMT_A_LAW
| AFMT_S16_BE
| AFMT_IMA_ADPCM
,
144 AFMT_U8
| AFMT_S16_LE
| AFMT_MU_LAW
| AFMT_A_LAW
| AFMT_S16_BE
| AFMT_IMA_ADPCM
147 static ad1848_info adev_info
[MAX_AUDIO_DEV
];
149 #define io_Index_Addr(d) ((d)->base)
150 #define io_Indexed_Data(d) ((d)->base+1)
151 #define io_Status(d) ((d)->base+2)
152 #define io_Polled_IO(d) ((d)->base+3)
156 #define CAP_F_TIMER 0x01
157 } capabilities
[10 /*devc->model */ ] = {
160 ,{CAP_F_TIMER
} /* MD_4231 */
161 ,{CAP_F_TIMER
} /* MD_4231A */
162 ,{CAP_F_TIMER
} /* MD_1845 */
163 ,{CAP_F_TIMER
} /* MD_4232 */
165 ,{CAP_F_TIMER
} /* MD_IWAVE */
167 ,{CAP_F_TIMER
} /* MD_1845_SSCAPE */
171 static int isapnp
= 1;
172 static int isapnpjump
;
175 static int audio_activated
;
182 static int ad1848_open(int dev
, int mode
);
183 static void ad1848_close(int dev
);
184 static void ad1848_output_block(int dev
, unsigned long buf
, int count
, int intrflag
);
185 static void ad1848_start_input(int dev
, unsigned long buf
, int count
, int intrflag
);
186 static int ad1848_prepare_for_output(int dev
, int bsize
, int bcount
);
187 static int ad1848_prepare_for_input(int dev
, int bsize
, int bcount
);
188 static void ad1848_halt(int dev
);
189 static void ad1848_halt_input(int dev
);
190 static void ad1848_halt_output(int dev
);
191 static void ad1848_trigger(int dev
, int bits
);
192 static irqreturn_t
adintr(int irq
, void *dev_id
);
194 #ifndef EXCLUDE_TIMERS
195 static int ad1848_tmr_install(int dev
);
196 static void ad1848_tmr_reprogram(int dev
);
199 static int ad_read(ad1848_info
* devc
, int reg
)
202 int timeout
= 900000;
204 while (timeout
> 0 && inb(devc
->base
) == 0x80) /*Are we initializing */
209 outb(((unsigned char) (reg
& 0xff) | devc
->MCE_bit
), io_Index_Addr(devc
));
210 x
= inb(io_Indexed_Data(devc
));
216 xreg
= (reg
& 0xff) - 32;
217 xra
= (((xreg
& 0x0f) << 4) & 0xf0) | 0x08 | ((xreg
& 0x10) >> 2);
218 outb(((unsigned char) (23 & 0xff) | devc
->MCE_bit
), io_Index_Addr(devc
));
219 outb(((unsigned char) (xra
& 0xff)), io_Indexed_Data(devc
));
220 x
= inb(io_Indexed_Data(devc
));
226 static void ad_write(ad1848_info
* devc
, int reg
, int data
)
228 int timeout
= 900000;
230 while (timeout
> 0 && inb(devc
->base
) == 0x80) /* Are we initializing */
235 outb(((unsigned char) (reg
& 0xff) | devc
->MCE_bit
), io_Index_Addr(devc
));
236 outb(((unsigned char) (data
& 0xff)), io_Indexed_Data(devc
));
242 xreg
= (reg
& 0xff) - 32;
243 xra
= (((xreg
& 0x0f) << 4) & 0xf0) | 0x08 | ((xreg
& 0x10) >> 2);
244 outb(((unsigned char) (23 & 0xff) | devc
->MCE_bit
), io_Index_Addr(devc
));
245 outb(((unsigned char) (xra
& 0xff)), io_Indexed_Data(devc
));
246 outb((unsigned char) (data
& 0xff), io_Indexed_Data(devc
));
250 static void wait_for_calibration(ad1848_info
* devc
)
255 * Wait until the auto calibration process has finished.
257 * 1) Wait until the chip becomes ready (reads don't return 0x80).
258 * 2) Wait until the ACI bit of I11 gets on and then off.
262 while (timeout
> 0 && inb(devc
->base
) == 0x80)
264 if (inb(devc
->base
) & 0x80)
265 printk(KERN_WARNING
"ad1848: Auto calibration timed out(1).\n");
268 while (timeout
> 0 && !(ad_read(devc
, 11) & 0x20))
270 if (!(ad_read(devc
, 11) & 0x20))
274 while (timeout
> 0 && (ad_read(devc
, 11) & 0x20))
276 if (ad_read(devc
, 11) & 0x20)
277 if ((devc
->model
!= MD_1845
) && (devc
->model
!= MD_1845_SSCAPE
))
278 printk(KERN_WARNING
"ad1848: Auto calibration timed out(3).\n");
281 static void ad_mute(ad1848_info
* devc
)
287 * Save old register settings and mute output channels
290 for (i
= 6; i
< 8; i
++)
292 prev
= devc
->saved_regs
[i
] = ad_read(devc
, i
);
297 static void ad_unmute(ad1848_info
* devc
)
301 static void ad_enter_MCE(ad1848_info
* devc
)
306 while (timeout
> 0 && inb(devc
->base
) == 0x80) /*Are we initializing */
309 devc
->MCE_bit
= 0x40;
310 prev
= inb(io_Index_Addr(devc
));
315 outb((devc
->MCE_bit
), io_Index_Addr(devc
));
318 static void ad_leave_MCE(ad1848_info
* devc
)
320 unsigned char prev
, acal
;
323 while (timeout
> 0 && inb(devc
->base
) == 0x80) /*Are we initializing */
326 acal
= ad_read(devc
, 9);
328 devc
->MCE_bit
= 0x00;
329 prev
= inb(io_Index_Addr(devc
));
330 outb((0x00), io_Index_Addr(devc
)); /* Clear the MCE bit */
332 if ((prev
& 0x40) == 0) /* Not in MCE mode */
336 outb((0x00), io_Index_Addr(devc
)); /* Clear the MCE bit */
337 if (acal
& 0x08) /* Auto calibration is enabled */
338 wait_for_calibration(devc
);
341 static int ad1848_set_recmask(ad1848_info
* devc
, int mask
)
343 unsigned char recdev
;
347 mask
&= devc
->supported_rec_devices
;
349 /* Rename the mixer bits if necessary */
350 for (i
= 0; i
< 32; i
++)
352 if (devc
->mixer_reroute
[i
] != i
)
357 mask
|= (1 << devc
->mixer_reroute
[i
]);
363 for (i
= 0; i
< 32; i
++) /* Count selected device bits */
367 spin_lock_irqsave(&devc
->lock
,flags
);
370 mask
= SOUND_MASK_MIC
;
371 else if (n
!= 1) { /* Too many devices selected */
372 mask
&= ~devc
->recmask
; /* Filter out active settings */
375 for (i
= 0; i
< 32; i
++) /* Count selected device bits */
380 mask
= SOUND_MASK_MIC
;
387 case SOUND_MASK_LINE
:
388 case SOUND_MASK_LINE3
:
393 case SOUND_MASK_LINE1
:
397 case SOUND_MASK_IMIX
:
402 mask
= SOUND_MASK_MIC
;
407 ad_write(devc
, 0, (ad_read(devc
, 0) & 0x3f) | recdev
);
408 ad_write(devc
, 1, (ad_read(devc
, 1) & 0x3f) | recdev
);
409 } else { /* soundpro */
414 for (i
= 0; i
< 32; i
++) { /* For each bit */
415 if ((devc
->supported_rec_devices
& (1 << i
)) == 0)
416 continue; /* Device not supported */
418 for (j
= LEFT_CHN
; j
<= RIGHT_CHN
; j
++) {
419 if (devc
->mix_devices
[i
][j
].nbits
== 0) /* Inexistent channel */
424 * set_rec_bit becomes 1 if the corresponding bit in mask is set
425 * then it gets flipped if the polarity is inverse
427 set_rec_bit
= ((mask
& (1 << i
)) != 0) ^ devc
->mix_devices
[i
][j
].recpol
;
429 val
= ad_read(devc
, devc
->mix_devices
[i
][j
].recreg
);
430 val
&= ~(1 << devc
->mix_devices
[i
][j
].recpos
);
431 val
|= (set_rec_bit
<< devc
->mix_devices
[i
][j
].recpos
);
432 ad_write(devc
, devc
->mix_devices
[i
][j
].recreg
, val
);
436 spin_unlock_irqrestore(&devc
->lock
,flags
);
438 /* Rename the mixer bits back if necessary */
439 for (i
= 0; i
< 32; i
++)
441 if (devc
->mixer_reroute
[i
] != i
)
443 if (mask
& (1 << devc
->mixer_reroute
[i
]))
445 mask
&= ~(1 << devc
->mixer_reroute
[i
]);
450 devc
->recmask
= mask
;
454 static void oss_change_bits(ad1848_info
*devc
, unsigned char *regval
,
455 unsigned char *muteval
, int dev
, int chn
, int newval
)
463 set_mute_bit
= (newval
== 0) ^ devc
->mix_devices
[dev
][chn
].mutepol
;
465 if (devc
->mix_devices
[dev
][chn
].polarity
== 1) /* Reverse */
466 newval
= 100 - newval
;
468 mask
= (1 << devc
->mix_devices
[dev
][chn
].nbits
) - 1;
469 shift
= devc
->mix_devices
[dev
][chn
].bitpos
;
471 if (devc
->mix_devices
[dev
][chn
].mutepos
== 8)
472 { /* if there is no mute bit */
473 mute
= 0; /* No mute bit; do nothing special */
474 mutemask
= ~0; /* No mute bit; do nothing special */
478 mute
= (set_mute_bit
<< devc
->mix_devices
[dev
][chn
].mutepos
);
479 mutemask
= ~(1 << devc
->mix_devices
[dev
][chn
].mutepos
);
482 newval
= (int) ((newval
* mask
) + 50) / 100; /* Scale it */
483 *regval
&= ~(mask
<< shift
); /* Clear bits */
484 *regval
|= (newval
& mask
) << shift
; /* Set new value */
486 *muteval
&= mutemask
;
490 static int ad1848_mixer_get(ad1848_info
* devc
, int dev
)
492 if (!((1 << dev
) & devc
->supported_devices
))
495 dev
= devc
->mixer_reroute
[dev
];
497 return devc
->levels
[dev
];
500 static void ad1848_mixer_set_channel(ad1848_info
*devc
, int dev
, int value
, int channel
)
502 int regoffs
, muteregoffs
;
503 unsigned char val
, muteval
;
506 regoffs
= devc
->mix_devices
[dev
][channel
].regno
;
507 muteregoffs
= devc
->mix_devices
[dev
][channel
].mutereg
;
508 val
= ad_read(devc
, regoffs
);
510 if (muteregoffs
!= regoffs
) {
511 muteval
= ad_read(devc
, muteregoffs
);
512 oss_change_bits(devc
, &val
, &muteval
, dev
, channel
, value
);
515 oss_change_bits(devc
, &val
, &val
, dev
, channel
, value
);
517 spin_lock_irqsave(&devc
->lock
,flags
);
518 ad_write(devc
, regoffs
, val
);
519 devc
->saved_regs
[regoffs
] = val
;
520 if (muteregoffs
!= regoffs
) {
521 ad_write(devc
, muteregoffs
, muteval
);
522 devc
->saved_regs
[muteregoffs
] = muteval
;
524 spin_unlock_irqrestore(&devc
->lock
,flags
);
527 static int ad1848_mixer_set(ad1848_info
* devc
, int dev
, int value
)
529 int left
= value
& 0x000000ff;
530 int right
= (value
& 0x0000ff00) >> 8;
536 if (!(devc
->supported_devices
& (1 << dev
)))
539 dev
= devc
->mixer_reroute
[dev
];
541 if (devc
->mix_devices
[dev
][LEFT_CHN
].nbits
== 0)
549 if (devc
->mix_devices
[dev
][RIGHT_CHN
].nbits
== 0) /* Mono control */
552 retvol
= left
| (right
<< 8);
555 left
= mix_cvt
[left
];
556 right
= mix_cvt
[right
];
558 devc
->levels
[dev
] = retvol
;
561 * Set the left channel
563 ad1848_mixer_set_channel(devc
, dev
, left
, LEFT_CHN
);
566 * Set the right channel
568 if (devc
->mix_devices
[dev
][RIGHT_CHN
].nbits
== 0)
570 ad1848_mixer_set_channel(devc
, dev
, right
, RIGHT_CHN
);
576 static void ad1848_mixer_reset(ad1848_info
* devc
)
582 devc
->mix_devices
= &(ad1848_mix_devices
[0]);
584 sprintf(name
, "%s_%d", devc
->chip_name
, nr_ad1848_devs
);
586 for (i
= 0; i
< 32; i
++)
587 devc
->mixer_reroute
[i
] = i
;
589 devc
->supported_rec_devices
= MODE1_REC_DEVICES
;
597 devc
->supported_devices
= MODE2_MIXER_DEVICES
;
601 devc
->supported_devices
= C930_MIXER_DEVICES
;
602 devc
->mix_devices
= &(c930_mix_devices
[0]);
606 devc
->supported_devices
= MODE3_MIXER_DEVICES
;
607 devc
->mix_devices
= &(iwave_mix_devices
[0]);
612 devc
->mix_devices
= &(cs42xb_mix_devices
[0]);
613 devc
->supported_devices
= MODE3_MIXER_DEVICES
;
618 devc
->supported_devices
= MODE3_MIXER_DEVICES
;
623 devc
->supported_devices
= SPRO_MIXER_DEVICES
;
624 devc
->supported_rec_devices
= SPRO_REC_DEVICES
;
625 devc
->mix_devices
= &(spro_mix_devices
[0]);
630 devc
->supported_devices
= MODE1_MIXER_DEVICES
;
633 devc
->orig_devices
= devc
->supported_devices
;
634 devc
->orig_rec_devices
= devc
->supported_rec_devices
;
636 devc
->levels
= load_mixer_volumes(name
, default_mixer_levels
, 1);
638 for (i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++)
640 if (devc
->supported_devices
& (1 << i
))
641 ad1848_mixer_set(devc
, i
, devc
->levels
[i
]);
644 ad1848_set_recmask(devc
, SOUND_MASK_MIC
);
646 devc
->mixer_output_port
= devc
->levels
[31] | AUDIO_HEADPHONE
| AUDIO_LINE_OUT
;
648 spin_lock_irqsave(&devc
->lock
,flags
);
650 if (devc
->mixer_output_port
& AUDIO_SPEAKER
)
651 ad_write(devc
, 26, ad_read(devc
, 26) & ~0x40); /* Unmute mono out */
653 ad_write(devc
, 26, ad_read(devc
, 26) | 0x40); /* Mute mono out */
656 * From the "wouldn't it be nice if the mixer API had (better)
657 * support for custom stuff" category
659 /* Enable surround mode and SB16 mixer */
660 ad_write(devc
, 16, 0x60);
662 spin_unlock_irqrestore(&devc
->lock
,flags
);
665 static int ad1848_mixer_ioctl(int dev
, unsigned int cmd
, void __user
*arg
)
667 ad1848_info
*devc
= mixer_devs
[dev
]->devc
;
670 if (cmd
== SOUND_MIXER_PRIVATE1
)
672 if (get_user(val
, (int __user
*)arg
))
678 val
&= (AUDIO_SPEAKER
| AUDIO_HEADPHONE
| AUDIO_LINE_OUT
);
679 devc
->mixer_output_port
= val
;
680 val
|= AUDIO_HEADPHONE
| AUDIO_LINE_OUT
; /* Always on */
681 devc
->mixer_output_port
= val
;
682 spin_lock_irqsave(&devc
->lock
,flags
);
683 if (val
& AUDIO_SPEAKER
)
684 ad_write(devc
, 26, ad_read(devc
, 26) & ~0x40); /* Unmute mono out */
686 ad_write(devc
, 26, ad_read(devc
, 26) | 0x40); /* Mute mono out */
687 spin_unlock_irqrestore(&devc
->lock
,flags
);
689 val
= devc
->mixer_output_port
;
690 return put_user(val
, (int __user
*)arg
);
692 if (cmd
== SOUND_MIXER_PRIVATE2
)
694 if (get_user(val
, (int __user
*)arg
))
696 return(ad1848_control(AD1848_MIXER_REROUTE
, val
));
698 if (((cmd
>> 8) & 0xff) == 'M')
700 if (_SIOC_DIR(cmd
) & _SIOC_WRITE
)
704 case SOUND_MIXER_RECSRC
:
705 if (get_user(val
, (int __user
*)arg
))
707 val
= ad1848_set_recmask(devc
, val
);
711 if (get_user(val
, (int __user
*)arg
))
713 val
= ad1848_mixer_set(devc
, cmd
& 0xff, val
);
716 return put_user(val
, (int __user
*)arg
);
726 case SOUND_MIXER_RECSRC
:
730 case SOUND_MIXER_DEVMASK
:
731 val
= devc
->supported_devices
;
734 case SOUND_MIXER_STEREODEVS
:
735 val
= devc
->supported_devices
;
736 if (devc
->model
!= MD_C930
)
737 val
&= ~(SOUND_MASK_SPEAKER
| SOUND_MASK_IMIX
);
740 case SOUND_MIXER_RECMASK
:
741 val
= devc
->supported_rec_devices
;
744 case SOUND_MIXER_CAPS
:
745 val
=SOUND_CAP_EXCL_INPUT
;
749 val
= ad1848_mixer_get(devc
, cmd
& 0xff);
752 return put_user(val
, (int __user
*)arg
);
759 static int ad1848_set_speed(int dev
, int arg
)
761 ad1848_info
*devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
762 ad1848_port_info
*portc
= (ad1848_port_info
*) audio_devs
[dev
]->portc
;
765 * The sampling speed is encoded in the least significant nibble of I8. The
766 * LSB selects the clock source (0=24.576 MHz, 1=16.9344 MHz) and other
767 * three bits select the divisor (indirectly):
769 * The available speeds are in the following table. Keep the speeds in
770 * the increasing order.
779 static speed_struct speed_table
[] =
781 {5510, (0 << 1) | 1},
782 {5510, (0 << 1) | 1},
783 {6620, (7 << 1) | 1},
784 {8000, (0 << 1) | 0},
785 {9600, (7 << 1) | 0},
786 {11025, (1 << 1) | 1},
787 {16000, (1 << 1) | 0},
788 {18900, (2 << 1) | 1},
789 {22050, (3 << 1) | 1},
790 {27420, (2 << 1) | 0},
791 {32000, (3 << 1) | 0},
792 {33075, (6 << 1) | 1},
793 {37800, (4 << 1) | 1},
794 {44100, (5 << 1) | 1},
795 {48000, (6 << 1) | 0}
798 int i
, n
, selected
= -1;
800 n
= sizeof(speed_table
) / sizeof(speed_struct
);
805 if (devc
->model
== MD_1845
|| devc
->model
== MD_1845_SSCAPE
) /* AD1845 has different timer than others */
813 portc
->speed_bits
= speed_table
[3].bits
;
816 if (arg
< speed_table
[0].speed
)
818 if (arg
> speed_table
[n
- 1].speed
)
821 for (i
= 1 /*really */ ; selected
== -1 && i
< n
; i
++)
823 if (speed_table
[i
].speed
== arg
)
825 else if (speed_table
[i
].speed
> arg
)
829 diff1
= arg
- speed_table
[i
- 1].speed
;
830 diff2
= speed_table
[i
].speed
- arg
;
840 printk(KERN_WARNING
"ad1848: Can't find speed???\n");
843 portc
->speed
= speed_table
[selected
].speed
;
844 portc
->speed_bits
= speed_table
[selected
].bits
;
848 static short ad1848_set_channels(int dev
, short arg
)
850 ad1848_port_info
*portc
= (ad1848_port_info
*) audio_devs
[dev
]->portc
;
852 if (arg
!= 1 && arg
!= 2)
853 return portc
->channels
;
855 portc
->channels
= arg
;
859 static unsigned int ad1848_set_bits(int dev
, unsigned int arg
)
861 ad1848_info
*devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
862 ad1848_port_info
*portc
= (ad1848_port_info
*) audio_devs
[dev
]->portc
;
864 static struct format_tbl
911 int i
, n
= sizeof(format2bits
) / sizeof(struct format_tbl
);
914 return portc
->audio_format
;
916 if (!(arg
& ad_format_mask
[devc
->model
]))
919 portc
->audio_format
= arg
;
921 for (i
= 0; i
< n
; i
++)
922 if (format2bits
[i
].format
== arg
)
924 if ((portc
->format_bits
= format2bits
[i
].bits
) == 0)
925 return portc
->audio_format
= AFMT_U8
; /* Was not supported */
929 /* Still hanging here. Something must be terribly wrong */
930 portc
->format_bits
= 0;
931 return portc
->audio_format
= AFMT_U8
;
934 static struct audio_driver ad1848_audio_driver
=
936 .owner
= THIS_MODULE
,
938 .close
= ad1848_close
,
939 .output_block
= ad1848_output_block
,
940 .start_input
= ad1848_start_input
,
941 .prepare_for_input
= ad1848_prepare_for_input
,
942 .prepare_for_output
= ad1848_prepare_for_output
,
943 .halt_io
= ad1848_halt
,
944 .halt_input
= ad1848_halt_input
,
945 .halt_output
= ad1848_halt_output
,
946 .trigger
= ad1848_trigger
,
947 .set_speed
= ad1848_set_speed
,
948 .set_bits
= ad1848_set_bits
,
949 .set_channels
= ad1848_set_channels
952 static struct mixer_operations ad1848_mixer_operations
=
954 .owner
= THIS_MODULE
,
956 .name
= "AD1848/CS4248/CS4231",
957 .ioctl
= ad1848_mixer_ioctl
960 static int ad1848_open(int dev
, int mode
)
963 ad1848_port_info
*portc
;
966 if (dev
< 0 || dev
>= num_audiodevs
)
969 devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
970 portc
= (ad1848_port_info
*) audio_devs
[dev
]->portc
;
972 /* here we don't have to protect against intr */
973 spin_lock(&devc
->lock
);
974 if (portc
->open_mode
|| (devc
->open_mode
& mode
))
976 spin_unlock(&devc
->lock
);
981 if (audio_devs
[dev
]->flags
& DMA_DUPLEX
)
985 devc
->intr_active
= 0;
986 devc
->audio_mode
= 0;
987 devc
->open_mode
|= mode
;
988 portc
->open_mode
= mode
;
989 spin_unlock(&devc
->lock
);
990 ad1848_trigger(dev
, 0);
992 if (mode
& OPEN_READ
)
993 devc
->record_dev
= dev
;
994 if (mode
& OPEN_WRITE
)
995 devc
->playback_dev
= dev
;
997 * Mute output until the playback really starts. This decreases clicking (hope so).
999 spin_lock_irqsave(&devc
->lock
,flags
);
1001 spin_unlock_irqrestore(&devc
->lock
,flags
);
1006 static void ad1848_close(int dev
)
1008 unsigned long flags
;
1009 ad1848_info
*devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
1010 ad1848_port_info
*portc
= (ad1848_port_info
*) audio_devs
[dev
]->portc
;
1012 devc
->intr_active
= 0;
1015 spin_lock_irqsave(&devc
->lock
,flags
);
1017 devc
->audio_mode
= 0;
1018 devc
->open_mode
&= ~portc
->open_mode
;
1019 portc
->open_mode
= 0;
1022 spin_unlock_irqrestore(&devc
->lock
,flags
);
1025 static void ad1848_output_block(int dev
, unsigned long buf
, int count
, int intrflag
)
1027 unsigned long flags
, cnt
;
1028 ad1848_info
*devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
1029 ad1848_port_info
*portc
= (ad1848_port_info
*) audio_devs
[dev
]->portc
;
1033 if (portc
->audio_format
== AFMT_IMA_ADPCM
)
1039 if (portc
->audio_format
& (AFMT_S16_LE
| AFMT_S16_BE
)) /* 16 bit data */
1042 if (portc
->channels
> 1)
1046 if ((devc
->audio_mode
& PCM_ENABLE_OUTPUT
) && (audio_devs
[dev
]->flags
& DMA_AUTOMODE
) &&
1048 cnt
== devc
->xfer_count
)
1050 devc
->audio_mode
|= PCM_ENABLE_OUTPUT
;
1051 devc
->intr_active
= 1;
1053 * Auto DMA mode on. No need to react
1056 spin_lock_irqsave(&devc
->lock
,flags
);
1058 ad_write(devc
, 15, (unsigned char) (cnt
& 0xff));
1059 ad_write(devc
, 14, (unsigned char) ((cnt
>> 8) & 0xff));
1061 devc
->xfer_count
= cnt
;
1062 devc
->audio_mode
|= PCM_ENABLE_OUTPUT
;
1063 devc
->intr_active
= 1;
1064 spin_unlock_irqrestore(&devc
->lock
,flags
);
1067 static void ad1848_start_input(int dev
, unsigned long buf
, int count
, int intrflag
)
1069 unsigned long flags
, cnt
;
1070 ad1848_info
*devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
1071 ad1848_port_info
*portc
= (ad1848_port_info
*) audio_devs
[dev
]->portc
;
1074 if (portc
->audio_format
== AFMT_IMA_ADPCM
)
1080 if (portc
->audio_format
& (AFMT_S16_LE
| AFMT_S16_BE
)) /* 16 bit data */
1083 if (portc
->channels
> 1)
1087 if ((devc
->audio_mode
& PCM_ENABLE_INPUT
) && (audio_devs
[dev
]->flags
& DMA_AUTOMODE
) &&
1089 cnt
== devc
->xfer_count
)
1091 devc
->audio_mode
|= PCM_ENABLE_INPUT
;
1092 devc
->intr_active
= 1;
1094 * Auto DMA mode on. No need to react
1097 spin_lock_irqsave(&devc
->lock
,flags
);
1099 if (devc
->model
== MD_1848
)
1101 ad_write(devc
, 15, (unsigned char) (cnt
& 0xff));
1102 ad_write(devc
, 14, (unsigned char) ((cnt
>> 8) & 0xff));
1106 ad_write(devc
, 31, (unsigned char) (cnt
& 0xff));
1107 ad_write(devc
, 30, (unsigned char) ((cnt
>> 8) & 0xff));
1112 devc
->xfer_count
= cnt
;
1113 devc
->audio_mode
|= PCM_ENABLE_INPUT
;
1114 devc
->intr_active
= 1;
1115 spin_unlock_irqrestore(&devc
->lock
,flags
);
1118 static int ad1848_prepare_for_output(int dev
, int bsize
, int bcount
)
1121 unsigned char fs
, old_fs
, tmp
= 0;
1122 unsigned long flags
;
1123 ad1848_info
*devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
1124 ad1848_port_info
*portc
= (ad1848_port_info
*) audio_devs
[dev
]->portc
;
1128 spin_lock_irqsave(&devc
->lock
,flags
);
1129 fs
= portc
->speed_bits
| (portc
->format_bits
<< 5);
1131 if (portc
->channels
> 1)
1134 ad_enter_MCE(devc
); /* Enables changes to the format select reg */
1136 if (devc
->model
== MD_1845
|| devc
->model
== MD_1845_SSCAPE
) /* Use alternate speed select registers */
1138 fs
&= 0xf0; /* Mask off the rate select bits */
1140 ad_write(devc
, 22, (portc
->speed
>> 8) & 0xff); /* Speed MSB */
1141 ad_write(devc
, 23, portc
->speed
& 0xff); /* Speed LSB */
1143 old_fs
= ad_read(devc
, 8);
1145 if (devc
->model
== MD_4232
|| devc
->model
>= MD_4236
)
1147 tmp
= ad_read(devc
, 16);
1148 ad_write(devc
, 16, tmp
| 0x30);
1150 if (devc
->model
== MD_IWAVE
)
1151 ad_write(devc
, 17, 0xc2); /* Disable variable frequency select */
1153 ad_write(devc
, 8, fs
);
1156 * Write to I8 starts resynchronization. Wait until it completes.
1160 while (timeout
< 100 && inb(devc
->base
) != 0x80)
1163 while (timeout
< 10000 && inb(devc
->base
) == 0x80)
1166 if (devc
->model
>= MD_4232
)
1167 ad_write(devc
, 16, tmp
& ~0x30);
1169 ad_leave_MCE(devc
); /*
1170 * Starts the calibration process.
1172 spin_unlock_irqrestore(&devc
->lock
,flags
);
1173 devc
->xfer_count
= 0;
1175 #ifndef EXCLUDE_TIMERS
1176 if (dev
== timer_installed
&& devc
->timer_running
)
1177 if ((fs
& 0x01) != (old_fs
& 0x01))
1179 ad1848_tmr_reprogram(dev
);
1182 ad1848_halt_output(dev
);
1186 static int ad1848_prepare_for_input(int dev
, int bsize
, int bcount
)
1189 unsigned char fs
, old_fs
, tmp
= 0;
1190 unsigned long flags
;
1191 ad1848_info
*devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
1192 ad1848_port_info
*portc
= (ad1848_port_info
*) audio_devs
[dev
]->portc
;
1194 if (devc
->audio_mode
)
1197 spin_lock_irqsave(&devc
->lock
,flags
);
1198 fs
= portc
->speed_bits
| (portc
->format_bits
<< 5);
1200 if (portc
->channels
> 1)
1203 ad_enter_MCE(devc
); /* Enables changes to the format select reg */
1205 if ((devc
->model
== MD_1845
) || (devc
->model
== MD_1845_SSCAPE
)) /* Use alternate speed select registers */
1207 fs
&= 0xf0; /* Mask off the rate select bits */
1209 ad_write(devc
, 22, (portc
->speed
>> 8) & 0xff); /* Speed MSB */
1210 ad_write(devc
, 23, portc
->speed
& 0xff); /* Speed LSB */
1212 if (devc
->model
== MD_4232
)
1214 tmp
= ad_read(devc
, 16);
1215 ad_write(devc
, 16, tmp
| 0x30);
1217 if (devc
->model
== MD_IWAVE
)
1218 ad_write(devc
, 17, 0xc2); /* Disable variable frequency select */
1221 * If mode >= 2 (CS4231), set I28. It's the capture format register.
1224 if (devc
->model
!= MD_1848
)
1226 old_fs
= ad_read(devc
, 28);
1227 ad_write(devc
, 28, fs
);
1230 * Write to I28 starts resynchronization. Wait until it completes.
1234 while (timeout
< 100 && inb(devc
->base
) != 0x80)
1238 while (timeout
< 10000 && inb(devc
->base
) == 0x80)
1241 if (devc
->model
!= MD_1848
&& devc
->model
!= MD_1845
&& devc
->model
!= MD_1845_SSCAPE
)
1244 * CS4231 compatible devices don't have separate sampling rate selection
1245 * register for recording an playback. The I8 register is shared so we have to
1246 * set the speed encoding bits of it too.
1248 unsigned char tmp
= portc
->speed_bits
| (ad_read(devc
, 8) & 0xf0);
1250 ad_write(devc
, 8, tmp
);
1252 * Write to I8 starts resynchronization. Wait until it completes.
1255 while (timeout
< 100 && inb(devc
->base
) != 0x80)
1259 while (timeout
< 10000 && inb(devc
->base
) == 0x80)
1264 { /* For AD1848 set I8. */
1266 old_fs
= ad_read(devc
, 8);
1267 ad_write(devc
, 8, fs
);
1269 * Write to I8 starts resynchronization. Wait until it completes.
1272 while (timeout
< 100 && inb(devc
->base
) != 0x80)
1275 while (timeout
< 10000 && inb(devc
->base
) == 0x80)
1279 if (devc
->model
== MD_4232
)
1280 ad_write(devc
, 16, tmp
& ~0x30);
1282 ad_leave_MCE(devc
); /*
1283 * Starts the calibration process.
1285 spin_unlock_irqrestore(&devc
->lock
,flags
);
1286 devc
->xfer_count
= 0;
1288 #ifndef EXCLUDE_TIMERS
1289 if (dev
== timer_installed
&& devc
->timer_running
)
1291 if ((fs
& 0x01) != (old_fs
& 0x01))
1293 ad1848_tmr_reprogram(dev
);
1297 ad1848_halt_input(dev
);
1301 static void ad1848_halt(int dev
)
1303 ad1848_info
*devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
1304 ad1848_port_info
*portc
= (ad1848_port_info
*) audio_devs
[dev
]->portc
;
1306 unsigned char bits
= ad_read(devc
, 9);
1308 if (bits
& 0x01 && (portc
->open_mode
& OPEN_WRITE
))
1309 ad1848_halt_output(dev
);
1311 if (bits
& 0x02 && (portc
->open_mode
& OPEN_READ
))
1312 ad1848_halt_input(dev
);
1313 devc
->audio_mode
= 0;
1316 static void ad1848_halt_input(int dev
)
1318 ad1848_info
*devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
1319 unsigned long flags
;
1321 if (!(ad_read(devc
, 9) & 0x02))
1322 return; /* Capture not enabled */
1324 spin_lock_irqsave(&devc
->lock
,flags
);
1331 if(!isa_dma_bridge_buggy
)
1332 disable_dma(audio_devs
[dev
]->dmap_in
->dma
);
1334 for (tmout
= 0; tmout
< 100000; tmout
++)
1335 if (ad_read(devc
, 11) & 0x10)
1337 ad_write(devc
, 9, ad_read(devc
, 9) & ~0x02); /* Stop capture */
1339 if(!isa_dma_bridge_buggy
)
1340 enable_dma(audio_devs
[dev
]->dmap_in
->dma
);
1341 devc
->audio_mode
&= ~PCM_ENABLE_INPUT
;
1344 outb(0, io_Status(devc
)); /* Clear interrupt status */
1345 outb(0, io_Status(devc
)); /* Clear interrupt status */
1347 devc
->audio_mode
&= ~PCM_ENABLE_INPUT
;
1349 spin_unlock_irqrestore(&devc
->lock
,flags
);
1352 static void ad1848_halt_output(int dev
)
1354 ad1848_info
*devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
1355 unsigned long flags
;
1357 if (!(ad_read(devc
, 9) & 0x01))
1358 return; /* Playback not enabled */
1360 spin_lock_irqsave(&devc
->lock
,flags
);
1366 if(!isa_dma_bridge_buggy
)
1367 disable_dma(audio_devs
[dev
]->dmap_out
->dma
);
1369 for (tmout
= 0; tmout
< 100000; tmout
++)
1370 if (ad_read(devc
, 11) & 0x10)
1372 ad_write(devc
, 9, ad_read(devc
, 9) & ~0x01); /* Stop playback */
1374 if(!isa_dma_bridge_buggy
)
1375 enable_dma(audio_devs
[dev
]->dmap_out
->dma
);
1377 devc
->audio_mode
&= ~PCM_ENABLE_OUTPUT
;
1380 outb((0), io_Status(devc
)); /* Clear interrupt status */
1381 outb((0), io_Status(devc
)); /* Clear interrupt status */
1383 devc
->audio_mode
&= ~PCM_ENABLE_OUTPUT
;
1385 spin_unlock_irqrestore(&devc
->lock
,flags
);
1388 static void ad1848_trigger(int dev
, int state
)
1390 ad1848_info
*devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
1391 ad1848_port_info
*portc
= (ad1848_port_info
*) audio_devs
[dev
]->portc
;
1392 unsigned long flags
;
1393 unsigned char tmp
, old
;
1395 spin_lock_irqsave(&devc
->lock
,flags
);
1396 state
&= devc
->audio_mode
;
1398 tmp
= old
= ad_read(devc
, 9);
1400 if (portc
->open_mode
& OPEN_READ
)
1402 if (state
& PCM_ENABLE_INPUT
)
1407 if (portc
->open_mode
& OPEN_WRITE
)
1409 if (state
& PCM_ENABLE_OUTPUT
)
1414 /* ad_mute(devc); */
1417 ad_write(devc
, 9, tmp
);
1420 spin_unlock_irqrestore(&devc
->lock
,flags
);
1423 static void ad1848_init_hw(ad1848_info
* devc
)
1429 * Initial values for the indirect registers of CS4248/AD1848.
1431 static int init_values_a
[] =
1433 0xa8, 0xa8, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
1434 0x00, 0x0c, 0x02, 0x00, 0x8a, 0x01, 0x00, 0x00,
1436 /* Positions 16 to 31 just for CS4231/2 and ad1845 */
1437 0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x1f, 0x40,
1438 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1441 static int init_values_b
[] =
1444 Values for the newer chips
1445 Some of the register initialization values were changed. In
1446 order to get rid of the click that preceded PCM playback,
1447 calibration was disabled on the 10th byte. On that same byte,
1448 dual DMA was enabled; on the 11th byte, ADC dithering was
1449 enabled, since that is theoretically desirable; on the 13th
1450 byte, Mode 3 was selected, to enable access to extended
1453 0xa8, 0xa8, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
1454 0x00, 0x00, 0x06, 0x00, 0xe0, 0x01, 0x00, 0x00,
1455 0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x1f, 0x40,
1456 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1460 * Select initialisation data
1463 init_values
= init_values_a
;
1464 if(devc
->model
>= MD_4236
)
1465 init_values
= init_values_b
;
1467 for (i
= 0; i
< 16; i
++)
1468 ad_write(devc
, i
, init_values
[i
]);
1471 ad_mute(devc
); /* Initialize some variables */
1472 ad_unmute(devc
); /* Leave it unmuted now */
1474 if (devc
->model
> MD_1848
)
1476 if (devc
->model
== MD_1845_SSCAPE
)
1477 ad_write(devc
, 12, ad_read(devc
, 12) | 0x50);
1479 ad_write(devc
, 12, ad_read(devc
, 12) | 0x40); /* Mode2 = enabled */
1481 if (devc
->model
== MD_IWAVE
)
1482 ad_write(devc
, 12, 0x6c); /* Select codec mode 3 */
1484 if (devc
->model
!= MD_1845_SSCAPE
)
1485 for (i
= 16; i
< 32; i
++)
1486 ad_write(devc
, i
, init_values
[i
]);
1488 if (devc
->model
== MD_IWAVE
)
1489 ad_write(devc
, 16, 0x30); /* Playback and capture counters enabled */
1491 if (devc
->model
> MD_1848
)
1493 if (devc
->audio_flags
& DMA_DUPLEX
)
1494 ad_write(devc
, 9, ad_read(devc
, 9) & ~0x04); /* Dual DMA mode */
1496 ad_write(devc
, 9, ad_read(devc
, 9) | 0x04); /* Single DMA mode */
1498 if (devc
->model
== MD_1845
|| devc
->model
== MD_1845_SSCAPE
)
1499 ad_write(devc
, 27, ad_read(devc
, 27) | 0x08); /* Alternate freq select enabled */
1501 if (devc
->model
== MD_IWAVE
)
1502 { /* Some magic Interwave specific initialization */
1503 ad_write(devc
, 12, 0x6c); /* Select codec mode 3 */
1504 ad_write(devc
, 16, 0x30); /* Playback and capture counters enabled */
1505 ad_write(devc
, 17, 0xc2); /* Alternate feature enable */
1510 devc
->audio_flags
&= ~DMA_DUPLEX
;
1511 ad_write(devc
, 9, ad_read(devc
, 9) | 0x04); /* Single DMA mode */
1513 ad_write(devc
, 12, ad_read(devc
, 12) | 0x40); /* Mode2 = enabled */
1516 outb((0), io_Status(devc
)); /* Clear pending interrupts */
1519 * Toggle the MCE bit. It completes the initialization phase.
1522 ad_enter_MCE(devc
); /* In case the bit was off */
1525 ad1848_mixer_reset(devc
);
1528 int ad1848_detect(struct resource
*ports
, int *ad_flags
, int *osp
)
1531 ad1848_info
*devc
= &adev_info
[nr_ad1848_devs
];
1532 unsigned char tmp1
= 0xff, tmp2
= 0xff;
1533 int optiC930
= 0; /* OPTi 82C930 flag */
1535 int ad1847_flag
= 0;
1536 int cs4248_flag
= 0;
1537 int sscape_flag
= 0;
1538 int io_base
= ports
->start
;
1542 DDB(printk("ad1848_detect(%x)\n", io_base
));
1546 if (*ad_flags
== 0x12345678)
1552 if (*ad_flags
== 0x87654321)
1558 if (*ad_flags
== 0x12345677)
1564 if (nr_ad1848_devs
>= MAX_AUDIO_DEV
)
1566 printk(KERN_ERR
"ad1848 - Too many audio devices\n");
1569 spin_lock_init(&devc
->lock
);
1570 devc
->base
= io_base
;
1572 devc
->timer_running
= 0;
1573 devc
->MCE_bit
= 0x40;
1575 devc
->open_mode
= 0;
1576 devc
->chip_name
= devc
->name
= "AD1848";
1577 devc
->model
= MD_1848
; /* AD1848 or CS4248 */
1578 devc
->levels
= NULL
;
1579 devc
->debug_flag
= 0;
1582 * Check that the I/O address is in use.
1584 * The bit 0x80 of the base I/O port is known to be 0 after the
1585 * chip has performed its power on initialization. Just assume
1586 * this has happened before the OS is starting.
1588 * If the I/O address is unused, it typically returns 0xff.
1591 if (inb(devc
->base
) == 0xff)
1593 DDB(printk("ad1848_detect: The base I/O address appears to be dead\n"));
1597 * Wait for the device to stop initialization
1600 DDB(printk("ad1848_detect() - step 0\n"));
1602 for (i
= 0; i
< 10000000; i
++)
1604 unsigned char x
= inb(devc
->base
);
1606 if (x
== 0xff || !(x
& 0x80))
1610 DDB(printk("ad1848_detect() - step A\n"));
1612 if (inb(devc
->base
) == 0x80) /* Not ready. Let's wait */
1615 if ((inb(devc
->base
) & 0x80) != 0x00) /* Not a AD1848 */
1617 DDB(printk("ad1848 detect error - step A (%02x)\n", (int) inb(devc
->base
)));
1622 * Test if it's possible to change contents of the indirect registers.
1623 * Registers 0 and 1 are ADC volume registers. The bit 0x10 is read only
1624 * so try to avoid using it.
1627 DDB(printk("ad1848_detect() - step B\n"));
1628 ad_write(devc
, 0, 0xaa);
1629 ad_write(devc
, 1, 0x45); /* 0x55 with bit 0x10 clear */
1631 if ((tmp1
= ad_read(devc
, 0)) != 0xaa || (tmp2
= ad_read(devc
, 1)) != 0x45)
1633 if (tmp2
== 0x65) /* AD1847 has couple of bits hardcoded to 1 */
1637 DDB(printk("ad1848 detect error - step B (%x/%x)\n", tmp1
, tmp2
));
1641 DDB(printk("ad1848_detect() - step C\n"));
1642 ad_write(devc
, 0, 0x45);
1643 ad_write(devc
, 1, 0xaa);
1645 if ((tmp1
= ad_read(devc
, 0)) != 0x45 || (tmp2
= ad_read(devc
, 1)) != 0xaa)
1647 if (tmp2
== 0x8a) /* AD1847 has few bits hardcoded to 1 */
1651 DDB(printk("ad1848 detect error - step C (%x/%x)\n", tmp1
, tmp2
));
1657 * The indirect register I12 has some read only bits. Let's
1658 * try to change them.
1661 DDB(printk("ad1848_detect() - step D\n"));
1662 tmp
= ad_read(devc
, 12);
1663 ad_write(devc
, 12, (~tmp
) & 0x0f);
1665 if ((tmp
& 0x0f) != ((tmp1
= ad_read(devc
, 12)) & 0x0f))
1667 DDB(printk("ad1848 detect error - step D (%x)\n", tmp1
));
1672 * NOTE! Last 4 bits of the reg I12 tell the chip revision.
1673 * 0x01=RevB and 0x0A=RevC.
1677 * The original AD1848/CS4248 has just 15 indirect registers. This means
1678 * that I0 and I16 should return the same value (etc.).
1679 * However this doesn't work with CS4248. Actually it seems to be impossible
1680 * to detect if the chip is a CS4231 or CS4248.
1681 * Ensure that the Mode2 enable bit of I12 is 0. Otherwise this test fails
1686 * OPTi 82C930 has mode2 control bit in another place. This test will fail
1687 * with it. Accept this situation as a possible indication of this chip.
1690 DDB(printk("ad1848_detect() - step F\n"));
1691 ad_write(devc
, 12, 0); /* Mode2=disabled */
1693 for (i
= 0; i
< 16; i
++)
1695 if ((tmp1
= ad_read(devc
, i
)) != (tmp2
= ad_read(devc
, i
+ 16)))
1697 DDB(printk("ad1848 detect step F(%d/%x/%x) - OPTi chip???\n", i
, tmp1
, tmp2
));
1705 * Try to switch the chip to mode2 (CS4231) by setting the MODE2 bit (0x40).
1706 * The bit 0x80 is always 1 in CS4248 and CS4231.
1709 DDB(printk("ad1848_detect() - step G\n"));
1711 if (ad_flags
&& *ad_flags
== 400)
1714 ad_write(devc
, 12, 0x40); /* Set mode2, clear 0x80 */
1720 tmp1
= ad_read(devc
, 12);
1724 *ad_flags
|= AD_F_CS4248
;
1726 devc
->chip_name
= "CS4248"; /* Our best knowledge just now */
1728 if (optiC930
|| (tmp1
& 0xc0) == (0x80 | 0x40))
1731 * CS4231 detected - is it?
1733 * Verify that setting I0 doesn't change I16.
1736 DDB(printk("ad1848_detect() - step H\n"));
1737 ad_write(devc
, 16, 0); /* Set I16 to known value */
1739 ad_write(devc
, 0, 0x45);
1740 if ((tmp1
= ad_read(devc
, 16)) != 0x45) /* No change -> CS4231? */
1742 ad_write(devc
, 0, 0xaa);
1743 if ((tmp1
= ad_read(devc
, 16)) == 0xaa) /* Rotten bits? */
1745 DDB(printk("ad1848 detect error - step H(%x)\n", tmp1
));
1750 * Verify that some bits of I25 are read only.
1753 DDB(printk("ad1848_detect() - step I\n"));
1754 tmp1
= ad_read(devc
, 25); /* Original bits */
1755 ad_write(devc
, 25, ~tmp1
); /* Invert all bits */
1756 if ((ad_read(devc
, 25) & 0xe7) == (tmp1
& 0xe7))
1761 * It's at least CS4231
1764 devc
->chip_name
= "CS4231";
1765 devc
->model
= MD_4231
;
1768 * It could be an AD1845 or CS4231A as well.
1769 * CS4231 and AD1845 report the same revision info in I25
1770 * while the CS4231A reports different.
1773 id
= ad_read(devc
, 25);
1774 if ((id
& 0xe7) == 0x80) /* Device busy??? */
1775 id
= ad_read(devc
, 25);
1776 if ((id
& 0xe7) == 0x80) /* Device still busy??? */
1777 id
= ad_read(devc
, 25);
1778 DDB(printk("ad1848_detect() - step J (%02x/%02x)\n", id
, ad_read(devc
, 25)));
1780 if ((id
& 0xe7) == 0x80) {
1782 * It must be a CS4231 or AD1845. The register I23 of
1783 * CS4231 is undefined and it appears to be read only.
1784 * AD1845 uses I23 for setting sample rate. Assume
1785 * the chip is AD1845 if I23 is changeable.
1788 unsigned char tmp
= ad_read(devc
, 23);
1789 ad_write(devc
, 23, ~tmp
);
1793 devc
->model
= MD_IWAVE
;
1794 devc
->chip_name
= "IWave";
1796 else if (ad_read(devc
, 23) != tmp
) /* AD1845 ? */
1798 devc
->chip_name
= "AD1845";
1799 devc
->model
= MD_1845
;
1801 else if (cs4248_flag
)
1804 *ad_flags
|= AD_F_CS4248
;
1805 devc
->chip_name
= "CS4248";
1806 devc
->model
= MD_1848
;
1807 ad_write(devc
, 12, ad_read(devc
, 12) & ~0x40); /* Mode2 off */
1809 ad_write(devc
, 23, tmp
); /* Restore */
1813 switch (id
& 0x1f) {
1814 case 3: /* CS4236/CS4235/CS42xB/CS4239 */
1817 ad_write(devc
, 12, ad_read(devc
, 12) | 0x60); /* switch to mode 3 */
1818 ad_write(devc
, 23, 0x9c); /* select extended register 25 */
1819 xid
= inb(io_Indexed_Data(devc
));
1820 ad_write(devc
, 12, ad_read(devc
, 12) & ~0x60); /* back to mode 0 */
1824 devc
->chip_name
= "CS4237B(B)";
1825 devc
->model
= MD_42xB
;
1828 /* Seems to be a 4238 ?? */
1829 devc
->chip_name
= "CS4238";
1830 devc
->model
= MD_42xB
;
1833 devc
->chip_name
= "CS4238B";
1834 devc
->model
= MD_42xB
;
1837 devc
->chip_name
= "CS4236B";
1838 devc
->model
= MD_4236
;
1841 devc
->chip_name
= "CS4237B";
1842 devc
->model
= MD_42xB
;
1845 devc
->chip_name
= "CS4235";
1846 devc
->model
= MD_4235
;
1849 devc
->chip_name
= "CS4239";
1850 devc
->model
= MD_4239
;
1853 printk("Chip ident is %X.\n", xid
&0x1F);
1854 devc
->chip_name
= "CS42xx";
1855 devc
->model
= MD_4232
;
1861 case 2: /* CS4232/CS4232A */
1862 devc
->chip_name
= "CS4232";
1863 devc
->model
= MD_4232
;
1867 if ((id
& 0xe0) == 0xa0)
1869 devc
->chip_name
= "CS4231A";
1870 devc
->model
= MD_4231A
;
1874 devc
->chip_name
= "CS4321";
1875 devc
->model
= MD_4231
;
1879 default: /* maybe */
1880 DDB(printk("ad1848: I25 = %02x/%02x\n", ad_read(devc
, 25), ad_read(devc
, 25) & 0xe7));
1883 devc
->chip_name
= "82C930";
1884 devc
->model
= MD_C930
;
1888 devc
->chip_name
= "CS4231";
1889 devc
->model
= MD_4231
;
1894 ad_write(devc
, 25, tmp1
); /* Restore bits */
1896 DDB(printk("ad1848_detect() - step K\n"));
1898 } else if (tmp1
== 0x0a) {
1900 * Is it perhaps a SoundPro CMI8330?
1901 * If so, then we should be able to change indirect registers
1902 * greater than I15 after activating MODE2, even though reading
1903 * back I12 does not show it.
1907 * Let's try comparing register values
1909 for (i
= 0; i
< 16; i
++) {
1910 if ((tmp1
= ad_read(devc
, i
)) != (tmp2
= ad_read(devc
, i
+ 16))) {
1911 DDB(printk("ad1848 detect step H(%d/%x/%x) - SoundPro chip?\n", i
, tmp1
, tmp2
));
1913 devc
->chip_name
= "SoundPro CMI 8330";
1919 DDB(printk("ad1848_detect() - step L\n"));
1922 if (devc
->model
!= MD_1848
)
1923 *ad_flags
|= AD_F_CS4231
;
1925 DDB(printk("ad1848_detect() - Detected OK\n"));
1927 if (devc
->model
== MD_1848
&& ad1847_flag
)
1928 devc
->chip_name
= "AD1847";
1931 if (sscape_flag
== 1)
1932 devc
->model
= MD_1845_SSCAPE
;
1937 int ad1848_init (char *name
, struct resource
*ports
, int irq
, int dma_playback
,
1938 int dma_capture
, int share_dma
, int *osp
, struct module
*owner
)
1941 * NOTE! If irq < 0, there is another driver which has allocated the IRQ
1942 * so that this driver doesn't need to allocate/deallocate it.
1943 * The actually used IRQ is ABS(irq).
1950 ad1848_info
*devc
= &adev_info
[nr_ad1848_devs
];
1952 ad1848_port_info
*portc
= NULL
;
1954 devc
->irq
= (irq
> 0) ? irq
: 0;
1955 devc
->open_mode
= 0;
1956 devc
->timer_ticks
= 0;
1957 devc
->dma1
= dma_playback
;
1958 devc
->dma2
= dma_capture
;
1959 devc
->subtype
= cfg
.card_subtype
;
1960 devc
->audio_flags
= DMA_AUTOMODE
;
1961 devc
->playback_dev
= devc
->record_dev
= 0;
1965 if (name
!= NULL
&& name
[0] != 0)
1967 "%s (%s)", name
, devc
->chip_name
);
1970 "Generic audio codec (%s)", devc
->chip_name
);
1972 rename_region(ports
, devc
->name
);
1974 conf_printf2(dev_name
, devc
->base
, devc
->irq
, dma_playback
, dma_capture
);
1976 if (devc
->model
== MD_1848
|| devc
->model
== MD_C930
)
1977 devc
->audio_flags
|= DMA_HARDSTOP
;
1979 if (devc
->model
> MD_1848
)
1981 if (devc
->dma1
== devc
->dma2
|| devc
->dma2
== -1 || devc
->dma1
== -1)
1982 devc
->audio_flags
&= ~DMA_DUPLEX
;
1984 devc
->audio_flags
|= DMA_DUPLEX
;
1987 portc
= kmalloc(sizeof(ad1848_port_info
), GFP_KERNEL
);
1989 release_region(devc
->base
, 4);
1993 if ((my_dev
= sound_install_audiodrv(AUDIO_DRIVER_VERSION
,
1995 &ad1848_audio_driver
,
1996 sizeof(struct audio_driver
),
1998 ad_format_mask
[devc
->model
],
2003 release_region(devc
->base
, 4);
2008 audio_devs
[my_dev
]->portc
= portc
;
2009 audio_devs
[my_dev
]->mixer_dev
= -1;
2011 audio_devs
[my_dev
]->d
->owner
= owner
;
2012 memset((char *) portc
, 0, sizeof(*portc
));
2016 ad1848_init_hw(devc
);
2020 devc
->dev_no
= my_dev
;
2021 if (request_irq(devc
->irq
, adintr
, 0, devc
->name
,
2022 (void *)(long)my_dev
) < 0)
2024 printk(KERN_WARNING
"ad1848: Unable to allocate IRQ\n");
2025 /* Don't free it either then.. */
2028 if (capabilities
[devc
->model
].flags
& CAP_F_TIMER
)
2032 unsigned char tmp
= ad_read(devc
, 16);
2035 devc
->timer_ticks
= 0;
2037 ad_write(devc
, 21, 0x00); /* Timer MSB */
2038 ad_write(devc
, 20, 0x10); /* Timer LSB */
2040 ad_write(devc
, 16, tmp
| 0x40); /* Enable timer */
2041 for (x
= 0; x
< 100000 && devc
->timer_ticks
== 0; x
++);
2042 ad_write(devc
, 16, tmp
& ~0x40); /* Disable timer */
2044 if (devc
->timer_ticks
== 0)
2045 printk(KERN_WARNING
"ad1848: Interrupt test failed (IRQ%d)\n", irq
);
2048 DDB(printk("Interrupt test OK\n"));
2056 devc
->irq_ok
= 1; /* Couldn't test. assume it's OK */
2058 devc
->dev_no
= my_dev
;
2060 #ifndef EXCLUDE_TIMERS
2061 if ((capabilities
[devc
->model
].flags
& CAP_F_TIMER
) &&
2063 ad1848_tmr_install(my_dev
);
2068 if (sound_alloc_dma(dma_playback
, devc
->name
))
2069 printk(KERN_WARNING
"ad1848.c: Can't allocate DMA%d\n", dma_playback
);
2071 if (dma_capture
!= dma_playback
)
2072 if (sound_alloc_dma(dma_capture
, devc
->name
))
2073 printk(KERN_WARNING
"ad1848.c: Can't allocate DMA%d\n", dma_capture
);
2076 if ((e
= sound_install_mixer(MIXER_DRIVER_VERSION
,
2078 &ad1848_mixer_operations
,
2079 sizeof(struct mixer_operations
),
2082 audio_devs
[my_dev
]->mixer_dev
= e
;
2084 mixer_devs
[e
]->owner
= owner
;
2089 int ad1848_control(int cmd
, int arg
)
2092 unsigned long flags
;
2094 if (nr_ad1848_devs
< 1)
2097 devc
= &adev_info
[nr_ad1848_devs
- 1];
2101 case AD1848_SET_XTAL
: /* Change clock frequency of AD1845 (only ) */
2102 if (devc
->model
!= MD_1845
&& devc
->model
!= MD_1845_SSCAPE
)
2104 spin_lock_irqsave(&devc
->lock
,flags
);
2106 ad_write(devc
, 29, (ad_read(devc
, 29) & 0x1f) | (arg
<< 5));
2108 spin_unlock_irqrestore(&devc
->lock
,flags
);
2111 case AD1848_MIXER_REROUTE
:
2113 int o
= (arg
>> 8) & 0xff;
2116 if (o
< 0 || o
>= SOUND_MIXER_NRDEVICES
)
2119 if (!(devc
->supported_devices
& (1 << o
)) &&
2120 !(devc
->supported_rec_devices
& (1 << o
)))
2123 if (n
== SOUND_MIXER_NONE
)
2124 { /* Just hide this control */
2125 ad1848_mixer_set(devc
, o
, 0); /* Shut up it */
2126 devc
->supported_devices
&= ~(1 << o
);
2127 devc
->supported_rec_devices
&= ~(1 << o
);
2131 /* Make the mixer control identified by o to appear as n */
2132 if (n
< 0 || n
>= SOUND_MIXER_NRDEVICES
)
2135 devc
->mixer_reroute
[n
] = o
; /* Rename the control */
2136 if (devc
->supported_devices
& (1 << o
))
2137 devc
->supported_devices
|= (1 << n
);
2138 if (devc
->supported_rec_devices
& (1 << o
))
2139 devc
->supported_rec_devices
|= (1 << n
);
2141 devc
->supported_devices
&= ~(1 << o
);
2142 devc
->supported_rec_devices
&= ~(1 << o
);
2149 void ad1848_unload(int io_base
, int irq
, int dma_playback
, int dma_capture
, int share_dma
)
2151 int i
, mixer
, dev
= 0;
2152 ad1848_info
*devc
= NULL
;
2154 for (i
= 0; devc
== NULL
&& i
< nr_ad1848_devs
; i
++)
2156 if (adev_info
[i
].base
== io_base
)
2158 devc
= &adev_info
[i
];
2165 kfree(audio_devs
[dev
]->portc
);
2166 release_region(devc
->base
, 4);
2170 if (devc
->irq
> 0) /* There is no point in freeing irq, if it wasn't allocated */
2171 free_irq(devc
->irq
, (void *)(long)devc
->dev_no
);
2173 sound_free_dma(dma_playback
);
2175 if (dma_playback
!= dma_capture
)
2176 sound_free_dma(dma_capture
);
2179 mixer
= audio_devs
[devc
->dev_no
]->mixer_dev
;
2181 sound_unload_mixerdev(mixer
);
2184 for ( ; i
< nr_ad1848_devs
; i
++)
2185 adev_info
[i
] = adev_info
[i
+1];
2188 printk(KERN_ERR
"ad1848: Can't find device to be unloaded. Base=%x\n", io_base
);
2191 static irqreturn_t
adintr(int irq
, void *dev_id
)
2193 unsigned char status
;
2196 int alt_stat
= 0xff;
2197 unsigned char c930_stat
= 0;
2201 devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
2203 interrupt_again
: /* Jump back here if int status doesn't reset */
2205 status
= inb(io_Status(devc
));
2208 printk(KERN_DEBUG
"adintr: Why?\n");
2209 if (devc
->model
== MD_1848
)
2210 outb((0), io_Status(devc
)); /* Clear interrupt status */
2214 if (devc
->model
== MD_C930
)
2215 { /* 82C930 has interrupt status register in MAD16 register MC11 */
2217 spin_lock(&devc
->lock
);
2219 /* 0xe0e is C930 address port
2220 * 0xe0f is C930 data port
2223 c930_stat
= inb(0xe0f);
2224 outb((~c930_stat
), 0xe0f);
2226 spin_unlock(&devc
->lock
);
2228 alt_stat
= (c930_stat
<< 2) & 0x30;
2230 else if (devc
->model
!= MD_1848
)
2232 spin_lock(&devc
->lock
);
2233 alt_stat
= ad_read(devc
, 24);
2234 ad_write(devc
, 24, ad_read(devc
, 24) & ~alt_stat
); /* Selective ack */
2235 spin_unlock(&devc
->lock
);
2238 if ((devc
->open_mode
& OPEN_READ
) && (devc
->audio_mode
& PCM_ENABLE_INPUT
) && (alt_stat
& 0x20))
2240 DMAbuf_inputintr(devc
->record_dev
);
2242 if ((devc
->open_mode
& OPEN_WRITE
) && (devc
->audio_mode
& PCM_ENABLE_OUTPUT
) &&
2245 DMAbuf_outputintr(devc
->playback_dev
, 1);
2247 if (devc
->model
!= MD_1848
&& (alt_stat
& 0x40)) /* Timer interrupt */
2249 devc
->timer_ticks
++;
2250 #ifndef EXCLUDE_TIMERS
2251 if (timer_installed
== dev
&& devc
->timer_running
)
2252 sound_timer_interrupt();
2257 * Sometimes playback or capture interrupts occur while a timer interrupt
2258 * is being handled. The interrupt will not be retriggered if we don't
2259 * handle it now. Check if an interrupt is still pending and restart
2260 * the handler in this case.
2262 if (inb(io_Status(devc
)) & 0x01 && cnt
++ < 4)
2264 goto interrupt_again
;
2270 * Experimental initialization sequence for the integrated sound system
2271 * of the Compaq Deskpro M.
2274 static int init_deskpro_m(struct address_info
*hw_config
)
2278 if ((tmp
= inb(0xc44)) == 0xff)
2280 DDB(printk("init_deskpro_m: Dead port 0xc44\n"));
2298 * Experimental initialization sequence for the integrated sound system
2299 * of Compaq Deskpro XL.
2302 static int init_deskpro(struct address_info
*hw_config
)
2306 if ((tmp
= inb(0xc44)) == 0xff)
2308 DDB(printk("init_deskpro: Dead port 0xc44\n"));
2311 outb((tmp
| 0x04), 0xc44); /* Select bank 1 */
2312 if (inb(0xc44) != 0x04)
2314 DDB(printk("init_deskpro: Invalid bank1 signature in port 0xc44\n"));
2318 * OK. It looks like a Deskpro so let's proceed.
2322 * I/O port 0xc44 Audio configuration register.
2324 * bits 0xc0: Audio revision bits
2325 * 0x00 = Compaq Business Audio
2326 * 0x40 = MS Sound System Compatible (reset default)
2329 * bit 0x20: No Wait State Enable
2330 * 0x00 = Disabled (reset default, DMA mode)
2331 * 0x20 = Enabled (programmed I/O mode)
2332 * bit 0x10: MS Sound System Decode Enable
2333 * 0x00 = Decoding disabled (reset default)
2334 * 0x10 = Decoding enabled
2335 * bit 0x08: FM Synthesis Decode Enable
2336 * 0x00 = Decoding Disabled (reset default)
2337 * 0x08 = Decoding enabled
2338 * bit 0x04 Bank select
2341 * bits 0x03 MSS Base address
2342 * 0x00 = 0x530 (reset default)
2349 /* Debug printing */
2350 printk("Port 0xc44 (before): ");
2351 outb((tmp
& ~0x04), 0xc44);
2352 printk("%02x ", inb(0xc44));
2353 outb((tmp
| 0x04), 0xc44);
2354 printk("%02x\n", inb(0xc44));
2357 /* Set bank 1 of the register */
2358 tmp
= 0x58; /* MSS Mode, MSS&FM decode enabled */
2360 switch (hw_config
->io_base
)
2375 DDB(printk("init_deskpro: Invalid MSS port %x\n", hw_config
->io_base
));
2378 outb((tmp
& ~0x04), 0xc44); /* Write to bank=0 */
2381 /* Debug printing */
2382 printk("Port 0xc44 (after): ");
2383 outb((tmp
& ~0x04), 0xc44); /* Select bank=0 */
2384 printk("%02x ", inb(0xc44));
2385 outb((tmp
| 0x04), 0xc44); /* Select bank=1 */
2386 printk("%02x\n", inb(0xc44));
2390 * I/O port 0xc45 FM Address Decode/MSS ID Register.
2392 * bank=0, bits 0xfe: FM synthesis Decode Compare bits 7:1 (default=0x88)
2393 * bank=0, bit 0x01: SBIC Power Control Bit
2395 * 0x01 = Powered down
2396 * bank=1, bits 0xfc: MSS ID (default=0x40)
2400 /* Debug printing */
2401 printk("Port 0xc45 (before): ");
2402 outb((tmp
& ~0x04), 0xc44); /* Select bank=0 */
2403 printk("%02x ", inb(0xc45));
2404 outb((tmp
| 0x04), 0xc44); /* Select bank=1 */
2405 printk("%02x\n", inb(0xc45));
2408 outb((tmp
& ~0x04), 0xc44); /* Select bank=0 */
2409 outb((0x88), 0xc45); /* FM base 7:0 = 0x88 */
2410 outb((tmp
| 0x04), 0xc44); /* Select bank=1 */
2411 outb((0x10), 0xc45); /* MSS ID = 0x10 (MSS port returns 0x04) */
2414 /* Debug printing */
2415 printk("Port 0xc45 (after): ");
2416 outb((tmp
& ~0x04), 0xc44); /* Select bank=0 */
2417 printk("%02x ", inb(0xc45));
2418 outb((tmp
| 0x04), 0xc44); /* Select bank=1 */
2419 printk("%02x\n", inb(0xc45));
2424 * I/O port 0xc46 FM Address Decode/Address ASIC Revision Register.
2426 * bank=0, bits 0xff: FM synthesis Decode Compare bits 15:8 (default=0x03)
2427 * bank=1, bits 0xff: Audio addressing ASIC id
2431 /* Debug printing */
2432 printk("Port 0xc46 (before): ");
2433 outb((tmp
& ~0x04), 0xc44); /* Select bank=0 */
2434 printk("%02x ", inb(0xc46));
2435 outb((tmp
| 0x04), 0xc44); /* Select bank=1 */
2436 printk("%02x\n", inb(0xc46));
2439 outb((tmp
& ~0x04), 0xc44); /* Select bank=0 */
2440 outb((0x03), 0xc46); /* FM base 15:8 = 0x03 */
2441 outb((tmp
| 0x04), 0xc44); /* Select bank=1 */
2442 outb((0x11), 0xc46); /* ASIC ID = 0x11 */
2445 /* Debug printing */
2446 printk("Port 0xc46 (after): ");
2447 outb((tmp
& ~0x04), 0xc44); /* Select bank=0 */
2448 printk("%02x ", inb(0xc46));
2449 outb((tmp
| 0x04), 0xc44); /* Select bank=1 */
2450 printk("%02x\n", inb(0xc46));
2454 * I/O port 0xc47 FM Address Decode Register.
2456 * bank=0, bits 0xff: Decode enable selection for various FM address bits
2457 * bank=1, bits 0xff: Reserved
2461 /* Debug printing */
2462 printk("Port 0xc47 (before): ");
2463 outb((tmp
& ~0x04), 0xc44); /* Select bank=0 */
2464 printk("%02x ", inb(0xc47));
2465 outb((tmp
| 0x04), 0xc44); /* Select bank=1 */
2466 printk("%02x\n", inb(0xc47));
2469 outb((tmp
& ~0x04), 0xc44); /* Select bank=0 */
2470 outb((0x7c), 0xc47); /* FM decode enable bits = 0x7c */
2471 outb((tmp
| 0x04), 0xc44); /* Select bank=1 */
2472 outb((0x00), 0xc47); /* Reserved bank1 = 0x00 */
2475 /* Debug printing */
2476 printk("Port 0xc47 (after): ");
2477 outb((tmp
& ~0x04), 0xc44); /* Select bank=0 */
2478 printk("%02x ", inb(0xc47));
2479 outb((tmp
| 0x04), 0xc44); /* Select bank=1 */
2480 printk("%02x\n", inb(0xc47));
2484 * I/O port 0xc6f = Audio Disable Function Register
2488 printk("Port 0xc6f (before) = %02x\n", inb(0xc6f));
2491 outb((0x80), 0xc6f);
2494 printk("Port 0xc6f (after) = %02x\n", inb(0xc6f));
2500 int probe_ms_sound(struct address_info
*hw_config
, struct resource
*ports
)
2504 DDB(printk("Entered probe_ms_sound(%x, %d)\n", hw_config
->io_base
, hw_config
->card_subtype
));
2506 if (hw_config
->card_subtype
== 1) /* Has no IRQ/DMA registers */
2508 /* check_opl3(0x388, hw_config); */
2509 return ad1848_detect(ports
, NULL
, hw_config
->osp
);
2512 if (deskpro_xl
&& hw_config
->card_subtype
== 2) /* Compaq Deskpro XL */
2514 if (!init_deskpro(hw_config
))
2518 if (deskpro_m
) /* Compaq Deskpro M */
2520 if (!init_deskpro_m(hw_config
))
2525 * Check if the IO port returns valid signature. The original MS Sound
2526 * system returns 0x04 while some cards (AudioTrix Pro for example)
2527 * return 0x00 or 0x0f.
2530 if ((tmp
= inb(hw_config
->io_base
+ 3)) == 0xff) /* Bus float */
2534 DDB(printk("I/O address is inactive (%x)\n", tmp
));
2535 if (!(ret
= ad1848_detect(ports
, NULL
, hw_config
->osp
)))
2539 DDB(printk("MSS signature = %x\n", tmp
& 0x3f));
2540 if ((tmp
& 0x3f) != 0x04 &&
2541 (tmp
& 0x3f) != 0x0f &&
2542 (tmp
& 0x3f) != 0x00)
2546 MDB(printk(KERN_ERR
"No MSS signature detected on port 0x%x (0x%x)\n", hw_config
->io_base
, (int) inb(hw_config
->io_base
+ 3)));
2547 DDB(printk("Trying to detect codec anyway but IRQ/DMA may not work\n"));
2548 if (!(ret
= ad1848_detect(ports
, NULL
, hw_config
->osp
)))
2551 hw_config
->card_subtype
= 1;
2554 if ((hw_config
->irq
!= 5) &&
2555 (hw_config
->irq
!= 7) &&
2556 (hw_config
->irq
!= 9) &&
2557 (hw_config
->irq
!= 10) &&
2558 (hw_config
->irq
!= 11) &&
2559 (hw_config
->irq
!= 12))
2561 printk(KERN_ERR
"MSS: Bad IRQ %d\n", hw_config
->irq
);
2564 if (hw_config
->dma
!= 0 && hw_config
->dma
!= 1 && hw_config
->dma
!= 3)
2566 printk(KERN_ERR
"MSS: Bad DMA %d\n", hw_config
->dma
);
2570 * Check that DMA0 is not in use with a 8 bit board.
2573 if (hw_config
->dma
== 0 && inb(hw_config
->io_base
+ 3) & 0x80)
2575 printk(KERN_ERR
"MSS: Can't use DMA0 with a 8 bit card/slot\n");
2578 if (hw_config
->irq
> 7 && hw_config
->irq
!= 9 && inb(hw_config
->io_base
+ 3) & 0x80)
2580 printk(KERN_ERR
"MSS: Can't use IRQ%d with a 8 bit card/slot\n", hw_config
->irq
);
2583 return ad1848_detect(ports
, NULL
, hw_config
->osp
);
2586 void attach_ms_sound(struct address_info
*hw_config
, struct resource
*ports
, struct module
*owner
)
2588 static signed char interrupt_bits
[12] =
2590 -1, -1, -1, -1, -1, 0x00, -1, 0x08, -1, 0x10, 0x18, 0x20
2595 static char dma_bits
[4] =
2600 int config_port
= hw_config
->io_base
+ 0;
2601 int version_port
= hw_config
->io_base
+ 3;
2602 int dma
= hw_config
->dma
;
2603 int dma2
= hw_config
->dma2
;
2605 if (hw_config
->card_subtype
== 1) /* Has no IRQ/DMA registers */
2607 hw_config
->slots
[0] = ad1848_init("MS Sound System", ports
,
2616 * Set the IRQ and DMA addresses.
2619 bits
= interrupt_bits
[hw_config
->irq
];
2622 printk(KERN_ERR
"MSS: Bad IRQ %d\n", hw_config
->irq
);
2623 release_region(ports
->start
, 4);
2624 release_region(ports
->start
- 4, 4);
2627 outb((bits
| 0x40), config_port
);
2628 if ((inb(version_port
) & 0x40) == 0)
2629 printk(KERN_ERR
"[MSS: IRQ Conflict?]\n");
2632 * Handle the capture DMA channel
2635 if (dma2
!= -1 && dma2
!= dma
)
2637 if (!((dma
== 0 && dma2
== 1) ||
2638 (dma
== 1 && dma2
== 0) ||
2639 (dma
== 3 && dma2
== 0)))
2640 { /* Unsupported combination. Try to swap channels */
2646 if ((dma
== 0 && dma2
== 1) ||
2647 (dma
== 1 && dma2
== 0) ||
2648 (dma
== 3 && dma2
== 0))
2650 dma2_bit
= 0x04; /* Enable capture DMA */
2654 printk(KERN_WARNING
"MSS: Invalid capture DMA\n");
2663 hw_config
->dma
= dma
;
2664 hw_config
->dma2
= dma2
;
2666 outb((bits
| dma_bits
[dma
] | dma2_bit
), config_port
); /* Write IRQ+DMA setup */
2668 hw_config
->slots
[0] = ad1848_init("MS Sound System", ports
,
2675 void unload_ms_sound(struct address_info
*hw_config
)
2677 ad1848_unload(hw_config
->io_base
+ 4,
2680 hw_config
->dma2
, 0);
2681 sound_unload_audiodev(hw_config
->slots
[0]);
2682 release_region(hw_config
->io_base
, 4);
2685 #ifndef EXCLUDE_TIMERS
2688 * Timer stuff (for /dev/music).
2691 static unsigned int current_interval
;
2693 static unsigned int ad1848_tmr_start(int dev
, unsigned int usecs
)
2695 unsigned long flags
;
2696 ad1848_info
*devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
2697 unsigned long xtal_nsecs
; /* nanoseconds per xtal oscillator tick */
2698 unsigned long divider
;
2700 spin_lock_irqsave(&devc
->lock
,flags
);
2703 * Length of the timer interval (in nanoseconds) depends on the
2704 * selected crystal oscillator. Check this from bit 0x01 of I8.
2706 * AD1845 has just one oscillator which has cycle time of 10.050 us
2707 * (when a 24.576 MHz xtal oscillator is used).
2709 * Convert requested interval to nanoseconds before computing
2710 * the timer divider.
2713 if (devc
->model
== MD_1845
|| devc
->model
== MD_1845_SSCAPE
)
2715 else if (ad_read(devc
, 8) & 0x01)
2720 divider
= (usecs
* 1000 + xtal_nsecs
/ 2) / xtal_nsecs
;
2722 if (divider
< 100) /* Don't allow shorter intervals than about 1ms */
2725 if (divider
> 65535) /* Overflow check */
2728 ad_write(devc
, 21, (divider
>> 8) & 0xff); /* Set upper bits */
2729 ad_write(devc
, 20, divider
& 0xff); /* Set lower bits */
2730 ad_write(devc
, 16, ad_read(devc
, 16) | 0x40); /* Start the timer */
2731 devc
->timer_running
= 1;
2732 spin_unlock_irqrestore(&devc
->lock
,flags
);
2734 return current_interval
= (divider
* xtal_nsecs
+ 500) / 1000;
2737 static void ad1848_tmr_reprogram(int dev
)
2740 * Audio driver has changed sampling rate so that a different xtal
2741 * oscillator was selected. We have to reprogram the timer rate.
2744 ad1848_tmr_start(dev
, current_interval
);
2745 sound_timer_syncinterval(current_interval
);
2748 static void ad1848_tmr_disable(int dev
)
2750 unsigned long flags
;
2751 ad1848_info
*devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
2753 spin_lock_irqsave(&devc
->lock
,flags
);
2754 ad_write(devc
, 16, ad_read(devc
, 16) & ~0x40);
2755 devc
->timer_running
= 0;
2756 spin_unlock_irqrestore(&devc
->lock
,flags
);
2759 static void ad1848_tmr_restart(int dev
)
2761 unsigned long flags
;
2762 ad1848_info
*devc
= (ad1848_info
*) audio_devs
[dev
]->devc
;
2764 if (current_interval
== 0)
2767 spin_lock_irqsave(&devc
->lock
,flags
);
2768 ad_write(devc
, 16, ad_read(devc
, 16) | 0x40);
2769 devc
->timer_running
= 1;
2770 spin_unlock_irqrestore(&devc
->lock
,flags
);
2773 static struct sound_lowlev_timer ad1848_tmr
=
2782 static int ad1848_tmr_install(int dev
)
2784 if (timer_installed
!= -1)
2785 return 0; /* Don't install another timer */
2787 timer_installed
= ad1848_tmr
.dev
= dev
;
2788 sound_timer_init(&ad1848_tmr
, audio_devs
[dev
]->name
);
2792 #endif /* EXCLUDE_TIMERS */
2794 EXPORT_SYMBOL(ad1848_detect
);
2795 EXPORT_SYMBOL(ad1848_init
);
2796 EXPORT_SYMBOL(ad1848_unload
);
2797 EXPORT_SYMBOL(ad1848_control
);
2798 EXPORT_SYMBOL(probe_ms_sound
);
2799 EXPORT_SYMBOL(attach_ms_sound
);
2800 EXPORT_SYMBOL(unload_ms_sound
);
2802 static int __initdata io
= -1;
2803 static int __initdata irq
= -1;
2804 static int __initdata dma
= -1;
2805 static int __initdata dma2
= -1;
2806 static int __initdata type
= 0;
2808 module_param_hw(io
, int, ioport
, 0); /* I/O for a raw AD1848 card */
2809 module_param_hw(irq
, int, irq
, 0); /* IRQ to use */
2810 module_param_hw(dma
, int, dma
, 0); /* First DMA channel */
2811 module_param_hw(dma2
, int, dma
, 0); /* Second DMA channel */
2812 module_param(type
, int, 0); /* Card type */
2813 module_param(deskpro_xl
, bool, 0); /* Special magic for Deskpro XL boxen */
2814 module_param(deskpro_m
, bool, 0); /* Special magic for Deskpro M box */
2815 module_param(soundpro
, bool, 0); /* More special magic for SoundPro chips */
2818 module_param(isapnp
, int, 0);
2819 module_param(isapnpjump
, int, 0);
2820 module_param(reverse
, bool, 0);
2821 MODULE_PARM_DESC(isapnp
, "When set to 0, Plug & Play support will be disabled");
2822 MODULE_PARM_DESC(isapnpjump
, "Jumps to a specific slot in the driver's PnP table. Use the source, Luke.");
2823 MODULE_PARM_DESC(reverse
, "When set to 1, will reverse ISAPnP search order");
2825 static struct pnp_dev
*ad1848_dev
= NULL
;
2827 /* Please add new entries at the end of the table */
2830 unsigned short card_vendor
, card_device
,
2832 short mss_io
, irq
, dma
, dma2
; /* index into isapnp table */
2834 } ad1848_isapnp_list
[] __initdata
= {
2835 {"CMI 8330 SoundPRO",
2836 ISAPNP_VENDOR('C','M','I'), ISAPNP_DEVICE(0x0001),
2837 ISAPNP_VENDOR('@','@','@'), ISAPNP_FUNCTION(0x0001),
2839 {"CS4232 based card",
2840 ISAPNP_ANY_ID
, ISAPNP_ANY_ID
,
2841 ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0000),
2843 {"CS4232 based card",
2844 ISAPNP_ANY_ID
, ISAPNP_ANY_ID
,
2845 ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0100),
2847 {"OPL3-SA2 WSS mode",
2848 ISAPNP_ANY_ID
, ISAPNP_ANY_ID
,
2849 ISAPNP_VENDOR('Y','M','H'), ISAPNP_FUNCTION(0x0021),
2851 {"Advanced Gravis InterWave Audio",
2852 ISAPNP_VENDOR('G','R','V'), ISAPNP_DEVICE(0x0001),
2853 ISAPNP_VENDOR('G','R','V'), ISAPNP_FUNCTION(0x0000),
2859 static struct isapnp_device_id id_table
[] = {
2860 { ISAPNP_VENDOR('C','M','I'), ISAPNP_DEVICE(0x0001),
2861 ISAPNP_VENDOR('@','@','@'), ISAPNP_FUNCTION(0x0001), 0 },
2862 { ISAPNP_ANY_ID
, ISAPNP_ANY_ID
,
2863 ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0000), 0 },
2864 { ISAPNP_ANY_ID
, ISAPNP_ANY_ID
,
2865 ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0100), 0 },
2866 /* The main driver for this card is opl3sa2
2867 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2868 ISAPNP_VENDOR('Y','M','H'), ISAPNP_FUNCTION(0x0021), 0 },
2870 { ISAPNP_VENDOR('G','R','V'), ISAPNP_DEVICE(0x0001),
2871 ISAPNP_VENDOR('G','R','V'), ISAPNP_FUNCTION(0x0000), 0 },
2875 MODULE_DEVICE_TABLE(isapnp
, id_table
);
2878 static struct pnp_dev
*activate_dev(char *devname
, char *resname
, struct pnp_dev
*dev
)
2882 err
= pnp_device_attach(dev
);
2886 if((err
= pnp_activate_dev(dev
)) < 0) {
2887 printk(KERN_ERR
"ad1848: %s %s config failed (out of resources?)[%d]\n", devname
, resname
, err
);
2889 pnp_device_detach(dev
);
2893 audio_activated
= 1;
2897 static struct pnp_dev __init
*ad1848_init_generic(struct pnp_card
*bus
,
2898 struct address_info
*hw_config
, int slot
)
2901 /* Configure Audio device */
2902 if((ad1848_dev
= pnp_find_dev(bus
, ad1848_isapnp_list
[slot
].vendor
, ad1848_isapnp_list
[slot
].function
, NULL
)))
2904 if((ad1848_dev
= activate_dev(ad1848_isapnp_list
[slot
].name
, "ad1848", ad1848_dev
)))
2906 hw_config
->io_base
= pnp_port_start(ad1848_dev
, ad1848_isapnp_list
[slot
].mss_io
);
2907 hw_config
->irq
= pnp_irq(ad1848_dev
, ad1848_isapnp_list
[slot
].irq
);
2908 hw_config
->dma
= pnp_dma(ad1848_dev
, ad1848_isapnp_list
[slot
].dma
);
2909 if(ad1848_isapnp_list
[slot
].dma2
!= -1)
2910 hw_config
->dma2
= pnp_dma(ad1848_dev
, ad1848_isapnp_list
[slot
].dma2
);
2912 hw_config
->dma2
= -1;
2913 hw_config
->card_subtype
= ad1848_isapnp_list
[slot
].type
;
2922 static int __init
ad1848_isapnp_init(struct address_info
*hw_config
, struct pnp_card
*bus
, int slot
)
2924 char *busname
= bus
->name
[0] ? bus
->name
: ad1848_isapnp_list
[slot
].name
;
2926 /* Initialize this baby. */
2928 if(ad1848_init_generic(bus
, hw_config
, slot
)) {
2931 printk(KERN_NOTICE
"ad1848: PnP reports '%s' at i/o %#x, irq %d, dma %d, %d\n",
2933 hw_config
->io_base
, hw_config
->irq
, hw_config
->dma
,
2940 static int __init
ad1848_isapnp_probe(struct address_info
*hw_config
)
2942 static int first
= 1;
2945 /* Count entries in sb_isapnp_list */
2946 for (i
= 0; ad1848_isapnp_list
[i
].card_vendor
!= 0; i
++);
2949 /* Check and adjust isapnpjump */
2950 if( isapnpjump
< 0 || isapnpjump
> i
) {
2951 isapnpjump
= reverse
? i
: 0;
2952 printk(KERN_ERR
"ad1848: Valid range for isapnpjump is 0-%d. Adjusted to %d.\n", i
, isapnpjump
);
2955 if(!first
|| !reverse
)
2958 while(ad1848_isapnp_list
[i
].card_vendor
!= 0) {
2959 static struct pnp_card
*bus
= NULL
;
2961 while ((bus
= pnp_find_card(
2962 ad1848_isapnp_list
[i
].card_vendor
,
2963 ad1848_isapnp_list
[i
].card_device
,
2966 if(ad1848_isapnp_init(hw_config
, bus
, i
)) {
2967 isapnpjump
= i
; /* start next search from here */
2971 i
+= reverse
? -1 : 1;
2979 static int __init
init_ad1848(void)
2981 printk(KERN_INFO
"ad1848/cs4248 codec driver Copyright (C) by Hannu Savolainen 1993-1996\n");
2984 if(isapnp
&& (ad1848_isapnp_probe(&cfg
) < 0) ) {
2985 printk(KERN_NOTICE
"ad1848: No ISAPnP cards found, trying standard ones...\n");
2991 struct resource
*ports
;
2994 if(irq
== -1 || dma
== -1) {
2995 printk(KERN_WARNING
"ad1848: must give I/O , IRQ and DMA.\n");
3003 cfg
.card_subtype
= type
;
3006 ports
= request_region(io
+ 4, 4, "ad1848");
3011 if (!request_region(io
, 4, "WSS config")) {
3012 release_region(io
+ 4, 4);
3016 if (!probe_ms_sound(&cfg
, ports
)) {
3017 release_region(io
+ 4, 4);
3018 release_region(io
, 4);
3021 attach_ms_sound(&cfg
, ports
, THIS_MODULE
);
3027 static void __exit
cleanup_ad1848(void)
3030 unload_ms_sound(&cfg
);
3035 pnp_device_detach(ad1848_dev
);
3040 module_init(init_ad1848
);
3041 module_exit(cleanup_ad1848
);
3044 static int __init
setup_ad1848(char *str
)
3046 /* io, irq, dma, dma2, type */
3049 str
= get_options(str
, ARRAY_SIZE(ints
), ints
);
3060 __setup("ad1848=", setup_ad1848
);
3062 MODULE_LICENSE("GPL");