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