[ALSA] Remove superfluous pcm_free callbacks
[safe/jmp/linux-2.6] / sound / isa / ad1848 / ad1848_lib.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *  Routines for control of AD1848/AD1847/CS4248
4  *
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
22 #define SNDRV_MAIN_OBJECT_FILE
23 #include <sound/driver.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/pm.h>
28 #include <linux/slab.h>
29 #include <linux/ioport.h>
30 #include <sound/core.h>
31 #include <sound/ad1848.h>
32 #include <sound/control.h>
33 #include <sound/pcm_params.h>
34
35 #include <asm/io.h>
36 #include <asm/dma.h>
37
38 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
39 MODULE_DESCRIPTION("Routines for control of AD1848/AD1847/CS4248");
40 MODULE_LICENSE("GPL");
41
42 #if 0
43 #define SNDRV_DEBUG_MCE
44 #endif
45
46 /*
47  *  Some variables
48  */
49
50 static unsigned char freq_bits[14] = {
51         /* 5510 */      0x00 | AD1848_XTAL2,
52         /* 6620 */      0x0E | AD1848_XTAL2,
53         /* 8000 */      0x00 | AD1848_XTAL1,
54         /* 9600 */      0x0E | AD1848_XTAL1,
55         /* 11025 */     0x02 | AD1848_XTAL2,
56         /* 16000 */     0x02 | AD1848_XTAL1,
57         /* 18900 */     0x04 | AD1848_XTAL2,
58         /* 22050 */     0x06 | AD1848_XTAL2,
59         /* 27042 */     0x04 | AD1848_XTAL1,
60         /* 32000 */     0x06 | AD1848_XTAL1,
61         /* 33075 */     0x0C | AD1848_XTAL2,
62         /* 37800 */     0x08 | AD1848_XTAL2,
63         /* 44100 */     0x0A | AD1848_XTAL2,
64         /* 48000 */     0x0C | AD1848_XTAL1
65 };
66
67 static unsigned int rates[14] = {
68         5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
69         27042, 32000, 33075, 37800, 44100, 48000
70 };
71
72 static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
73         .count = 14,
74         .list = rates,
75         .mask = 0,
76 };
77
78 static unsigned char snd_ad1848_original_image[16] =
79 {
80         0x00,                   /* 00 - lic */
81         0x00,                   /* 01 - ric */
82         0x9f,                   /* 02 - la1ic */
83         0x9f,                   /* 03 - ra1ic */
84         0x9f,                   /* 04 - la2ic */
85         0x9f,                   /* 05 - ra2ic */
86         0xbf,                   /* 06 - loc */
87         0xbf,                   /* 07 - roc */
88         0x20,                   /* 08 - dfr */
89         AD1848_AUTOCALIB,       /* 09 - ic */
90         0x00,                   /* 0a - pc */
91         0x00,                   /* 0b - ti */
92         0x00,                   /* 0c - mi */
93         0x00,                   /* 0d - lbc */
94         0x00,                   /* 0e - dru */
95         0x00,                   /* 0f - drl */
96 };
97
98 /*
99  *  Basic I/O functions
100  */
101
102 void snd_ad1848_out(ad1848_t *chip,
103                            unsigned char reg,
104                            unsigned char value)
105 {
106         int timeout;
107
108         for (timeout = 250; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--)
109                 udelay(100);
110 #ifdef CONFIG_SND_DEBUG
111         if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT)
112                 snd_printk(KERN_WARNING "auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
113 #endif
114         outb(chip->mce_bit | reg, AD1848P(chip, REGSEL));
115         outb(chip->image[reg] = value, AD1848P(chip, REG));
116         mb();
117 #if 0
118         printk("codec out - reg 0x%x = 0x%x\n", chip->mce_bit | reg, value);
119 #endif
120 }
121
122 static void snd_ad1848_dout(ad1848_t *chip,
123                             unsigned char reg, unsigned char value)
124 {
125         int timeout;
126
127         for (timeout = 250; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--)
128                 udelay(100);
129         outb(chip->mce_bit | reg, AD1848P(chip, REGSEL));
130         outb(value, AD1848P(chip, REG));
131         mb();
132 }
133
134 static unsigned char snd_ad1848_in(ad1848_t *chip, unsigned char reg)
135 {
136         int timeout;
137
138         for (timeout = 250; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--)
139                 udelay(100);
140 #ifdef CONFIG_SND_DEBUG
141         if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT)
142                 snd_printk(KERN_WARNING "auto calibration time out - reg = 0x%x\n", reg);
143 #endif
144         outb(chip->mce_bit | reg, AD1848P(chip, REGSEL));
145         mb();
146         return inb(AD1848P(chip, REG));
147 }
148
149 #if 0
150
151 static void snd_ad1848_debug(ad1848_t *chip)
152 {
153         printk("AD1848 REGS:      INDEX = 0x%02x  ", inb(AD1848P(chip, REGSEL)));
154         printk("                 STATUS = 0x%02x\n", inb(AD1848P(chip, STATUS)));
155         printk("  0x00: left input      = 0x%02x  ", snd_ad1848_in(chip, 0x00));
156         printk("  0x08: playback format = 0x%02x\n", snd_ad1848_in(chip, 0x08));
157         printk("  0x01: right input     = 0x%02x  ", snd_ad1848_in(chip, 0x01));
158         printk("  0x09: iface (CFIG 1)  = 0x%02x\n", snd_ad1848_in(chip, 0x09));
159         printk("  0x02: AUXA left       = 0x%02x  ", snd_ad1848_in(chip, 0x02));
160         printk("  0x0a: pin control     = 0x%02x\n", snd_ad1848_in(chip, 0x0a));
161         printk("  0x03: AUXA right      = 0x%02x  ", snd_ad1848_in(chip, 0x03));
162         printk("  0x0b: init & status   = 0x%02x\n", snd_ad1848_in(chip, 0x0b));
163         printk("  0x04: AUXB left       = 0x%02x  ", snd_ad1848_in(chip, 0x04));
164         printk("  0x0c: revision & mode = 0x%02x\n", snd_ad1848_in(chip, 0x0c));
165         printk("  0x05: AUXB right      = 0x%02x  ", snd_ad1848_in(chip, 0x05));
166         printk("  0x0d: loopback        = 0x%02x\n", snd_ad1848_in(chip, 0x0d));
167         printk("  0x06: left output     = 0x%02x  ", snd_ad1848_in(chip, 0x06));
168         printk("  0x0e: data upr count  = 0x%02x\n", snd_ad1848_in(chip, 0x0e));
169         printk("  0x07: right output    = 0x%02x  ", snd_ad1848_in(chip, 0x07));
170         printk("  0x0f: data lwr count  = 0x%02x\n", snd_ad1848_in(chip, 0x0f));
171 }
172
173 #endif
174
175 /*
176  *  AD1848 detection / MCE routines
177  */
178
179 static void snd_ad1848_mce_up(ad1848_t *chip)
180 {
181         unsigned long flags;
182         int timeout;
183
184         for (timeout = 250; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--)
185                 udelay(100);
186 #ifdef CONFIG_SND_DEBUG
187         if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT)
188                 snd_printk(KERN_WARNING "mce_up - auto calibration time out (0)\n");
189 #endif
190         spin_lock_irqsave(&chip->reg_lock, flags);
191         chip->mce_bit |= AD1848_MCE;
192         timeout = inb(AD1848P(chip, REGSEL));
193         if (timeout == 0x80)
194                 snd_printk(KERN_WARNING "mce_up [0x%lx]: serious init problem - codec still busy\n", chip->port);
195         if (!(timeout & AD1848_MCE))
196                 outb(chip->mce_bit | (timeout & 0x1f), AD1848P(chip, REGSEL));
197         spin_unlock_irqrestore(&chip->reg_lock, flags);
198 }
199
200 static void snd_ad1848_mce_down(ad1848_t *chip)
201 {
202         unsigned long flags;
203         int timeout;
204         signed long time;
205
206         spin_lock_irqsave(&chip->reg_lock, flags);
207         for (timeout = 5; timeout > 0; timeout--)
208                 inb(AD1848P(chip, REGSEL));
209         /* end of cleanup sequence */
210         for (timeout = 12000; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--)
211                 udelay(100);
212 #if 0
213         printk("(1) timeout = %i\n", timeout);
214 #endif
215 #ifdef CONFIG_SND_DEBUG
216         if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT)
217                 snd_printk(KERN_WARNING "mce_down [0x%lx] - auto calibration time out (0)\n", AD1848P(chip, REGSEL));
218 #endif
219         chip->mce_bit &= ~AD1848_MCE;
220         timeout = inb(AD1848P(chip, REGSEL));
221         outb(chip->mce_bit | (timeout & 0x1f), AD1848P(chip, REGSEL));
222         if (timeout == 0x80)
223                 snd_printk(KERN_WARNING "mce_down [0x%lx]: serious init problem - codec still busy\n", chip->port);
224         if ((timeout & AD1848_MCE) == 0) {
225                 spin_unlock_irqrestore(&chip->reg_lock, flags);
226                 return;
227         }
228         /* calibration process */
229
230         for (timeout = 500; timeout > 0 && (snd_ad1848_in(chip, AD1848_TEST_INIT) & AD1848_CALIB_IN_PROGRESS) == 0; timeout--);
231         if ((snd_ad1848_in(chip, AD1848_TEST_INIT) & AD1848_CALIB_IN_PROGRESS) == 0) {
232                 snd_printd("mce_down - auto calibration time out (1)\n");
233                 spin_unlock_irqrestore(&chip->reg_lock, flags);
234                 return;
235         }
236 #if 0
237         printk("(2) timeout = %i, jiffies = %li\n", timeout, jiffies);
238 #endif
239         time = HZ / 4;
240         while (snd_ad1848_in(chip, AD1848_TEST_INIT) & AD1848_CALIB_IN_PROGRESS) {
241                 spin_unlock_irqrestore(&chip->reg_lock, flags);
242                 if (time <= 0) {
243                         snd_printk(KERN_ERR "mce_down - auto calibration time out (2)\n");
244                         return;
245                 }
246                 time = schedule_timeout_interruptible(time);
247                 spin_lock_irqsave(&chip->reg_lock, flags);
248         }
249 #if 0
250         printk("(3) jiffies = %li\n", jiffies);
251 #endif
252         time = HZ / 10;
253         while (inb(AD1848P(chip, REGSEL)) & AD1848_INIT) {
254                 spin_unlock_irqrestore(&chip->reg_lock, flags);
255                 if (time <= 0) {
256                         snd_printk(KERN_ERR "mce_down - auto calibration time out (3)\n");
257                         return;
258                 }
259                 time = schedule_timeout_interruptible(time);
260                 spin_lock_irqsave(&chip->reg_lock, flags);
261         }
262         spin_unlock_irqrestore(&chip->reg_lock, flags);
263 #if 0
264         printk("(4) jiffies = %li\n", jiffies);
265         snd_printk("mce_down - exit = 0x%x\n", inb(AD1848P(chip, REGSEL)));
266 #endif
267 }
268
269 static unsigned int snd_ad1848_get_count(unsigned char format,
270                                          unsigned int size)
271 {
272         switch (format & 0xe0) {
273         case AD1848_LINEAR_16:
274                 size >>= 1;
275                 break;
276         }
277         if (format & AD1848_STEREO)
278                 size >>= 1;
279         return size;
280 }
281
282 static int snd_ad1848_trigger(ad1848_t *chip, unsigned char what,
283                               int channel, int cmd)
284 {
285         int result = 0;
286
287 #if 0
288         printk("codec trigger!!! - what = %i, enable = %i, status = 0x%x\n", what, enable, inb(AD1848P(card, STATUS)));
289 #endif
290         spin_lock(&chip->reg_lock);
291         if (cmd == SNDRV_PCM_TRIGGER_START) {
292                 if (chip->image[AD1848_IFACE_CTRL] & what) {
293                         spin_unlock(&chip->reg_lock);
294                         return 0;
295                 }
296                 snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL] |= what);
297                 chip->mode |= AD1848_MODE_RUNNING;
298         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
299                 if (!(chip->image[AD1848_IFACE_CTRL] & what)) {
300                         spin_unlock(&chip->reg_lock);
301                         return 0;
302                 }
303                 snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL] &= ~what);
304                 chip->mode &= ~AD1848_MODE_RUNNING;
305         } else {
306                 result = -EINVAL;
307         }
308         spin_unlock(&chip->reg_lock);
309         return result;
310 }
311
312 /*
313  *  CODEC I/O
314  */
315
316 static unsigned char snd_ad1848_get_rate(unsigned int rate)
317 {
318         int i;
319
320         for (i = 0; i < 14; i++)
321                 if (rate == rates[i])
322                         return freq_bits[i];
323         snd_BUG();
324         return freq_bits[13];
325 }
326
327 static int snd_ad1848_ioctl(snd_pcm_substream_t * substream,
328                             unsigned int cmd, void *arg)
329 {
330         return snd_pcm_lib_ioctl(substream, cmd, arg);
331 }
332
333 static unsigned char snd_ad1848_get_format(int format, int channels)
334 {
335         unsigned char rformat;
336
337         rformat = AD1848_LINEAR_8;
338         switch (format) {
339         case SNDRV_PCM_FORMAT_A_LAW:    rformat = AD1848_ALAW_8; break;
340         case SNDRV_PCM_FORMAT_MU_LAW:   rformat = AD1848_ULAW_8; break;
341         case SNDRV_PCM_FORMAT_S16_LE:   rformat = AD1848_LINEAR_16; break;
342         }
343         if (channels > 1)
344                 rformat |= AD1848_STEREO;
345 #if 0
346         snd_printk("get_format: 0x%x (mode=0x%x)\n", format, mode);
347 #endif
348         return rformat;
349 }
350
351 static void snd_ad1848_calibrate_mute(ad1848_t *chip, int mute)
352 {
353         unsigned long flags;
354         
355         mute = mute ? 1 : 0;
356         spin_lock_irqsave(&chip->reg_lock, flags);
357         if (chip->calibrate_mute == mute) {
358                 spin_unlock_irqrestore(&chip->reg_lock, flags);
359                 return;
360         }
361         if (!mute) {
362                 snd_ad1848_dout(chip, AD1848_LEFT_INPUT, chip->image[AD1848_LEFT_INPUT]);
363                 snd_ad1848_dout(chip, AD1848_RIGHT_INPUT, chip->image[AD1848_RIGHT_INPUT]);
364         }
365         snd_ad1848_dout(chip, AD1848_AUX1_LEFT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX1_LEFT_INPUT]);
366         snd_ad1848_dout(chip, AD1848_AUX1_RIGHT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX1_RIGHT_INPUT]);
367         snd_ad1848_dout(chip, AD1848_AUX2_LEFT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX2_LEFT_INPUT]);
368         snd_ad1848_dout(chip, AD1848_AUX2_RIGHT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX2_RIGHT_INPUT]);
369         snd_ad1848_dout(chip, AD1848_LEFT_OUTPUT, mute ? 0x80 : chip->image[AD1848_LEFT_OUTPUT]);
370         snd_ad1848_dout(chip, AD1848_RIGHT_OUTPUT, mute ? 0x80 : chip->image[AD1848_RIGHT_OUTPUT]);
371         chip->calibrate_mute = mute;
372         spin_unlock_irqrestore(&chip->reg_lock, flags);
373 }
374
375 static void snd_ad1848_set_data_format(ad1848_t *chip, snd_pcm_hw_params_t *hw_params)
376 {
377         if (hw_params == NULL) {
378                 chip->image[AD1848_DATA_FORMAT] = 0x20;
379         } else {
380                 chip->image[AD1848_DATA_FORMAT] =
381                     snd_ad1848_get_format(params_format(hw_params), params_channels(hw_params)) |
382                     snd_ad1848_get_rate(params_rate(hw_params));
383         }
384         // snd_printk(">>> pmode = 0x%x, dfr = 0x%x\n", pstr->mode, chip->image[AD1848_DATA_FORMAT]);
385 }
386
387 static int snd_ad1848_open(ad1848_t *chip, unsigned int mode)
388 {
389         unsigned long flags;
390
391         down(&chip->open_mutex);
392         if (chip->mode & AD1848_MODE_OPEN) {
393                 up(&chip->open_mutex);
394                 return -EAGAIN;
395         }
396         snd_ad1848_mce_down(chip);
397
398 #ifdef SNDRV_DEBUG_MCE
399         snd_printk("open: (1)\n");
400 #endif
401         snd_ad1848_mce_up(chip);
402         spin_lock_irqsave(&chip->reg_lock, flags);
403         chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO |
404                              AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO |
405                              AD1848_CALIB_MODE);
406         chip->image[AD1848_IFACE_CTRL] |= AD1848_AUTOCALIB;
407         snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL]);
408         spin_unlock_irqrestore(&chip->reg_lock, flags);
409         snd_ad1848_mce_down(chip);
410
411 #ifdef SNDRV_DEBUG_MCE
412         snd_printk("open: (2)\n");
413 #endif
414
415         snd_ad1848_set_data_format(chip, NULL);
416
417         snd_ad1848_mce_up(chip);
418         spin_lock_irqsave(&chip->reg_lock, flags);
419         snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]);
420         spin_unlock_irqrestore(&chip->reg_lock, flags);
421         snd_ad1848_mce_down(chip);
422
423 #ifdef SNDRV_DEBUG_MCE
424         snd_printk("open: (3)\n");
425 #endif
426
427         /* ok. now enable and ack CODEC IRQ */
428         spin_lock_irqsave(&chip->reg_lock, flags);
429         outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
430         outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
431         chip->image[AD1848_PIN_CTRL] |= AD1848_IRQ_ENABLE;
432         snd_ad1848_out(chip, AD1848_PIN_CTRL, chip->image[AD1848_PIN_CTRL]);
433         spin_unlock_irqrestore(&chip->reg_lock, flags);
434
435         chip->mode = mode;
436         up(&chip->open_mutex);
437
438         return 0;
439 }
440
441 static void snd_ad1848_close(ad1848_t *chip)
442 {
443         unsigned long flags;
444
445         down(&chip->open_mutex);
446         if (!chip->mode) {
447                 up(&chip->open_mutex);
448                 return;
449         }
450         /* disable IRQ */
451         spin_lock_irqsave(&chip->reg_lock, flags);
452         outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
453         outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
454         chip->image[AD1848_PIN_CTRL] &= ~AD1848_IRQ_ENABLE;
455         snd_ad1848_out(chip, AD1848_PIN_CTRL, chip->image[AD1848_PIN_CTRL]);
456         spin_unlock_irqrestore(&chip->reg_lock, flags);
457
458         /* now disable capture & playback */
459
460         snd_ad1848_mce_up(chip);
461         spin_lock_irqsave(&chip->reg_lock, flags);
462         chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO |
463                              AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO);
464         snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL]);
465         spin_unlock_irqrestore(&chip->reg_lock, flags);
466         snd_ad1848_mce_down(chip);
467
468         /* clear IRQ again */
469         spin_lock_irqsave(&chip->reg_lock, flags);
470         outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
471         outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
472         spin_unlock_irqrestore(&chip->reg_lock, flags);
473
474         chip->mode = 0;
475         up(&chip->open_mutex);
476 }
477
478 /*
479  *  ok.. exported functions..
480  */
481
482 static int snd_ad1848_playback_trigger(snd_pcm_substream_t * substream,
483                                        int cmd)
484 {
485         ad1848_t *chip = snd_pcm_substream_chip(substream);
486         return snd_ad1848_trigger(chip, AD1848_PLAYBACK_ENABLE, SNDRV_PCM_STREAM_PLAYBACK, cmd);
487 }
488
489 static int snd_ad1848_capture_trigger(snd_pcm_substream_t * substream,
490                                       int cmd)
491 {
492         ad1848_t *chip = snd_pcm_substream_chip(substream);
493         return snd_ad1848_trigger(chip, AD1848_CAPTURE_ENABLE, SNDRV_PCM_STREAM_CAPTURE, cmd);
494 }
495
496 static int snd_ad1848_playback_hw_params(snd_pcm_substream_t * substream,
497                                          snd_pcm_hw_params_t * hw_params)
498 {
499         ad1848_t *chip = snd_pcm_substream_chip(substream);
500         unsigned long flags;
501         int err;
502
503         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
504                 return err;
505         snd_ad1848_calibrate_mute(chip, 1);
506         snd_ad1848_set_data_format(chip, hw_params);
507         snd_ad1848_mce_up(chip);
508         spin_lock_irqsave(&chip->reg_lock, flags);
509         snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]);
510         spin_unlock_irqrestore(&chip->reg_lock, flags);
511         snd_ad1848_mce_down(chip);
512         snd_ad1848_calibrate_mute(chip, 0);
513         return 0;
514 }
515
516 static int snd_ad1848_playback_hw_free(snd_pcm_substream_t * substream)
517 {
518         return snd_pcm_lib_free_pages(substream);
519 }
520
521 static int snd_ad1848_playback_prepare(snd_pcm_substream_t * substream)
522 {
523         ad1848_t *chip = snd_pcm_substream_chip(substream);
524         snd_pcm_runtime_t *runtime = substream->runtime;
525         unsigned long flags;
526         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
527         unsigned int count = snd_pcm_lib_period_bytes(substream);
528
529         chip->dma_size = size;
530         chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO);
531         snd_dma_program(chip->dma, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
532         count = snd_ad1848_get_count(chip->image[AD1848_DATA_FORMAT], count) - 1;
533         spin_lock_irqsave(&chip->reg_lock, flags);
534         snd_ad1848_out(chip, AD1848_DATA_LWR_CNT, (unsigned char) count);
535         snd_ad1848_out(chip, AD1848_DATA_UPR_CNT, (unsigned char) (count >> 8));
536         spin_unlock_irqrestore(&chip->reg_lock, flags);
537         return 0;
538 }
539
540 static int snd_ad1848_capture_hw_params(snd_pcm_substream_t * substream,
541                                         snd_pcm_hw_params_t * hw_params)
542 {
543         ad1848_t *chip = snd_pcm_substream_chip(substream);
544         unsigned long flags;
545         int err;
546
547         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
548                 return err;
549         snd_ad1848_calibrate_mute(chip, 1);
550         snd_ad1848_set_data_format(chip, hw_params);
551         snd_ad1848_mce_up(chip);
552         spin_lock_irqsave(&chip->reg_lock, flags);
553         snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]);
554         spin_unlock_irqrestore(&chip->reg_lock, flags);
555         snd_ad1848_mce_down(chip);
556         snd_ad1848_calibrate_mute(chip, 0);
557         return 0;
558 }
559
560 static int snd_ad1848_capture_hw_free(snd_pcm_substream_t * substream)
561 {
562         return snd_pcm_lib_free_pages(substream);
563 }
564
565 static int snd_ad1848_capture_prepare(snd_pcm_substream_t * substream)
566 {
567         ad1848_t *chip = snd_pcm_substream_chip(substream);
568         snd_pcm_runtime_t *runtime = substream->runtime;
569         unsigned long flags;
570         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
571         unsigned int count = snd_pcm_lib_period_bytes(substream);
572
573         chip->dma_size = size;
574         chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO);
575         snd_dma_program(chip->dma, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
576         count = snd_ad1848_get_count(chip->image[AD1848_DATA_FORMAT], count) - 1;
577         spin_lock_irqsave(&chip->reg_lock, flags);
578         snd_ad1848_out(chip, AD1848_DATA_LWR_CNT, (unsigned char) count);
579         snd_ad1848_out(chip, AD1848_DATA_UPR_CNT, (unsigned char) (count >> 8));
580         spin_unlock_irqrestore(&chip->reg_lock, flags);
581         return 0;
582 }
583
584 static irqreturn_t snd_ad1848_interrupt(int irq, void *dev_id, struct pt_regs *regs)
585 {
586         ad1848_t *chip = dev_id;
587
588         if ((chip->mode & AD1848_MODE_PLAY) && chip->playback_substream &&
589             (chip->mode & AD1848_MODE_RUNNING))
590                 snd_pcm_period_elapsed(chip->playback_substream);
591         if ((chip->mode & AD1848_MODE_CAPTURE) && chip->capture_substream &&
592             (chip->mode & AD1848_MODE_RUNNING))
593                 snd_pcm_period_elapsed(chip->capture_substream);
594         outb(0, AD1848P(chip, STATUS)); /* clear global interrupt bit */
595         return IRQ_HANDLED;
596 }
597
598 static snd_pcm_uframes_t snd_ad1848_playback_pointer(snd_pcm_substream_t * substream)
599 {
600         ad1848_t *chip = snd_pcm_substream_chip(substream);
601         size_t ptr;
602         
603         if (!(chip->image[AD1848_IFACE_CTRL] & AD1848_PLAYBACK_ENABLE))
604                 return 0;
605         ptr = snd_dma_pointer(chip->dma, chip->dma_size);
606         return bytes_to_frames(substream->runtime, ptr);
607 }
608
609 static snd_pcm_uframes_t snd_ad1848_capture_pointer(snd_pcm_substream_t * substream)
610 {
611         ad1848_t *chip = snd_pcm_substream_chip(substream);
612         size_t ptr;
613
614         if (!(chip->image[AD1848_IFACE_CTRL] & AD1848_CAPTURE_ENABLE))
615                 return 0;
616         ptr = snd_dma_pointer(chip->dma, chip->dma_size);
617         return bytes_to_frames(substream->runtime, ptr);
618 }
619
620 /*
621
622  */
623
624 static void snd_ad1848_thinkpad_twiddle(ad1848_t *chip, int on) {
625
626         int tmp;
627
628         if (!chip->thinkpad_flag) return;
629
630         outb(0x1c, AD1848_THINKPAD_CTL_PORT1);
631         tmp = inb(AD1848_THINKPAD_CTL_PORT2);
632
633         if (on)
634                 /* turn it on */
635                 tmp |= AD1848_THINKPAD_CS4248_ENABLE_BIT;
636         else
637                 /* turn it off */
638                 tmp &= ~AD1848_THINKPAD_CS4248_ENABLE_BIT;
639         
640         outb(tmp, AD1848_THINKPAD_CTL_PORT2);
641
642 }
643
644 #ifdef CONFIG_PM
645 static int snd_ad1848_suspend(snd_card_t *card, pm_message_t state)
646 {
647         ad1848_t *chip = card->pm_private_data;
648
649         snd_pcm_suspend_all(chip->pcm);
650         /* FIXME: save registers? */
651
652         if (chip->thinkpad_flag)
653                 snd_ad1848_thinkpad_twiddle(chip, 0);
654
655         return 0;
656 }
657
658 static int snd_ad1848_resume(snd_card_t *card)
659 {
660         ad1848_t *chip = card->pm_private_data;
661
662         if (chip->thinkpad_flag)
663                 snd_ad1848_thinkpad_twiddle(chip, 1);
664
665         /* FIXME: restore registers? */
666
667         return 0;
668 }
669 #endif /* CONFIG_PM */
670
671 static int snd_ad1848_probe(ad1848_t * chip)
672 {
673         unsigned long flags;
674         int i, id, rev, ad1847;
675         unsigned char *ptr;
676
677 #if 0
678         snd_ad1848_debug(chip);
679 #endif
680         id = ad1847 = 0;
681         for (i = 0; i < 1000; i++) {
682                 mb();
683                 if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT)
684                         udelay(500);
685                 else {
686                         spin_lock_irqsave(&chip->reg_lock, flags);
687                         snd_ad1848_out(chip, AD1848_MISC_INFO, 0x00);
688                         snd_ad1848_out(chip, AD1848_LEFT_INPUT, 0xaa);
689                         snd_ad1848_out(chip, AD1848_RIGHT_INPUT, 0x45);
690                         rev = snd_ad1848_in(chip, AD1848_RIGHT_INPUT);
691                         if (rev == 0x65) {
692                                 spin_unlock_irqrestore(&chip->reg_lock, flags);
693                                 id = 1;
694                                 ad1847 = 1;
695                                 break;
696                         }
697                         if (snd_ad1848_in(chip, AD1848_LEFT_INPUT) == 0xaa && rev == 0x45) {
698                                 spin_unlock_irqrestore(&chip->reg_lock, flags);
699                                 id = 1;
700                                 break;
701                         }
702                         spin_unlock_irqrestore(&chip->reg_lock, flags);
703                 }
704         }
705         if (id != 1)
706                 return -ENODEV; /* no valid device found */
707         if (chip->hardware == AD1848_HW_DETECT) {
708                 if (ad1847) {
709                         chip->hardware = AD1848_HW_AD1847;
710                 } else {
711                         chip->hardware = AD1848_HW_AD1848;
712                         rev = snd_ad1848_in(chip, AD1848_MISC_INFO);
713                         if (rev & 0x80) {
714                                 chip->hardware = AD1848_HW_CS4248;
715                         } else if ((rev & 0x0f) == 0x0a) {
716                                 snd_ad1848_out(chip, AD1848_MISC_INFO, 0x40);
717                                 for (i = 0; i < 16; ++i) {
718                                         if (snd_ad1848_in(chip, i) != snd_ad1848_in(chip, i + 16)) {
719                                                 chip->hardware = AD1848_HW_CMI8330;
720                                                 break;
721                                         }
722                                 }
723                                 snd_ad1848_out(chip, AD1848_MISC_INFO, 0x00);
724                         }
725                 }
726         }
727         spin_lock_irqsave(&chip->reg_lock, flags);
728         inb(AD1848P(chip, STATUS));     /* clear any pendings IRQ */
729         outb(0, AD1848P(chip, STATUS));
730         mb();
731         spin_unlock_irqrestore(&chip->reg_lock, flags);
732
733         chip->image[AD1848_MISC_INFO] = 0x00;
734         chip->image[AD1848_IFACE_CTRL] =
735             (chip->image[AD1848_IFACE_CTRL] & ~AD1848_SINGLE_DMA) | AD1848_SINGLE_DMA;
736         ptr = (unsigned char *) &chip->image;
737         snd_ad1848_mce_down(chip);
738         spin_lock_irqsave(&chip->reg_lock, flags);
739         for (i = 0; i < 16; i++)        /* ok.. fill all AD1848 registers */
740                 snd_ad1848_out(chip, i, *ptr++);
741         spin_unlock_irqrestore(&chip->reg_lock, flags);
742         snd_ad1848_mce_up(chip);
743         snd_ad1848_mce_down(chip);
744         return 0;               /* all things are ok.. */
745 }
746
747 /*
748
749  */
750
751 static snd_pcm_hardware_t snd_ad1848_playback =
752 {
753         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
754                                  SNDRV_PCM_INFO_MMAP_VALID),
755         .formats =              (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
756                                  SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE),
757         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
758         .rate_min =             5510,
759         .rate_max =             48000,
760         .channels_min =         1,
761         .channels_max =         2,
762         .buffer_bytes_max =     (128*1024),
763         .period_bytes_min =     64,
764         .period_bytes_max =     (128*1024),
765         .periods_min =          1,
766         .periods_max =          1024,
767         .fifo_size =            0,
768 };
769
770 static snd_pcm_hardware_t snd_ad1848_capture =
771 {
772         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
773                                  SNDRV_PCM_INFO_MMAP_VALID),
774         .formats =              (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
775                                  SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE),
776         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
777         .rate_min =             5510,
778         .rate_max =             48000,
779         .channels_min =         1,
780         .channels_max =         2,
781         .buffer_bytes_max =     (128*1024),
782         .period_bytes_min =     64,
783         .period_bytes_max =     (128*1024),
784         .periods_min =          1,
785         .periods_max =          1024,
786         .fifo_size =            0,
787 };
788
789 /*
790
791  */
792
793 static int snd_ad1848_playback_open(snd_pcm_substream_t * substream)
794 {
795         ad1848_t *chip = snd_pcm_substream_chip(substream);
796         snd_pcm_runtime_t *runtime = substream->runtime;
797         int err;
798
799         if ((err = snd_ad1848_open(chip, AD1848_MODE_PLAY)) < 0)
800                 return err;
801         chip->playback_substream = substream;
802         runtime->hw = snd_ad1848_playback;
803         snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.buffer_bytes_max);
804         snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.period_bytes_max);
805         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
806         return 0;
807 }
808
809 static int snd_ad1848_capture_open(snd_pcm_substream_t * substream)
810 {
811         ad1848_t *chip = snd_pcm_substream_chip(substream);
812         snd_pcm_runtime_t *runtime = substream->runtime;
813         int err;
814
815         if ((err = snd_ad1848_open(chip, AD1848_MODE_CAPTURE)) < 0)
816                 return err;
817         chip->capture_substream = substream;
818         runtime->hw = snd_ad1848_capture;
819         snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.buffer_bytes_max);
820         snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.period_bytes_max);
821         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
822         return 0;
823 }
824
825 static int snd_ad1848_playback_close(snd_pcm_substream_t * substream)
826 {
827         ad1848_t *chip = snd_pcm_substream_chip(substream);
828
829         chip->mode &= ~AD1848_MODE_PLAY;
830         chip->playback_substream = NULL;
831         snd_ad1848_close(chip);
832         return 0;
833 }
834
835 static int snd_ad1848_capture_close(snd_pcm_substream_t * substream)
836 {
837         ad1848_t *chip = snd_pcm_substream_chip(substream);
838
839         chip->mode &= ~AD1848_MODE_CAPTURE;
840         chip->capture_substream = NULL;
841         snd_ad1848_close(chip);
842         return 0;
843 }
844
845 static int snd_ad1848_free(ad1848_t *chip)
846 {
847         release_and_free_resource(chip->res_port);
848         if (chip->irq >= 0)
849                 free_irq(chip->irq, (void *) chip);
850         if (chip->dma >= 0) {
851                 snd_dma_disable(chip->dma);
852                 free_dma(chip->dma);
853         }
854         kfree(chip);
855         return 0;
856 }
857
858 static int snd_ad1848_dev_free(snd_device_t *device)
859 {
860         ad1848_t *chip = device->device_data;
861         return snd_ad1848_free(chip);
862 }
863
864 static const char *snd_ad1848_chip_id(ad1848_t *chip)
865 {
866         switch (chip->hardware) {
867         case AD1848_HW_AD1847:  return "AD1847";
868         case AD1848_HW_AD1848:  return "AD1848";
869         case AD1848_HW_CS4248:  return "CS4248";
870         case AD1848_HW_CMI8330: return "CMI8330/C3D";
871         default:                return "???";
872         }
873 }
874
875 int snd_ad1848_create(snd_card_t * card,
876                       unsigned long port,
877                       int irq, int dma,
878                       unsigned short hardware,
879                       ad1848_t ** rchip)
880 {
881         static snd_device_ops_t ops = {
882                 .dev_free =     snd_ad1848_dev_free,
883         };
884         ad1848_t *chip;
885         int err;
886
887         *rchip = NULL;
888         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
889         if (chip == NULL)
890                 return -ENOMEM;
891         spin_lock_init(&chip->reg_lock);
892         init_MUTEX(&chip->open_mutex);
893         chip->card = card;
894         chip->port = port;
895         chip->irq = -1;
896         chip->dma = -1;
897         chip->hardware = hardware;
898         memcpy(&chip->image, &snd_ad1848_original_image, sizeof(snd_ad1848_original_image));
899         
900         if ((chip->res_port = request_region(port, 4, "AD1848")) == NULL) {
901                 snd_printk(KERN_ERR "ad1848: can't grab port 0x%lx\n", port);
902                 snd_ad1848_free(chip);
903                 return -EBUSY;
904         }
905         if (request_irq(irq, snd_ad1848_interrupt, SA_INTERRUPT, "AD1848", (void *) chip)) {
906                 snd_printk(KERN_ERR "ad1848: can't grab IRQ %d\n", irq);
907                 snd_ad1848_free(chip);
908                 return -EBUSY;
909         }
910         chip->irq = irq;
911         if (request_dma(dma, "AD1848")) {
912                 snd_printk(KERN_ERR "ad1848: can't grab DMA %d\n", dma);
913                 snd_ad1848_free(chip);
914                 return -EBUSY;
915         }
916         chip->dma = dma;
917
918         if (hardware == AD1848_HW_THINKPAD) {
919                 chip->thinkpad_flag = 1;
920                 chip->hardware = AD1848_HW_DETECT; /* reset */
921                 snd_ad1848_thinkpad_twiddle(chip, 1);
922                 snd_card_set_isa_pm_callback(card, snd_ad1848_suspend, snd_ad1848_resume, chip);
923         }
924
925         if (snd_ad1848_probe(chip) < 0) {
926                 snd_ad1848_free(chip);
927                 return -ENODEV;
928         }
929
930         /* Register device */
931         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
932                 snd_ad1848_free(chip);
933                 return err;
934         }
935
936         *rchip = chip;
937         return 0;
938 }
939
940 static snd_pcm_ops_t snd_ad1848_playback_ops = {
941         .open =         snd_ad1848_playback_open,
942         .close =        snd_ad1848_playback_close,
943         .ioctl =        snd_ad1848_ioctl,
944         .hw_params =    snd_ad1848_playback_hw_params,
945         .hw_free =      snd_ad1848_playback_hw_free,
946         .prepare =      snd_ad1848_playback_prepare,
947         .trigger =      snd_ad1848_playback_trigger,
948         .pointer =      snd_ad1848_playback_pointer,
949 };
950
951 static snd_pcm_ops_t snd_ad1848_capture_ops = {
952         .open =         snd_ad1848_capture_open,
953         .close =        snd_ad1848_capture_close,
954         .ioctl =        snd_ad1848_ioctl,
955         .hw_params =    snd_ad1848_capture_hw_params,
956         .hw_free =      snd_ad1848_capture_hw_free,
957         .prepare =      snd_ad1848_capture_prepare,
958         .trigger =      snd_ad1848_capture_trigger,
959         .pointer =      snd_ad1848_capture_pointer,
960 };
961
962 int snd_ad1848_pcm(ad1848_t *chip, int device, snd_pcm_t **rpcm)
963 {
964         snd_pcm_t *pcm;
965         int err;
966
967         if ((err = snd_pcm_new(chip->card, "AD1848", device, 1, 1, &pcm)) < 0)
968                 return err;
969
970         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ad1848_playback_ops);
971         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ad1848_capture_ops);
972
973         pcm->private_data = chip;
974         pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
975         strcpy(pcm->name, snd_ad1848_chip_id(chip));
976
977         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
978                                               snd_dma_isa_data(),
979                                               64*1024, chip->dma > 3 ? 128*1024 : 64*1024);
980
981         chip->pcm = pcm;
982         if (rpcm)
983                 *rpcm = pcm;
984         return 0;
985 }
986
987 const snd_pcm_ops_t *snd_ad1848_get_pcm_ops(int direction)
988 {
989         return direction == SNDRV_PCM_STREAM_PLAYBACK ?
990                 &snd_ad1848_playback_ops : &snd_ad1848_capture_ops;
991 }
992
993 /*
994  *  MIXER part
995  */
996
997 static int snd_ad1848_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
998 {
999         static char *texts[4] = {
1000                 "Line", "Aux", "Mic", "Mix"
1001         };
1002
1003         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1004         uinfo->count = 2;
1005         uinfo->value.enumerated.items = 4;
1006         if (uinfo->value.enumerated.item > 3)
1007                 uinfo->value.enumerated.item = 3;
1008         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1009         return 0;
1010 }
1011
1012 static int snd_ad1848_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1013 {
1014         ad1848_t *chip = snd_kcontrol_chip(kcontrol);
1015         unsigned long flags;
1016         
1017         spin_lock_irqsave(&chip->reg_lock, flags);
1018         ucontrol->value.enumerated.item[0] = (chip->image[AD1848_LEFT_INPUT] & AD1848_MIXS_ALL) >> 6;
1019         ucontrol->value.enumerated.item[1] = (chip->image[AD1848_RIGHT_INPUT] & AD1848_MIXS_ALL) >> 6;
1020         spin_unlock_irqrestore(&chip->reg_lock, flags);
1021         return 0;
1022 }
1023
1024 static int snd_ad1848_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1025 {
1026         ad1848_t *chip = snd_kcontrol_chip(kcontrol);
1027         unsigned long flags;
1028         unsigned short left, right;
1029         int change;
1030         
1031         if (ucontrol->value.enumerated.item[0] > 3 ||
1032             ucontrol->value.enumerated.item[1] > 3)
1033                 return -EINVAL;
1034         left = ucontrol->value.enumerated.item[0] << 6;
1035         right = ucontrol->value.enumerated.item[1] << 6;
1036         spin_lock_irqsave(&chip->reg_lock, flags);
1037         left = (chip->image[AD1848_LEFT_INPUT] & ~AD1848_MIXS_ALL) | left;
1038         right = (chip->image[AD1848_RIGHT_INPUT] & ~AD1848_MIXS_ALL) | right;
1039         change = left != chip->image[AD1848_LEFT_INPUT] ||
1040                  right != chip->image[AD1848_RIGHT_INPUT];
1041         snd_ad1848_out(chip, AD1848_LEFT_INPUT, left);
1042         snd_ad1848_out(chip, AD1848_RIGHT_INPUT, right);
1043         spin_unlock_irqrestore(&chip->reg_lock, flags);
1044         return change;
1045 }
1046
1047 static int snd_ad1848_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1048 {
1049         int mask = (kcontrol->private_value >> 16) & 0xff;
1050
1051         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1052         uinfo->count = 1;
1053         uinfo->value.integer.min = 0;
1054         uinfo->value.integer.max = mask;
1055         return 0;
1056 }
1057
1058 static int snd_ad1848_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1059 {
1060         ad1848_t *chip = snd_kcontrol_chip(kcontrol);
1061         unsigned long flags;
1062         int reg = kcontrol->private_value & 0xff;
1063         int shift = (kcontrol->private_value >> 8) & 0xff;
1064         int mask = (kcontrol->private_value >> 16) & 0xff;
1065         int invert = (kcontrol->private_value >> 24) & 0xff;
1066         
1067         spin_lock_irqsave(&chip->reg_lock, flags);
1068         ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
1069         spin_unlock_irqrestore(&chip->reg_lock, flags);
1070         if (invert)
1071                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1072         return 0;
1073 }
1074
1075 static int snd_ad1848_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1076 {
1077         ad1848_t *chip = snd_kcontrol_chip(kcontrol);
1078         unsigned long flags;
1079         int reg = kcontrol->private_value & 0xff;
1080         int shift = (kcontrol->private_value >> 8) & 0xff;
1081         int mask = (kcontrol->private_value >> 16) & 0xff;
1082         int invert = (kcontrol->private_value >> 24) & 0xff;
1083         int change;
1084         unsigned short val;
1085         
1086         val = (ucontrol->value.integer.value[0] & mask);
1087         if (invert)
1088                 val = mask - val;
1089         val <<= shift;
1090         spin_lock_irqsave(&chip->reg_lock, flags);
1091         val = (chip->image[reg] & ~(mask << shift)) | val;
1092         change = val != chip->image[reg];
1093         snd_ad1848_out(chip, reg, val);
1094         spin_unlock_irqrestore(&chip->reg_lock, flags);
1095         return change;
1096 }
1097
1098 static int snd_ad1848_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1099 {
1100         int mask = (kcontrol->private_value >> 24) & 0xff;
1101
1102         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1103         uinfo->count = 2;
1104         uinfo->value.integer.min = 0;
1105         uinfo->value.integer.max = mask;
1106         return 0;
1107 }
1108
1109 static int snd_ad1848_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1110 {
1111         ad1848_t *chip = snd_kcontrol_chip(kcontrol);
1112         unsigned long flags;
1113         int left_reg = kcontrol->private_value & 0xff;
1114         int right_reg = (kcontrol->private_value >> 8) & 0xff;
1115         int shift_left = (kcontrol->private_value >> 16) & 0x07;
1116         int shift_right = (kcontrol->private_value >> 19) & 0x07;
1117         int mask = (kcontrol->private_value >> 24) & 0xff;
1118         int invert = (kcontrol->private_value >> 22) & 1;
1119         
1120         spin_lock_irqsave(&chip->reg_lock, flags);
1121         ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
1122         ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
1123         spin_unlock_irqrestore(&chip->reg_lock, flags);
1124         if (invert) {
1125                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1126                 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1127         }
1128         return 0;
1129 }
1130
1131 static int snd_ad1848_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1132 {
1133         ad1848_t *chip = snd_kcontrol_chip(kcontrol);
1134         unsigned long flags;
1135         int left_reg = kcontrol->private_value & 0xff;
1136         int right_reg = (kcontrol->private_value >> 8) & 0xff;
1137         int shift_left = (kcontrol->private_value >> 16) & 0x07;
1138         int shift_right = (kcontrol->private_value >> 19) & 0x07;
1139         int mask = (kcontrol->private_value >> 24) & 0xff;
1140         int invert = (kcontrol->private_value >> 22) & 1;
1141         int change;
1142         unsigned short val1, val2;
1143         
1144         val1 = ucontrol->value.integer.value[0] & mask;
1145         val2 = ucontrol->value.integer.value[1] & mask;
1146         if (invert) {
1147                 val1 = mask - val1;
1148                 val2 = mask - val2;
1149         }
1150         val1 <<= shift_left;
1151         val2 <<= shift_right;
1152         spin_lock_irqsave(&chip->reg_lock, flags);
1153         if (left_reg != right_reg) {
1154                 val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
1155                 val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
1156                 change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
1157                 snd_ad1848_out(chip, left_reg, val1);
1158                 snd_ad1848_out(chip, right_reg, val2);
1159         } else {
1160                 val1 = (chip->image[left_reg] & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1161                 change = val1 != chip->image[left_reg];
1162                 snd_ad1848_out(chip, left_reg, val1);           
1163         }
1164         spin_unlock_irqrestore(&chip->reg_lock, flags);
1165         return change;
1166 }
1167
1168 /*
1169  */
1170 int snd_ad1848_add_ctl(ad1848_t *chip, const char *name, int index, int type, unsigned long value)
1171 {
1172         static snd_kcontrol_new_t newctls[] = {
1173                 [AD1848_MIX_SINGLE] = {
1174                         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1175                         .info = snd_ad1848_info_single,
1176                         .get = snd_ad1848_get_single,
1177                         .put = snd_ad1848_put_single,
1178                 },
1179                 [AD1848_MIX_DOUBLE] = {
1180                         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1181                         .info = snd_ad1848_info_double,
1182                         .get = snd_ad1848_get_double,
1183                         .put = snd_ad1848_put_double,
1184                 },
1185                 [AD1848_MIX_CAPTURE] = {
1186                         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1187                         .info = snd_ad1848_info_mux,
1188                         .get = snd_ad1848_get_mux,
1189                         .put = snd_ad1848_put_mux,
1190                 },
1191         };
1192         snd_kcontrol_t *ctl;
1193         int err;
1194
1195         ctl = snd_ctl_new1(&newctls[type], chip);
1196         if (! ctl)
1197                 return -ENOMEM;
1198         strlcpy(ctl->id.name, name, sizeof(ctl->id.name));
1199         ctl->id.index = index;
1200         ctl->private_value = value;
1201         if ((err = snd_ctl_add(chip->card, ctl)) < 0) {
1202                 snd_ctl_free_one(ctl);
1203                 return err;
1204         }
1205         return 0;
1206 }
1207
1208
1209 static struct ad1848_mix_elem snd_ad1848_controls[] = {
1210 AD1848_DOUBLE("PCM Playback Switch", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 7, 7, 1, 1),
1211 AD1848_DOUBLE("PCM Playback Volume", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 0, 0, 63, 1),
1212 AD1848_DOUBLE("Aux Playback Switch", 0, AD1848_AUX1_LEFT_INPUT, AD1848_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
1213 AD1848_DOUBLE("Aux Playback Volume", 0, AD1848_AUX1_LEFT_INPUT, AD1848_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
1214 AD1848_DOUBLE("Aux Playback Switch", 1, AD1848_AUX2_LEFT_INPUT, AD1848_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
1215 AD1848_DOUBLE("Aux Playback Volume", 1, AD1848_AUX2_LEFT_INPUT, AD1848_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
1216 AD1848_DOUBLE("Capture Volume", 0, AD1848_LEFT_INPUT, AD1848_RIGHT_INPUT, 0, 0, 15, 0),
1217 {
1218         .name = "Capture Source",
1219         .type = AD1848_MIX_CAPTURE,
1220 },
1221 AD1848_SINGLE("Loopback Capture Switch", 0, AD1848_LOOPBACK, 0, 1, 0),
1222 AD1848_SINGLE("Loopback Capture Volume", 0, AD1848_LOOPBACK, 1, 63, 0)
1223 };
1224                                         
1225 int snd_ad1848_mixer(ad1848_t *chip)
1226 {
1227         snd_card_t *card;
1228         snd_pcm_t *pcm;
1229         unsigned int idx;
1230         int err;
1231
1232         snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
1233
1234         pcm = chip->pcm;
1235         card = chip->card;
1236
1237         strcpy(card->mixername, pcm->name);
1238
1239         for (idx = 0; idx < ARRAY_SIZE(snd_ad1848_controls); idx++)
1240                 if ((err = snd_ad1848_add_ctl_elem(chip, &snd_ad1848_controls[idx])) < 0)
1241                         return err;
1242
1243         return 0;
1244 }
1245
1246 EXPORT_SYMBOL(snd_ad1848_out);
1247 EXPORT_SYMBOL(snd_ad1848_create);
1248 EXPORT_SYMBOL(snd_ad1848_pcm);
1249 EXPORT_SYMBOL(snd_ad1848_get_pcm_ops);
1250 EXPORT_SYMBOL(snd_ad1848_mixer);
1251 EXPORT_SYMBOL(snd_ad1848_add_ctl);
1252
1253 /*
1254  *  INIT part
1255  */
1256
1257 static int __init alsa_ad1848_init(void)
1258 {
1259         return 0;
1260 }
1261
1262 static void __exit alsa_ad1848_exit(void)
1263 {
1264 }
1265
1266 module_init(alsa_ad1848_init)
1267 module_exit(alsa_ad1848_exit)