1*433d6423SLionel Sambuc /* Best viewed with tabsize 4 */
2*433d6423SLionel Sambuc
3*433d6423SLionel Sambuc /* Ensoniq ES1370 driver
4*433d6423SLionel Sambuc *
5*433d6423SLionel Sambuc * aka AudioPCI '97
6*433d6423SLionel Sambuc *
7*433d6423SLionel Sambuc * This is the main file of the ES1370 sound driver. There is no main function
8*433d6423SLionel Sambuc * over here, instead the main function is located in the generic dma driver.
9*433d6423SLionel Sambuc * All this driver does is implement the interface audio/audio_fw.h. All
10*433d6423SLionel Sambuc * functions having the prefix 'drv_' are dictated by audio/audio_fw.h. The
11*433d6423SLionel Sambuc * function prototypes you see below define a set of private helper functions.
12*433d6423SLionel Sambuc * Control over the AK4531 codec is delegated ak4531.c.
13*433d6423SLionel Sambuc *
14*433d6423SLionel Sambuc * September 2007 ES1370 driver (Pieter Hijma),
15*433d6423SLionel Sambuc * based on ES1371 driver by Laurens Bronwasser
16*433d6423SLionel Sambuc */
17*433d6423SLionel Sambuc
18*433d6423SLionel Sambuc #include <machine/pci.h>
19*433d6423SLionel Sambuc
20*433d6423SLionel Sambuc #include <minix/audio_fw.h>
21*433d6423SLionel Sambuc #include "es1370.h"
22*433d6423SLionel Sambuc #include "ak4531.h"
23*433d6423SLionel Sambuc #include "pci_helper.h"
24*433d6423SLionel Sambuc
25*433d6423SLionel Sambuc
26*433d6423SLionel Sambuc /* reg(n) will be the device specific addresses */
27*433d6423SLionel Sambuc #define reg(n) (dev.base + (n))
28*433d6423SLionel Sambuc
29*433d6423SLionel Sambuc
30*433d6423SLionel Sambuc /* prototypes of private functions */
31*433d6423SLionel Sambuc static int detect_hw(void);
32*433d6423SLionel Sambuc static int disable_int(int sub_dev);
33*433d6423SLionel Sambuc static int set_stereo(u32_t stereo, int sub_dev);
34*433d6423SLionel Sambuc static int set_bits(u32_t nr_of_bits, int sub_dev);
35*433d6423SLionel Sambuc static int set_sample_rate(u32_t rate, int sub_dev);
36*433d6423SLionel Sambuc static int set_sign(u32_t val, int sub_dev);
37*433d6423SLionel Sambuc static int get_max_frag_size(u32_t * val, int *len, int sub_dev);
38*433d6423SLionel Sambuc static int set_frag_size(u32_t fragment_size, int sub_dev);
39*433d6423SLionel Sambuc static int set_int_cnt(int sub_dev);
40*433d6423SLionel Sambuc static int free_buf(u32_t *val, int *len, int sub_dev);
41*433d6423SLionel Sambuc static int get_samples_in_buf(u32_t *val, int *len, int sub_dev);
42*433d6423SLionel Sambuc static int get_set_volume(struct volume_level *level, int *len, int
43*433d6423SLionel Sambuc sub_dev, int flag);
44*433d6423SLionel Sambuc static int reset(int sub_dev);
45*433d6423SLionel Sambuc
46*433d6423SLionel Sambuc
47*433d6423SLionel Sambuc DEV_STRUCT dev;
48*433d6423SLionel Sambuc aud_sub_dev_conf_t aud_conf[4];
49*433d6423SLionel Sambuc
50*433d6423SLionel Sambuc
51*433d6423SLionel Sambuc sub_dev_t sub_dev[4];
52*433d6423SLionel Sambuc special_file_t special_file[4];
53*433d6423SLionel Sambuc drv_t drv;
54*433d6423SLionel Sambuc
55*433d6423SLionel Sambuc
drv_init(void)56*433d6423SLionel Sambuc int drv_init(void) {
57*433d6423SLionel Sambuc drv.DriverName = DRIVER_NAME;
58*433d6423SLionel Sambuc drv.NrOfSubDevices = 4;
59*433d6423SLionel Sambuc drv.NrOfSpecialFiles = 4;
60*433d6423SLionel Sambuc
61*433d6423SLionel Sambuc sub_dev[DAC1_CHAN].readable = 0;
62*433d6423SLionel Sambuc sub_dev[DAC1_CHAN].writable = 1;
63*433d6423SLionel Sambuc sub_dev[DAC1_CHAN].DmaSize = 64 * 1024;
64*433d6423SLionel Sambuc sub_dev[DAC1_CHAN].NrOfDmaFragments = 2;
65*433d6423SLionel Sambuc sub_dev[DAC1_CHAN].MinFragmentSize = 1024;
66*433d6423SLionel Sambuc sub_dev[DAC1_CHAN].NrOfExtraBuffers = 4;
67*433d6423SLionel Sambuc
68*433d6423SLionel Sambuc sub_dev[ADC1_CHAN].readable = 1;
69*433d6423SLionel Sambuc sub_dev[ADC1_CHAN].writable = 0;
70*433d6423SLionel Sambuc sub_dev[ADC1_CHAN].DmaSize = 64 * 1024;
71*433d6423SLionel Sambuc sub_dev[ADC1_CHAN].NrOfDmaFragments = 2;
72*433d6423SLionel Sambuc sub_dev[ADC1_CHAN].MinFragmentSize = 1024;
73*433d6423SLionel Sambuc sub_dev[ADC1_CHAN].NrOfExtraBuffers = 4;
74*433d6423SLionel Sambuc
75*433d6423SLionel Sambuc sub_dev[MIXER].writable = 0;
76*433d6423SLionel Sambuc sub_dev[MIXER].readable = 0;
77*433d6423SLionel Sambuc
78*433d6423SLionel Sambuc sub_dev[DAC2_CHAN].readable = 0;
79*433d6423SLionel Sambuc sub_dev[DAC2_CHAN].writable = 1;
80*433d6423SLionel Sambuc sub_dev[DAC2_CHAN].DmaSize = 64 * 1024;
81*433d6423SLionel Sambuc sub_dev[DAC2_CHAN].NrOfDmaFragments = 2;
82*433d6423SLionel Sambuc sub_dev[DAC2_CHAN].MinFragmentSize = 1024;
83*433d6423SLionel Sambuc sub_dev[DAC2_CHAN].NrOfExtraBuffers = 4;
84*433d6423SLionel Sambuc
85*433d6423SLionel Sambuc special_file[0].minor_dev_nr = 0;
86*433d6423SLionel Sambuc special_file[0].write_chan = DAC1_CHAN;
87*433d6423SLionel Sambuc special_file[0].read_chan = NO_CHANNEL;
88*433d6423SLionel Sambuc special_file[0].io_ctl = DAC1_CHAN;
89*433d6423SLionel Sambuc
90*433d6423SLionel Sambuc special_file[1].minor_dev_nr = 1;
91*433d6423SLionel Sambuc special_file[1].write_chan = NO_CHANNEL;
92*433d6423SLionel Sambuc special_file[1].read_chan = ADC1_CHAN;
93*433d6423SLionel Sambuc special_file[1].io_ctl = ADC1_CHAN;
94*433d6423SLionel Sambuc
95*433d6423SLionel Sambuc special_file[2].minor_dev_nr = 2;
96*433d6423SLionel Sambuc special_file[2].write_chan = NO_CHANNEL;
97*433d6423SLionel Sambuc special_file[2].read_chan = NO_CHANNEL;
98*433d6423SLionel Sambuc special_file[2].io_ctl = MIXER;
99*433d6423SLionel Sambuc
100*433d6423SLionel Sambuc special_file[3].minor_dev_nr = 3;
101*433d6423SLionel Sambuc special_file[3].write_chan = DAC2_CHAN;
102*433d6423SLionel Sambuc special_file[3].read_chan = NO_CHANNEL;
103*433d6423SLionel Sambuc special_file[3].io_ctl = DAC2_CHAN;
104*433d6423SLionel Sambuc
105*433d6423SLionel Sambuc return OK;
106*433d6423SLionel Sambuc }
107*433d6423SLionel Sambuc
108*433d6423SLionel Sambuc
drv_init_hw(void)109*433d6423SLionel Sambuc int drv_init_hw (void) {
110*433d6423SLionel Sambuc u16_t i, j;
111*433d6423SLionel Sambuc u16_t chip_sel_ctrl_reg;
112*433d6423SLionel Sambuc
113*433d6423SLionel Sambuc /* First, detect the hardware */
114*433d6423SLionel Sambuc if (detect_hw() != OK) {
115*433d6423SLionel Sambuc return EIO;
116*433d6423SLionel Sambuc }
117*433d6423SLionel Sambuc
118*433d6423SLionel Sambuc /* PCI command register
119*433d6423SLionel Sambuc * enable the SERR# driver, PCI bus mastering and I/O access
120*433d6423SLionel Sambuc */
121*433d6423SLionel Sambuc pci_attr_w16 (dev.devind, PCI_CR, SERR_EN|PCI_MASTER|IO_ACCESS);
122*433d6423SLionel Sambuc
123*433d6423SLionel Sambuc /* turn everything off */
124*433d6423SLionel Sambuc pci_outl(reg(CHIP_SEL_CTRL), 0x0UL);
125*433d6423SLionel Sambuc
126*433d6423SLionel Sambuc /* turn off legacy (legacy control is undocumented) */
127*433d6423SLionel Sambuc pci_outl(reg(LEGACY), 0x0UL);
128*433d6423SLionel Sambuc pci_outl(reg(LEGACY+4), 0x0UL);
129*433d6423SLionel Sambuc
130*433d6423SLionel Sambuc /* turn off serial interface */
131*433d6423SLionel Sambuc pci_outl(reg(SERIAL_INTERFACE_CTRL), 0x0UL);
132*433d6423SLionel Sambuc /*pci_outl(reg(SERIAL_INTERFACE_CTRL), 0x3UL);*/
133*433d6423SLionel Sambuc
134*433d6423SLionel Sambuc
135*433d6423SLionel Sambuc /* enable the codec */
136*433d6423SLionel Sambuc chip_sel_ctrl_reg = pci_inw(reg(CHIP_SEL_CTRL));
137*433d6423SLionel Sambuc chip_sel_ctrl_reg |= XCTL0 | CDC_EN;
138*433d6423SLionel Sambuc pci_outw(reg(CHIP_SEL_CTRL), chip_sel_ctrl_reg);
139*433d6423SLionel Sambuc
140*433d6423SLionel Sambuc /* initialize the codec */
141*433d6423SLionel Sambuc if (ak4531_init(reg(CODEC_WRITE_ADDRESS),
142*433d6423SLionel Sambuc reg(INTERRUPT_STATUS), CWRIP, reg(0)) < 0) {
143*433d6423SLionel Sambuc return EINVAL;
144*433d6423SLionel Sambuc }
145*433d6423SLionel Sambuc
146*433d6423SLionel Sambuc /* clear all the memory */
147*433d6423SLionel Sambuc for (i = 0; i < 0x10; ++i) {
148*433d6423SLionel Sambuc pci_outb(reg(MEM_PAGE), i);
149*433d6423SLionel Sambuc for (j = 0; j < 0x10; j += 4) {
150*433d6423SLionel Sambuc pci_outl (reg(MEMORY) + j, 0x0UL);
151*433d6423SLionel Sambuc }
152*433d6423SLionel Sambuc }
153*433d6423SLionel Sambuc
154*433d6423SLionel Sambuc /* initialize variables for each sub_device */
155*433d6423SLionel Sambuc for (i = 0; i < drv.NrOfSubDevices; i++) {
156*433d6423SLionel Sambuc if(i != MIXER) {
157*433d6423SLionel Sambuc aud_conf[i].busy = 0;
158*433d6423SLionel Sambuc aud_conf[i].stereo = DEFAULT_STEREO;
159*433d6423SLionel Sambuc aud_conf[i].sample_rate = DEFAULT_RATE;
160*433d6423SLionel Sambuc aud_conf[i].nr_of_bits = DEFAULT_NR_OF_BITS;
161*433d6423SLionel Sambuc aud_conf[i].sign = DEFAULT_SIGNED;
162*433d6423SLionel Sambuc aud_conf[i].fragment_size =
163*433d6423SLionel Sambuc sub_dev[i].DmaSize / sub_dev[i].NrOfDmaFragments;
164*433d6423SLionel Sambuc }
165*433d6423SLionel Sambuc }
166*433d6423SLionel Sambuc return OK;
167*433d6423SLionel Sambuc }
168*433d6423SLionel Sambuc
169*433d6423SLionel Sambuc
detect_hw(void)170*433d6423SLionel Sambuc static int detect_hw(void) {
171*433d6423SLionel Sambuc u32_t device;
172*433d6423SLionel Sambuc int devind;
173*433d6423SLionel Sambuc u16_t v_id, d_id;
174*433d6423SLionel Sambuc
175*433d6423SLionel Sambuc /* detect_hw tries to find device and get IRQ and base address
176*433d6423SLionel Sambuc with a little (much) help from the PCI library.
177*433d6423SLionel Sambuc This code is quite device independent and you can copy it.
178*433d6423SLionel Sambuc (just make sure to get the bugs out first)*/
179*433d6423SLionel Sambuc
180*433d6423SLionel Sambuc pci_init();
181*433d6423SLionel Sambuc /* get first device and then search through the list */
182*433d6423SLionel Sambuc device = pci_first_dev(&devind, &v_id, &d_id);
183*433d6423SLionel Sambuc while( device > 0 ) {
184*433d6423SLionel Sambuc /* if we have a match...break */
185*433d6423SLionel Sambuc if (v_id == VENDOR_ID && d_id == DEVICE_ID) break;
186*433d6423SLionel Sambuc device = pci_next_dev(&devind, &v_id, &d_id);
187*433d6423SLionel Sambuc }
188*433d6423SLionel Sambuc
189*433d6423SLionel Sambuc /* did we find anything? */
190*433d6423SLionel Sambuc if (v_id != VENDOR_ID || d_id != DEVICE_ID) {
191*433d6423SLionel Sambuc return EIO;
192*433d6423SLionel Sambuc }
193*433d6423SLionel Sambuc
194*433d6423SLionel Sambuc pci_reserve(devind);
195*433d6423SLionel Sambuc
196*433d6423SLionel Sambuc dev.name = pci_dev_name(v_id, d_id);
197*433d6423SLionel Sambuc
198*433d6423SLionel Sambuc /* get base address of our device, ignore least signif. bit
199*433d6423SLionel Sambuc this last bit thing could be device dependent, i don't know */
200*433d6423SLionel Sambuc dev.base = pci_attr_r32(devind, PCI_BAR) & 0xfffffffe;
201*433d6423SLionel Sambuc
202*433d6423SLionel Sambuc /* get IRQ */
203*433d6423SLionel Sambuc dev.irq = pci_attr_r8(devind, PCI_ILR);
204*433d6423SLionel Sambuc dev.revision = pci_attr_r8(devind, PCI_REV);
205*433d6423SLionel Sambuc dev.d_id = d_id;
206*433d6423SLionel Sambuc dev.v_id = v_id;
207*433d6423SLionel Sambuc dev.devind = devind; /* pci device identifier */
208*433d6423SLionel Sambuc
209*433d6423SLionel Sambuc return OK;
210*433d6423SLionel Sambuc }
211*433d6423SLionel Sambuc
212*433d6423SLionel Sambuc
reset(int chan)213*433d6423SLionel Sambuc static int reset(int chan) {
214*433d6423SLionel Sambuc drv_stop(chan);
215*433d6423SLionel Sambuc sub_dev[chan].OutOfData = 1;
216*433d6423SLionel Sambuc
217*433d6423SLionel Sambuc return OK;
218*433d6423SLionel Sambuc }
219*433d6423SLionel Sambuc
220*433d6423SLionel Sambuc
drv_reset()221*433d6423SLionel Sambuc int drv_reset() {
222*433d6423SLionel Sambuc return OK;
223*433d6423SLionel Sambuc }
224*433d6423SLionel Sambuc
225*433d6423SLionel Sambuc
drv_start(int sub_dev,int UNUSED (DmaMode))226*433d6423SLionel Sambuc int drv_start(int sub_dev, int UNUSED(DmaMode)) {
227*433d6423SLionel Sambuc u32_t enable_bit, result = 0;
228*433d6423SLionel Sambuc
229*433d6423SLionel Sambuc /* Write default values to device in case user failed to configure.
230*433d6423SLionel Sambuc If user did configure properly, everything is written twice.
231*433d6423SLionel Sambuc please raise your hand if you object against to this strategy...*/
232*433d6423SLionel Sambuc result |= set_sample_rate(aud_conf[sub_dev].sample_rate, sub_dev);
233*433d6423SLionel Sambuc result |= set_stereo(aud_conf[sub_dev].stereo, sub_dev);
234*433d6423SLionel Sambuc result |= set_bits(aud_conf[sub_dev].nr_of_bits, sub_dev);
235*433d6423SLionel Sambuc result |= set_sign(aud_conf[sub_dev].sign, sub_dev);
236*433d6423SLionel Sambuc
237*433d6423SLionel Sambuc /* set the interrupt count */
238*433d6423SLionel Sambuc result |= set_int_cnt(sub_dev);
239*433d6423SLionel Sambuc
240*433d6423SLionel Sambuc if (result) {
241*433d6423SLionel Sambuc return EIO;
242*433d6423SLionel Sambuc }
243*433d6423SLionel Sambuc
244*433d6423SLionel Sambuc /* if device currently paused, resume */
245*433d6423SLionel Sambuc drv_resume(sub_dev);
246*433d6423SLionel Sambuc
247*433d6423SLionel Sambuc switch(sub_dev) {
248*433d6423SLionel Sambuc case ADC1_CHAN: enable_bit = ADC1_EN;break;
249*433d6423SLionel Sambuc case DAC1_CHAN: enable_bit = DAC1_EN;break;
250*433d6423SLionel Sambuc case DAC2_CHAN: enable_bit = DAC2_EN;break;
251*433d6423SLionel Sambuc default: return EINVAL;
252*433d6423SLionel Sambuc }
253*433d6423SLionel Sambuc
254*433d6423SLionel Sambuc /* enable interrupts from 'sub device' */
255*433d6423SLionel Sambuc drv_reenable_int(sub_dev);
256*433d6423SLionel Sambuc
257*433d6423SLionel Sambuc /* this means play!!! */
258*433d6423SLionel Sambuc pci_outw(reg(CHIP_SEL_CTRL), pci_inw(reg(CHIP_SEL_CTRL)) | enable_bit);
259*433d6423SLionel Sambuc
260*433d6423SLionel Sambuc aud_conf[sub_dev].busy = 1;
261*433d6423SLionel Sambuc
262*433d6423SLionel Sambuc return OK;
263*433d6423SLionel Sambuc }
264*433d6423SLionel Sambuc
265*433d6423SLionel Sambuc
drv_stop(int sub_dev)266*433d6423SLionel Sambuc int drv_stop(int sub_dev)
267*433d6423SLionel Sambuc {
268*433d6423SLionel Sambuc u32_t enable_bit;
269*433d6423SLionel Sambuc int status;
270*433d6423SLionel Sambuc
271*433d6423SLionel Sambuc switch(sub_dev) {
272*433d6423SLionel Sambuc case ADC1_CHAN: enable_bit = ADC1_EN;break;
273*433d6423SLionel Sambuc case DAC1_CHAN: enable_bit = DAC1_EN;break;
274*433d6423SLionel Sambuc case DAC2_CHAN: enable_bit = DAC2_EN;break;
275*433d6423SLionel Sambuc default: return EINVAL;
276*433d6423SLionel Sambuc }
277*433d6423SLionel Sambuc
278*433d6423SLionel Sambuc /* stop the specified channel */
279*433d6423SLionel Sambuc pci_outw(reg(CHIP_SEL_CTRL),
280*433d6423SLionel Sambuc pci_inw(reg(CHIP_SEL_CTRL)) & ~enable_bit);
281*433d6423SLionel Sambuc aud_conf[sub_dev].busy = 0;
282*433d6423SLionel Sambuc status = disable_int(sub_dev);
283*433d6423SLionel Sambuc
284*433d6423SLionel Sambuc return status;
285*433d6423SLionel Sambuc }
286*433d6423SLionel Sambuc
287*433d6423SLionel Sambuc
288*433d6423SLionel Sambuc /* all IO-ctl's sent to the upper driver are passed to this function */
drv_io_ctl(unsigned long request,void * val,int * len,int sub_dev)289*433d6423SLionel Sambuc int drv_io_ctl(unsigned long request, void * val, int * len, int sub_dev) {
290*433d6423SLionel Sambuc
291*433d6423SLionel Sambuc int status;
292*433d6423SLionel Sambuc
293*433d6423SLionel Sambuc switch(request) {
294*433d6423SLionel Sambuc case DSPIORATE:
295*433d6423SLionel Sambuc status = set_sample_rate(*((u32_t *) val), sub_dev); break;
296*433d6423SLionel Sambuc case DSPIOSTEREO:
297*433d6423SLionel Sambuc status = set_stereo(*((u32_t *) val), sub_dev); break;
298*433d6423SLionel Sambuc case DSPIOBITS:
299*433d6423SLionel Sambuc status = set_bits(*((u32_t *) val), sub_dev); break;
300*433d6423SLionel Sambuc case DSPIOSIZE:
301*433d6423SLionel Sambuc status = set_frag_size(*((u32_t *) val), sub_dev); break;
302*433d6423SLionel Sambuc case DSPIOSIGN:
303*433d6423SLionel Sambuc status = set_sign(*((u32_t *) val), sub_dev); break;
304*433d6423SLionel Sambuc case DSPIOMAX:
305*433d6423SLionel Sambuc status = get_max_frag_size(val, len, sub_dev); break;
306*433d6423SLionel Sambuc case DSPIORESET:
307*433d6423SLionel Sambuc status = reset(sub_dev); break;
308*433d6423SLionel Sambuc case DSPIOFREEBUF:
309*433d6423SLionel Sambuc status = free_buf(val, len, sub_dev); break;
310*433d6423SLionel Sambuc case DSPIOSAMPLESINBUF:
311*433d6423SLionel Sambuc status = get_samples_in_buf(val, len, sub_dev); break;
312*433d6423SLionel Sambuc case DSPIOPAUSE:
313*433d6423SLionel Sambuc status = drv_pause(sub_dev); break;
314*433d6423SLionel Sambuc case DSPIORESUME:
315*433d6423SLionel Sambuc status = drv_resume(sub_dev); break;
316*433d6423SLionel Sambuc case MIXIOGETVOLUME:
317*433d6423SLionel Sambuc status = get_set_volume(val, len, sub_dev, 0); break;
318*433d6423SLionel Sambuc case MIXIOSETVOLUME:
319*433d6423SLionel Sambuc status = get_set_volume(val, len, sub_dev, 1); break;
320*433d6423SLionel Sambuc default:
321*433d6423SLionel Sambuc status = EINVAL; break;
322*433d6423SLionel Sambuc }
323*433d6423SLionel Sambuc
324*433d6423SLionel Sambuc return status;
325*433d6423SLionel Sambuc }
326*433d6423SLionel Sambuc
327*433d6423SLionel Sambuc
drv_get_irq(char * irq)328*433d6423SLionel Sambuc int drv_get_irq(char *irq) {
329*433d6423SLionel Sambuc *irq = dev.irq;
330*433d6423SLionel Sambuc return OK;
331*433d6423SLionel Sambuc }
332*433d6423SLionel Sambuc
333*433d6423SLionel Sambuc
drv_get_frag_size(u32_t * frag_size,int sub_dev)334*433d6423SLionel Sambuc int drv_get_frag_size(u32_t *frag_size, int sub_dev) {
335*433d6423SLionel Sambuc *frag_size = aud_conf[sub_dev].fragment_size;
336*433d6423SLionel Sambuc return OK;
337*433d6423SLionel Sambuc }
338*433d6423SLionel Sambuc
339*433d6423SLionel Sambuc
drv_set_dma(u32_t dma,u32_t length,int chan)340*433d6423SLionel Sambuc int drv_set_dma(u32_t dma, u32_t length, int chan) {
341*433d6423SLionel Sambuc /* dma length in bytes,
342*433d6423SLionel Sambuc max is 64k long words for es1370 = 256k bytes */
343*433d6423SLionel Sambuc u32_t page, frame_count_reg, dma_add_reg;
344*433d6423SLionel Sambuc
345*433d6423SLionel Sambuc switch(chan) {
346*433d6423SLionel Sambuc case ADC1_CHAN: page = ADC_MEM_PAGE;
347*433d6423SLionel Sambuc frame_count_reg = ADC_BUFFER_SIZE;
348*433d6423SLionel Sambuc dma_add_reg = ADC_PCI_ADDRESS;
349*433d6423SLionel Sambuc break;
350*433d6423SLionel Sambuc case DAC1_CHAN: page = DAC_MEM_PAGE;
351*433d6423SLionel Sambuc frame_count_reg = DAC1_BUFFER_SIZE;
352*433d6423SLionel Sambuc dma_add_reg = DAC1_PCI_ADDRESS;
353*433d6423SLionel Sambuc break;
354*433d6423SLionel Sambuc case DAC2_CHAN: page = DAC_MEM_PAGE;
355*433d6423SLionel Sambuc frame_count_reg = DAC2_BUFFER_SIZE;
356*433d6423SLionel Sambuc dma_add_reg = DAC2_PCI_ADDRESS;
357*433d6423SLionel Sambuc break;
358*433d6423SLionel Sambuc default: return EIO;
359*433d6423SLionel Sambuc }
360*433d6423SLionel Sambuc pci_outb(reg(MEM_PAGE), page);
361*433d6423SLionel Sambuc pci_outl(reg(dma_add_reg), dma);
362*433d6423SLionel Sambuc
363*433d6423SLionel Sambuc /* device expects long word count in stead of bytes */
364*433d6423SLionel Sambuc length /= 4;
365*433d6423SLionel Sambuc
366*433d6423SLionel Sambuc /* It seems that register _CURRENT_COUNT is overwritten, but this is
367*433d6423SLionel Sambuc * the way to go. The register frame_count_reg is only longword
368*433d6423SLionel Sambuc * addressable.
369*433d6423SLionel Sambuc * It expects length -1
370*433d6423SLionel Sambuc */
371*433d6423SLionel Sambuc pci_outl(reg(frame_count_reg), (u32_t) (length - 1));
372*433d6423SLionel Sambuc
373*433d6423SLionel Sambuc return OK;
374*433d6423SLionel Sambuc }
375*433d6423SLionel Sambuc
376*433d6423SLionel Sambuc
377*433d6423SLionel Sambuc /* return status of the interrupt summary bit */
drv_int_sum(void)378*433d6423SLionel Sambuc int drv_int_sum(void) {
379*433d6423SLionel Sambuc return pci_inl(reg(INTERRUPT_STATUS)) & INTR;
380*433d6423SLionel Sambuc }
381*433d6423SLionel Sambuc
382*433d6423SLionel Sambuc
drv_int(int sub_dev)383*433d6423SLionel Sambuc int drv_int(int sub_dev) {
384*433d6423SLionel Sambuc u32_t int_status;
385*433d6423SLionel Sambuc u32_t bit;
386*433d6423SLionel Sambuc
387*433d6423SLionel Sambuc /* return status of interrupt bit of specified channel*/
388*433d6423SLionel Sambuc switch (sub_dev) {
389*433d6423SLionel Sambuc case DAC1_CHAN: bit = DAC1; break;
390*433d6423SLionel Sambuc case DAC2_CHAN: bit = DAC2; break;
391*433d6423SLionel Sambuc case ADC1_CHAN: bit = ADC; break;
392*433d6423SLionel Sambuc default: return EINVAL;
393*433d6423SLionel Sambuc }
394*433d6423SLionel Sambuc
395*433d6423SLionel Sambuc int_status = pci_inl(reg(INTERRUPT_STATUS)) & bit;
396*433d6423SLionel Sambuc
397*433d6423SLionel Sambuc return int_status;
398*433d6423SLionel Sambuc }
399*433d6423SLionel Sambuc
400*433d6423SLionel Sambuc
drv_reenable_int(int chan)401*433d6423SLionel Sambuc int drv_reenable_int(int chan) {
402*433d6423SLionel Sambuc u16_t ser_interface, int_en_bit;
403*433d6423SLionel Sambuc
404*433d6423SLionel Sambuc switch(chan) {
405*433d6423SLionel Sambuc case ADC1_CHAN: int_en_bit = R1_INT_EN; break;
406*433d6423SLionel Sambuc case DAC1_CHAN: int_en_bit = P1_INTR_EN; break;
407*433d6423SLionel Sambuc case DAC2_CHAN: int_en_bit = P2_INTR_EN; break;
408*433d6423SLionel Sambuc default: return EINVAL;
409*433d6423SLionel Sambuc }
410*433d6423SLionel Sambuc
411*433d6423SLionel Sambuc /* clear and reenable an interrupt */
412*433d6423SLionel Sambuc ser_interface = pci_inw(reg(SERIAL_INTERFACE_CTRL));
413*433d6423SLionel Sambuc pci_outw(reg(SERIAL_INTERFACE_CTRL), ser_interface & ~int_en_bit);
414*433d6423SLionel Sambuc pci_outw(reg(SERIAL_INTERFACE_CTRL), ser_interface | int_en_bit);
415*433d6423SLionel Sambuc
416*433d6423SLionel Sambuc return OK;
417*433d6423SLionel Sambuc }
418*433d6423SLionel Sambuc
419*433d6423SLionel Sambuc
drv_pause(int sub_dev)420*433d6423SLionel Sambuc int drv_pause(int sub_dev) {
421*433d6423SLionel Sambuc u32_t pause_bit;
422*433d6423SLionel Sambuc
423*433d6423SLionel Sambuc disable_int(sub_dev); /* don't send interrupts */
424*433d6423SLionel Sambuc
425*433d6423SLionel Sambuc switch(sub_dev) {
426*433d6423SLionel Sambuc case DAC1_CHAN: pause_bit = P1_PAUSE;break;
427*433d6423SLionel Sambuc case DAC2_CHAN: pause_bit = P2_PAUSE;break;
428*433d6423SLionel Sambuc default: return EINVAL;
429*433d6423SLionel Sambuc }
430*433d6423SLionel Sambuc
431*433d6423SLionel Sambuc /* pause */
432*433d6423SLionel Sambuc pci_outl(reg(SERIAL_INTERFACE_CTRL),
433*433d6423SLionel Sambuc pci_inl(reg(SERIAL_INTERFACE_CTRL)) | pause_bit);
434*433d6423SLionel Sambuc
435*433d6423SLionel Sambuc return OK;
436*433d6423SLionel Sambuc }
437*433d6423SLionel Sambuc
438*433d6423SLionel Sambuc
drv_resume(int sub_dev)439*433d6423SLionel Sambuc int drv_resume(int sub_dev) {
440*433d6423SLionel Sambuc u32_t pause_bit = 0;
441*433d6423SLionel Sambuc
442*433d6423SLionel Sambuc drv_reenable_int(sub_dev); /* enable interrupts */
443*433d6423SLionel Sambuc
444*433d6423SLionel Sambuc switch(sub_dev) {
445*433d6423SLionel Sambuc case DAC1_CHAN: pause_bit = P1_PAUSE;break;
446*433d6423SLionel Sambuc case DAC2_CHAN: pause_bit = P2_PAUSE;break;
447*433d6423SLionel Sambuc default: return EINVAL;
448*433d6423SLionel Sambuc }
449*433d6423SLionel Sambuc
450*433d6423SLionel Sambuc /* clear pause bit */
451*433d6423SLionel Sambuc pci_outl(reg(SERIAL_INTERFACE_CTRL),
452*433d6423SLionel Sambuc pci_inl(reg(SERIAL_INTERFACE_CTRL)) & ~pause_bit);
453*433d6423SLionel Sambuc
454*433d6423SLionel Sambuc return OK;
455*433d6423SLionel Sambuc }
456*433d6423SLionel Sambuc
457*433d6423SLionel Sambuc
set_bits(u32_t nr_of_bits,int sub_dev)458*433d6423SLionel Sambuc static int set_bits(u32_t nr_of_bits, int sub_dev) {
459*433d6423SLionel Sambuc /* set format bits for specified channel. */
460*433d6423SLionel Sambuc u16_t size_16_bit, ser_interface;
461*433d6423SLionel Sambuc
462*433d6423SLionel Sambuc switch(sub_dev) {
463*433d6423SLionel Sambuc case ADC1_CHAN: size_16_bit = R1_S_EB; break;
464*433d6423SLionel Sambuc case DAC1_CHAN: size_16_bit = P1_S_EB; break;
465*433d6423SLionel Sambuc case DAC2_CHAN: size_16_bit = P2_S_EB; break;
466*433d6423SLionel Sambuc default: return EINVAL;
467*433d6423SLionel Sambuc }
468*433d6423SLionel Sambuc
469*433d6423SLionel Sambuc ser_interface = pci_inw(reg(SERIAL_INTERFACE_CTRL));
470*433d6423SLionel Sambuc ser_interface &= ~size_16_bit;
471*433d6423SLionel Sambuc switch(nr_of_bits) {
472*433d6423SLionel Sambuc case 16: ser_interface |= size_16_bit;break;
473*433d6423SLionel Sambuc case 8: break;
474*433d6423SLionel Sambuc default: return EINVAL;
475*433d6423SLionel Sambuc }
476*433d6423SLionel Sambuc pci_outw(reg(SERIAL_INTERFACE_CTRL), ser_interface);
477*433d6423SLionel Sambuc aud_conf[sub_dev].nr_of_bits = nr_of_bits;
478*433d6423SLionel Sambuc return OK;
479*433d6423SLionel Sambuc }
480*433d6423SLionel Sambuc
481*433d6423SLionel Sambuc
set_stereo(u32_t stereo,int sub_dev)482*433d6423SLionel Sambuc static int set_stereo(u32_t stereo, int sub_dev) {
483*433d6423SLionel Sambuc /* set format bits for specified channel. */
484*433d6423SLionel Sambuc u16_t stereo_bit, ser_interface;
485*433d6423SLionel Sambuc
486*433d6423SLionel Sambuc switch(sub_dev) {
487*433d6423SLionel Sambuc case ADC1_CHAN: stereo_bit = R1_S_MB; break;
488*433d6423SLionel Sambuc case DAC1_CHAN: stereo_bit = P1_S_MB; break;
489*433d6423SLionel Sambuc case DAC2_CHAN: stereo_bit = P2_S_MB; break;
490*433d6423SLionel Sambuc default: return EINVAL;
491*433d6423SLionel Sambuc }
492*433d6423SLionel Sambuc ser_interface = pci_inw(reg(SERIAL_INTERFACE_CTRL));
493*433d6423SLionel Sambuc ser_interface &= ~stereo_bit;
494*433d6423SLionel Sambuc if (stereo) {
495*433d6423SLionel Sambuc ser_interface |= stereo_bit;
496*433d6423SLionel Sambuc }
497*433d6423SLionel Sambuc pci_outw(reg(SERIAL_INTERFACE_CTRL), ser_interface);
498*433d6423SLionel Sambuc aud_conf[sub_dev].stereo = stereo;
499*433d6423SLionel Sambuc
500*433d6423SLionel Sambuc return OK;
501*433d6423SLionel Sambuc }
502*433d6423SLionel Sambuc
503*433d6423SLionel Sambuc
set_sign(u32_t UNUSED (val),int UNUSED (sub_dev))504*433d6423SLionel Sambuc static int set_sign(u32_t UNUSED(val), int UNUSED(sub_dev)) {
505*433d6423SLionel Sambuc return OK;
506*433d6423SLionel Sambuc }
507*433d6423SLionel Sambuc
508*433d6423SLionel Sambuc
set_frag_size(u32_t fragment_size,int sub_dev_nr)509*433d6423SLionel Sambuc static int set_frag_size(u32_t fragment_size, int sub_dev_nr) {
510*433d6423SLionel Sambuc if (fragment_size > (sub_dev[sub_dev_nr].DmaSize /
511*433d6423SLionel Sambuc sub_dev[sub_dev_nr].NrOfDmaFragments) ||
512*433d6423SLionel Sambuc fragment_size < sub_dev[sub_dev_nr].MinFragmentSize) {
513*433d6423SLionel Sambuc return EINVAL;
514*433d6423SLionel Sambuc }
515*433d6423SLionel Sambuc aud_conf[sub_dev_nr].fragment_size = fragment_size;
516*433d6423SLionel Sambuc
517*433d6423SLionel Sambuc return OK;
518*433d6423SLionel Sambuc }
519*433d6423SLionel Sambuc
520*433d6423SLionel Sambuc
set_sample_rate(u32_t rate,int sub_dev)521*433d6423SLionel Sambuc static int set_sample_rate(u32_t rate, int sub_dev) {
522*433d6423SLionel Sambuc /* currently only 44.1kHz */
523*433d6423SLionel Sambuc u32_t controlRegister;
524*433d6423SLionel Sambuc
525*433d6423SLionel Sambuc if (rate > MAX_RATE || rate < MIN_RATE) {
526*433d6423SLionel Sambuc return EINVAL;
527*433d6423SLionel Sambuc }
528*433d6423SLionel Sambuc
529*433d6423SLionel Sambuc controlRegister = pci_inl(reg(CHIP_SEL_CTRL));
530*433d6423SLionel Sambuc controlRegister |= FREQ_44K100;
531*433d6423SLionel Sambuc pci_outl(reg(CHIP_SEL_CTRL), controlRegister);
532*433d6423SLionel Sambuc
533*433d6423SLionel Sambuc aud_conf[sub_dev].sample_rate = rate;
534*433d6423SLionel Sambuc
535*433d6423SLionel Sambuc return OK;
536*433d6423SLionel Sambuc }
537*433d6423SLionel Sambuc
538*433d6423SLionel Sambuc
set_int_cnt(int chan)539*433d6423SLionel Sambuc static int set_int_cnt(int chan) {
540*433d6423SLionel Sambuc /* Write interrupt count for specified channel.
541*433d6423SLionel Sambuc After <DspFragmentSize> bytes, an interrupt will be generated */
542*433d6423SLionel Sambuc
543*433d6423SLionel Sambuc int sample_count;
544*433d6423SLionel Sambuc u16_t int_cnt_reg;
545*433d6423SLionel Sambuc
546*433d6423SLionel Sambuc if (aud_conf[chan].fragment_size >
547*433d6423SLionel Sambuc (sub_dev[chan].DmaSize / sub_dev[chan].NrOfDmaFragments)
548*433d6423SLionel Sambuc || aud_conf[chan].fragment_size < sub_dev[chan].MinFragmentSize) {
549*433d6423SLionel Sambuc return EINVAL;
550*433d6423SLionel Sambuc }
551*433d6423SLionel Sambuc
552*433d6423SLionel Sambuc switch(chan) {
553*433d6423SLionel Sambuc case ADC1_CHAN: int_cnt_reg = ADC_SAMP_CT; break;
554*433d6423SLionel Sambuc case DAC1_CHAN: int_cnt_reg = DAC1_SAMP_CT; break;
555*433d6423SLionel Sambuc case DAC2_CHAN: int_cnt_reg = DAC2_SAMP_CT; break;
556*433d6423SLionel Sambuc default: return EINVAL;
557*433d6423SLionel Sambuc }
558*433d6423SLionel Sambuc
559*433d6423SLionel Sambuc sample_count = aud_conf[chan].fragment_size;
560*433d6423SLionel Sambuc
561*433d6423SLionel Sambuc /* adjust sample count according to sample format */
562*433d6423SLionel Sambuc if( aud_conf[chan].stereo == TRUE ) sample_count >>= 1;
563*433d6423SLionel Sambuc switch(aud_conf[chan].nr_of_bits) {
564*433d6423SLionel Sambuc case 16: sample_count >>= 1;break;
565*433d6423SLionel Sambuc case 8: break;
566*433d6423SLionel Sambuc default: return EINVAL;
567*433d6423SLionel Sambuc }
568*433d6423SLionel Sambuc
569*433d6423SLionel Sambuc /* set the sample count - 1 for the specified channel. */
570*433d6423SLionel Sambuc pci_outw(reg(int_cnt_reg), sample_count - 1);
571*433d6423SLionel Sambuc
572*433d6423SLionel Sambuc return OK;
573*433d6423SLionel Sambuc }
574*433d6423SLionel Sambuc
575*433d6423SLionel Sambuc
get_max_frag_size(u32_t * val,int * len,int sub_dev_nr)576*433d6423SLionel Sambuc static int get_max_frag_size(u32_t * val, int * len, int sub_dev_nr) {
577*433d6423SLionel Sambuc *len = sizeof(*val);
578*433d6423SLionel Sambuc *val = (sub_dev[sub_dev_nr].DmaSize /
579*433d6423SLionel Sambuc sub_dev[sub_dev_nr].NrOfDmaFragments);
580*433d6423SLionel Sambuc return OK;
581*433d6423SLionel Sambuc }
582*433d6423SLionel Sambuc
583*433d6423SLionel Sambuc
disable_int(int chan)584*433d6423SLionel Sambuc static int disable_int(int chan) {
585*433d6423SLionel Sambuc u16_t ser_interface, int_en_bit;
586*433d6423SLionel Sambuc
587*433d6423SLionel Sambuc switch(chan) {
588*433d6423SLionel Sambuc case ADC1_CHAN: int_en_bit = R1_INT_EN; break;
589*433d6423SLionel Sambuc case DAC1_CHAN: int_en_bit = P1_INTR_EN; break;
590*433d6423SLionel Sambuc case DAC2_CHAN: int_en_bit = P2_INTR_EN; break;
591*433d6423SLionel Sambuc default: return EINVAL;
592*433d6423SLionel Sambuc }
593*433d6423SLionel Sambuc /* clear the interrupt */
594*433d6423SLionel Sambuc ser_interface = pci_inw(reg(SERIAL_INTERFACE_CTRL));
595*433d6423SLionel Sambuc pci_outw(reg(SERIAL_INTERFACE_CTRL), ser_interface & ~int_en_bit);
596*433d6423SLionel Sambuc return OK;
597*433d6423SLionel Sambuc }
598*433d6423SLionel Sambuc
599*433d6423SLionel Sambuc
get_samples_in_buf(u32_t * samples_in_buf,int * len,int chan)600*433d6423SLionel Sambuc static int get_samples_in_buf (u32_t *samples_in_buf, int *len, int chan) {
601*433d6423SLionel Sambuc u16_t samp_ct_reg;
602*433d6423SLionel Sambuc u16_t curr_samp_ct_reg;
603*433d6423SLionel Sambuc u16_t curr_samp_ct; /* counts back from SAMP_CT till 0 */
604*433d6423SLionel Sambuc
605*433d6423SLionel Sambuc *len = sizeof(*samples_in_buf);
606*433d6423SLionel Sambuc
607*433d6423SLionel Sambuc switch(chan) {
608*433d6423SLionel Sambuc case ADC1_CHAN:
609*433d6423SLionel Sambuc curr_samp_ct_reg = ADC_CURR_SAMP_CT;
610*433d6423SLionel Sambuc samp_ct_reg = ADC_SAMP_CT; break;
611*433d6423SLionel Sambuc case DAC1_CHAN:
612*433d6423SLionel Sambuc curr_samp_ct_reg = DAC1_CURR_SAMP_CT;
613*433d6423SLionel Sambuc samp_ct_reg = DAC1_SAMP_CT; break;
614*433d6423SLionel Sambuc case DAC2_CHAN:
615*433d6423SLionel Sambuc curr_samp_ct_reg = DAC2_CURR_SAMP_CT;
616*433d6423SLionel Sambuc samp_ct_reg = DAC2_SAMP_CT; break;
617*433d6423SLionel Sambuc default: return EINVAL;
618*433d6423SLionel Sambuc }
619*433d6423SLionel Sambuc
620*433d6423SLionel Sambuc /* TODO: is this inw useful? */
621*433d6423SLionel Sambuc (void) pci_inw(reg(samp_ct_reg));
622*433d6423SLionel Sambuc curr_samp_ct = pci_inw(reg(curr_samp_ct_reg));
623*433d6423SLionel Sambuc
624*433d6423SLionel Sambuc *samples_in_buf = (u32_t) (sub_dev[chan].BufLength * 8192) +
625*433d6423SLionel Sambuc curr_samp_ct;
626*433d6423SLionel Sambuc
627*433d6423SLionel Sambuc return OK;
628*433d6423SLionel Sambuc }
629*433d6423SLionel Sambuc
630*433d6423SLionel Sambuc
631*433d6423SLionel Sambuc /* returns 1 if there are free buffers */
free_buf(u32_t * val,int * len,int sub_dev_nr)632*433d6423SLionel Sambuc static int free_buf (u32_t *val, int *len, int sub_dev_nr) {
633*433d6423SLionel Sambuc *len = sizeof(*val);
634*433d6423SLionel Sambuc if (sub_dev[sub_dev_nr].BufLength ==
635*433d6423SLionel Sambuc sub_dev[sub_dev_nr].NrOfExtraBuffers) {
636*433d6423SLionel Sambuc *val = 0;
637*433d6423SLionel Sambuc }
638*433d6423SLionel Sambuc else {
639*433d6423SLionel Sambuc *val = 1;
640*433d6423SLionel Sambuc }
641*433d6423SLionel Sambuc return OK;
642*433d6423SLionel Sambuc }
643*433d6423SLionel Sambuc
644*433d6423SLionel Sambuc
get_set_volume(struct volume_level * level,int * len,int sub_dev,int flag)645*433d6423SLionel Sambuc static int get_set_volume(struct volume_level *level, int *len, int sub_dev,
646*433d6423SLionel Sambuc int flag) {
647*433d6423SLionel Sambuc *len = sizeof(struct volume_level);
648*433d6423SLionel Sambuc if (sub_dev == MIXER) {
649*433d6423SLionel Sambuc return ak4531_get_set_volume(level, flag);
650*433d6423SLionel Sambuc }
651*433d6423SLionel Sambuc else {
652*433d6423SLionel Sambuc return EINVAL;
653*433d6423SLionel Sambuc }
654*433d6423SLionel Sambuc }
655