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