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