1 /* Best viewed with tabsize 4 */
3 /* Ensoniq ES1370 driver
7 * This is the main file of the ES1370 sound driver. There is no main function
8 * over here, instead the main function is located in the generic dma driver.
9 * All this driver does is implement the interface audio/audio_fw.h. All
10 * functions having the prefix 'drv_' are dictated by audio/audio_fw.h. The
11 * function prototypes you see below define a set of private helper functions.
12 * Control over the AK4531 codec is delegated ak4531.c.
14 * September 2007 ES1370 driver (Pieter Hijma),
15 * based on ES1371 driver by Laurens Bronwasser
18 #include <machine/pci.h>
20 #include <minix/audio_fw.h>
23 #include "pci_helper.h"
26 /* reg(n) will be the device specific addresses */
27 #define reg(n) (dev.base + (n))
30 /* prototypes of private functions */
31 static int detect_hw(void);
32 static int disable_int(int sub_dev
);
33 static int set_stereo(u32_t stereo
, int sub_dev
);
34 static int set_bits(u32_t nr_of_bits
, int sub_dev
);
35 static int set_sample_rate(u32_t rate
, int sub_dev
);
36 static int set_sign(u32_t val
, int sub_dev
);
37 static int get_max_frag_size(u32_t
* val
, int *len
, int sub_dev
);
38 static int set_frag_size(u32_t fragment_size
, int sub_dev
);
39 static int set_int_cnt(int sub_dev
);
40 static int free_buf(u32_t
*val
, int *len
, int sub_dev
);
41 static int get_samples_in_buf(u32_t
*val
, int *len
, int sub_dev
);
42 static int get_set_volume(struct volume_level
*level
, int *len
, int
44 static int reset(int sub_dev
);
48 aud_sub_dev_conf_t aud_conf
[4];
52 special_file_t special_file
[4];
57 drv
.DriverName
= DRIVER_NAME
;
58 drv
.NrOfSubDevices
= 4;
59 drv
.NrOfSpecialFiles
= 4;
61 sub_dev
[DAC1_CHAN
].readable
= 0;
62 sub_dev
[DAC1_CHAN
].writable
= 1;
63 sub_dev
[DAC1_CHAN
].DmaSize
= 64 * 1024;
64 sub_dev
[DAC1_CHAN
].NrOfDmaFragments
= 2;
65 sub_dev
[DAC1_CHAN
].MinFragmentSize
= 1024;
66 sub_dev
[DAC1_CHAN
].NrOfExtraBuffers
= 4;
68 sub_dev
[ADC1_CHAN
].readable
= 1;
69 sub_dev
[ADC1_CHAN
].writable
= 0;
70 sub_dev
[ADC1_CHAN
].DmaSize
= 64 * 1024;
71 sub_dev
[ADC1_CHAN
].NrOfDmaFragments
= 2;
72 sub_dev
[ADC1_CHAN
].MinFragmentSize
= 1024;
73 sub_dev
[ADC1_CHAN
].NrOfExtraBuffers
= 4;
75 sub_dev
[MIXER
].writable
= 0;
76 sub_dev
[MIXER
].readable
= 0;
78 sub_dev
[DAC2_CHAN
].readable
= 0;
79 sub_dev
[DAC2_CHAN
].writable
= 1;
80 sub_dev
[DAC2_CHAN
].DmaSize
= 64 * 1024;
81 sub_dev
[DAC2_CHAN
].NrOfDmaFragments
= 2;
82 sub_dev
[DAC2_CHAN
].MinFragmentSize
= 1024;
83 sub_dev
[DAC2_CHAN
].NrOfExtraBuffers
= 4;
85 special_file
[0].minor_dev_nr
= 0;
86 special_file
[0].write_chan
= DAC1_CHAN
;
87 special_file
[0].read_chan
= NO_CHANNEL
;
88 special_file
[0].io_ctl
= DAC1_CHAN
;
90 special_file
[1].minor_dev_nr
= 1;
91 special_file
[1].write_chan
= NO_CHANNEL
;
92 special_file
[1].read_chan
= ADC1_CHAN
;
93 special_file
[1].io_ctl
= ADC1_CHAN
;
95 special_file
[2].minor_dev_nr
= 2;
96 special_file
[2].write_chan
= NO_CHANNEL
;
97 special_file
[2].read_chan
= NO_CHANNEL
;
98 special_file
[2].io_ctl
= MIXER
;
100 special_file
[3].minor_dev_nr
= 3;
101 special_file
[3].write_chan
= DAC2_CHAN
;
102 special_file
[3].read_chan
= NO_CHANNEL
;
103 special_file
[3].io_ctl
= DAC2_CHAN
;
109 int drv_init_hw (void) {
111 u16_t chip_sel_ctrl_reg
;
113 /* First, detect the hardware */
114 if (detect_hw() != OK
) {
118 /* PCI command register
119 * enable the SERR# driver, PCI bus mastering and I/O access
121 pci_attr_w16 (dev
.devind
, PCI_CR
, SERR_EN
|PCI_MASTER
|IO_ACCESS
);
123 /* turn everything off */
124 pci_outl(reg(CHIP_SEL_CTRL
), 0x0UL
);
126 /* turn off legacy (legacy control is undocumented) */
127 pci_outl(reg(LEGACY
), 0x0UL
);
128 pci_outl(reg(LEGACY
+4), 0x0UL
);
130 /* turn off serial interface */
131 pci_outl(reg(SERIAL_INTERFACE_CTRL
), 0x0UL
);
132 /*pci_outl(reg(SERIAL_INTERFACE_CTRL), 0x3UL);*/
135 /* enable the codec */
136 chip_sel_ctrl_reg
= pci_inw(reg(CHIP_SEL_CTRL
));
137 chip_sel_ctrl_reg
|= XCTL0
| CDC_EN
;
138 pci_outw(reg(CHIP_SEL_CTRL
), chip_sel_ctrl_reg
);
140 /* initialize the codec */
141 if (ak4531_init(reg(CODEC_WRITE_ADDRESS
),
142 reg(INTERRUPT_STATUS
), CWRIP
, reg(0)) < 0) {
146 /* clear all the memory */
147 for (i
= 0; i
< 0x10; ++i
) {
148 pci_outb(reg(MEM_PAGE
), i
);
149 for (j
= 0; j
< 0x10; j
+= 4) {
150 pci_outl (reg(MEMORY
) + j
, 0x0UL
);
154 /* initialize variables for each sub_device */
155 for (i
= 0; i
< drv
.NrOfSubDevices
; i
++) {
157 aud_conf
[i
].busy
= 0;
158 aud_conf
[i
].stereo
= DEFAULT_STEREO
;
159 aud_conf
[i
].sample_rate
= DEFAULT_RATE
;
160 aud_conf
[i
].nr_of_bits
= DEFAULT_NR_OF_BITS
;
161 aud_conf
[i
].sign
= DEFAULT_SIGNED
;
162 aud_conf
[i
].fragment_size
=
163 sub_dev
[i
].DmaSize
/ sub_dev
[i
].NrOfDmaFragments
;
170 static int detect_hw(void) {
175 /* detect_hw tries to find device and get IRQ and base address
176 with a little (much) help from the PCI library.
177 This code is quite device independent and you can copy it.
178 (just make sure to get the bugs out first)*/
181 /* get first device and then search through the list */
182 device
= pci_first_dev(&devind
, &v_id
, &d_id
);
183 while( device
> 0 ) {
184 /* if we have a match...break */
185 if (v_id
== VENDOR_ID
&& d_id
== DEVICE_ID
) break;
186 device
= pci_next_dev(&devind
, &v_id
, &d_id
);
189 /* did we find anything? */
190 if (v_id
!= VENDOR_ID
|| d_id
!= DEVICE_ID
) {
196 dev
.name
= pci_dev_name(v_id
, d_id
);
198 /* get base address of our device, ignore least signif. bit
199 this last bit thing could be device dependent, i don't know */
200 dev
.base
= pci_attr_r32(devind
, PCI_BAR
) & 0xfffffffe;
203 dev
.irq
= pci_attr_r8(devind
, PCI_ILR
);
204 dev
.revision
= pci_attr_r8(devind
, PCI_REV
);
207 dev
.devind
= devind
; /* pci device identifier */
213 static int reset(int chan
) {
215 sub_dev
[chan
].OutOfData
= 1;
226 int drv_start(int sub_dev
, int UNUSED(DmaMode
)) {
227 u32_t enable_bit
, result
= 0;
229 /* Write default values to device in case user failed to configure.
230 If user did configure properly, everything is written twice.
231 please raise your hand if you object against to this strategy...*/
232 result
|= set_sample_rate(aud_conf
[sub_dev
].sample_rate
, sub_dev
);
233 result
|= set_stereo(aud_conf
[sub_dev
].stereo
, sub_dev
);
234 result
|= set_bits(aud_conf
[sub_dev
].nr_of_bits
, sub_dev
);
235 result
|= set_sign(aud_conf
[sub_dev
].sign
, sub_dev
);
237 /* set the interrupt count */
238 result
|= set_int_cnt(sub_dev
);
244 /* if device currently paused, resume */
248 case ADC1_CHAN
: enable_bit
= ADC1_EN
;break;
249 case DAC1_CHAN
: enable_bit
= DAC1_EN
;break;
250 case DAC2_CHAN
: enable_bit
= DAC2_EN
;break;
251 default: return EINVAL
;
254 /* enable interrupts from 'sub device' */
255 drv_reenable_int(sub_dev
);
257 /* this means play!!! */
258 pci_outw(reg(CHIP_SEL_CTRL
), pci_inw(reg(CHIP_SEL_CTRL
)) | enable_bit
);
260 aud_conf
[sub_dev
].busy
= 1;
266 int drv_stop(int sub_dev
)
272 case ADC1_CHAN
: enable_bit
= ADC1_EN
;break;
273 case DAC1_CHAN
: enable_bit
= DAC1_EN
;break;
274 case DAC2_CHAN
: enable_bit
= DAC2_EN
;break;
275 default: return EINVAL
;
278 /* stop the specified channel */
279 pci_outw(reg(CHIP_SEL_CTRL
),
280 pci_inw(reg(CHIP_SEL_CTRL
)) & ~enable_bit
);
281 aud_conf
[sub_dev
].busy
= 0;
282 status
= disable_int(sub_dev
);
288 /* all IO-ctl's sent to the upper driver are passed to this function */
289 int drv_io_ctl(int request
, void * val
, int * len
, int sub_dev
) {
295 status
= set_sample_rate(*((u32_t
*) val
), sub_dev
); break;
297 status
= set_stereo(*((u32_t
*) val
), sub_dev
); break;
299 status
= set_bits(*((u32_t
*) val
), sub_dev
); break;
301 status
= set_frag_size(*((u32_t
*) val
), sub_dev
); break;
303 status
= set_sign(*((u32_t
*) val
), sub_dev
); break;
305 status
= get_max_frag_size(val
, len
, sub_dev
); break;
307 status
= reset(sub_dev
); break;
309 status
= free_buf(val
, len
, sub_dev
); break;
310 case DSPIOSAMPLESINBUF
:
311 status
= get_samples_in_buf(val
, len
, sub_dev
); break;
313 status
= drv_pause(sub_dev
); break;
315 status
= drv_resume(sub_dev
); break;
317 status
= get_set_volume(val
, len
, sub_dev
, 0); break;
319 status
= get_set_volume(val
, len
, sub_dev
, 1); break;
321 status
= EINVAL
; break;
328 int drv_get_irq(char *irq
) {
334 int drv_get_frag_size(u32_t
*frag_size
, int sub_dev
) {
335 *frag_size
= aud_conf
[sub_dev
].fragment_size
;
340 int drv_set_dma(u32_t dma
, u32_t length
, int chan
) {
341 /* dma length in bytes,
342 max is 64k long words for es1370 = 256k bytes */
343 u32_t page
, frame_count_reg
, dma_add_reg
;
346 case ADC1_CHAN
: page
= ADC_MEM_PAGE
;
347 frame_count_reg
= ADC_BUFFER_SIZE
;
348 dma_add_reg
= ADC_PCI_ADDRESS
;
350 case DAC1_CHAN
: page
= DAC_MEM_PAGE
;
351 frame_count_reg
= DAC1_BUFFER_SIZE
;
352 dma_add_reg
= DAC1_PCI_ADDRESS
;
354 case DAC2_CHAN
: page
= DAC_MEM_PAGE
;
355 frame_count_reg
= DAC2_BUFFER_SIZE
;
356 dma_add_reg
= DAC2_PCI_ADDRESS
;
360 pci_outb(reg(MEM_PAGE
), page
);
361 pci_outl(reg(dma_add_reg
), dma
);
363 /* device expects long word count in stead of bytes */
366 /* It seems that register _CURRENT_COUNT is overwritten, but this is
367 * the way to go. The register frame_count_reg is only longword
369 * It expects length -1
371 pci_outl(reg(frame_count_reg
), (u32_t
) (length
- 1));
377 /* return status of the interrupt summary bit */
378 int drv_int_sum(void) {
379 return pci_inl(reg(INTERRUPT_STATUS
)) & INTR
;
383 int drv_int(int sub_dev
) {
387 /* return status of interrupt bit of specified channel*/
389 case DAC1_CHAN
: bit
= DAC1
; break;
390 case DAC2_CHAN
: bit
= DAC2
; break;
391 case ADC1_CHAN
: bit
= ADC
; break;
392 default: return EINVAL
;
395 int_status
= pci_inl(reg(INTERRUPT_STATUS
)) & bit
;
401 int drv_reenable_int(int chan
) {
402 u16_t ser_interface
, int_en_bit
;
405 case ADC1_CHAN
: int_en_bit
= R1_INT_EN
; break;
406 case DAC1_CHAN
: int_en_bit
= P1_INTR_EN
; break;
407 case DAC2_CHAN
: int_en_bit
= P2_INTR_EN
; break;
408 default: return EINVAL
;
411 /* clear and reenable an interrupt */
412 ser_interface
= pci_inw(reg(SERIAL_INTERFACE_CTRL
));
413 pci_outw(reg(SERIAL_INTERFACE_CTRL
), ser_interface
& ~int_en_bit
);
414 pci_outw(reg(SERIAL_INTERFACE_CTRL
), ser_interface
| int_en_bit
);
420 int drv_pause(int sub_dev
) {
423 disable_int(sub_dev
); /* don't send interrupts */
426 case DAC1_CHAN
: pause_bit
= P1_PAUSE
;break;
427 case DAC2_CHAN
: pause_bit
= P2_PAUSE
;break;
428 default: return EINVAL
;
432 pci_outl(reg(SERIAL_INTERFACE_CTRL
),
433 pci_inl(reg(SERIAL_INTERFACE_CTRL
)) | pause_bit
);
439 int drv_resume(int sub_dev
) {
442 drv_reenable_int(sub_dev
); /* enable interrupts */
445 case DAC1_CHAN
: pause_bit
= P1_PAUSE
;break;
446 case DAC2_CHAN
: pause_bit
= P2_PAUSE
;break;
447 default: return EINVAL
;
450 /* clear pause bit */
451 pci_outl(reg(SERIAL_INTERFACE_CTRL
),
452 pci_inl(reg(SERIAL_INTERFACE_CTRL
)) & ~pause_bit
);
458 static int set_bits(u32_t nr_of_bits
, int sub_dev
) {
459 /* set format bits for specified channel. */
460 u16_t size_16_bit
, ser_interface
;
463 case ADC1_CHAN
: size_16_bit
= R1_S_EB
; break;
464 case DAC1_CHAN
: size_16_bit
= P1_S_EB
; break;
465 case DAC2_CHAN
: size_16_bit
= P2_S_EB
; break;
466 default: return EINVAL
;
469 ser_interface
= pci_inw(reg(SERIAL_INTERFACE_CTRL
));
470 ser_interface
&= ~size_16_bit
;
472 case 16: ser_interface
|= size_16_bit
;break;
474 default: return EINVAL
;
476 pci_outw(reg(SERIAL_INTERFACE_CTRL
), ser_interface
);
477 aud_conf
[sub_dev
].nr_of_bits
= nr_of_bits
;
482 static int set_stereo(u32_t stereo
, int sub_dev
) {
483 /* set format bits for specified channel. */
484 u16_t stereo_bit
, ser_interface
;
487 case ADC1_CHAN
: stereo_bit
= R1_S_MB
; break;
488 case DAC1_CHAN
: stereo_bit
= P1_S_MB
; break;
489 case DAC2_CHAN
: stereo_bit
= P2_S_MB
; break;
490 default: return EINVAL
;
492 ser_interface
= pci_inw(reg(SERIAL_INTERFACE_CTRL
));
493 ser_interface
&= ~stereo_bit
;
495 ser_interface
|= stereo_bit
;
497 pci_outw(reg(SERIAL_INTERFACE_CTRL
), ser_interface
);
498 aud_conf
[sub_dev
].stereo
= stereo
;
504 static int set_sign(u32_t
UNUSED(val
), int UNUSED(sub_dev
)) {
509 static int set_frag_size(u32_t fragment_size
, int sub_dev_nr
) {
510 if (fragment_size
> (sub_dev
[sub_dev_nr
].DmaSize
/
511 sub_dev
[sub_dev_nr
].NrOfDmaFragments
) ||
512 fragment_size
< sub_dev
[sub_dev_nr
].MinFragmentSize
) {
515 aud_conf
[sub_dev_nr
].fragment_size
= fragment_size
;
521 static int set_sample_rate(u32_t rate
, int sub_dev
) {
522 /* currently only 44.1kHz */
523 u32_t controlRegister
;
525 if (rate
> MAX_RATE
|| rate
< MIN_RATE
) {
529 controlRegister
= pci_inl(reg(CHIP_SEL_CTRL
));
530 controlRegister
|= FREQ_44K100
;
531 pci_outl(reg(CHIP_SEL_CTRL
), controlRegister
);
533 aud_conf
[sub_dev
].sample_rate
= rate
;
539 static int set_int_cnt(int chan
) {
540 /* Write interrupt count for specified channel.
541 After <DspFragmentSize> bytes, an interrupt will be generated */
546 if (aud_conf
[chan
].fragment_size
>
547 (sub_dev
[chan
].DmaSize
/ sub_dev
[chan
].NrOfDmaFragments
)
548 || aud_conf
[chan
].fragment_size
< sub_dev
[chan
].MinFragmentSize
) {
553 case ADC1_CHAN
: int_cnt_reg
= ADC_SAMP_CT
; break;
554 case DAC1_CHAN
: int_cnt_reg
= DAC1_SAMP_CT
; break;
555 case DAC2_CHAN
: int_cnt_reg
= DAC2_SAMP_CT
; break;
556 default: return EINVAL
;
559 sample_count
= aud_conf
[chan
].fragment_size
;
561 /* adjust sample count according to sample format */
562 if( aud_conf
[chan
].stereo
== TRUE
) sample_count
>>= 1;
563 switch(aud_conf
[chan
].nr_of_bits
) {
564 case 16: sample_count
>>= 1;break;
566 default: return EINVAL
;
569 /* set the sample count - 1 for the specified channel. */
570 pci_outw(reg(int_cnt_reg
), sample_count
- 1);
576 static int get_max_frag_size(u32_t
* val
, int * len
, int sub_dev_nr
) {
578 *val
= (sub_dev
[sub_dev_nr
].DmaSize
/
579 sub_dev
[sub_dev_nr
].NrOfDmaFragments
);
584 static int disable_int(int chan
) {
585 u16_t ser_interface
, int_en_bit
;
588 case ADC1_CHAN
: int_en_bit
= R1_INT_EN
; break;
589 case DAC1_CHAN
: int_en_bit
= P1_INTR_EN
; break;
590 case DAC2_CHAN
: int_en_bit
= P2_INTR_EN
; break;
591 default: return EINVAL
;
593 /* clear the interrupt */
594 ser_interface
= pci_inw(reg(SERIAL_INTERFACE_CTRL
));
595 pci_outw(reg(SERIAL_INTERFACE_CTRL
), ser_interface
& ~int_en_bit
);
600 static int get_samples_in_buf (u32_t
*samples_in_buf
, int *len
, int chan
) {
602 u16_t curr_samp_ct_reg
;
603 u16_t curr_samp_ct
; /* counts back from SAMP_CT till 0 */
605 *len
= sizeof(*samples_in_buf
);
609 curr_samp_ct_reg
= ADC_CURR_SAMP_CT
;
610 samp_ct_reg
= ADC_SAMP_CT
; break;
612 curr_samp_ct_reg
= DAC1_CURR_SAMP_CT
;
613 samp_ct_reg
= DAC1_SAMP_CT
; break;
615 curr_samp_ct_reg
= DAC2_CURR_SAMP_CT
;
616 samp_ct_reg
= DAC2_SAMP_CT
; break;
617 default: return EINVAL
;
620 /* TODO: is this inw useful? */
621 (void) pci_inw(reg(samp_ct_reg
));
622 curr_samp_ct
= pci_inw(reg(curr_samp_ct_reg
));
624 *samples_in_buf
= (u32_t
) (sub_dev
[chan
].BufLength
* 8192) +
631 /* returns 1 if there are free buffers */
632 static int free_buf (u32_t
*val
, int *len
, int sub_dev_nr
) {
634 if (sub_dev
[sub_dev_nr
].BufLength
==
635 sub_dev
[sub_dev_nr
].NrOfExtraBuffers
) {
645 static int get_set_volume(struct volume_level
*level
, int *len
, int sub_dev
,
647 *len
= sizeof(struct volume_level
);
648 if (sub_dev
== MIXER
) {
649 return ak4531_get_set_volume(level
, flag
);