2 * Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/slab.h>
27 #include <linux/ioport.h>
28 #include <sound/core.h>
29 #include <sound/ad1848.h>
30 #include <sound/control.h>
31 #include <sound/tlv.h>
32 #include <sound/pcm_params.h>
37 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
38 MODULE_DESCRIPTION("Routines for control of AD1848/AD1847/CS4248");
39 MODULE_LICENSE("GPL");
42 #define SNDRV_DEBUG_MCE
49 static unsigned char freq_bits[14] = {
50 /* 5510 */ 0x00 | AD1848_XTAL2,
51 /* 6620 */ 0x0E | AD1848_XTAL2,
52 /* 8000 */ 0x00 | AD1848_XTAL1,
53 /* 9600 */ 0x0E | AD1848_XTAL1,
54 /* 11025 */ 0x02 | AD1848_XTAL2,
55 /* 16000 */ 0x02 | AD1848_XTAL1,
56 /* 18900 */ 0x04 | AD1848_XTAL2,
57 /* 22050 */ 0x06 | AD1848_XTAL2,
58 /* 27042 */ 0x04 | AD1848_XTAL1,
59 /* 32000 */ 0x06 | AD1848_XTAL1,
60 /* 33075 */ 0x0C | AD1848_XTAL2,
61 /* 37800 */ 0x08 | AD1848_XTAL2,
62 /* 44100 */ 0x0A | AD1848_XTAL2,
63 /* 48000 */ 0x0C | AD1848_XTAL1
66 static unsigned int rates[14] = {
67 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
68 27042, 32000, 33075, 37800, 44100, 48000
71 static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
72 .count = ARRAY_SIZE(rates),
77 static unsigned char snd_ad1848_original_image[16] =
81 0x9f, /* 02 - la1ic */
82 0x9f, /* 03 - ra1ic */
83 0x9f, /* 04 - la2ic */
84 0x9f, /* 05 - ra2ic */
88 AD1848_AUTOCALIB, /* 09 - ic */
101 static void snd_ad1848_wait(struct snd_wss *chip)
105 for (timeout = 250; timeout > 0; timeout--) {
106 if ((inb(chip->port + CS4231P(REGSEL)) & AD1848_INIT) == 0)
112 void snd_ad1848_out(struct snd_wss *chip,
116 snd_ad1848_wait(chip);
117 #ifdef CONFIG_SND_DEBUG
118 if (inb(chip->port + CS4231P(REGSEL)) & AD1848_INIT)
119 snd_printk(KERN_WARNING "auto calibration time out - "
120 "reg = 0x%x, value = 0x%x\n", reg, value);
122 outb(chip->mce_bit | reg, chip->port + CS4231P(REGSEL));
123 outb(chip->image[reg] = value, chip->port + CS4231P(REG));
125 snd_printdd("codec out - reg 0x%x = 0x%x\n",
126 chip->mce_bit | reg, value);
129 EXPORT_SYMBOL(snd_ad1848_out);
131 static void snd_ad1848_dout(struct snd_wss *chip,
132 unsigned char reg, unsigned char value)
134 snd_ad1848_wait(chip);
135 outb(chip->mce_bit | reg, chip->port + CS4231P(REGSEL));
136 outb(value, chip->port + CS4231P(REG));
140 static unsigned char snd_ad1848_in(struct snd_wss *chip, unsigned char reg)
142 snd_ad1848_wait(chip);
143 #ifdef CONFIG_SND_DEBUG
144 if (inb(chip->port + CS4231P(REGSEL)) & AD1848_INIT)
145 snd_printk(KERN_WARNING "auto calibration time out - "
146 "reg = 0x%x\n", reg);
148 outb(chip->mce_bit | reg, chip->port + CS4231P(REGSEL));
150 return inb(chip->port + CS4231P(REG));
155 static void snd_ad1848_debug(struct snd_wss *chip)
157 printk(KERN_DEBUG "AD1848 REGS: INDEX = 0x%02x ", inb(chip->port + CS4231P(REGSEL)));
158 printk(KERN_DEBUG " STATUS = 0x%02x\n", inb(chip->port + CS4231P(STATUS)));
159 printk(KERN_DEBUG " 0x00: left input = 0x%02x ", snd_ad1848_in(chip, 0x00));
160 printk(KERN_DEBUG " 0x08: playback format = 0x%02x\n", snd_ad1848_in(chip, 0x08));
161 printk(KERN_DEBUG " 0x01: right input = 0x%02x ", snd_ad1848_in(chip, 0x01));
162 printk(KERN_DEBUG " 0x09: iface (CFIG 1) = 0x%02x\n", snd_ad1848_in(chip, 0x09));
163 printk(KERN_DEBUG " 0x02: AUXA left = 0x%02x ", snd_ad1848_in(chip, 0x02));
164 printk(KERN_DEBUG " 0x0a: pin control = 0x%02x\n", snd_ad1848_in(chip, 0x0a));
165 printk(KERN_DEBUG " 0x03: AUXA right = 0x%02x ", snd_ad1848_in(chip, 0x03));
166 printk(KERN_DEBUG " 0x0b: init & status = 0x%02x\n", snd_ad1848_in(chip, 0x0b));
167 printk(KERN_DEBUG " 0x04: AUXB left = 0x%02x ", snd_ad1848_in(chip, 0x04));
168 printk(KERN_DEBUG " 0x0c: revision & mode = 0x%02x\n", snd_ad1848_in(chip, 0x0c));
169 printk(KERN_DEBUG " 0x05: AUXB right = 0x%02x ", snd_ad1848_in(chip, 0x05));
170 printk(KERN_DEBUG " 0x0d: loopback = 0x%02x\n", snd_ad1848_in(chip, 0x0d));
171 printk(KERN_DEBUG " 0x06: left output = 0x%02x ", snd_ad1848_in(chip, 0x06));
172 printk(KERN_DEBUG " 0x0e: data upr count = 0x%02x\n", snd_ad1848_in(chip, 0x0e));
173 printk(KERN_DEBUG " 0x07: right output = 0x%02x ", snd_ad1848_in(chip, 0x07));
174 printk(KERN_DEBUG " 0x0f: data lwr count = 0x%02x\n", snd_ad1848_in(chip, 0x0f));
180 * AD1848 detection / MCE routines
183 static void snd_ad1848_mce_up(struct snd_wss *chip)
188 snd_ad1848_wait(chip);
189 #ifdef CONFIG_SND_DEBUG
190 if (inb(chip->port + CS4231P(REGSEL)) & AD1848_INIT)
191 snd_printk(KERN_WARNING "mce_up - auto calibration time out (0)\n");
193 spin_lock_irqsave(&chip->reg_lock, flags);
194 chip->mce_bit |= AD1848_MCE;
195 timeout = inb(chip->port + CS4231P(REGSEL));
197 snd_printk(KERN_WARNING "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),
200 chip->port + CS4231P(REGSEL));
201 spin_unlock_irqrestore(&chip->reg_lock, flags);
204 static void snd_ad1848_mce_down(struct snd_wss *chip)
206 unsigned long flags, timeout;
209 spin_lock_irqsave(&chip->reg_lock, flags);
210 for (timeout = 5; timeout > 0; timeout--)
211 inb(chip->port + CS4231P(REGSEL));
212 /* end of cleanup sequence */
213 for (timeout = 12000;
214 timeout > 0 && (inb(chip->port + CS4231P(REGSEL)) & AD1848_INIT);
218 snd_printdd("(1) timeout = %ld\n", timeout);
220 #ifdef CONFIG_SND_DEBUG
221 if (inb(chip->port + CS4231P(REGSEL)) & AD1848_INIT)
222 snd_printk(KERN_WARNING
223 "mce_down [0x%lx] - auto calibration time out (0)\n",
224 chip->port + CS4231P(REGSEL));
227 chip->mce_bit &= ~AD1848_MCE;
228 reg = inb(chip->port + CS4231P(REGSEL));
229 outb(chip->mce_bit | (reg & 0x1f), chip->port + CS4231P(REGSEL));
231 snd_printk(KERN_WARNING "mce_down [0x%lx]: serious init problem - codec still busy\n", chip->port);
232 if ((reg & AD1848_MCE) == 0) {
233 spin_unlock_irqrestore(&chip->reg_lock, flags);
238 * Wait for auto-calibration (AC) process to finish, i.e. ACI to go low.
239 * It may take up to 5 sample periods (at most 907 us @ 5.5125 kHz) for
240 * the process to _start_, so it is important to wait at least that long
241 * before checking. Otherwise we might think AC has finished when it
242 * has in fact not begun. It could take 128 (no AC) or 384 (AC) cycles
243 * for ACI to drop. This gives a wait of at most 70 ms with a more
244 * typical value of 3-9 ms.
246 timeout = jiffies + msecs_to_jiffies(250);
248 spin_unlock_irqrestore(&chip->reg_lock, flags);
250 spin_lock_irqsave(&chip->reg_lock, flags);
251 reg = snd_ad1848_in(chip, AD1848_TEST_INIT) &
252 AD1848_CALIB_IN_PROGRESS;
253 } while (reg && time_before(jiffies, timeout));
254 spin_unlock_irqrestore(&chip->reg_lock, flags);
257 "mce_down - auto calibration time out (2)\n");
259 snd_printdd("(4) jiffies = %lu\n", jiffies);
260 snd_printd("mce_down - exit = 0x%x\n",
261 inb(chip->port + CS4231P(REGSEL)));
264 static unsigned int snd_ad1848_get_count(unsigned char format,
267 switch (format & 0xe0) {
268 case AD1848_LINEAR_16:
272 if (format & AD1848_STEREO)
277 static int snd_ad1848_trigger(struct snd_wss *chip, unsigned char what,
278 int channel, int cmd)
283 printk("codec trigger!!! - what = %i, enable = %i, status = 0x%x\n", what, enable, inb(AD1848P(card, STATUS)));
285 spin_lock(&chip->reg_lock);
286 if (cmd == SNDRV_PCM_TRIGGER_START) {
287 if (chip->image[AD1848_IFACE_CTRL] & what) {
288 spin_unlock(&chip->reg_lock);
291 snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL] |= what);
292 } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
293 if (!(chip->image[AD1848_IFACE_CTRL] & what)) {
294 spin_unlock(&chip->reg_lock);
297 snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL] &= ~what);
301 spin_unlock(&chip->reg_lock);
309 static unsigned char snd_ad1848_get_rate(unsigned int rate)
313 for (i = 0; i < ARRAY_SIZE(rates); i++)
314 if (rate == rates[i])
317 return freq_bits[ARRAY_SIZE(rates) - 1];
320 static int snd_ad1848_ioctl(struct snd_pcm_substream *substream,
321 unsigned int cmd, void *arg)
323 return snd_pcm_lib_ioctl(substream, cmd, arg);
326 static unsigned char snd_ad1848_get_format(int format, int channels)
328 unsigned char rformat;
330 rformat = AD1848_LINEAR_8;
332 case SNDRV_PCM_FORMAT_A_LAW: rformat = AD1848_ALAW_8; break;
333 case SNDRV_PCM_FORMAT_MU_LAW: rformat = AD1848_ULAW_8; break;
334 case SNDRV_PCM_FORMAT_S16_LE: rformat = AD1848_LINEAR_16; break;
337 rformat |= AD1848_STEREO;
339 snd_printk("get_format: 0x%x (mode=0x%x)\n", format, mode);
344 static void snd_ad1848_calibrate_mute(struct snd_wss *chip, int mute)
349 spin_lock_irqsave(&chip->reg_lock, flags);
350 if (chip->calibrate_mute == mute) {
351 spin_unlock_irqrestore(&chip->reg_lock, flags);
355 snd_ad1848_dout(chip, AD1848_LEFT_INPUT, chip->image[AD1848_LEFT_INPUT]);
356 snd_ad1848_dout(chip, AD1848_RIGHT_INPUT, chip->image[AD1848_RIGHT_INPUT]);
358 snd_ad1848_dout(chip, AD1848_AUX1_LEFT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX1_LEFT_INPUT]);
359 snd_ad1848_dout(chip, AD1848_AUX1_RIGHT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX1_RIGHT_INPUT]);
360 snd_ad1848_dout(chip, AD1848_AUX2_LEFT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX2_LEFT_INPUT]);
361 snd_ad1848_dout(chip, AD1848_AUX2_RIGHT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX2_RIGHT_INPUT]);
362 snd_ad1848_dout(chip, AD1848_LEFT_OUTPUT, mute ? 0x80 : chip->image[AD1848_LEFT_OUTPUT]);
363 snd_ad1848_dout(chip, AD1848_RIGHT_OUTPUT, mute ? 0x80 : chip->image[AD1848_RIGHT_OUTPUT]);
364 chip->calibrate_mute = mute;
365 spin_unlock_irqrestore(&chip->reg_lock, flags);
368 static void snd_ad1848_set_data_format(struct snd_wss *chip,
369 struct snd_pcm_hw_params *hw_params)
371 if (hw_params == NULL) {
372 chip->image[AD1848_DATA_FORMAT] = 0x20;
374 chip->image[AD1848_DATA_FORMAT] =
375 snd_ad1848_get_format(params_format(hw_params), params_channels(hw_params)) |
376 snd_ad1848_get_rate(params_rate(hw_params));
378 // snd_printk(">>> pmode = 0x%x, dfr = 0x%x\n", pstr->mode, chip->image[AD1848_DATA_FORMAT]);
381 static int snd_ad1848_open(struct snd_wss *chip, unsigned int mode)
385 if (chip->mode & WSS_MODE_OPEN)
388 snd_ad1848_mce_down(chip);
390 #ifdef SNDRV_DEBUG_MCE
391 snd_printk("open: (1)\n");
393 snd_ad1848_mce_up(chip);
394 spin_lock_irqsave(&chip->reg_lock, flags);
395 chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO |
396 AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO |
398 chip->image[AD1848_IFACE_CTRL] |= AD1848_AUTOCALIB;
399 snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL]);
400 spin_unlock_irqrestore(&chip->reg_lock, flags);
401 snd_ad1848_mce_down(chip);
403 #ifdef SNDRV_DEBUG_MCE
404 snd_printk("open: (2)\n");
407 snd_ad1848_set_data_format(chip, NULL);
409 snd_ad1848_mce_up(chip);
410 spin_lock_irqsave(&chip->reg_lock, flags);
411 snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]);
412 spin_unlock_irqrestore(&chip->reg_lock, flags);
413 snd_ad1848_mce_down(chip);
415 #ifdef SNDRV_DEBUG_MCE
416 snd_printk("open: (3)\n");
419 /* ok. now enable and ack CODEC IRQ */
420 spin_lock_irqsave(&chip->reg_lock, flags);
421 outb(0, chip->port + CS4231P(STATUS)); /* clear IRQ */
422 outb(0, chip->port + CS4231P(STATUS)); /* clear IRQ */
423 chip->image[AD1848_PIN_CTRL] |= AD1848_IRQ_ENABLE;
424 snd_ad1848_out(chip, AD1848_PIN_CTRL, chip->image[AD1848_PIN_CTRL]);
425 spin_unlock_irqrestore(&chip->reg_lock, flags);
432 static void snd_ad1848_close(struct snd_wss *chip)
439 spin_lock_irqsave(&chip->reg_lock, flags);
440 outb(0, chip->port + CS4231P(STATUS)); /* clear IRQ */
441 outb(0, chip->port + CS4231P(STATUS)); /* clear IRQ */
442 chip->image[AD1848_PIN_CTRL] &= ~AD1848_IRQ_ENABLE;
443 snd_ad1848_out(chip, AD1848_PIN_CTRL, chip->image[AD1848_PIN_CTRL]);
444 spin_unlock_irqrestore(&chip->reg_lock, flags);
446 /* now disable capture & playback */
448 snd_ad1848_mce_up(chip);
449 spin_lock_irqsave(&chip->reg_lock, flags);
450 chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO |
451 AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO);
452 snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL]);
453 spin_unlock_irqrestore(&chip->reg_lock, flags);
454 snd_ad1848_mce_down(chip);
456 /* clear IRQ again */
457 spin_lock_irqsave(&chip->reg_lock, flags);
458 outb(0, chip->port + CS4231P(STATUS)); /* clear IRQ */
459 outb(0, chip->port + CS4231P(STATUS)); /* clear IRQ */
460 spin_unlock_irqrestore(&chip->reg_lock, flags);
466 * ok.. exported functions..
469 static int snd_ad1848_playback_trigger(struct snd_pcm_substream *substream,
472 struct snd_wss *chip = snd_pcm_substream_chip(substream);
473 return snd_ad1848_trigger(chip, AD1848_PLAYBACK_ENABLE, SNDRV_PCM_STREAM_PLAYBACK, cmd);
476 static int snd_ad1848_capture_trigger(struct snd_pcm_substream *substream,
479 struct snd_wss *chip = snd_pcm_substream_chip(substream);
480 return snd_ad1848_trigger(chip, AD1848_CAPTURE_ENABLE, SNDRV_PCM_STREAM_CAPTURE, cmd);
483 static int snd_ad1848_playback_hw_params(struct snd_pcm_substream *substream,
484 struct snd_pcm_hw_params *hw_params)
486 struct snd_wss *chip = snd_pcm_substream_chip(substream);
490 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
492 snd_ad1848_calibrate_mute(chip, 1);
493 snd_ad1848_set_data_format(chip, hw_params);
494 snd_ad1848_mce_up(chip);
495 spin_lock_irqsave(&chip->reg_lock, flags);
496 snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]);
497 spin_unlock_irqrestore(&chip->reg_lock, flags);
498 snd_ad1848_mce_down(chip);
499 snd_ad1848_calibrate_mute(chip, 0);
503 static int snd_ad1848_playback_hw_free(struct snd_pcm_substream *substream)
505 return snd_pcm_lib_free_pages(substream);
508 static int snd_ad1848_playback_prepare(struct snd_pcm_substream *substream)
510 struct snd_wss *chip = snd_pcm_substream_chip(substream);
511 struct snd_pcm_runtime *runtime = substream->runtime;
513 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
514 unsigned int count = snd_pcm_lib_period_bytes(substream);
516 chip->p_dma_size = size;
517 chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO);
518 snd_dma_program(chip->dma1, runtime->dma_addr, size,
519 DMA_MODE_WRITE | DMA_AUTOINIT);
520 count = snd_ad1848_get_count(chip->image[AD1848_DATA_FORMAT], count) - 1;
521 spin_lock_irqsave(&chip->reg_lock, flags);
522 snd_ad1848_out(chip, AD1848_DATA_LWR_CNT, (unsigned char) count);
523 snd_ad1848_out(chip, AD1848_DATA_UPR_CNT, (unsigned char) (count >> 8));
524 spin_unlock_irqrestore(&chip->reg_lock, flags);
528 static int snd_ad1848_capture_hw_params(struct snd_pcm_substream *substream,
529 struct snd_pcm_hw_params *hw_params)
531 struct snd_wss *chip = snd_pcm_substream_chip(substream);
535 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
537 snd_ad1848_calibrate_mute(chip, 1);
538 snd_ad1848_set_data_format(chip, hw_params);
539 snd_ad1848_mce_up(chip);
540 spin_lock_irqsave(&chip->reg_lock, flags);
541 snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]);
542 spin_unlock_irqrestore(&chip->reg_lock, flags);
543 snd_ad1848_mce_down(chip);
544 snd_ad1848_calibrate_mute(chip, 0);
548 static int snd_ad1848_capture_hw_free(struct snd_pcm_substream *substream)
550 return snd_pcm_lib_free_pages(substream);
553 static int snd_ad1848_capture_prepare(struct snd_pcm_substream *substream)
555 struct snd_wss *chip = snd_pcm_substream_chip(substream);
556 struct snd_pcm_runtime *runtime = substream->runtime;
558 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
559 unsigned int count = snd_pcm_lib_period_bytes(substream);
561 chip->c_dma_size = size;
562 chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO);
563 snd_dma_program(chip->dma2, runtime->dma_addr, size,
564 DMA_MODE_READ | DMA_AUTOINIT);
565 count = snd_ad1848_get_count(chip->image[AD1848_DATA_FORMAT], count) - 1;
566 spin_lock_irqsave(&chip->reg_lock, flags);
567 snd_ad1848_out(chip, AD1848_DATA_LWR_CNT, (unsigned char) count);
568 snd_ad1848_out(chip, AD1848_DATA_UPR_CNT, (unsigned char) (count >> 8));
569 spin_unlock_irqrestore(&chip->reg_lock, flags);
573 static irqreturn_t snd_ad1848_interrupt(int irq, void *dev_id)
575 struct snd_wss *chip = dev_id;
577 if ((chip->mode & WSS_MODE_PLAY) && chip->playback_substream)
578 snd_pcm_period_elapsed(chip->playback_substream);
579 if ((chip->mode & WSS_MODE_RECORD) && chip->capture_substream)
580 snd_pcm_period_elapsed(chip->capture_substream);
581 outb(0, chip->port + CS4231P(STATUS)); /* clear global interrupt bit */
585 static snd_pcm_uframes_t snd_ad1848_playback_pointer(struct snd_pcm_substream *substream)
587 struct snd_wss *chip = snd_pcm_substream_chip(substream);
590 if (!(chip->image[AD1848_IFACE_CTRL] & AD1848_PLAYBACK_ENABLE))
592 ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
593 return bytes_to_frames(substream->runtime, ptr);
596 static snd_pcm_uframes_t snd_ad1848_capture_pointer(struct snd_pcm_substream *substream)
598 struct snd_wss *chip = snd_pcm_substream_chip(substream);
601 if (!(chip->image[AD1848_IFACE_CTRL] & AD1848_CAPTURE_ENABLE))
603 ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
604 return bytes_to_frames(substream->runtime, ptr);
611 static void snd_ad1848_thinkpad_twiddle(struct snd_wss *chip, int on)
615 if (!chip->thinkpad_flag) return;
617 outb(0x1c, AD1848_THINKPAD_CTL_PORT1);
618 tmp = inb(AD1848_THINKPAD_CTL_PORT2);
622 tmp |= AD1848_THINKPAD_CS4248_ENABLE_BIT;
625 tmp &= ~AD1848_THINKPAD_CS4248_ENABLE_BIT;
627 outb(tmp, AD1848_THINKPAD_CTL_PORT2);
632 static void snd_ad1848_suspend(struct snd_wss *chip)
634 snd_pcm_suspend_all(chip->pcm);
635 if (chip->thinkpad_flag)
636 snd_ad1848_thinkpad_twiddle(chip, 0);
639 static void snd_ad1848_resume(struct snd_wss *chip)
643 if (chip->thinkpad_flag)
644 snd_ad1848_thinkpad_twiddle(chip, 1);
646 /* clear any pendings IRQ */
647 inb(chip->port + CS4231P(STATUS));
648 outb(0, chip->port + CS4231P(STATUS));
651 snd_ad1848_mce_down(chip);
652 for (i = 0; i < 16; i++)
653 snd_ad1848_out(chip, i, chip->image[i]);
654 snd_ad1848_mce_up(chip);
655 snd_ad1848_mce_down(chip);
657 #endif /* CONFIG_PM */
659 static int snd_ad1848_probe(struct snd_wss *chip)
662 int i, id, rev, ad1847;
666 snd_ad1848_debug(chip);
669 for (i = 0; i < 1000; i++) {
671 if (inb(chip->port + CS4231P(REGSEL)) & AD1848_INIT)
674 spin_lock_irqsave(&chip->reg_lock, flags);
675 snd_ad1848_out(chip, AD1848_MISC_INFO, 0x00);
676 snd_ad1848_out(chip, AD1848_LEFT_INPUT, 0xaa);
677 snd_ad1848_out(chip, AD1848_RIGHT_INPUT, 0x45);
678 rev = snd_ad1848_in(chip, AD1848_RIGHT_INPUT);
680 spin_unlock_irqrestore(&chip->reg_lock, flags);
685 if (snd_ad1848_in(chip, AD1848_LEFT_INPUT) == 0xaa && rev == 0x45) {
686 spin_unlock_irqrestore(&chip->reg_lock, flags);
690 spin_unlock_irqrestore(&chip->reg_lock, flags);
694 return -ENODEV; /* no valid device found */
695 if (chip->hardware == WSS_HW_DETECT) {
697 chip->hardware = WSS_HW_AD1847;
699 chip->hardware = WSS_HW_AD1848;
700 rev = snd_ad1848_in(chip, AD1848_MISC_INFO);
702 chip->hardware = WSS_HW_CS4248;
703 } else if ((rev & 0x0f) == 0x0a) {
704 snd_ad1848_out(chip, AD1848_MISC_INFO, 0x40);
705 for (i = 0; i < 16; ++i) {
706 if (snd_ad1848_in(chip, i) != snd_ad1848_in(chip, i + 16)) {
707 chip->hardware = WSS_HW_CMI8330;
711 snd_ad1848_out(chip, AD1848_MISC_INFO, 0x00);
715 spin_lock_irqsave(&chip->reg_lock, flags);
716 inb(chip->port + CS4231P(STATUS)); /* clear any pendings IRQ */
717 outb(0, chip->port + CS4231P(STATUS));
719 spin_unlock_irqrestore(&chip->reg_lock, flags);
721 chip->image[AD1848_MISC_INFO] = 0x00;
722 chip->image[AD1848_IFACE_CTRL] =
723 (chip->image[AD1848_IFACE_CTRL] & ~AD1848_SINGLE_DMA) | AD1848_SINGLE_DMA;
724 ptr = (unsigned char *) &chip->image;
725 snd_ad1848_mce_down(chip);
726 spin_lock_irqsave(&chip->reg_lock, flags);
727 for (i = 0; i < 16; i++) /* ok.. fill all AD1848 registers */
728 snd_ad1848_out(chip, i, *ptr++);
729 spin_unlock_irqrestore(&chip->reg_lock, flags);
730 snd_ad1848_mce_up(chip);
731 snd_ad1848_mce_down(chip);
732 return 0; /* all things are ok.. */
739 static struct snd_pcm_hardware snd_ad1848_playback =
741 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
742 SNDRV_PCM_INFO_MMAP_VALID),
743 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
744 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE),
745 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
750 .buffer_bytes_max = (128*1024),
751 .period_bytes_min = 64,
752 .period_bytes_max = (128*1024),
758 static struct snd_pcm_hardware snd_ad1848_capture =
760 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
761 SNDRV_PCM_INFO_MMAP_VALID),
762 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
763 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE),
764 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
769 .buffer_bytes_max = (128*1024),
770 .period_bytes_min = 64,
771 .period_bytes_max = (128*1024),
781 static int snd_ad1848_playback_open(struct snd_pcm_substream *substream)
783 struct snd_wss *chip = snd_pcm_substream_chip(substream);
784 struct snd_pcm_runtime *runtime = substream->runtime;
787 err = snd_ad1848_open(chip, WSS_MODE_PLAY);
790 chip->playback_substream = substream;
791 runtime->hw = snd_ad1848_playback;
792 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
793 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
794 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
798 static int snd_ad1848_capture_open(struct snd_pcm_substream *substream)
800 struct snd_wss *chip = snd_pcm_substream_chip(substream);
801 struct snd_pcm_runtime *runtime = substream->runtime;
804 err = snd_ad1848_open(chip, WSS_MODE_RECORD);
807 chip->capture_substream = substream;
808 runtime->hw = snd_ad1848_capture;
809 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
810 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
811 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
815 static int snd_ad1848_playback_close(struct snd_pcm_substream *substream)
817 struct snd_wss *chip = snd_pcm_substream_chip(substream);
819 chip->mode &= ~WSS_MODE_PLAY;
820 chip->playback_substream = NULL;
821 snd_ad1848_close(chip);
825 static int snd_ad1848_capture_close(struct snd_pcm_substream *substream)
827 struct snd_wss *chip = snd_pcm_substream_chip(substream);
829 chip->mode &= ~WSS_MODE_RECORD;
830 chip->capture_substream = NULL;
831 snd_ad1848_close(chip);
835 static int snd_ad1848_free(struct snd_wss *chip)
837 release_and_free_resource(chip->res_port);
839 free_irq(chip->irq, (void *) chip);
840 if (chip->dma1 >= 0) {
841 snd_dma_disable(chip->dma1);
842 free_dma(chip->dma1);
848 static int snd_ad1848_dev_free(struct snd_device *device)
850 struct snd_wss *chip = device->device_data;
851 return snd_ad1848_free(chip);
854 static const char *snd_ad1848_chip_id(struct snd_wss *chip)
856 switch (chip->hardware) {
857 case AD1848_HW_AD1847: return "AD1847";
858 case AD1848_HW_AD1848: return "AD1848";
859 case AD1848_HW_CS4248: return "CS4248";
860 case AD1848_HW_CMI8330: return "CMI8330/C3D";
861 default: return "???";
865 int snd_ad1848_create(struct snd_card *card,
868 unsigned short hardware,
869 struct snd_wss **rchip)
871 static struct snd_device_ops ops = {
872 .dev_free = snd_ad1848_dev_free,
874 struct snd_wss *chip;
878 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
881 spin_lock_init(&chip->reg_lock);
887 chip->single_dma = 1;
888 chip->hardware = hardware;
889 memcpy(&chip->image, &snd_ad1848_original_image, sizeof(snd_ad1848_original_image));
891 if ((chip->res_port = request_region(port, 4, "AD1848")) == NULL) {
892 snd_printk(KERN_ERR "ad1848: can't grab port 0x%lx\n", port);
893 snd_ad1848_free(chip);
896 if (request_irq(irq, snd_ad1848_interrupt, IRQF_DISABLED, "AD1848", (void *) chip)) {
897 snd_printk(KERN_ERR "ad1848: can't grab IRQ %d\n", irq);
898 snd_ad1848_free(chip);
902 if (request_dma(dma, "AD1848")) {
903 snd_printk(KERN_ERR "ad1848: can't grab DMA %d\n", dma);
904 snd_ad1848_free(chip);
910 if (hardware == WSS_HW_THINKPAD) {
911 chip->thinkpad_flag = 1;
912 chip->hardware = WSS_HW_DETECT; /* reset */
913 snd_ad1848_thinkpad_twiddle(chip, 1);
916 if (snd_ad1848_probe(chip) < 0) {
917 snd_ad1848_free(chip);
921 /* Register device */
922 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
923 snd_ad1848_free(chip);
928 chip->suspend = snd_ad1848_suspend;
929 chip->resume = snd_ad1848_resume;
936 EXPORT_SYMBOL(snd_ad1848_create);
938 static struct snd_pcm_ops snd_ad1848_playback_ops = {
939 .open = snd_ad1848_playback_open,
940 .close = snd_ad1848_playback_close,
941 .ioctl = snd_ad1848_ioctl,
942 .hw_params = snd_ad1848_playback_hw_params,
943 .hw_free = snd_ad1848_playback_hw_free,
944 .prepare = snd_ad1848_playback_prepare,
945 .trigger = snd_ad1848_playback_trigger,
946 .pointer = snd_ad1848_playback_pointer,
949 static struct snd_pcm_ops snd_ad1848_capture_ops = {
950 .open = snd_ad1848_capture_open,
951 .close = snd_ad1848_capture_close,
952 .ioctl = snd_ad1848_ioctl,
953 .hw_params = snd_ad1848_capture_hw_params,
954 .hw_free = snd_ad1848_capture_hw_free,
955 .prepare = snd_ad1848_capture_prepare,
956 .trigger = snd_ad1848_capture_trigger,
957 .pointer = snd_ad1848_capture_pointer,
960 int snd_ad1848_pcm(struct snd_wss *chip, int device, struct snd_pcm **rpcm)
965 if ((err = snd_pcm_new(chip->card, "AD1848", device, 1, 1, &pcm)) < 0)
968 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ad1848_playback_ops);
969 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ad1848_capture_ops);
971 pcm->private_data = chip;
972 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
973 strcpy(pcm->name, snd_ad1848_chip_id(chip));
975 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
979 128 * 1024 : 64 * 1024);
987 EXPORT_SYMBOL(snd_ad1848_pcm);
989 const struct snd_pcm_ops *snd_ad1848_get_pcm_ops(int direction)
991 return direction == SNDRV_PCM_STREAM_PLAYBACK ?
992 &snd_ad1848_playback_ops : &snd_ad1848_capture_ops;
995 EXPORT_SYMBOL(snd_ad1848_get_pcm_ops);
1001 static int __init alsa_ad1848_init(void)
1006 static void __exit alsa_ad1848_exit(void)
1010 module_init(alsa_ad1848_init)
1011 module_exit(alsa_ad1848_exit)