[ALSA] Merge ad1816a-lib module to ad1816a
[linux-2.6.git] / sound / isa / ad1816a / ad1816a_lib.c
1
2 /*
3     ad1816a.c - lowlevel code for Analog Devices AD1816A chip.
4     Copyright (C) 1999-2000 by Massimo Piccioni <dafastidio@libero.it>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 */
20
21 #include <sound/driver.h>
22 #include <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/slab.h>
26 #include <linux/ioport.h>
27 #include <sound/core.h>
28 #include <sound/ad1816a.h>
29
30 #include <asm/io.h>
31 #include <asm/dma.h>
32
33 static inline int snd_ad1816a_busy_wait(struct snd_ad1816a *chip)
34 {
35         int timeout;
36
37         for (timeout = 1000; timeout-- > 0; udelay(10))
38                 if (inb(AD1816A_REG(AD1816A_CHIP_STATUS)) & AD1816A_READY)
39                         return 0;
40
41         snd_printk("chip busy.\n");
42         return -EBUSY;
43 }
44
45 static inline unsigned char snd_ad1816a_in(struct snd_ad1816a *chip, unsigned char reg)
46 {
47         snd_ad1816a_busy_wait(chip);
48         return inb(AD1816A_REG(reg));
49 }
50
51 static inline void snd_ad1816a_out(struct snd_ad1816a *chip, unsigned char reg,
52                             unsigned char value)
53 {
54         snd_ad1816a_busy_wait(chip);
55         outb(value, AD1816A_REG(reg));
56 }
57
58 static inline void snd_ad1816a_out_mask(struct snd_ad1816a *chip, unsigned char reg,
59                                  unsigned char mask, unsigned char value)
60 {
61         snd_ad1816a_out(chip, reg,
62                 (value & mask) | (snd_ad1816a_in(chip, reg) & ~mask));
63 }
64
65 static unsigned short snd_ad1816a_read(struct snd_ad1816a *chip, unsigned char reg)
66 {
67         snd_ad1816a_out(chip, AD1816A_INDIR_ADDR, reg & 0x3f);
68         return snd_ad1816a_in(chip, AD1816A_INDIR_DATA_LOW) |
69                 (snd_ad1816a_in(chip, AD1816A_INDIR_DATA_HIGH) << 8);
70 }
71
72 static void snd_ad1816a_write(struct snd_ad1816a *chip, unsigned char reg,
73                               unsigned short value)
74 {
75         snd_ad1816a_out(chip, AD1816A_INDIR_ADDR, reg & 0x3f);
76         snd_ad1816a_out(chip, AD1816A_INDIR_DATA_LOW, value & 0xff);
77         snd_ad1816a_out(chip, AD1816A_INDIR_DATA_HIGH, (value >> 8) & 0xff);
78 }
79
80 static void snd_ad1816a_write_mask(struct snd_ad1816a *chip, unsigned char reg,
81                                    unsigned short mask, unsigned short value)
82 {
83         snd_ad1816a_write(chip, reg,
84                 (value & mask) | (snd_ad1816a_read(chip, reg) & ~mask));
85 }
86
87
88 static unsigned char snd_ad1816a_get_format(struct snd_ad1816a *chip,
89                                             unsigned int format, int channels)
90 {
91         unsigned char retval = AD1816A_FMT_LINEAR_8;
92
93         switch (format) {
94         case SNDRV_PCM_FORMAT_MU_LAW:
95                 retval = AD1816A_FMT_ULAW_8;
96                 break;
97         case SNDRV_PCM_FORMAT_A_LAW:
98                 retval = AD1816A_FMT_ALAW_8;
99                 break;
100         case SNDRV_PCM_FORMAT_S16_LE:
101                 retval = AD1816A_FMT_LINEAR_16_LIT;
102                 break;
103         case SNDRV_PCM_FORMAT_S16_BE:
104                 retval = AD1816A_FMT_LINEAR_16_BIG;
105         }
106         return (channels > 1) ? (retval | AD1816A_FMT_STEREO) : retval;
107 }
108
109 static int snd_ad1816a_open(struct snd_ad1816a *chip, unsigned int mode)
110 {
111         unsigned long flags;
112
113         spin_lock_irqsave(&chip->lock, flags);
114
115         if (chip->mode & mode) {
116                 spin_unlock_irqrestore(&chip->lock, flags);
117                 return -EAGAIN;
118         }
119
120         switch ((mode &= AD1816A_MODE_OPEN)) {
121         case AD1816A_MODE_PLAYBACK:
122                 snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
123                         AD1816A_PLAYBACK_IRQ_PENDING, 0x00);
124                 snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
125                         AD1816A_PLAYBACK_IRQ_ENABLE, 0xffff);
126                 break;
127         case AD1816A_MODE_CAPTURE:
128                 snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
129                         AD1816A_CAPTURE_IRQ_PENDING, 0x00);
130                 snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
131                         AD1816A_CAPTURE_IRQ_ENABLE, 0xffff);
132                 break;
133         case AD1816A_MODE_TIMER:
134                 snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
135                         AD1816A_TIMER_IRQ_PENDING, 0x00);
136                 snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
137                         AD1816A_TIMER_IRQ_ENABLE, 0xffff);
138         }
139         chip->mode |= mode;
140
141         spin_unlock_irqrestore(&chip->lock, flags);
142         return 0;
143 }
144
145 static void snd_ad1816a_close(struct snd_ad1816a *chip, unsigned int mode)
146 {
147         unsigned long flags;
148
149         spin_lock_irqsave(&chip->lock, flags);
150
151         switch ((mode &= AD1816A_MODE_OPEN)) {
152         case AD1816A_MODE_PLAYBACK:
153                 snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
154                         AD1816A_PLAYBACK_IRQ_PENDING, 0x00);
155                 snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
156                         AD1816A_PLAYBACK_IRQ_ENABLE, 0x0000);
157                 break;
158         case AD1816A_MODE_CAPTURE:
159                 snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
160                         AD1816A_CAPTURE_IRQ_PENDING, 0x00);
161                 snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
162                         AD1816A_CAPTURE_IRQ_ENABLE, 0x0000);
163                 break;
164         case AD1816A_MODE_TIMER:
165                 snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS,
166                         AD1816A_TIMER_IRQ_PENDING, 0x00);
167                 snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
168                         AD1816A_TIMER_IRQ_ENABLE, 0x0000);
169         }
170         if (!((chip->mode &= ~mode) & AD1816A_MODE_OPEN))
171                 chip->mode = 0;
172
173         spin_unlock_irqrestore(&chip->lock, flags);
174 }
175
176
177 static int snd_ad1816a_trigger(struct snd_ad1816a *chip, unsigned char what,
178                                int channel, int cmd)
179 {
180         int error = 0;
181
182         switch (cmd) {
183         case SNDRV_PCM_TRIGGER_START:
184         case SNDRV_PCM_TRIGGER_STOP:
185                 spin_lock(&chip->lock);
186                 cmd = (cmd == SNDRV_PCM_TRIGGER_START) ? 0xff: 0x00;
187                 if (what & AD1816A_PLAYBACK_ENABLE)
188                         snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
189                                 AD1816A_PLAYBACK_ENABLE, cmd);
190                 if (what & AD1816A_CAPTURE_ENABLE)
191                         snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
192                                 AD1816A_CAPTURE_ENABLE, cmd);
193                 spin_unlock(&chip->lock);
194                 break;
195         default:
196                 snd_printk("invalid trigger mode 0x%x.\n", what);
197                 error = -EINVAL;
198         }
199
200         return error;
201 }
202
203 static int snd_ad1816a_playback_trigger(struct snd_pcm_substream *substream, int cmd)
204 {
205         struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
206         return snd_ad1816a_trigger(chip, AD1816A_PLAYBACK_ENABLE,
207                 SNDRV_PCM_STREAM_PLAYBACK, cmd);
208 }
209
210 static int snd_ad1816a_capture_trigger(struct snd_pcm_substream *substream, int cmd)
211 {
212         struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
213         return snd_ad1816a_trigger(chip, AD1816A_CAPTURE_ENABLE,
214                 SNDRV_PCM_STREAM_CAPTURE, cmd);
215 }
216
217 static int snd_ad1816a_hw_params(struct snd_pcm_substream *substream,
218                                  struct snd_pcm_hw_params *hw_params)
219 {
220         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
221 }
222
223 static int snd_ad1816a_hw_free(struct snd_pcm_substream *substream)
224 {
225         return snd_pcm_lib_free_pages(substream);
226 }
227
228 static int snd_ad1816a_playback_prepare(struct snd_pcm_substream *substream)
229 {
230         struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
231         unsigned long flags;
232         struct snd_pcm_runtime *runtime = substream->runtime;
233         unsigned int size, rate;
234
235         spin_lock_irqsave(&chip->lock, flags);
236
237         chip->p_dma_size = size = snd_pcm_lib_buffer_bytes(substream);
238         snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
239                 AD1816A_PLAYBACK_ENABLE | AD1816A_PLAYBACK_PIO, 0x00);
240
241         snd_dma_program(chip->dma1, runtime->dma_addr, size,
242                         DMA_MODE_WRITE | DMA_AUTOINIT);
243
244         rate = runtime->rate;
245         if (chip->clock_freq)
246                 rate = (rate * 33000) / chip->clock_freq;
247         snd_ad1816a_write(chip, AD1816A_PLAYBACK_SAMPLE_RATE, rate);
248         snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
249                 AD1816A_FMT_ALL | AD1816A_FMT_STEREO,
250                 snd_ad1816a_get_format(chip, runtime->format,
251                         runtime->channels));
252
253         snd_ad1816a_write(chip, AD1816A_PLAYBACK_BASE_COUNT,
254                 snd_pcm_lib_period_bytes(substream) / 4 - 1);
255
256         spin_unlock_irqrestore(&chip->lock, flags);
257         return 0;
258 }
259
260 static int snd_ad1816a_capture_prepare(struct snd_pcm_substream *substream)
261 {
262         struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
263         unsigned long flags;
264         struct snd_pcm_runtime *runtime = substream->runtime;
265         unsigned int size, rate;
266
267         spin_lock_irqsave(&chip->lock, flags);
268
269         chip->c_dma_size = size = snd_pcm_lib_buffer_bytes(substream);
270         snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
271                 AD1816A_CAPTURE_ENABLE | AD1816A_CAPTURE_PIO, 0x00);
272
273         snd_dma_program(chip->dma2, runtime->dma_addr, size,
274                         DMA_MODE_READ | DMA_AUTOINIT);
275
276         rate = runtime->rate;
277         if (chip->clock_freq)
278                 rate = (rate * 33000) / chip->clock_freq;
279         snd_ad1816a_write(chip, AD1816A_CAPTURE_SAMPLE_RATE, rate);
280         snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
281                 AD1816A_FMT_ALL | AD1816A_FMT_STEREO,
282                 snd_ad1816a_get_format(chip, runtime->format,
283                         runtime->channels));
284
285         snd_ad1816a_write(chip, AD1816A_CAPTURE_BASE_COUNT,
286                 snd_pcm_lib_period_bytes(substream) / 4 - 1);
287
288         spin_unlock_irqrestore(&chip->lock, flags);
289         return 0;
290 }
291
292
293 static snd_pcm_uframes_t snd_ad1816a_playback_pointer(struct snd_pcm_substream *substream)
294 {
295         struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
296         size_t ptr;
297         if (!(chip->mode & AD1816A_MODE_PLAYBACK))
298                 return 0;
299         ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
300         return bytes_to_frames(substream->runtime, ptr);
301 }
302
303 static snd_pcm_uframes_t snd_ad1816a_capture_pointer(struct snd_pcm_substream *substream)
304 {
305         struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
306         size_t ptr;
307         if (!(chip->mode & AD1816A_MODE_CAPTURE))
308                 return 0;
309         ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
310         return bytes_to_frames(substream->runtime, ptr);
311 }
312
313
314 static irqreturn_t snd_ad1816a_interrupt(int irq, void *dev_id, struct pt_regs *regs)
315 {
316         struct snd_ad1816a *chip = dev_id;
317         unsigned char status;
318
319         spin_lock(&chip->lock);
320         status = snd_ad1816a_in(chip, AD1816A_INTERRUPT_STATUS);
321         spin_unlock(&chip->lock);
322
323         if ((status & AD1816A_PLAYBACK_IRQ_PENDING) && chip->playback_substream)
324                 snd_pcm_period_elapsed(chip->playback_substream);
325
326         if ((status & AD1816A_CAPTURE_IRQ_PENDING) && chip->capture_substream)
327                 snd_pcm_period_elapsed(chip->capture_substream);
328
329         if ((status & AD1816A_TIMER_IRQ_PENDING) && chip->timer)
330                 snd_timer_interrupt(chip->timer, chip->timer->sticks);
331
332         spin_lock(&chip->lock);
333         snd_ad1816a_out(chip, AD1816A_INTERRUPT_STATUS, 0x00);
334         spin_unlock(&chip->lock);
335         return IRQ_HANDLED;
336 }
337
338
339 static struct snd_pcm_hardware snd_ad1816a_playback = {
340         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
341                                  SNDRV_PCM_INFO_MMAP_VALID),
342         .formats =              (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
343                                  SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
344                                  SNDRV_PCM_FMTBIT_S16_BE),
345         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
346         .rate_min =             4000,
347         .rate_max =             55200,
348         .channels_min =         1,
349         .channels_max =         2,
350         .buffer_bytes_max =     (128*1024),
351         .period_bytes_min =     64,
352         .period_bytes_max =     (128*1024),
353         .periods_min =          1,
354         .periods_max =          1024,
355         .fifo_size =            0,
356 };
357
358 static struct snd_pcm_hardware snd_ad1816a_capture = {
359         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
360                                  SNDRV_PCM_INFO_MMAP_VALID),
361         .formats =              (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
362                                  SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
363                                  SNDRV_PCM_FMTBIT_S16_BE),
364         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
365         .rate_min =             4000,
366         .rate_max =             55200,
367         .channels_min =         1,
368         .channels_max =         2,
369         .buffer_bytes_max =     (128*1024),
370         .period_bytes_min =     64,
371         .period_bytes_max =     (128*1024),
372         .periods_min =          1,
373         .periods_max =          1024,
374         .fifo_size =            0,
375 };
376
377 #if 0 /* not used now */
378 static int snd_ad1816a_timer_close(struct snd_timer *timer)
379 {
380         struct snd_ad1816a *chip = snd_timer_chip(timer);
381         snd_ad1816a_close(chip, AD1816A_MODE_TIMER);
382         return 0;
383 }
384
385 static int snd_ad1816a_timer_open(struct snd_timer *timer)
386 {
387         struct snd_ad1816a *chip = snd_timer_chip(timer);
388         snd_ad1816a_open(chip, AD1816A_MODE_TIMER);
389         return 0;
390 }
391
392 static unsigned long snd_ad1816a_timer_resolution(struct snd_timer *timer)
393 {
394         snd_assert(timer != NULL, return 0);
395
396         return 10000;
397 }
398
399 static int snd_ad1816a_timer_start(struct snd_timer *timer)
400 {
401         unsigned short bits;
402         unsigned long flags;
403         struct snd_ad1816a *chip = snd_timer_chip(timer);
404         spin_lock_irqsave(&chip->lock, flags);
405         bits = snd_ad1816a_read(chip, AD1816A_INTERRUPT_ENABLE);
406
407         if (!(bits & AD1816A_TIMER_ENABLE)) {
408                 snd_ad1816a_write(chip, AD1816A_TIMER_BASE_COUNT,
409                         timer->sticks & 0xffff);
410
411                 snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
412                         AD1816A_TIMER_ENABLE, 0xffff);
413         }
414         spin_unlock_irqrestore(&chip->lock, flags);
415         return 0;
416 }
417
418 static int snd_ad1816a_timer_stop(struct snd_timer *timer)
419 {
420         unsigned long flags;
421         struct snd_ad1816a *chip = snd_timer_chip(timer);
422         spin_lock_irqsave(&chip->lock, flags);
423
424         snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
425                 AD1816A_TIMER_ENABLE, 0x0000);
426
427         spin_unlock_irqrestore(&chip->lock, flags);
428         return 0;
429 }
430
431 static struct snd_timer_hardware snd_ad1816a_timer_table = {
432         .flags =        SNDRV_TIMER_HW_AUTO,
433         .resolution =   10000,
434         .ticks =        65535,
435         .open =         snd_ad1816a_timer_open,
436         .close =        snd_ad1816a_timer_close,
437         .c_resolution = snd_ad1816a_timer_resolution,
438         .start =        snd_ad1816a_timer_start,
439         .stop =         snd_ad1816a_timer_stop,
440 };
441 #endif /* not used now */
442
443
444 static int snd_ad1816a_playback_open(struct snd_pcm_substream *substream)
445 {
446         struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
447         struct snd_pcm_runtime *runtime = substream->runtime;
448         int error;
449
450         if ((error = snd_ad1816a_open(chip, AD1816A_MODE_PLAYBACK)) < 0)
451                 return error;
452         snd_pcm_set_sync(substream);
453         runtime->hw = snd_ad1816a_playback;
454         snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
455         snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
456         chip->playback_substream = substream;
457         return 0;
458 }
459
460 static int snd_ad1816a_capture_open(struct snd_pcm_substream *substream)
461 {
462         struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
463         struct snd_pcm_runtime *runtime = substream->runtime;
464         int error;
465
466         if ((error = snd_ad1816a_open(chip, AD1816A_MODE_CAPTURE)) < 0)
467                 return error;
468         snd_pcm_set_sync(substream);
469         runtime->hw = snd_ad1816a_capture;
470         snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
471         snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
472         chip->capture_substream = substream;
473         return 0;
474 }
475
476 static int snd_ad1816a_playback_close(struct snd_pcm_substream *substream)
477 {
478         struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
479
480         chip->playback_substream = NULL;
481         snd_ad1816a_close(chip, AD1816A_MODE_PLAYBACK);
482         return 0;
483 }
484
485 static int snd_ad1816a_capture_close(struct snd_pcm_substream *substream)
486 {
487         struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
488
489         chip->capture_substream = NULL;
490         snd_ad1816a_close(chip, AD1816A_MODE_CAPTURE);
491         return 0;
492 }
493
494
495 static void __devinit snd_ad1816a_init(struct snd_ad1816a *chip)
496 {
497         unsigned long flags;
498
499         spin_lock_irqsave(&chip->lock, flags);
500
501         snd_ad1816a_out(chip, AD1816A_INTERRUPT_STATUS, 0x00);
502         snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
503                 AD1816A_PLAYBACK_ENABLE | AD1816A_PLAYBACK_PIO, 0x00);
504         snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
505                 AD1816A_CAPTURE_ENABLE | AD1816A_CAPTURE_PIO, 0x00);
506         snd_ad1816a_write(chip, AD1816A_INTERRUPT_ENABLE, 0x0000);
507         snd_ad1816a_write_mask(chip, AD1816A_CHIP_CONFIG,
508                 AD1816A_CAPTURE_NOT_EQUAL | AD1816A_WSS_ENABLE, 0xffff);
509         snd_ad1816a_write(chip, AD1816A_DSP_CONFIG, 0x0000);
510         snd_ad1816a_write(chip, AD1816A_POWERDOWN_CTRL, 0x0000);
511
512         spin_unlock_irqrestore(&chip->lock, flags);
513 }
514
515 static int __devinit snd_ad1816a_probe(struct snd_ad1816a *chip)
516 {
517         unsigned long flags;
518
519         spin_lock_irqsave(&chip->lock, flags);
520
521         switch (chip->version = snd_ad1816a_read(chip, AD1816A_VERSION_ID)) {
522         case 0:
523                 chip->hardware = AD1816A_HW_AD1815;
524                 break;
525         case 1:
526                 chip->hardware = AD1816A_HW_AD18MAX10;
527                 break;
528         case 3:
529                 chip->hardware = AD1816A_HW_AD1816A;
530                 break;
531         default:
532                 chip->hardware = AD1816A_HW_AUTO;
533         }
534
535         spin_unlock_irqrestore(&chip->lock, flags);
536         return 0;
537 }
538
539 static int snd_ad1816a_free(struct snd_ad1816a *chip)
540 {
541         release_and_free_resource(chip->res_port);
542         if (chip->irq >= 0)
543                 free_irq(chip->irq, (void *) chip);
544         if (chip->dma1 >= 0) {
545                 snd_dma_disable(chip->dma1);
546                 free_dma(chip->dma1);
547         }
548         if (chip->dma2 >= 0) {
549                 snd_dma_disable(chip->dma2);
550                 free_dma(chip->dma2);
551         }
552         kfree(chip);
553         return 0;
554 }
555
556 static int snd_ad1816a_dev_free(struct snd_device *device)
557 {
558         struct snd_ad1816a *chip = device->device_data;
559         return snd_ad1816a_free(chip);
560 }
561
562 static const char __devinit *snd_ad1816a_chip_id(struct snd_ad1816a *chip)
563 {
564         switch (chip->hardware) {
565         case AD1816A_HW_AD1816A: return "AD1816A";
566         case AD1816A_HW_AD1815: return "AD1815";
567         case AD1816A_HW_AD18MAX10: return "AD18max10";
568         default:
569                 snd_printk("Unknown chip version %d:%d.\n",
570                         chip->version, chip->hardware);
571                 return "AD1816A - unknown";
572         }
573 }
574
575 int __devinit snd_ad1816a_create(struct snd_card *card,
576                                  unsigned long port, int irq, int dma1, int dma2,
577                                  struct snd_ad1816a **rchip)
578 {
579         static struct snd_device_ops ops = {
580                 .dev_free =     snd_ad1816a_dev_free,
581         };
582         int error;
583         struct snd_ad1816a *chip;
584
585         *rchip = NULL;
586
587         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
588         if (chip == NULL)
589                 return -ENOMEM;
590         chip->irq = -1;
591         chip->dma1 = -1;
592         chip->dma2 = -1;
593
594         if ((chip->res_port = request_region(port, 16, "AD1816A")) == NULL) {
595                 snd_printk(KERN_ERR "ad1816a: can't grab port 0x%lx\n", port);
596                 snd_ad1816a_free(chip);
597                 return -EBUSY;
598         }
599         if (request_irq(irq, snd_ad1816a_interrupt, SA_INTERRUPT, "AD1816A", (void *) chip)) {
600                 snd_printk(KERN_ERR "ad1816a: can't grab IRQ %d\n", irq);
601                 snd_ad1816a_free(chip);
602                 return -EBUSY;
603         }
604         chip->irq = irq;
605         if (request_dma(dma1, "AD1816A - 1")) {
606                 snd_printk(KERN_ERR "ad1816a: can't grab DMA1 %d\n", dma1);
607                 snd_ad1816a_free(chip);
608                 return -EBUSY;
609         }
610         chip->dma1 = dma1;
611         if (request_dma(dma2, "AD1816A - 2")) {
612                 snd_printk(KERN_ERR "ad1816a: can't grab DMA2 %d\n", dma2);
613                 snd_ad1816a_free(chip);
614                 return -EBUSY;
615         }
616         chip->dma2 = dma2;
617
618         chip->card = card;
619         chip->port = port;
620         spin_lock_init(&chip->lock);
621
622         if ((error = snd_ad1816a_probe(chip))) {
623                 snd_ad1816a_free(chip);
624                 return error;
625         }
626
627         snd_ad1816a_init(chip);
628
629         /* Register device */
630         if ((error = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
631                 snd_ad1816a_free(chip);
632                 return error;
633         }
634
635         *rchip = chip;
636         return 0;
637 }
638
639 static struct snd_pcm_ops snd_ad1816a_playback_ops = {
640         .open =         snd_ad1816a_playback_open,
641         .close =        snd_ad1816a_playback_close,
642         .ioctl =        snd_pcm_lib_ioctl,
643         .hw_params =    snd_ad1816a_hw_params,
644         .hw_free =      snd_ad1816a_hw_free,
645         .prepare =      snd_ad1816a_playback_prepare,
646         .trigger =      snd_ad1816a_playback_trigger,
647         .pointer =      snd_ad1816a_playback_pointer,
648 };
649
650 static struct snd_pcm_ops snd_ad1816a_capture_ops = {
651         .open =         snd_ad1816a_capture_open,
652         .close =        snd_ad1816a_capture_close,
653         .ioctl =        snd_pcm_lib_ioctl,
654         .hw_params =    snd_ad1816a_hw_params,
655         .hw_free =      snd_ad1816a_hw_free,
656         .prepare =      snd_ad1816a_capture_prepare,
657         .trigger =      snd_ad1816a_capture_trigger,
658         .pointer =      snd_ad1816a_capture_pointer,
659 };
660
661 int __devinit snd_ad1816a_pcm(struct snd_ad1816a *chip, int device, struct snd_pcm **rpcm)
662 {
663         int error;
664         struct snd_pcm *pcm;
665
666         if ((error = snd_pcm_new(chip->card, "AD1816A", device, 1, 1, &pcm)))
667                 return error;
668
669         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ad1816a_playback_ops);
670         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ad1816a_capture_ops);
671
672         pcm->private_data = chip;
673         pcm->info_flags = (chip->dma1 == chip->dma2 ) ? SNDRV_PCM_INFO_JOINT_DUPLEX : 0;
674
675         strcpy(pcm->name, snd_ad1816a_chip_id(chip));
676         snd_ad1816a_init(chip);
677
678         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
679                                               snd_dma_isa_data(),
680                                               64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
681
682         chip->pcm = pcm;
683         if (rpcm)
684                 *rpcm = pcm;
685         return 0;
686 }
687
688 #if 0 /* not used now */
689 int __devinit snd_ad1816a_timer(struct snd_ad1816a *chip, int device, struct snd_timer **rtimer)
690 {
691         struct snd_timer *timer;
692         struct snd_timer_id tid;
693         int error;
694
695         tid.dev_class = SNDRV_TIMER_CLASS_CARD;
696         tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
697         tid.card = chip->card->number;
698         tid.device = device;
699         tid.subdevice = 0;
700         if ((error = snd_timer_new(chip->card, "AD1816A", &tid, &timer)) < 0)
701                 return error;
702         strcpy(timer->name, snd_ad1816a_chip_id(chip));
703         timer->private_data = chip;
704         chip->timer = timer;
705         timer->hw = snd_ad1816a_timer_table;
706         if (rtimer)
707                 *rtimer = timer;
708         return 0;
709 }
710 #endif /* not used now */
711
712 /*
713  *
714  */
715
716 static int snd_ad1816a_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
717 {
718         static char *texts[8] = {
719                 "Line", "Mix", "CD", "Synth", "Video",
720                 "Mic", "Phone",
721         };
722
723         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
724         uinfo->count = 2;
725         uinfo->value.enumerated.items = 7;
726         if (uinfo->value.enumerated.item > 6)
727                 uinfo->value.enumerated.item = 6;
728         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
729         return 0;
730 }
731
732 static int snd_ad1816a_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
733 {
734         struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
735         unsigned long flags;
736         unsigned short val;
737         
738         spin_lock_irqsave(&chip->lock, flags);
739         val = snd_ad1816a_read(chip, AD1816A_ADC_SOURCE_SEL);
740         spin_unlock_irqrestore(&chip->lock, flags);
741         ucontrol->value.enumerated.item[0] = (val >> 12) & 7;
742         ucontrol->value.enumerated.item[1] = (val >> 4) & 7;
743         return 0;
744 }
745
746 static int snd_ad1816a_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
747 {
748         struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
749         unsigned long flags;
750         unsigned short val;
751         int change;
752         
753         if (ucontrol->value.enumerated.item[0] > 6 ||
754             ucontrol->value.enumerated.item[1] > 6)
755                 return -EINVAL;
756         val = (ucontrol->value.enumerated.item[0] << 12) |
757               (ucontrol->value.enumerated.item[1] << 4);
758         spin_lock_irqsave(&chip->lock, flags);
759         change = snd_ad1816a_read(chip, AD1816A_ADC_SOURCE_SEL) != val;
760         snd_ad1816a_write(chip, AD1816A_ADC_SOURCE_SEL, val);
761         spin_unlock_irqrestore(&chip->lock, flags);
762         return change;
763 }
764
765 #define AD1816A_SINGLE(xname, reg, shift, mask, invert) \
766 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ad1816a_info_single, \
767   .get = snd_ad1816a_get_single, .put = snd_ad1816a_put_single, \
768   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
769
770 static int snd_ad1816a_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
771 {
772         int mask = (kcontrol->private_value >> 16) & 0xff;
773
774         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
775         uinfo->count = 1;
776         uinfo->value.integer.min = 0;
777         uinfo->value.integer.max = mask;
778         return 0;
779 }
780
781 static int snd_ad1816a_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
782 {
783         struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
784         unsigned long flags;
785         int reg = kcontrol->private_value & 0xff;
786         int shift = (kcontrol->private_value >> 8) & 0xff;
787         int mask = (kcontrol->private_value >> 16) & 0xff;
788         int invert = (kcontrol->private_value >> 24) & 0xff;
789         
790         spin_lock_irqsave(&chip->lock, flags);
791         ucontrol->value.integer.value[0] = (snd_ad1816a_read(chip, reg) >> shift) & mask;
792         spin_unlock_irqrestore(&chip->lock, flags);
793         if (invert)
794                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
795         return 0;
796 }
797
798 static int snd_ad1816a_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
799 {
800         struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
801         unsigned long flags;
802         int reg = kcontrol->private_value & 0xff;
803         int shift = (kcontrol->private_value >> 8) & 0xff;
804         int mask = (kcontrol->private_value >> 16) & 0xff;
805         int invert = (kcontrol->private_value >> 24) & 0xff;
806         int change;
807         unsigned short old_val, val;
808         
809         val = (ucontrol->value.integer.value[0] & mask);
810         if (invert)
811                 val = mask - val;
812         val <<= shift;
813         spin_lock_irqsave(&chip->lock, flags);
814         old_val = snd_ad1816a_read(chip, reg);
815         val = (old_val & ~(mask << shift)) | val;
816         change = val != old_val;
817         snd_ad1816a_write(chip, reg, val);
818         spin_unlock_irqrestore(&chip->lock, flags);
819         return change;
820 }
821
822 #define AD1816A_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
823 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ad1816a_info_double, \
824   .get = snd_ad1816a_get_double, .put = snd_ad1816a_put_double, \
825   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
826
827 static int snd_ad1816a_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
828 {
829         int mask = (kcontrol->private_value >> 16) & 0xff;
830
831         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
832         uinfo->count = 2;
833         uinfo->value.integer.min = 0;
834         uinfo->value.integer.max = mask;
835         return 0;
836 }
837
838 static int snd_ad1816a_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
839 {
840         struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
841         unsigned long flags;
842         int reg = kcontrol->private_value & 0xff;
843         int shift_left = (kcontrol->private_value >> 8) & 0x0f;
844         int shift_right = (kcontrol->private_value >> 12) & 0x0f;
845         int mask = (kcontrol->private_value >> 16) & 0xff;
846         int invert = (kcontrol->private_value >> 24) & 0xff;
847         unsigned short val;
848         
849         spin_lock_irqsave(&chip->lock, flags);
850         val = snd_ad1816a_read(chip, reg);
851         ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
852         ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
853         spin_unlock_irqrestore(&chip->lock, flags);
854         if (invert) {
855                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
856                 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
857         }
858         return 0;
859 }
860
861 static int snd_ad1816a_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
862 {
863         struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
864         unsigned long flags;
865         int reg = kcontrol->private_value & 0xff;
866         int shift_left = (kcontrol->private_value >> 8) & 0x0f;
867         int shift_right = (kcontrol->private_value >> 12) & 0x0f;
868         int mask = (kcontrol->private_value >> 16) & 0xff;
869         int invert = (kcontrol->private_value >> 24) & 0xff;
870         int change;
871         unsigned short old_val, val1, val2;
872         
873         val1 = ucontrol->value.integer.value[0] & mask;
874         val2 = ucontrol->value.integer.value[1] & mask;
875         if (invert) {
876                 val1 = mask - val1;
877                 val2 = mask - val2;
878         }
879         val1 <<= shift_left;
880         val2 <<= shift_right;
881         spin_lock_irqsave(&chip->lock, flags);
882         old_val = snd_ad1816a_read(chip, reg);
883         val1 = (old_val & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
884         change = val1 != old_val;
885         snd_ad1816a_write(chip, reg, val1);
886         spin_unlock_irqrestore(&chip->lock, flags);
887         return change;
888 }
889
890 static struct snd_kcontrol_new snd_ad1816a_controls[] __devinitdata = {
891 AD1816A_DOUBLE("Master Playback Switch", AD1816A_MASTER_ATT, 15, 7, 1, 1),
892 AD1816A_DOUBLE("Master Playback Volume", AD1816A_MASTER_ATT, 8, 0, 31, 1),
893 AD1816A_DOUBLE("PCM Playback Switch", AD1816A_VOICE_ATT, 15, 7, 1, 1),
894 AD1816A_DOUBLE("PCM Playback Volume", AD1816A_VOICE_ATT, 8, 0, 63, 1),
895 AD1816A_DOUBLE("Line Playback Switch", AD1816A_LINE_GAIN_ATT, 15, 7, 1, 1),
896 AD1816A_DOUBLE("Line Playback Volume", AD1816A_LINE_GAIN_ATT, 8, 0, 31, 1),
897 AD1816A_DOUBLE("CD Playback Switch", AD1816A_CD_GAIN_ATT, 15, 7, 1, 1),
898 AD1816A_DOUBLE("CD Playback Volume", AD1816A_CD_GAIN_ATT, 8, 0, 31, 1),
899 AD1816A_DOUBLE("Synth Playback Switch", AD1816A_SYNTH_GAIN_ATT, 15, 7, 1, 1),
900 AD1816A_DOUBLE("Synth Playback Volume", AD1816A_SYNTH_GAIN_ATT, 8, 0, 31, 1),
901 AD1816A_DOUBLE("FM Playback Switch", AD1816A_FM_ATT, 15, 7, 1, 1),
902 AD1816A_DOUBLE("FM Playback Volume", AD1816A_FM_ATT, 8, 0, 63, 1),
903 AD1816A_SINGLE("Mic Playback Switch", AD1816A_MIC_GAIN_ATT, 15, 1, 1),
904 AD1816A_SINGLE("Mic Playback Volume", AD1816A_MIC_GAIN_ATT, 8, 31, 1),
905 AD1816A_SINGLE("Mic Boost", AD1816A_MIC_GAIN_ATT, 14, 1, 0),
906 AD1816A_DOUBLE("Video Playback Switch", AD1816A_VID_GAIN_ATT, 15, 7, 1, 1),
907 AD1816A_DOUBLE("Video Playback Volume", AD1816A_VID_GAIN_ATT, 8, 0, 31, 1),
908 AD1816A_SINGLE("Phone Capture Switch", AD1816A_PHONE_IN_GAIN_ATT, 15, 1, 1),
909 AD1816A_SINGLE("Phone Capture Volume", AD1816A_PHONE_IN_GAIN_ATT, 0, 15, 1),
910 AD1816A_SINGLE("Phone Playback Switch", AD1816A_PHONE_OUT_ATT, 7, 1, 1),
911 AD1816A_SINGLE("Phone Playback Volume", AD1816A_PHONE_OUT_ATT, 0, 31, 1),
912 {
913         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
914         .name = "Capture Source",
915         .info = snd_ad1816a_info_mux,
916         .get = snd_ad1816a_get_mux,
917         .put = snd_ad1816a_put_mux,
918 },
919 AD1816A_DOUBLE("Capture Switch", AD1816A_ADC_PGA, 15, 7, 1, 1),
920 AD1816A_DOUBLE("Capture Volume", AD1816A_ADC_PGA, 8, 0, 15, 0),
921 AD1816A_SINGLE("3D Control - Switch", AD1816A_3D_PHAT_CTRL, 15, 1, 1),
922 AD1816A_SINGLE("3D Control - Level", AD1816A_3D_PHAT_CTRL, 0, 15, 0),
923 };
924                                         
925 int __devinit snd_ad1816a_mixer(struct snd_ad1816a *chip)
926 {
927         struct snd_card *card;
928         unsigned int idx;
929         int err;
930
931         snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);
932
933         card = chip->card;
934
935         strcpy(card->mixername, snd_ad1816a_chip_id(chip));
936
937         for (idx = 0; idx < ARRAY_SIZE(snd_ad1816a_controls); idx++) {
938                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ad1816a_controls[idx], chip))) < 0)
939                         return err;
940         }
941         return 0;
942 }