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
20 #include "../framework/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 FORWARD
_PROTOTYPE( int detect_hw
, (void) );
32 FORWARD
_PROTOTYPE( int disable_int
, (int sub_dev
) );
33 FORWARD
_PROTOTYPE( int set_stereo
, (u32_t stereo
, int sub_dev
) );
34 FORWARD
_PROTOTYPE( int set_bits
, (u32_t nr_of_bits
, int sub_dev
) );
35 FORWARD
_PROTOTYPE( int set_sample_rate
, (u32_t rate
, int sub_dev
) );
36 FORWARD
_PROTOTYPE( int set_sign
, (u32_t val
, int sub_dev
) );
37 FORWARD
_PROTOTYPE( int get_max_frag_size
,
38 (u32_t
* val
, int *len
, int sub_dev
) );
39 FORWARD
_PROTOTYPE( int set_frag_size
, (u32_t fragment_size
, int sub_dev
) );
40 FORWARD
_PROTOTYPE( int set_int_cnt
, (int sub_dev
) );
41 FORWARD
_PROTOTYPE( int free_buf
, (u32_t
*val
, int *len
, int sub_dev
) );
42 FORWARD
_PROTOTYPE( int get_samples_in_buf
,
43 (u32_t
*val
, int *len
, int sub_dev
) );
44 FORWARD
_PROTOTYPE( int get_set_volume
, (struct volume_level
*level
, int *len
,
45 int sub_dev
, int flag
) );
46 FORWARD
_PROTOTYPE( int reset
, (int sub_dev
) );
50 aud_sub_dev_conf_t aud_conf
[4];
53 PUBLIC sub_dev_t sub_dev
[4];
54 PUBLIC special_file_t special_file
[4];
58 PUBLIC
int drv_init(void) {
59 drv
.DriverName
= DRIVER_NAME
;
60 drv
.NrOfSubDevices
= 4;
61 drv
.NrOfSpecialFiles
= 4;
63 sub_dev
[DAC1_CHAN
].readable
= 0;
64 sub_dev
[DAC1_CHAN
].writable
= 1;
65 sub_dev
[DAC1_CHAN
].DmaSize
= 64 * 1024;
66 sub_dev
[DAC1_CHAN
].NrOfDmaFragments
= 2;
67 sub_dev
[DAC1_CHAN
].MinFragmentSize
= 1024;
68 sub_dev
[DAC1_CHAN
].NrOfExtraBuffers
= 4;
70 sub_dev
[ADC1_CHAN
].readable
= 1;
71 sub_dev
[ADC1_CHAN
].writable
= 0;
72 sub_dev
[ADC1_CHAN
].DmaSize
= 64 * 1024;
73 sub_dev
[ADC1_CHAN
].NrOfDmaFragments
= 2;
74 sub_dev
[ADC1_CHAN
].MinFragmentSize
= 1024;
75 sub_dev
[ADC1_CHAN
].NrOfExtraBuffers
= 4;
77 sub_dev
[MIXER
].writable
= 0;
78 sub_dev
[MIXER
].readable
= 0;
80 sub_dev
[DAC2_CHAN
].readable
= 0;
81 sub_dev
[DAC2_CHAN
].writable
= 1;
82 sub_dev
[DAC2_CHAN
].DmaSize
= 64 * 1024;
83 sub_dev
[DAC2_CHAN
].NrOfDmaFragments
= 2;
84 sub_dev
[DAC2_CHAN
].MinFragmentSize
= 1024;
85 sub_dev
[DAC2_CHAN
].NrOfExtraBuffers
= 4;
87 special_file
[0].minor_dev_nr
= 0;
88 special_file
[0].write_chan
= DAC1_CHAN
;
89 special_file
[0].read_chan
= NO_CHANNEL
;
90 special_file
[0].io_ctl
= DAC1_CHAN
;
92 special_file
[1].minor_dev_nr
= 1;
93 special_file
[1].write_chan
= NO_CHANNEL
;
94 special_file
[1].read_chan
= ADC1_CHAN
;
95 special_file
[1].io_ctl
= ADC1_CHAN
;
97 special_file
[2].minor_dev_nr
= 2;
98 special_file
[2].write_chan
= NO_CHANNEL
;
99 special_file
[2].read_chan
= NO_CHANNEL
;
100 special_file
[2].io_ctl
= MIXER
;
102 special_file
[3].minor_dev_nr
= 3;
103 special_file
[3].write_chan
= DAC2_CHAN
;
104 special_file
[3].read_chan
= NO_CHANNEL
;
105 special_file
[3].io_ctl
= DAC2_CHAN
;
111 PUBLIC
int drv_init_hw (void) {
113 u16_t chip_sel_ctrl_reg
;
115 /* First, detect the hardware */
116 if (detect_hw() != OK
) {
120 /* PCI command register
121 * enable the SERR# driver, PCI bus mastering and I/O access
123 pci_attr_w16 (dev
.devind
, PCI_CR
, SERR_EN
|PCI_MASTER
|IO_ACCESS
);
125 /* turn everything off */
126 pci_outl(reg(CHIP_SEL_CTRL
), 0x0UL
);
128 /* turn off legacy (legacy control is undocumented) */
129 pci_outl(reg(LEGACY
), 0x0UL
);
130 pci_outl(reg(LEGACY
+4), 0x0UL
);
132 /* turn off serial interface */
133 pci_outl(reg(SERIAL_INTERFACE_CTRL
), 0x0UL
);
134 /*pci_outl(reg(SERIAL_INTERFACE_CTRL), 0x3UL);*/
137 /* enable the codec */
138 chip_sel_ctrl_reg
= pci_inw(reg(CHIP_SEL_CTRL
));
139 chip_sel_ctrl_reg
|= XCTL0
| CDC_EN
;
140 pci_outw(reg(CHIP_SEL_CTRL
), chip_sel_ctrl_reg
);
142 /* initialize the codec */
143 if (ak4531_init(reg(CODEC_WRITE_ADDRESS
),
144 reg(INTERRUPT_STATUS
), CWRIP
, reg(0)) < 0) {
148 /* clear all the memory */
149 for (i
= 0; i
< 0x10; ++i
) {
150 pci_outb(reg(MEM_PAGE
), i
);
151 for (j
= 0; j
< 0x10; j
+= 4) {
152 pci_outl (reg(MEMORY
) + j
, 0x0UL
);
156 /* initialize variables for each sub_device */
157 for (i
= 0; i
< drv
.NrOfSubDevices
; i
++) {
159 aud_conf
[i
].busy
= 0;
160 aud_conf
[i
].stereo
= DEFAULT_STEREO
;
161 aud_conf
[i
].sample_rate
= DEFAULT_RATE
;
162 aud_conf
[i
].nr_of_bits
= DEFAULT_NR_OF_BITS
;
163 aud_conf
[i
].sign
= DEFAULT_SIGNED
;
164 aud_conf
[i
].fragment_size
=
165 sub_dev
[i
].DmaSize
/ sub_dev
[i
].NrOfDmaFragments
;
172 PRIVATE
int detect_hw(void) {
177 /* detect_hw tries to find device and get IRQ and base address
178 with a little (much) help from the PCI library.
179 This code is quite device independent and you can copy it.
180 (just make sure to get the bugs out first)*/
183 /* get first device and then search through the list */
184 device
= pci_first_dev(&devind
, &v_id
, &d_id
);
185 while( device
> 0 ) {
186 /* if we have a match...break */
187 if (v_id
== VENDOR_ID
&& d_id
== DEVICE_ID
) break;
188 device
= pci_next_dev(&devind
, &v_id
, &d_id
);
191 /* did we find anything? */
192 if (v_id
!= VENDOR_ID
|| d_id
!= DEVICE_ID
) {
198 dev
.name
= pci_dev_name(v_id
, d_id
);
200 /* get base address of our device, ignore least signif. bit
201 this last bit thing could be device dependent, i don't know */
202 dev
.base
= pci_attr_r32(devind
, PCI_BAR
) & 0xfffffffe;
205 dev
.irq
= pci_attr_r8(devind
, PCI_ILR
);
206 dev
.revision
= pci_attr_r8(devind
, PCI_REV
);
209 dev
.devind
= devind
; /* pci device identifier */
215 PRIVATE
int reset(int chan
) {
217 sub_dev
[chan
].OutOfData
= 1;
228 int drv_start(int sub_dev
, int DmaMode
) {
229 u32_t enable_bit
, result
= 0;
231 /* Write default values to device in case user failed to configure.
232 If user did configure properly, everything is written twice.
233 please raise your hand if you object against to this strategy...*/
234 result
|= set_sample_rate(aud_conf
[sub_dev
].sample_rate
, sub_dev
);
235 result
|= set_stereo(aud_conf
[sub_dev
].stereo
, sub_dev
);
236 result
|= set_bits(aud_conf
[sub_dev
].nr_of_bits
, sub_dev
);
237 result
|= set_sign(aud_conf
[sub_dev
].sign
, sub_dev
);
239 /* set the interrupt count */
240 result
|= set_int_cnt(sub_dev
);
246 /* if device currently paused, resume */
250 case ADC1_CHAN
: enable_bit
= ADC1_EN
;break;
251 case DAC1_CHAN
: enable_bit
= DAC1_EN
;break;
252 case DAC2_CHAN
: enable_bit
= DAC2_EN
;break;
253 default: return EINVAL
;
256 /* enable interrupts from 'sub device' */
257 drv_reenable_int(sub_dev
);
259 /* this means play!!! */
260 pci_outw(reg(CHIP_SEL_CTRL
), pci_inw(reg(CHIP_SEL_CTRL
)) | enable_bit
);
262 aud_conf
[sub_dev
].busy
= 1;
268 int drv_stop(int sub_dev
)
274 case ADC1_CHAN
: enable_bit
= ADC1_EN
;break;
275 case DAC1_CHAN
: enable_bit
= DAC1_EN
;break;
276 case DAC2_CHAN
: enable_bit
= DAC2_EN
;break;
277 default: return EINVAL
;
280 /* stop the specified channel */
281 pci_outw(reg(CHIP_SEL_CTRL
),
282 pci_inw(reg(CHIP_SEL_CTRL
)) & ~enable_bit
);
283 aud_conf
[sub_dev
].busy
= 0;
284 status
= disable_int(sub_dev
);
290 /* all IO-ctl's sent to the upper driver are passed to this function */
291 int drv_io_ctl(int request
, void * val
, int * len
, int sub_dev
) {
297 status
= set_sample_rate(*((u32_t
*) val
), sub_dev
); break;
299 status
= set_stereo(*((u32_t
*) val
), sub_dev
); break;
301 status
= set_bits(*((u32_t
*) val
), sub_dev
); break;
303 status
= set_frag_size(*((u32_t
*) val
), sub_dev
); break;
305 status
= set_sign(*((u32_t
*) val
), sub_dev
); break;
307 status
= get_max_frag_size(val
, len
, sub_dev
); break;
309 status
= reset(sub_dev
); break;
311 status
= free_buf(val
, len
, sub_dev
); break;
312 case DSPIOSAMPLESINBUF
:
313 status
= get_samples_in_buf(val
, len
, sub_dev
); break;
315 status
= drv_pause(sub_dev
); break;
317 status
= drv_resume(sub_dev
); break;
319 status
= get_set_volume(val
, len
, sub_dev
, 0); break;
321 status
= get_set_volume(val
, len
, sub_dev
, 1); break;
323 status
= EINVAL
; break;
330 int drv_get_irq(char *irq
) {
336 int drv_get_frag_size(u32_t
*frag_size
, int sub_dev
) {
337 *frag_size
= aud_conf
[sub_dev
].fragment_size
;
342 int drv_set_dma(u32_t dma
, u32_t length
, int chan
) {
343 /* dma length in bytes,
344 max is 64k long words for es1370 = 256k bytes */
345 u32_t page
, frame_count_reg
, dma_add_reg
;
348 case ADC1_CHAN
: page
= ADC_MEM_PAGE
;
349 frame_count_reg
= ADC_BUFFER_SIZE
;
350 dma_add_reg
= ADC_PCI_ADDRESS
;
352 case DAC1_CHAN
: page
= DAC_MEM_PAGE
;
353 frame_count_reg
= DAC1_BUFFER_SIZE
;
354 dma_add_reg
= DAC1_PCI_ADDRESS
;
356 case DAC2_CHAN
: page
= DAC_MEM_PAGE
;
357 frame_count_reg
= DAC2_BUFFER_SIZE
;
358 dma_add_reg
= DAC2_PCI_ADDRESS
;
362 pci_outb(reg(MEM_PAGE
), page
);
363 pci_outl(reg(dma_add_reg
), dma
);
365 /* device expects long word count in stead of bytes */
368 /* It seems that register _CURRENT_COUNT is overwritten, but this is
369 * the way to go. The register frame_count_reg is only longword
371 * It expects length -1
373 pci_outl(reg(frame_count_reg
), (u32_t
) (length
- 1));
379 /* return status of the interrupt summary bit */
380 int drv_int_sum(void) {
381 return pci_inl(reg(INTERRUPT_STATUS
)) & INTR
;
385 int drv_int(int sub_dev
) {
389 /* return status of interrupt bit of specified channel*/
391 case DAC1_CHAN
: bit
= DAC1
; break;
392 case DAC2_CHAN
: bit
= DAC2
; break;
393 case ADC1_CHAN
: bit
= ADC
; break;
394 default: return EINVAL
;
397 int_status
= pci_inl(reg(INTERRUPT_STATUS
)) & bit
;
403 int drv_reenable_int(int chan
) {
404 u16_t ser_interface
, int_en_bit
;
407 case ADC1_CHAN
: int_en_bit
= R1_INT_EN
; break;
408 case DAC1_CHAN
: int_en_bit
= P1_INTR_EN
; break;
409 case DAC2_CHAN
: int_en_bit
= P2_INTR_EN
; break;
410 default: return EINVAL
;
413 /* clear and reenable an interrupt */
414 ser_interface
= pci_inw(reg(SERIAL_INTERFACE_CTRL
));
415 pci_outw(reg(SERIAL_INTERFACE_CTRL
), ser_interface
& ~int_en_bit
);
416 pci_outw(reg(SERIAL_INTERFACE_CTRL
), ser_interface
| int_en_bit
);
422 int drv_pause(int sub_dev
) {
425 disable_int(sub_dev
); /* don't send interrupts */
428 case DAC1_CHAN
: pause_bit
= P1_PAUSE
;break;
429 case DAC2_CHAN
: pause_bit
= P2_PAUSE
;break;
430 default: return EINVAL
;
434 pci_outl(reg(SERIAL_INTERFACE_CTRL
),
435 pci_inl(reg(SERIAL_INTERFACE_CTRL
)) | pause_bit
);
441 int drv_resume(int sub_dev
) {
444 drv_reenable_int(sub_dev
); /* enable interrupts */
447 case DAC1_CHAN
: pause_bit
= P1_PAUSE
;break;
448 case DAC2_CHAN
: pause_bit
= P2_PAUSE
;break;
449 default: return EINVAL
;
452 /* clear pause bit */
453 pci_outl(reg(SERIAL_INTERFACE_CTRL
),
454 pci_inl(reg(SERIAL_INTERFACE_CTRL
)) & ~pause_bit
);
460 PRIVATE
int set_bits(u32_t nr_of_bits
, int sub_dev
) {
461 /* set format bits for specified channel. */
462 u16_t size_16_bit
, ser_interface
;
465 case ADC1_CHAN
: size_16_bit
= R1_S_EB
; break;
466 case DAC1_CHAN
: size_16_bit
= P1_S_EB
; break;
467 case DAC2_CHAN
: size_16_bit
= P2_S_EB
; break;
468 default: return EINVAL
;
471 ser_interface
= pci_inw(reg(SERIAL_INTERFACE_CTRL
));
472 ser_interface
&= ~size_16_bit
;
474 case 16: ser_interface
|= size_16_bit
;break;
476 default: return EINVAL
;
478 pci_outw(reg(SERIAL_INTERFACE_CTRL
), ser_interface
);
479 aud_conf
[sub_dev
].nr_of_bits
= nr_of_bits
;
484 PRIVATE
int set_stereo(u32_t stereo
, int sub_dev
) {
485 /* set format bits for specified channel. */
486 u16_t stereo_bit
, ser_interface
;
489 case ADC1_CHAN
: stereo_bit
= R1_S_MB
; break;
490 case DAC1_CHAN
: stereo_bit
= P1_S_MB
; break;
491 case DAC2_CHAN
: stereo_bit
= P2_S_MB
; break;
492 default: return EINVAL
;
494 ser_interface
= pci_inw(reg(SERIAL_INTERFACE_CTRL
));
495 ser_interface
&= ~stereo_bit
;
497 ser_interface
|= stereo_bit
;
499 pci_outw(reg(SERIAL_INTERFACE_CTRL
), ser_interface
);
500 aud_conf
[sub_dev
].stereo
= stereo
;
506 PRIVATE
int set_sign(u32_t val
, int sub_dev
) {
511 PRIVATE
int set_frag_size(u32_t fragment_size
, int sub_dev_nr
) {
512 if (fragment_size
> (sub_dev
[sub_dev_nr
].DmaSize
/
513 sub_dev
[sub_dev_nr
].NrOfDmaFragments
) ||
514 fragment_size
< sub_dev
[sub_dev_nr
].MinFragmentSize
) {
517 aud_conf
[sub_dev_nr
].fragment_size
= fragment_size
;
523 PRIVATE
int set_sample_rate(u32_t rate
, int sub_dev
) {
524 /* currently only 44.1kHz */
525 u32_t controlRegister
;
527 if (rate
> MAX_RATE
|| rate
< MIN_RATE
) {
531 controlRegister
= pci_inl(reg(CHIP_SEL_CTRL
));
532 controlRegister
|= FREQ_44K100
;
533 pci_outl(reg(CHIP_SEL_CTRL
), controlRegister
);
535 aud_conf
[sub_dev
].sample_rate
= rate
;
541 PRIVATE
int set_int_cnt(int chan
) {
542 /* Write interrupt count for specified channel.
543 After <DspFragmentSize> bytes, an interrupt will be generated */
548 if (aud_conf
[chan
].fragment_size
>
549 (sub_dev
[chan
].DmaSize
/ sub_dev
[chan
].NrOfDmaFragments
)
550 || aud_conf
[chan
].fragment_size
< sub_dev
[chan
].MinFragmentSize
) {
555 case ADC1_CHAN
: int_cnt_reg
= ADC_SAMP_CT
; break;
556 case DAC1_CHAN
: int_cnt_reg
= DAC1_SAMP_CT
; break;
557 case DAC2_CHAN
: int_cnt_reg
= DAC2_SAMP_CT
; break;
558 default: return EINVAL
;
561 sample_count
= aud_conf
[chan
].fragment_size
;
563 /* adjust sample count according to sample format */
564 if( aud_conf
[chan
].stereo
== TRUE
) sample_count
>>= 1;
565 switch(aud_conf
[chan
].nr_of_bits
) {
566 case 16: sample_count
>>= 1;break;
568 default: return EINVAL
;
571 /* set the sample count - 1 for the specified channel. */
572 pci_outw(reg(int_cnt_reg
), sample_count
- 1);
578 PRIVATE
int get_max_frag_size(u32_t
* val
, int * len
, int sub_dev_nr
) {
580 *val
= (sub_dev
[sub_dev_nr
].DmaSize
/
581 sub_dev
[sub_dev_nr
].NrOfDmaFragments
);
586 PRIVATE
int disable_int(int chan
) {
587 u16_t ser_interface
, int_en_bit
;
590 case ADC1_CHAN
: int_en_bit
= R1_INT_EN
; break;
591 case DAC1_CHAN
: int_en_bit
= P1_INTR_EN
; break;
592 case DAC2_CHAN
: int_en_bit
= P2_INTR_EN
; break;
593 default: return EINVAL
;
595 /* clear the interrupt */
596 ser_interface
= pci_inw(reg(SERIAL_INTERFACE_CTRL
));
597 pci_outw(reg(SERIAL_INTERFACE_CTRL
), ser_interface
& ~int_en_bit
);
602 PRIVATE
int get_samples_in_buf (u32_t
*samples_in_buf
, int *len
, int chan
) {
604 u16_t curr_samp_ct_reg
;
605 u16_t samp_ct
; /* nr of samples - 1 that will be played back */
606 u16_t curr_samp_ct
; /* counts back from SAMP_CT till 0 */
608 *len
= sizeof(*samples_in_buf
);
612 curr_samp_ct_reg
= ADC_CURR_SAMP_CT
;
613 samp_ct_reg
= ADC_SAMP_CT
; break;
615 curr_samp_ct_reg
= DAC1_CURR_SAMP_CT
;
616 samp_ct_reg
= DAC1_SAMP_CT
; break;
618 curr_samp_ct_reg
= DAC2_CURR_SAMP_CT
;
619 samp_ct_reg
= DAC2_SAMP_CT
; break;
620 default: return EINVAL
;
623 samp_ct
= pci_inw(reg(samp_ct_reg
));
624 curr_samp_ct
= pci_inw(reg(curr_samp_ct_reg
));
626 *samples_in_buf
= (u32_t
) (sub_dev
[chan
].BufLength
* 8192) +
633 /* returns 1 if there are free buffers */
634 PRIVATE
int free_buf (u32_t
*val
, int *len
, int sub_dev_nr
) {
636 if (sub_dev
[sub_dev_nr
].BufLength
==
637 sub_dev
[sub_dev_nr
].NrOfExtraBuffers
) {
647 PRIVATE
int get_set_volume(struct volume_level
*level
, int *len
, int sub_dev
,
649 *len
= sizeof(struct volume_level
);
650 if (sub_dev
== MIXER
) {
651 return ak4531_get_set_volume(level
, flag
);