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