[ALSA] Fix schedule_timeout usage
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / isa / ad1848 / ad1848_lib.c
blob891bacc94f68cc0e6da6fe943c9cf779a7f6aed3
1 /*
2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Routines for control of AD1848/AD1847/CS4248
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.
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.
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
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>
35 #include <asm/io.h>
36 #include <asm/dma.h>
38 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
39 MODULE_DESCRIPTION("Routines for control of AD1848/AD1847/CS4248");
40 MODULE_LICENSE("GPL");
42 #if 0
43 #define SNDRV_DEBUG_MCE
44 #endif
47 * Some variables
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
67 static unsigned int rates[14] = {
68 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
69 27042, 32000, 33075, 37800, 44100, 48000
72 static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
73 .count = 14,
74 .list = rates,
75 .mask = 0,
78 static unsigned char snd_ad1848_original_image[16] =
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 */
99 * Basic I/O functions
102 void snd_ad1848_out(ad1848_t *chip,
103 unsigned char reg,
104 unsigned char value)
106 int timeout;
108 for (timeout = 250; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--)
109 udelay(100);
110 #ifdef CONFIG_SND_DEBUG
111 if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT)
112 snd_printk(KERN_WARNING "auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
113 #endif
114 outb(chip->mce_bit | reg, AD1848P(chip, REGSEL));
115 outb(chip->image[reg] = value, AD1848P(chip, REG));
116 mb();
117 #if 0
118 printk("codec out - reg 0x%x = 0x%x\n", chip->mce_bit | reg, value);
119 #endif
122 static void snd_ad1848_dout(ad1848_t *chip,
123 unsigned char reg, unsigned char value)
125 int timeout;
127 for (timeout = 250; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--)
128 udelay(100);
129 outb(chip->mce_bit | reg, AD1848P(chip, REGSEL));
130 outb(value, AD1848P(chip, REG));
131 mb();
134 static unsigned char snd_ad1848_in(ad1848_t *chip, unsigned char reg)
136 int timeout;
138 for (timeout = 250; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--)
139 udelay(100);
140 #ifdef CONFIG_SND_DEBUG
141 if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT)
142 snd_printk(KERN_WARNING "auto calibration time out - reg = 0x%x\n", reg);
143 #endif
144 outb(chip->mce_bit | reg, AD1848P(chip, REGSEL));
145 mb();
146 return inb(AD1848P(chip, REG));
149 #if 0
151 static void snd_ad1848_debug(ad1848_t *chip)
153 printk("AD1848 REGS: INDEX = 0x%02x ", inb(AD1848P(chip, REGSEL)));
154 printk(" STATUS = 0x%02x\n", inb(AD1848P(chip, STATUS)));
155 printk(" 0x00: left input = 0x%02x ", snd_ad1848_in(chip, 0x00));
156 printk(" 0x08: playback format = 0x%02x\n", snd_ad1848_in(chip, 0x08));
157 printk(" 0x01: right input = 0x%02x ", snd_ad1848_in(chip, 0x01));
158 printk(" 0x09: iface (CFIG 1) = 0x%02x\n", snd_ad1848_in(chip, 0x09));
159 printk(" 0x02: AUXA left = 0x%02x ", snd_ad1848_in(chip, 0x02));
160 printk(" 0x0a: pin control = 0x%02x\n", snd_ad1848_in(chip, 0x0a));
161 printk(" 0x03: AUXA right = 0x%02x ", snd_ad1848_in(chip, 0x03));
162 printk(" 0x0b: init & status = 0x%02x\n", snd_ad1848_in(chip, 0x0b));
163 printk(" 0x04: AUXB left = 0x%02x ", snd_ad1848_in(chip, 0x04));
164 printk(" 0x0c: revision & mode = 0x%02x\n", snd_ad1848_in(chip, 0x0c));
165 printk(" 0x05: AUXB right = 0x%02x ", snd_ad1848_in(chip, 0x05));
166 printk(" 0x0d: loopback = 0x%02x\n", snd_ad1848_in(chip, 0x0d));
167 printk(" 0x06: left output = 0x%02x ", snd_ad1848_in(chip, 0x06));
168 printk(" 0x0e: data upr count = 0x%02x\n", snd_ad1848_in(chip, 0x0e));
169 printk(" 0x07: right output = 0x%02x ", snd_ad1848_in(chip, 0x07));
170 printk(" 0x0f: data lwr count = 0x%02x\n", snd_ad1848_in(chip, 0x0f));
173 #endif
176 * AD1848 detection / MCE routines
179 static void snd_ad1848_mce_up(ad1848_t *chip)
181 unsigned long flags;
182 int timeout;
184 for (timeout = 250; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--)
185 udelay(100);
186 #ifdef CONFIG_SND_DEBUG
187 if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT)
188 snd_printk(KERN_WARNING "mce_up - auto calibration time out (0)\n");
189 #endif
190 spin_lock_irqsave(&chip->reg_lock, flags);
191 chip->mce_bit |= AD1848_MCE;
192 timeout = inb(AD1848P(chip, REGSEL));
193 if (timeout == 0x80)
194 snd_printk(KERN_WARNING "mce_up [0x%lx]: serious init problem - codec still busy\n", chip->port);
195 if (!(timeout & AD1848_MCE))
196 outb(chip->mce_bit | (timeout & 0x1f), AD1848P(chip, REGSEL));
197 spin_unlock_irqrestore(&chip->reg_lock, flags);
200 static void snd_ad1848_mce_down(ad1848_t *chip)
202 unsigned long flags;
203 int timeout;
204 signed long time;
206 spin_lock_irqsave(&chip->reg_lock, flags);
207 for (timeout = 5; timeout > 0; timeout--)
208 inb(AD1848P(chip, REGSEL));
209 /* end of cleanup sequence */
210 for (timeout = 12000; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--)
211 udelay(100);
212 #if 0
213 printk("(1) timeout = %i\n", timeout);
214 #endif
215 #ifdef CONFIG_SND_DEBUG
216 if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT)
217 snd_printk(KERN_WARNING "mce_down [0x%lx] - auto calibration time out (0)\n", AD1848P(chip, REGSEL));
218 #endif
219 chip->mce_bit &= ~AD1848_MCE;
220 timeout = inb(AD1848P(chip, REGSEL));
221 outb(chip->mce_bit | (timeout & 0x1f), AD1848P(chip, REGSEL));
222 if (timeout == 0x80)
223 snd_printk(KERN_WARNING "mce_down [0x%lx]: serious init problem - codec still busy\n", chip->port);
224 if ((timeout & AD1848_MCE) == 0) {
225 spin_unlock_irqrestore(&chip->reg_lock, flags);
226 return;
228 /* calibration process */
230 for (timeout = 500; timeout > 0 && (snd_ad1848_in(chip, AD1848_TEST_INIT) & AD1848_CALIB_IN_PROGRESS) == 0; timeout--);
231 if ((snd_ad1848_in(chip, AD1848_TEST_INIT) & AD1848_CALIB_IN_PROGRESS) == 0) {
232 snd_printd("mce_down - auto calibration time out (1)\n");
233 spin_unlock_irqrestore(&chip->reg_lock, flags);
234 return;
236 #if 0
237 printk("(2) timeout = %i, jiffies = %li\n", timeout, jiffies);
238 #endif
239 time = HZ / 4;
240 while (snd_ad1848_in(chip, AD1848_TEST_INIT) & AD1848_CALIB_IN_PROGRESS) {
241 spin_unlock_irqrestore(&chip->reg_lock, flags);
242 if (time <= 0) {
243 snd_printk(KERN_ERR "mce_down - auto calibration time out (2)\n");
244 return;
246 time = schedule_timeout_interruptible(time);
247 spin_lock_irqsave(&chip->reg_lock, flags);
249 #if 0
250 printk("(3) jiffies = %li\n", jiffies);
251 #endif
252 time = HZ / 10;
253 while (inb(AD1848P(chip, REGSEL)) & AD1848_INIT) {
254 spin_unlock_irqrestore(&chip->reg_lock, flags);
255 if (time <= 0) {
256 snd_printk(KERN_ERR "mce_down - auto calibration time out (3)\n");
257 return;
259 time = schedule_timeout_interruptible(time);
260 spin_lock_irqsave(&chip->reg_lock, flags);
262 spin_unlock_irqrestore(&chip->reg_lock, flags);
263 #if 0
264 printk("(4) jiffies = %li\n", jiffies);
265 snd_printk("mce_down - exit = 0x%x\n", inb(AD1848P(chip, REGSEL)));
266 #endif
269 static unsigned int snd_ad1848_get_count(unsigned char format,
270 unsigned int size)
272 switch (format & 0xe0) {
273 case AD1848_LINEAR_16:
274 size >>= 1;
275 break;
277 if (format & AD1848_STEREO)
278 size >>= 1;
279 return size;
282 static int snd_ad1848_trigger(ad1848_t *chip, unsigned char what,
283 int channel, int cmd)
285 int result = 0;
287 #if 0
288 printk("codec trigger!!! - what = %i, enable = %i, status = 0x%x\n", what, enable, inb(AD1848P(card, STATUS)));
289 #endif
290 spin_lock(&chip->reg_lock);
291 if (cmd == SNDRV_PCM_TRIGGER_START) {
292 if (chip->image[AD1848_IFACE_CTRL] & what) {
293 spin_unlock(&chip->reg_lock);
294 return 0;
296 snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL] |= what);
297 chip->mode |= AD1848_MODE_RUNNING;
298 } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
299 if (!(chip->image[AD1848_IFACE_CTRL] & what)) {
300 spin_unlock(&chip->reg_lock);
301 return 0;
303 snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL] &= ~what);
304 chip->mode &= ~AD1848_MODE_RUNNING;
305 } else {
306 result = -EINVAL;
308 spin_unlock(&chip->reg_lock);
309 return result;
313 * CODEC I/O
316 static unsigned char snd_ad1848_get_rate(unsigned int rate)
318 int i;
320 for (i = 0; i < 14; i++)
321 if (rate == rates[i])
322 return freq_bits[i];
323 snd_BUG();
324 return freq_bits[13];
327 static int snd_ad1848_ioctl(snd_pcm_substream_t * substream,
328 unsigned int cmd, void *arg)
330 return snd_pcm_lib_ioctl(substream, cmd, arg);
333 static unsigned char snd_ad1848_get_format(int format, int channels)
335 unsigned char rformat;
337 rformat = AD1848_LINEAR_8;
338 switch (format) {
339 case SNDRV_PCM_FORMAT_A_LAW: rformat = AD1848_ALAW_8; break;
340 case SNDRV_PCM_FORMAT_MU_LAW: rformat = AD1848_ULAW_8; break;
341 case SNDRV_PCM_FORMAT_S16_LE: rformat = AD1848_LINEAR_16; break;
343 if (channels > 1)
344 rformat |= AD1848_STEREO;
345 #if 0
346 snd_printk("get_format: 0x%x (mode=0x%x)\n", format, mode);
347 #endif
348 return rformat;
351 static void snd_ad1848_calibrate_mute(ad1848_t *chip, int mute)
353 unsigned long flags;
355 mute = mute ? 1 : 0;
356 spin_lock_irqsave(&chip->reg_lock, flags);
357 if (chip->calibrate_mute == mute) {
358 spin_unlock_irqrestore(&chip->reg_lock, flags);
359 return;
361 if (!mute) {
362 snd_ad1848_dout(chip, AD1848_LEFT_INPUT, chip->image[AD1848_LEFT_INPUT]);
363 snd_ad1848_dout(chip, AD1848_RIGHT_INPUT, chip->image[AD1848_RIGHT_INPUT]);
365 snd_ad1848_dout(chip, AD1848_AUX1_LEFT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX1_LEFT_INPUT]);
366 snd_ad1848_dout(chip, AD1848_AUX1_RIGHT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX1_RIGHT_INPUT]);
367 snd_ad1848_dout(chip, AD1848_AUX2_LEFT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX2_LEFT_INPUT]);
368 snd_ad1848_dout(chip, AD1848_AUX2_RIGHT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX2_RIGHT_INPUT]);
369 snd_ad1848_dout(chip, AD1848_LEFT_OUTPUT, mute ? 0x80 : chip->image[AD1848_LEFT_OUTPUT]);
370 snd_ad1848_dout(chip, AD1848_RIGHT_OUTPUT, mute ? 0x80 : chip->image[AD1848_RIGHT_OUTPUT]);
371 chip->calibrate_mute = mute;
372 spin_unlock_irqrestore(&chip->reg_lock, flags);
375 static void snd_ad1848_set_data_format(ad1848_t *chip, snd_pcm_hw_params_t *hw_params)
377 if (hw_params == NULL) {
378 chip->image[AD1848_DATA_FORMAT] = 0x20;
379 } else {
380 chip->image[AD1848_DATA_FORMAT] =
381 snd_ad1848_get_format(params_format(hw_params), params_channels(hw_params)) |
382 snd_ad1848_get_rate(params_rate(hw_params));
384 // snd_printk(">>> pmode = 0x%x, dfr = 0x%x\n", pstr->mode, chip->image[AD1848_DATA_FORMAT]);
387 static int snd_ad1848_open(ad1848_t *chip, unsigned int mode)
389 unsigned long flags;
391 down(&chip->open_mutex);
392 if (chip->mode & AD1848_MODE_OPEN) {
393 up(&chip->open_mutex);
394 return -EAGAIN;
396 snd_ad1848_mce_down(chip);
398 #ifdef SNDRV_DEBUG_MCE
399 snd_printk("open: (1)\n");
400 #endif
401 snd_ad1848_mce_up(chip);
402 spin_lock_irqsave(&chip->reg_lock, flags);
403 chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO |
404 AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO |
405 AD1848_CALIB_MODE);
406 chip->image[AD1848_IFACE_CTRL] |= AD1848_AUTOCALIB;
407 snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL]);
408 spin_unlock_irqrestore(&chip->reg_lock, flags);
409 snd_ad1848_mce_down(chip);
411 #ifdef SNDRV_DEBUG_MCE
412 snd_printk("open: (2)\n");
413 #endif
415 snd_ad1848_set_data_format(chip, NULL);
417 snd_ad1848_mce_up(chip);
418 spin_lock_irqsave(&chip->reg_lock, flags);
419 snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]);
420 spin_unlock_irqrestore(&chip->reg_lock, flags);
421 snd_ad1848_mce_down(chip);
423 #ifdef SNDRV_DEBUG_MCE
424 snd_printk("open: (3)\n");
425 #endif
427 /* ok. now enable and ack CODEC IRQ */
428 spin_lock_irqsave(&chip->reg_lock, flags);
429 outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
430 outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
431 chip->image[AD1848_PIN_CTRL] |= AD1848_IRQ_ENABLE;
432 snd_ad1848_out(chip, AD1848_PIN_CTRL, chip->image[AD1848_PIN_CTRL]);
433 spin_unlock_irqrestore(&chip->reg_lock, flags);
435 chip->mode = mode;
436 up(&chip->open_mutex);
438 return 0;
441 static void snd_ad1848_close(ad1848_t *chip)
443 unsigned long flags;
445 down(&chip->open_mutex);
446 if (!chip->mode) {
447 up(&chip->open_mutex);
448 return;
450 /* disable IRQ */
451 spin_lock_irqsave(&chip->reg_lock, flags);
452 outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
453 outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
454 chip->image[AD1848_PIN_CTRL] &= ~AD1848_IRQ_ENABLE;
455 snd_ad1848_out(chip, AD1848_PIN_CTRL, chip->image[AD1848_PIN_CTRL]);
456 spin_unlock_irqrestore(&chip->reg_lock, flags);
458 /* now disable capture & playback */
460 snd_ad1848_mce_up(chip);
461 spin_lock_irqsave(&chip->reg_lock, flags);
462 chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO |
463 AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO);
464 snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL]);
465 spin_unlock_irqrestore(&chip->reg_lock, flags);
466 snd_ad1848_mce_down(chip);
468 /* clear IRQ again */
469 spin_lock_irqsave(&chip->reg_lock, flags);
470 outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
471 outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
472 spin_unlock_irqrestore(&chip->reg_lock, flags);
474 chip->mode = 0;
475 up(&chip->open_mutex);
479 * ok.. exported functions..
482 static int snd_ad1848_playback_trigger(snd_pcm_substream_t * substream,
483 int cmd)
485 ad1848_t *chip = snd_pcm_substream_chip(substream);
486 return snd_ad1848_trigger(chip, AD1848_PLAYBACK_ENABLE, SNDRV_PCM_STREAM_PLAYBACK, cmd);
489 static int snd_ad1848_capture_trigger(snd_pcm_substream_t * substream,
490 int cmd)
492 ad1848_t *chip = snd_pcm_substream_chip(substream);
493 return snd_ad1848_trigger(chip, AD1848_CAPTURE_ENABLE, SNDRV_PCM_STREAM_CAPTURE, cmd);
496 static int snd_ad1848_playback_hw_params(snd_pcm_substream_t * substream,
497 snd_pcm_hw_params_t * hw_params)
499 ad1848_t *chip = snd_pcm_substream_chip(substream);
500 unsigned long flags;
501 int err;
503 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
504 return err;
505 snd_ad1848_calibrate_mute(chip, 1);
506 snd_ad1848_set_data_format(chip, hw_params);
507 snd_ad1848_mce_up(chip);
508 spin_lock_irqsave(&chip->reg_lock, flags);
509 snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]);
510 spin_unlock_irqrestore(&chip->reg_lock, flags);
511 snd_ad1848_mce_down(chip);
512 snd_ad1848_calibrate_mute(chip, 0);
513 return 0;
516 static int snd_ad1848_playback_hw_free(snd_pcm_substream_t * substream)
518 return snd_pcm_lib_free_pages(substream);
521 static int snd_ad1848_playback_prepare(snd_pcm_substream_t * substream)
523 ad1848_t *chip = snd_pcm_substream_chip(substream);
524 snd_pcm_runtime_t *runtime = substream->runtime;
525 unsigned long flags;
526 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
527 unsigned int count = snd_pcm_lib_period_bytes(substream);
529 chip->dma_size = size;
530 chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO);
531 snd_dma_program(chip->dma, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
532 count = snd_ad1848_get_count(chip->image[AD1848_DATA_FORMAT], count) - 1;
533 spin_lock_irqsave(&chip->reg_lock, flags);
534 snd_ad1848_out(chip, AD1848_DATA_LWR_CNT, (unsigned char) count);
535 snd_ad1848_out(chip, AD1848_DATA_UPR_CNT, (unsigned char) (count >> 8));
536 spin_unlock_irqrestore(&chip->reg_lock, flags);
537 return 0;
540 static int snd_ad1848_capture_hw_params(snd_pcm_substream_t * substream,
541 snd_pcm_hw_params_t * hw_params)
543 ad1848_t *chip = snd_pcm_substream_chip(substream);
544 unsigned long flags;
545 int err;
547 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
548 return err;
549 snd_ad1848_calibrate_mute(chip, 1);
550 snd_ad1848_set_data_format(chip, hw_params);
551 snd_ad1848_mce_up(chip);
552 spin_lock_irqsave(&chip->reg_lock, flags);
553 snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]);
554 spin_unlock_irqrestore(&chip->reg_lock, flags);
555 snd_ad1848_mce_down(chip);
556 snd_ad1848_calibrate_mute(chip, 0);
557 return 0;
560 static int snd_ad1848_capture_hw_free(snd_pcm_substream_t * substream)
562 return snd_pcm_lib_free_pages(substream);
565 static int snd_ad1848_capture_prepare(snd_pcm_substream_t * substream)
567 ad1848_t *chip = snd_pcm_substream_chip(substream);
568 snd_pcm_runtime_t *runtime = substream->runtime;
569 unsigned long flags;
570 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
571 unsigned int count = snd_pcm_lib_period_bytes(substream);
573 chip->dma_size = size;
574 chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO);
575 snd_dma_program(chip->dma, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
576 count = snd_ad1848_get_count(chip->image[AD1848_DATA_FORMAT], count) - 1;
577 spin_lock_irqsave(&chip->reg_lock, flags);
578 snd_ad1848_out(chip, AD1848_DATA_LWR_CNT, (unsigned char) count);
579 snd_ad1848_out(chip, AD1848_DATA_UPR_CNT, (unsigned char) (count >> 8));
580 spin_unlock_irqrestore(&chip->reg_lock, flags);
581 return 0;
584 static irqreturn_t snd_ad1848_interrupt(int irq, void *dev_id, struct pt_regs *regs)
586 ad1848_t *chip = dev_id;
588 if ((chip->mode & AD1848_MODE_PLAY) && chip->playback_substream &&
589 (chip->mode & AD1848_MODE_RUNNING))
590 snd_pcm_period_elapsed(chip->playback_substream);
591 if ((chip->mode & AD1848_MODE_CAPTURE) && chip->capture_substream &&
592 (chip->mode & AD1848_MODE_RUNNING))
593 snd_pcm_period_elapsed(chip->capture_substream);
594 outb(0, AD1848P(chip, STATUS)); /* clear global interrupt bit */
595 return IRQ_HANDLED;
598 static snd_pcm_uframes_t snd_ad1848_playback_pointer(snd_pcm_substream_t * substream)
600 ad1848_t *chip = snd_pcm_substream_chip(substream);
601 size_t ptr;
603 if (!(chip->image[AD1848_IFACE_CTRL] & AD1848_PLAYBACK_ENABLE))
604 return 0;
605 ptr = snd_dma_pointer(chip->dma, chip->dma_size);
606 return bytes_to_frames(substream->runtime, ptr);
609 static snd_pcm_uframes_t snd_ad1848_capture_pointer(snd_pcm_substream_t * substream)
611 ad1848_t *chip = snd_pcm_substream_chip(substream);
612 size_t ptr;
614 if (!(chip->image[AD1848_IFACE_CTRL] & AD1848_CAPTURE_ENABLE))
615 return 0;
616 ptr = snd_dma_pointer(chip->dma, chip->dma_size);
617 return bytes_to_frames(substream->runtime, ptr);
624 static void snd_ad1848_thinkpad_twiddle(ad1848_t *chip, int on) {
626 int tmp;
628 if (!chip->thinkpad_flag) return;
630 outb(0x1c, AD1848_THINKPAD_CTL_PORT1);
631 tmp = inb(AD1848_THINKPAD_CTL_PORT2);
633 if (on)
634 /* turn it on */
635 tmp |= AD1848_THINKPAD_CS4248_ENABLE_BIT;
636 else
637 /* turn it off */
638 tmp &= ~AD1848_THINKPAD_CS4248_ENABLE_BIT;
640 outb(tmp, AD1848_THINKPAD_CTL_PORT2);
644 #ifdef CONFIG_PM
645 static int snd_ad1848_suspend(snd_card_t *card, pm_message_t state)
647 ad1848_t *chip = card->pm_private_data;
649 snd_pcm_suspend_all(chip->pcm);
650 /* FIXME: save registers? */
652 if (chip->thinkpad_flag)
653 snd_ad1848_thinkpad_twiddle(chip, 0);
655 return 0;
658 static int snd_ad1848_resume(snd_card_t *card)
660 ad1848_t *chip = card->pm_private_data;
662 if (chip->thinkpad_flag)
663 snd_ad1848_thinkpad_twiddle(chip, 1);
665 /* FIXME: restore registers? */
667 return 0;
669 #endif /* CONFIG_PM */
671 static int snd_ad1848_probe(ad1848_t * chip)
673 unsigned long flags;
674 int i, id, rev, ad1847;
675 unsigned char *ptr;
677 #if 0
678 snd_ad1848_debug(chip);
679 #endif
680 id = ad1847 = 0;
681 for (i = 0; i < 1000; i++) {
682 mb();
683 if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT)
684 udelay(500);
685 else {
686 spin_lock_irqsave(&chip->reg_lock, flags);
687 snd_ad1848_out(chip, AD1848_MISC_INFO, 0x00);
688 snd_ad1848_out(chip, AD1848_LEFT_INPUT, 0xaa);
689 snd_ad1848_out(chip, AD1848_RIGHT_INPUT, 0x45);
690 rev = snd_ad1848_in(chip, AD1848_RIGHT_INPUT);
691 if (rev == 0x65) {
692 spin_unlock_irqrestore(&chip->reg_lock, flags);
693 id = 1;
694 ad1847 = 1;
695 break;
697 if (snd_ad1848_in(chip, AD1848_LEFT_INPUT) == 0xaa && rev == 0x45) {
698 spin_unlock_irqrestore(&chip->reg_lock, flags);
699 id = 1;
700 break;
702 spin_unlock_irqrestore(&chip->reg_lock, flags);
705 if (id != 1)
706 return -ENODEV; /* no valid device found */
707 if (chip->hardware == AD1848_HW_DETECT) {
708 if (ad1847) {
709 chip->hardware = AD1848_HW_AD1847;
710 } else {
711 chip->hardware = AD1848_HW_AD1848;
712 rev = snd_ad1848_in(chip, AD1848_MISC_INFO);
713 if (rev & 0x80) {
714 chip->hardware = AD1848_HW_CS4248;
715 } else if ((rev & 0x0f) == 0x0a) {
716 snd_ad1848_out(chip, AD1848_MISC_INFO, 0x40);
717 for (i = 0; i < 16; ++i) {
718 if (snd_ad1848_in(chip, i) != snd_ad1848_in(chip, i + 16)) {
719 chip->hardware = AD1848_HW_CMI8330;
720 break;
723 snd_ad1848_out(chip, AD1848_MISC_INFO, 0x00);
727 spin_lock_irqsave(&chip->reg_lock, flags);
728 inb(AD1848P(chip, STATUS)); /* clear any pendings IRQ */
729 outb(0, AD1848P(chip, STATUS));
730 mb();
731 spin_unlock_irqrestore(&chip->reg_lock, flags);
733 chip->image[AD1848_MISC_INFO] = 0x00;
734 chip->image[AD1848_IFACE_CTRL] =
735 (chip->image[AD1848_IFACE_CTRL] & ~AD1848_SINGLE_DMA) | AD1848_SINGLE_DMA;
736 ptr = (unsigned char *) &chip->image;
737 snd_ad1848_mce_down(chip);
738 spin_lock_irqsave(&chip->reg_lock, flags);
739 for (i = 0; i < 16; i++) /* ok.. fill all AD1848 registers */
740 snd_ad1848_out(chip, i, *ptr++);
741 spin_unlock_irqrestore(&chip->reg_lock, flags);
742 snd_ad1848_mce_up(chip);
743 snd_ad1848_mce_down(chip);
744 return 0; /* all things are ok.. */
751 static snd_pcm_hardware_t snd_ad1848_playback =
753 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
754 SNDRV_PCM_INFO_MMAP_VALID),
755 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
756 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE),
757 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
758 .rate_min = 5510,
759 .rate_max = 48000,
760 .channels_min = 1,
761 .channels_max = 2,
762 .buffer_bytes_max = (128*1024),
763 .period_bytes_min = 64,
764 .period_bytes_max = (128*1024),
765 .periods_min = 1,
766 .periods_max = 1024,
767 .fifo_size = 0,
770 static snd_pcm_hardware_t snd_ad1848_capture =
772 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
773 SNDRV_PCM_INFO_MMAP_VALID),
774 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
775 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE),
776 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
777 .rate_min = 5510,
778 .rate_max = 48000,
779 .channels_min = 1,
780 .channels_max = 2,
781 .buffer_bytes_max = (128*1024),
782 .period_bytes_min = 64,
783 .period_bytes_max = (128*1024),
784 .periods_min = 1,
785 .periods_max = 1024,
786 .fifo_size = 0,
793 static int snd_ad1848_playback_open(snd_pcm_substream_t * substream)
795 ad1848_t *chip = snd_pcm_substream_chip(substream);
796 snd_pcm_runtime_t *runtime = substream->runtime;
797 int err;
799 if ((err = snd_ad1848_open(chip, AD1848_MODE_PLAY)) < 0)
800 return err;
801 chip->playback_substream = substream;
802 runtime->hw = snd_ad1848_playback;
803 snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.buffer_bytes_max);
804 snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.period_bytes_max);
805 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
806 return 0;
809 static int snd_ad1848_capture_open(snd_pcm_substream_t * substream)
811 ad1848_t *chip = snd_pcm_substream_chip(substream);
812 snd_pcm_runtime_t *runtime = substream->runtime;
813 int err;
815 if ((err = snd_ad1848_open(chip, AD1848_MODE_CAPTURE)) < 0)
816 return err;
817 chip->capture_substream = substream;
818 runtime->hw = snd_ad1848_capture;
819 snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.buffer_bytes_max);
820 snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.period_bytes_max);
821 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
822 return 0;
825 static int snd_ad1848_playback_close(snd_pcm_substream_t * substream)
827 ad1848_t *chip = snd_pcm_substream_chip(substream);
829 chip->mode &= ~AD1848_MODE_PLAY;
830 chip->playback_substream = NULL;
831 snd_ad1848_close(chip);
832 return 0;
835 static int snd_ad1848_capture_close(snd_pcm_substream_t * substream)
837 ad1848_t *chip = snd_pcm_substream_chip(substream);
839 chip->mode &= ~AD1848_MODE_CAPTURE;
840 chip->capture_substream = NULL;
841 snd_ad1848_close(chip);
842 return 0;
845 static int snd_ad1848_free(ad1848_t *chip)
847 release_and_free_resource(chip->res_port);
848 if (chip->irq >= 0)
849 free_irq(chip->irq, (void *) chip);
850 if (chip->dma >= 0) {
851 snd_dma_disable(chip->dma);
852 free_dma(chip->dma);
854 kfree(chip);
855 return 0;
858 static int snd_ad1848_dev_free(snd_device_t *device)
860 ad1848_t *chip = device->device_data;
861 return snd_ad1848_free(chip);
864 static const char *snd_ad1848_chip_id(ad1848_t *chip)
866 switch (chip->hardware) {
867 case AD1848_HW_AD1847: return "AD1847";
868 case AD1848_HW_AD1848: return "AD1848";
869 case AD1848_HW_CS4248: return "CS4248";
870 case AD1848_HW_CMI8330: return "CMI8330/C3D";
871 default: return "???";
875 int snd_ad1848_create(snd_card_t * card,
876 unsigned long port,
877 int irq, int dma,
878 unsigned short hardware,
879 ad1848_t ** rchip)
881 static snd_device_ops_t ops = {
882 .dev_free = snd_ad1848_dev_free,
884 ad1848_t *chip;
885 int err;
887 *rchip = NULL;
888 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
889 if (chip == NULL)
890 return -ENOMEM;
891 spin_lock_init(&chip->reg_lock);
892 init_MUTEX(&chip->open_mutex);
893 chip->card = card;
894 chip->port = port;
895 chip->irq = -1;
896 chip->dma = -1;
897 chip->hardware = hardware;
898 memcpy(&chip->image, &snd_ad1848_original_image, sizeof(snd_ad1848_original_image));
900 if ((chip->res_port = request_region(port, 4, "AD1848")) == NULL) {
901 snd_printk(KERN_ERR "ad1848: can't grab port 0x%lx\n", port);
902 snd_ad1848_free(chip);
903 return -EBUSY;
905 if (request_irq(irq, snd_ad1848_interrupt, SA_INTERRUPT, "AD1848", (void *) chip)) {
906 snd_printk(KERN_ERR "ad1848: can't grab IRQ %d\n", irq);
907 snd_ad1848_free(chip);
908 return -EBUSY;
910 chip->irq = irq;
911 if (request_dma(dma, "AD1848")) {
912 snd_printk(KERN_ERR "ad1848: can't grab DMA %d\n", dma);
913 snd_ad1848_free(chip);
914 return -EBUSY;
916 chip->dma = dma;
918 if (hardware == AD1848_HW_THINKPAD) {
919 chip->thinkpad_flag = 1;
920 chip->hardware = AD1848_HW_DETECT; /* reset */
921 snd_ad1848_thinkpad_twiddle(chip, 1);
922 snd_card_set_isa_pm_callback(card, snd_ad1848_suspend, snd_ad1848_resume, chip);
925 if (snd_ad1848_probe(chip) < 0) {
926 snd_ad1848_free(chip);
927 return -ENODEV;
930 /* Register device */
931 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
932 snd_ad1848_free(chip);
933 return err;
936 *rchip = chip;
937 return 0;
940 static snd_pcm_ops_t snd_ad1848_playback_ops = {
941 .open = snd_ad1848_playback_open,
942 .close = snd_ad1848_playback_close,
943 .ioctl = snd_ad1848_ioctl,
944 .hw_params = snd_ad1848_playback_hw_params,
945 .hw_free = snd_ad1848_playback_hw_free,
946 .prepare = snd_ad1848_playback_prepare,
947 .trigger = snd_ad1848_playback_trigger,
948 .pointer = snd_ad1848_playback_pointer,
951 static snd_pcm_ops_t snd_ad1848_capture_ops = {
952 .open = snd_ad1848_capture_open,
953 .close = snd_ad1848_capture_close,
954 .ioctl = snd_ad1848_ioctl,
955 .hw_params = snd_ad1848_capture_hw_params,
956 .hw_free = snd_ad1848_capture_hw_free,
957 .prepare = snd_ad1848_capture_prepare,
958 .trigger = snd_ad1848_capture_trigger,
959 .pointer = snd_ad1848_capture_pointer,
962 static void snd_ad1848_pcm_free(snd_pcm_t *pcm)
964 ad1848_t *chip = pcm->private_data;
965 chip->pcm = NULL;
966 snd_pcm_lib_preallocate_free_for_all(pcm);
969 int snd_ad1848_pcm(ad1848_t *chip, int device, snd_pcm_t **rpcm)
971 snd_pcm_t *pcm;
972 int err;
974 if ((err = snd_pcm_new(chip->card, "AD1848", device, 1, 1, &pcm)) < 0)
975 return err;
977 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ad1848_playback_ops);
978 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ad1848_capture_ops);
980 pcm->private_free = snd_ad1848_pcm_free;
981 pcm->private_data = chip;
982 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
983 strcpy(pcm->name, snd_ad1848_chip_id(chip));
985 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
986 snd_dma_isa_data(),
987 64*1024, chip->dma > 3 ? 128*1024 : 64*1024);
989 chip->pcm = pcm;
990 if (rpcm)
991 *rpcm = pcm;
992 return 0;
995 const snd_pcm_ops_t *snd_ad1848_get_pcm_ops(int direction)
997 return direction == SNDRV_PCM_STREAM_PLAYBACK ?
998 &snd_ad1848_playback_ops : &snd_ad1848_capture_ops;
1002 * MIXER part
1005 static int snd_ad1848_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1007 static char *texts[4] = {
1008 "Line", "Aux", "Mic", "Mix"
1011 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1012 uinfo->count = 2;
1013 uinfo->value.enumerated.items = 4;
1014 if (uinfo->value.enumerated.item > 3)
1015 uinfo->value.enumerated.item = 3;
1016 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1017 return 0;
1020 static int snd_ad1848_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1022 ad1848_t *chip = snd_kcontrol_chip(kcontrol);
1023 unsigned long flags;
1025 spin_lock_irqsave(&chip->reg_lock, flags);
1026 ucontrol->value.enumerated.item[0] = (chip->image[AD1848_LEFT_INPUT] & AD1848_MIXS_ALL) >> 6;
1027 ucontrol->value.enumerated.item[1] = (chip->image[AD1848_RIGHT_INPUT] & AD1848_MIXS_ALL) >> 6;
1028 spin_unlock_irqrestore(&chip->reg_lock, flags);
1029 return 0;
1032 static int snd_ad1848_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1034 ad1848_t *chip = snd_kcontrol_chip(kcontrol);
1035 unsigned long flags;
1036 unsigned short left, right;
1037 int change;
1039 if (ucontrol->value.enumerated.item[0] > 3 ||
1040 ucontrol->value.enumerated.item[1] > 3)
1041 return -EINVAL;
1042 left = ucontrol->value.enumerated.item[0] << 6;
1043 right = ucontrol->value.enumerated.item[1] << 6;
1044 spin_lock_irqsave(&chip->reg_lock, flags);
1045 left = (chip->image[AD1848_LEFT_INPUT] & ~AD1848_MIXS_ALL) | left;
1046 right = (chip->image[AD1848_RIGHT_INPUT] & ~AD1848_MIXS_ALL) | right;
1047 change = left != chip->image[AD1848_LEFT_INPUT] ||
1048 right != chip->image[AD1848_RIGHT_INPUT];
1049 snd_ad1848_out(chip, AD1848_LEFT_INPUT, left);
1050 snd_ad1848_out(chip, AD1848_RIGHT_INPUT, right);
1051 spin_unlock_irqrestore(&chip->reg_lock, flags);
1052 return change;
1055 static int snd_ad1848_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1057 int mask = (kcontrol->private_value >> 16) & 0xff;
1059 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1060 uinfo->count = 1;
1061 uinfo->value.integer.min = 0;
1062 uinfo->value.integer.max = mask;
1063 return 0;
1066 static int snd_ad1848_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1068 ad1848_t *chip = snd_kcontrol_chip(kcontrol);
1069 unsigned long flags;
1070 int reg = kcontrol->private_value & 0xff;
1071 int shift = (kcontrol->private_value >> 8) & 0xff;
1072 int mask = (kcontrol->private_value >> 16) & 0xff;
1073 int invert = (kcontrol->private_value >> 24) & 0xff;
1075 spin_lock_irqsave(&chip->reg_lock, flags);
1076 ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
1077 spin_unlock_irqrestore(&chip->reg_lock, flags);
1078 if (invert)
1079 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1080 return 0;
1083 static int snd_ad1848_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1085 ad1848_t *chip = snd_kcontrol_chip(kcontrol);
1086 unsigned long flags;
1087 int reg = kcontrol->private_value & 0xff;
1088 int shift = (kcontrol->private_value >> 8) & 0xff;
1089 int mask = (kcontrol->private_value >> 16) & 0xff;
1090 int invert = (kcontrol->private_value >> 24) & 0xff;
1091 int change;
1092 unsigned short val;
1094 val = (ucontrol->value.integer.value[0] & mask);
1095 if (invert)
1096 val = mask - val;
1097 val <<= shift;
1098 spin_lock_irqsave(&chip->reg_lock, flags);
1099 val = (chip->image[reg] & ~(mask << shift)) | val;
1100 change = val != chip->image[reg];
1101 snd_ad1848_out(chip, reg, val);
1102 spin_unlock_irqrestore(&chip->reg_lock, flags);
1103 return change;
1106 static int snd_ad1848_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1108 int mask = (kcontrol->private_value >> 24) & 0xff;
1110 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1111 uinfo->count = 2;
1112 uinfo->value.integer.min = 0;
1113 uinfo->value.integer.max = mask;
1114 return 0;
1117 static int snd_ad1848_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1119 ad1848_t *chip = snd_kcontrol_chip(kcontrol);
1120 unsigned long flags;
1121 int left_reg = kcontrol->private_value & 0xff;
1122 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1123 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1124 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1125 int mask = (kcontrol->private_value >> 24) & 0xff;
1126 int invert = (kcontrol->private_value >> 22) & 1;
1128 spin_lock_irqsave(&chip->reg_lock, flags);
1129 ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
1130 ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
1131 spin_unlock_irqrestore(&chip->reg_lock, flags);
1132 if (invert) {
1133 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1134 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1136 return 0;
1139 static int snd_ad1848_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1141 ad1848_t *chip = snd_kcontrol_chip(kcontrol);
1142 unsigned long flags;
1143 int left_reg = kcontrol->private_value & 0xff;
1144 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1145 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1146 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1147 int mask = (kcontrol->private_value >> 24) & 0xff;
1148 int invert = (kcontrol->private_value >> 22) & 1;
1149 int change;
1150 unsigned short val1, val2;
1152 val1 = ucontrol->value.integer.value[0] & mask;
1153 val2 = ucontrol->value.integer.value[1] & mask;
1154 if (invert) {
1155 val1 = mask - val1;
1156 val2 = mask - val2;
1158 val1 <<= shift_left;
1159 val2 <<= shift_right;
1160 spin_lock_irqsave(&chip->reg_lock, flags);
1161 if (left_reg != right_reg) {
1162 val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
1163 val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
1164 change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
1165 snd_ad1848_out(chip, left_reg, val1);
1166 snd_ad1848_out(chip, right_reg, val2);
1167 } else {
1168 val1 = (chip->image[left_reg] & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1169 change = val1 != chip->image[left_reg];
1170 snd_ad1848_out(chip, left_reg, val1);
1172 spin_unlock_irqrestore(&chip->reg_lock, flags);
1173 return change;
1178 int snd_ad1848_add_ctl(ad1848_t *chip, const char *name, int index, int type, unsigned long value)
1180 static snd_kcontrol_new_t newctls[] = {
1181 [AD1848_MIX_SINGLE] = {
1182 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1183 .info = snd_ad1848_info_single,
1184 .get = snd_ad1848_get_single,
1185 .put = snd_ad1848_put_single,
1187 [AD1848_MIX_DOUBLE] = {
1188 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1189 .info = snd_ad1848_info_double,
1190 .get = snd_ad1848_get_double,
1191 .put = snd_ad1848_put_double,
1193 [AD1848_MIX_CAPTURE] = {
1194 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1195 .info = snd_ad1848_info_mux,
1196 .get = snd_ad1848_get_mux,
1197 .put = snd_ad1848_put_mux,
1200 snd_kcontrol_t *ctl;
1201 int err;
1203 ctl = snd_ctl_new1(&newctls[type], chip);
1204 if (! ctl)
1205 return -ENOMEM;
1206 strlcpy(ctl->id.name, name, sizeof(ctl->id.name));
1207 ctl->id.index = index;
1208 ctl->private_value = value;
1209 if ((err = snd_ctl_add(chip->card, ctl)) < 0) {
1210 snd_ctl_free_one(ctl);
1211 return err;
1213 return 0;
1217 static struct ad1848_mix_elem snd_ad1848_controls[] = {
1218 AD1848_DOUBLE("PCM Playback Switch", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 7, 7, 1, 1),
1219 AD1848_DOUBLE("PCM Playback Volume", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 0, 0, 63, 1),
1220 AD1848_DOUBLE("Aux Playback Switch", 0, AD1848_AUX1_LEFT_INPUT, AD1848_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
1221 AD1848_DOUBLE("Aux Playback Volume", 0, AD1848_AUX1_LEFT_INPUT, AD1848_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
1222 AD1848_DOUBLE("Aux Playback Switch", 1, AD1848_AUX2_LEFT_INPUT, AD1848_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
1223 AD1848_DOUBLE("Aux Playback Volume", 1, AD1848_AUX2_LEFT_INPUT, AD1848_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
1224 AD1848_DOUBLE("Capture Volume", 0, AD1848_LEFT_INPUT, AD1848_RIGHT_INPUT, 0, 0, 15, 0),
1226 .name = "Capture Source",
1227 .type = AD1848_MIX_CAPTURE,
1229 AD1848_SINGLE("Loopback Capture Switch", 0, AD1848_LOOPBACK, 0, 1, 0),
1230 AD1848_SINGLE("Loopback Capture Volume", 0, AD1848_LOOPBACK, 1, 63, 0)
1233 int snd_ad1848_mixer(ad1848_t *chip)
1235 snd_card_t *card;
1236 snd_pcm_t *pcm;
1237 unsigned int idx;
1238 int err;
1240 snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
1242 pcm = chip->pcm;
1243 card = chip->card;
1245 strcpy(card->mixername, pcm->name);
1247 for (idx = 0; idx < ARRAY_SIZE(snd_ad1848_controls); idx++)
1248 if ((err = snd_ad1848_add_ctl_elem(chip, &snd_ad1848_controls[idx])) < 0)
1249 return err;
1251 return 0;
1254 EXPORT_SYMBOL(snd_ad1848_out);
1255 EXPORT_SYMBOL(snd_ad1848_create);
1256 EXPORT_SYMBOL(snd_ad1848_pcm);
1257 EXPORT_SYMBOL(snd_ad1848_get_pcm_ops);
1258 EXPORT_SYMBOL(snd_ad1848_mixer);
1259 EXPORT_SYMBOL(snd_ad1848_add_ctl);
1262 * INIT part
1265 static int __init alsa_ad1848_init(void)
1267 return 0;
1270 static void __exit alsa_ad1848_exit(void)
1274 module_init(alsa_ad1848_init)
1275 module_exit(alsa_ad1848_exit)