2 * The driver for the ForteMedia FM801 based soundcards
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
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 #include <sound/driver.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/pci.h>
27 #include <linux/slab.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/ac97_codec.h>
31 #include <sound/mpu401.h>
32 #include <sound/opl3.h>
34 #include <sound/initval.h>
38 #define chip_t fm801_t
40 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
41 MODULE_DESCRIPTION("ForteMedia FM801");
42 MODULE_LICENSE("GPL");
43 MODULE_CLASSES("{sound}");
44 MODULE_DEVICES("{{ForteMedia,FM801},"
45 "{Genius,SoundMaker Live 5.1}}");
47 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
48 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
49 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
51 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
52 MODULE_PARM_DESC(index, "Index value for the FM801 soundcard.");
53 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
54 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
55 MODULE_PARM_DESC(id, "ID string for the FM801 soundcard.");
56 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
57 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
58 MODULE_PARM_DESC(enable, "Enable FM801 soundcard.");
59 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
65 #define FM801_REG(chip, reg) (chip->port + FM801_##reg)
67 #define FM801_PCM_VOL 0x00 /* PCM Output Volume */
68 #define FM801_FM_VOL 0x02 /* FM Output Volume */
69 #define FM801_I2S_VOL 0x04 /* I2S Volume */
70 #define FM801_REC_SRC 0x06 /* Record Source */
71 #define FM801_PLY_CTRL 0x08 /* Playback Control */
72 #define FM801_PLY_COUNT 0x0a /* Playback Count */
73 #define FM801_PLY_BUF1 0x0c /* Playback Bufer I */
74 #define FM801_PLY_BUF2 0x10 /* Playback Buffer II */
75 #define FM801_CAP_CTRL 0x14 /* Capture Control */
76 #define FM801_CAP_COUNT 0x16 /* Capture Count */
77 #define FM801_CAP_BUF1 0x18 /* Capture Buffer I */
78 #define FM801_CAP_BUF2 0x1c /* Capture Buffer II */
79 #define FM801_CODEC_CTRL 0x22 /* Codec Control */
80 #define FM801_I2S_MODE 0x24 /* I2S Mode Control */
81 #define FM801_VOLUME 0x26 /* Volume Up/Down/Mute Status */
82 #define FM801_I2C_CTRL 0x29 /* I2C Control */
83 #define FM801_AC97_CMD 0x2a /* AC'97 Command */
84 #define FM801_AC97_DATA 0x2c /* AC'97 Data */
85 #define FM801_MPU401_DATA 0x30 /* MPU401 Data */
86 #define FM801_MPU401_CMD 0x31 /* MPU401 Command */
87 #define FM801_GPIO_CTRL 0x52 /* General Purpose I/O Control */
88 #define FM801_GEN_CTRL 0x54 /* General Control */
89 #define FM801_IRQ_MASK 0x56 /* Interrupt Mask */
90 #define FM801_IRQ_STATUS 0x5a /* Interrupt Status */
91 #define FM801_OPL3_BANK0 0x68 /* OPL3 Status Read / Bank 0 Write */
92 #define FM801_OPL3_DATA0 0x69 /* OPL3 Data 0 Write */
93 #define FM801_OPL3_BANK1 0x6a /* OPL3 Bank 1 Write */
94 #define FM801_OPL3_DATA1 0x6b /* OPL3 Bank 1 Write */
95 #define FM801_POWERDOWN 0x70 /* Blocks Power Down Control */
97 #define FM801_AC97_ADDR_SHIFT 10
99 /* playback and record control register bits */
100 #define FM801_BUF1_LAST (1<<1)
101 #define FM801_BUF2_LAST (1<<2)
102 #define FM801_START (1<<5)
103 #define FM801_PAUSE (1<<6)
104 #define FM801_IMMED_STOP (1<<7)
105 #define FM801_RATE_SHIFT 8
106 #define FM801_RATE_MASK (15 << FM801_RATE_SHIFT)
107 #define FM801_CHANNELS_4 (1<<12) /* playback only */
108 #define FM801_CHANNELS_6 (2<<12) /* playback only */
109 #define FM801_CHANNELS_6MS (3<<12) /* playback only */
110 #define FM801_CHANNELS_MASK (3<<12)
111 #define FM801_16BIT (1<<14)
112 #define FM801_STEREO (1<<15)
114 /* IRQ status bits */
115 #define FM801_IRQ_PLAYBACK (1<<8)
116 #define FM801_IRQ_CAPTURE (1<<9)
117 #define FM801_IRQ_VOLUME (1<<14)
118 #define FM801_IRQ_MPU (1<<15)
124 typedef struct _snd_fm801 fm801_t;
129 unsigned long port; /* I/O port number */
130 struct resource *res_port;
131 unsigned int multichannel: 1, /* multichannel support */
132 secondary: 1; /* secondary codec */
133 unsigned char secondary_addr; /* address of the secondary codec */
135 unsigned short ply_ctrl; /* playback control */
136 unsigned short cap_ctrl; /* capture control */
138 unsigned long ply_buffer;
139 unsigned int ply_buf;
140 unsigned int ply_count;
141 unsigned int ply_size;
142 unsigned int ply_pos;
144 unsigned long cap_buffer;
145 unsigned int cap_buf;
146 unsigned int cap_count;
147 unsigned int cap_size;
148 unsigned int cap_pos;
156 snd_rawmidi_t *rmidi;
157 snd_pcm_substream_t *playback_substream;
158 snd_pcm_substream_t *capture_substream;
159 unsigned int p_dma_size;
160 unsigned int c_dma_size;
163 snd_info_entry_t *proc_entry;
166 static struct pci_device_id snd_fm801_ids[] = {
167 { 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, }, /* FM801 */
171 MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
174 * common I/O routines
177 static int snd_fm801_update_bits(fm801_t *chip, unsigned short reg,
178 unsigned short mask, unsigned short value)
181 unsigned short old, new;
183 spin_lock(&chip->reg_lock);
184 old = inw(chip->port + reg);
185 new = (old & ~mask) | value;
188 outw(new, chip->port + reg);
189 spin_unlock(&chip->reg_lock);
193 static void snd_fm801_codec_write(ac97_t *ac97,
197 fm801_t *chip = snd_magic_cast(fm801_t, ac97->private_data, return);
201 * Wait until the codec interface is not ready..
203 for (idx = 0; idx < 100; idx++) {
204 if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
208 snd_printk("AC'97 interface is busy (1)\n");
212 /* write data and address */
213 outw(val, FM801_REG(chip, AC97_DATA));
214 outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
216 * Wait until the write command is not completed..
218 for (idx = 0; idx < 1000; idx++) {
219 if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
223 snd_printk("AC'97 interface #%d is busy (2)\n", ac97->num);
226 static unsigned short snd_fm801_codec_read(ac97_t *ac97, unsigned short reg)
228 fm801_t *chip = snd_magic_cast(fm801_t, ac97->private_data, return -ENXIO);
232 * Wait until the codec interface is not ready..
234 for (idx = 0; idx < 100; idx++) {
235 if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
239 snd_printk("AC'97 interface is busy (1)\n");
244 outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | (1<<7), FM801_REG(chip, AC97_CMD));
245 for (idx = 0; idx < 100; idx++) {
246 if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
250 snd_printk("AC'97 interface #%d is busy (2)\n", ac97->num);
254 for (idx = 0; idx < 1000; idx++) {
255 if (inw(FM801_REG(chip, AC97_CMD)) & (1<<8))
259 snd_printk("AC'97 interface #%d is not valid (2)\n", ac97->num);
263 return inw(FM801_REG(chip, AC97_DATA));
266 static unsigned int rates[] = {
267 5500, 8000, 9600, 11025,
268 16000, 19200, 22050, 32000,
272 #define RATES sizeof(rates) / sizeof(rates[0])
274 static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
280 static unsigned int channels[] = {
284 #define CHANNELS sizeof(channels) / sizeof(channels[0])
286 static snd_pcm_hw_constraint_list_t hw_constraints_channels = {
293 * Sample rate routines
296 static unsigned short snd_fm801_rate_bits(unsigned int rate)
300 for (idx = 0; idx < 11; idx++)
301 if (rates[idx] == rate)
311 static int snd_fm801_playback_trigger(snd_pcm_substream_t * substream,
314 fm801_t *chip = snd_pcm_substream_chip(substream);
316 spin_lock(&chip->reg_lock);
318 case SNDRV_PCM_TRIGGER_START:
319 chip->ply_ctrl &= ~(FM801_BUF1_LAST |
322 chip->ply_ctrl |= FM801_START |
325 case SNDRV_PCM_TRIGGER_STOP:
326 chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
328 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
329 chip->ply_ctrl |= FM801_PAUSE;
331 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
332 chip->ply_ctrl &= ~FM801_PAUSE;
335 spin_unlock(&chip->reg_lock);
339 outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
340 spin_unlock(&chip->reg_lock);
344 static int snd_fm801_capture_trigger(snd_pcm_substream_t * substream,
347 fm801_t *chip = snd_pcm_substream_chip(substream);
349 spin_lock(&chip->reg_lock);
351 case SNDRV_PCM_TRIGGER_START:
352 chip->cap_ctrl &= ~(FM801_BUF1_LAST |
355 chip->cap_ctrl |= FM801_START |
358 case SNDRV_PCM_TRIGGER_STOP:
359 chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
361 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
362 chip->cap_ctrl |= FM801_PAUSE;
364 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
365 chip->cap_ctrl &= ~FM801_PAUSE;
368 spin_unlock(&chip->reg_lock);
372 outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
373 spin_unlock(&chip->reg_lock);
377 static int snd_fm801_hw_params(snd_pcm_substream_t * substream,
378 snd_pcm_hw_params_t * hw_params)
380 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
383 static int snd_fm801_hw_free(snd_pcm_substream_t * substream)
385 return snd_pcm_lib_free_pages(substream);
388 static int snd_fm801_playback_prepare(snd_pcm_substream_t * substream)
391 fm801_t *chip = snd_pcm_substream_chip(substream);
392 snd_pcm_runtime_t *runtime = substream->runtime;
394 chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
395 chip->ply_count = snd_pcm_lib_period_bytes(substream);
396 spin_lock_irqsave(&chip->reg_lock, flags);
397 chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
398 FM801_STEREO | FM801_RATE_MASK |
399 FM801_CHANNELS_MASK);
400 if (snd_pcm_format_width(runtime->format) == 16)
401 chip->ply_ctrl |= FM801_16BIT;
402 if (runtime->channels > 1) {
403 chip->ply_ctrl |= FM801_STEREO;
404 if (runtime->channels == 4)
405 chip->ply_ctrl |= FM801_CHANNELS_4;
406 else if (runtime->channels == 6)
407 chip->ply_ctrl |= FM801_CHANNELS_6;
409 chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
411 outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
412 outw(chip->ply_count - 1, FM801_REG(chip, PLY_COUNT));
413 chip->ply_buffer = runtime->dma_addr;
415 outl(chip->ply_buffer, FM801_REG(chip, PLY_BUF1));
416 outl(chip->ply_buffer + (chip->ply_count % chip->ply_size), FM801_REG(chip, PLY_BUF2));
417 spin_unlock_irqrestore(&chip->reg_lock, flags);
421 static int snd_fm801_capture_prepare(snd_pcm_substream_t * substream)
424 fm801_t *chip = snd_pcm_substream_chip(substream);
425 snd_pcm_runtime_t *runtime = substream->runtime;
427 chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
428 chip->cap_count = snd_pcm_lib_period_bytes(substream);
429 spin_lock_irqsave(&chip->reg_lock, flags);
430 chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
431 FM801_STEREO | FM801_RATE_MASK);
432 if (snd_pcm_format_width(runtime->format) == 16)
433 chip->cap_ctrl |= FM801_16BIT;
434 if (runtime->channels > 1)
435 chip->cap_ctrl |= FM801_STEREO;
436 chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
438 outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
439 outw(chip->cap_count - 1, FM801_REG(chip, CAP_COUNT));
440 chip->cap_buffer = runtime->dma_addr;
442 outl(chip->cap_buffer, FM801_REG(chip, CAP_BUF1));
443 outl(chip->cap_buffer + (chip->cap_count % chip->cap_size), FM801_REG(chip, CAP_BUF2));
444 spin_unlock_irqrestore(&chip->reg_lock, flags);
448 static snd_pcm_uframes_t snd_fm801_playback_pointer(snd_pcm_substream_t * substream)
450 fm801_t *chip = snd_pcm_substream_chip(substream);
454 if (!(chip->ply_ctrl & FM801_START))
456 spin_lock_irqsave(&chip->reg_lock, flags);
457 ptr = chip->ply_pos + (chip->ply_count - 1) - inw(FM801_REG(chip, PLY_COUNT));
458 if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_PLAYBACK) {
459 ptr += chip->ply_count;
460 ptr %= chip->ply_size;
462 spin_unlock_irqrestore(&chip->reg_lock, flags);
463 return bytes_to_frames(substream->runtime, ptr);
466 static snd_pcm_uframes_t snd_fm801_capture_pointer(snd_pcm_substream_t * substream)
468 fm801_t *chip = snd_pcm_substream_chip(substream);
472 if (!(chip->cap_ctrl & FM801_START))
474 spin_lock_irqsave(&chip->reg_lock, flags);
475 ptr = chip->cap_pos + (chip->cap_count - 1) - inw(FM801_REG(chip, CAP_COUNT));
476 if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_CAPTURE) {
477 ptr += chip->cap_count;
478 ptr %= chip->cap_size;
480 spin_unlock_irqrestore(&chip->reg_lock, flags);
481 return bytes_to_frames(substream->runtime, ptr);
484 static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id, struct pt_regs *regs)
486 fm801_t *chip = snd_magic_cast(fm801_t, dev_id, return IRQ_NONE);
487 unsigned short status;
490 status = inw(FM801_REG(chip, IRQ_STATUS));
491 status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
495 outw(status, FM801_REG(chip, IRQ_STATUS));
496 if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
497 spin_lock(&chip->reg_lock);
499 chip->ply_pos += chip->ply_count;
500 chip->ply_pos %= chip->ply_size;
501 tmp = chip->ply_pos + chip->ply_count;
502 tmp %= chip->ply_size;
503 outl(chip->ply_buffer + tmp,
504 (chip->ply_buf & 1) ?
505 FM801_REG(chip, PLY_BUF1) :
506 FM801_REG(chip, PLY_BUF2));
507 spin_unlock(&chip->reg_lock);
508 snd_pcm_period_elapsed(chip->playback_substream);
510 if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
511 spin_lock(&chip->reg_lock);
513 chip->cap_pos += chip->cap_count;
514 chip->cap_pos %= chip->cap_size;
515 tmp = chip->cap_pos + chip->cap_count;
516 tmp %= chip->cap_size;
517 outl(chip->cap_buffer + tmp,
518 (chip->cap_buf & 1) ?
519 FM801_REG(chip, CAP_BUF1) :
520 FM801_REG(chip, CAP_BUF2));
521 spin_unlock(&chip->reg_lock);
522 snd_pcm_period_elapsed(chip->capture_substream);
524 if (chip->rmidi && (status & FM801_IRQ_MPU))
525 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
526 if (status & FM801_IRQ_VOLUME)
532 static snd_pcm_hardware_t snd_fm801_playback =
534 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
535 SNDRV_PCM_INFO_BLOCK_TRANSFER |
536 SNDRV_PCM_INFO_PAUSE |
537 SNDRV_PCM_INFO_MMAP_VALID),
538 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
539 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
544 .buffer_bytes_max = (128*1024),
545 .period_bytes_min = 64,
546 .period_bytes_max = (128*1024),
552 static snd_pcm_hardware_t snd_fm801_capture =
554 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
555 SNDRV_PCM_INFO_BLOCK_TRANSFER |
556 SNDRV_PCM_INFO_PAUSE |
557 SNDRV_PCM_INFO_MMAP_VALID),
558 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
559 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
564 .buffer_bytes_max = (128*1024),
565 .period_bytes_min = 64,
566 .period_bytes_max = (128*1024),
572 static int snd_fm801_playback_open(snd_pcm_substream_t * substream)
574 fm801_t *chip = snd_pcm_substream_chip(substream);
575 snd_pcm_runtime_t *runtime = substream->runtime;
578 chip->playback_substream = substream;
579 runtime->hw = snd_fm801_playback;
580 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
581 if (chip->multichannel) {
582 runtime->hw.channels_max = 6;
583 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels);
585 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
590 static int snd_fm801_capture_open(snd_pcm_substream_t * substream)
592 fm801_t *chip = snd_pcm_substream_chip(substream);
593 snd_pcm_runtime_t *runtime = substream->runtime;
596 chip->capture_substream = substream;
597 runtime->hw = snd_fm801_capture;
598 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
599 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
604 static int snd_fm801_playback_close(snd_pcm_substream_t * substream)
606 fm801_t *chip = snd_pcm_substream_chip(substream);
608 chip->playback_substream = NULL;
612 static int snd_fm801_capture_close(snd_pcm_substream_t * substream)
614 fm801_t *chip = snd_pcm_substream_chip(substream);
616 chip->capture_substream = NULL;
620 static snd_pcm_ops_t snd_fm801_playback_ops = {
621 .open = snd_fm801_playback_open,
622 .close = snd_fm801_playback_close,
623 .ioctl = snd_pcm_lib_ioctl,
624 .hw_params = snd_fm801_hw_params,
625 .hw_free = snd_fm801_hw_free,
626 .prepare = snd_fm801_playback_prepare,
627 .trigger = snd_fm801_playback_trigger,
628 .pointer = snd_fm801_playback_pointer,
631 static snd_pcm_ops_t snd_fm801_capture_ops = {
632 .open = snd_fm801_capture_open,
633 .close = snd_fm801_capture_close,
634 .ioctl = snd_pcm_lib_ioctl,
635 .hw_params = snd_fm801_hw_params,
636 .hw_free = snd_fm801_hw_free,
637 .prepare = snd_fm801_capture_prepare,
638 .trigger = snd_fm801_capture_trigger,
639 .pointer = snd_fm801_capture_pointer,
642 static void snd_fm801_pcm_free(snd_pcm_t *pcm)
644 fm801_t *chip = snd_magic_cast(fm801_t, pcm->private_data, return);
646 snd_pcm_lib_preallocate_free_for_all(pcm);
649 static int __devinit snd_fm801_pcm(fm801_t *chip, int device, snd_pcm_t ** rpcm)
656 if ((err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm)) < 0)
659 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
660 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
662 pcm->private_data = chip;
663 pcm->private_free = snd_fm801_pcm_free;
665 strcpy(pcm->name, "FM801");
668 snd_pcm_lib_preallocate_pci_pages_for_all(chip->pci, pcm, chip->multichannel ? 128*1024 : 64*1024, 128*1024);
679 #define FM801_SINGLE(xname, reg, shift, mask, invert) \
680 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
681 .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
682 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
684 static int snd_fm801_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
686 int mask = (kcontrol->private_value >> 16) & 0xff;
688 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
690 uinfo->value.integer.min = 0;
691 uinfo->value.integer.max = mask;
695 static int snd_fm801_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
697 fm801_t *chip = snd_kcontrol_chip(kcontrol);
698 int reg = kcontrol->private_value & 0xff;
699 int shift = (kcontrol->private_value >> 8) & 0xff;
700 int mask = (kcontrol->private_value >> 16) & 0xff;
701 int invert = (kcontrol->private_value >> 24) & 0xff;
703 ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift) & mask;
705 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
709 static int snd_fm801_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
711 fm801_t *chip = snd_kcontrol_chip(kcontrol);
712 int reg = kcontrol->private_value & 0xff;
713 int shift = (kcontrol->private_value >> 8) & 0xff;
714 int mask = (kcontrol->private_value >> 16) & 0xff;
715 int invert = (kcontrol->private_value >> 24) & 0xff;
718 val = (ucontrol->value.integer.value[0] & mask);
721 return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
724 #define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
725 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
726 .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
727 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
729 static int snd_fm801_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
731 int mask = (kcontrol->private_value >> 16) & 0xff;
733 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
735 uinfo->value.integer.min = 0;
736 uinfo->value.integer.max = mask;
740 static int snd_fm801_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
742 fm801_t *chip = snd_kcontrol_chip(kcontrol);
743 int reg = kcontrol->private_value & 0xff;
744 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
745 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
746 int mask = (kcontrol->private_value >> 16) & 0xff;
747 int invert = (kcontrol->private_value >> 24) & 0xff;
749 spin_lock(&chip->reg_lock);
750 ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift_left) & mask;
751 ucontrol->value.integer.value[1] = (inw(chip->port + reg) >> shift_right) & mask;
752 spin_unlock(&chip->reg_lock);
754 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
755 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
760 static int snd_fm801_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
762 fm801_t *chip = snd_kcontrol_chip(kcontrol);
763 int reg = kcontrol->private_value & 0xff;
764 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
765 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
766 int mask = (kcontrol->private_value >> 16) & 0xff;
767 int invert = (kcontrol->private_value >> 24) & 0xff;
768 unsigned short val1, val2;
770 val1 = ucontrol->value.integer.value[0] & mask;
771 val2 = ucontrol->value.integer.value[1] & mask;
776 return snd_fm801_update_bits(chip, reg,
777 (mask << shift_left) | (mask << shift_right),
778 (val1 << shift_left ) | (val2 << shift_right));
781 static int snd_fm801_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
783 static char *texts[5] = {
784 "AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
787 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
789 uinfo->value.enumerated.items = 5;
790 if (uinfo->value.enumerated.item > 4)
791 uinfo->value.enumerated.item = 4;
792 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
796 static int snd_fm801_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
798 fm801_t *chip = snd_kcontrol_chip(kcontrol);
801 val = inw(FM801_REG(chip, REC_SRC)) & 7;
804 ucontrol->value.enumerated.item[0] = val;
808 static int snd_fm801_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
810 fm801_t *chip = snd_kcontrol_chip(kcontrol);
813 if ((val = ucontrol->value.enumerated.item[0]) > 4)
815 return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
818 #define FM801_CONTROLS (sizeof(snd_fm801_controls)/sizeof(snd_kcontrol_new_t))
820 static snd_kcontrol_new_t snd_fm801_controls[] __devinitdata = {
821 FM801_DOUBLE("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1),
822 FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
823 FM801_DOUBLE("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1),
824 FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
825 FM801_DOUBLE("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1),
826 FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
828 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
829 .name = "Digital Capture Source",
830 .info = snd_fm801_info_mux,
831 .get = snd_fm801_get_mux,
832 .put = snd_fm801_put_mux,
836 #define FM801_CONTROLS_MULTI (sizeof(snd_fm801_controls_multi)/sizeof(snd_kcontrol_new_t))
838 static snd_kcontrol_new_t snd_fm801_controls_multi[] __devinitdata = {
839 FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
840 FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
841 FM801_SINGLE("IEC958 Capture Switch", FM801_I2S_MODE, 8, 1, 0),
842 FM801_SINGLE("IEC958 Raw Data Playback Switch", FM801_I2S_MODE, 9, 1, 0),
843 FM801_SINGLE("IEC958 Raw Data Capture Switch", FM801_I2S_MODE, 10, 1, 0),
844 FM801_SINGLE("IEC958 Playback Switch", FM801_GEN_CTRL, 2, 1, 0),
847 static void snd_fm801_mixer_free_ac97(ac97_t *ac97)
849 fm801_t *chip = snd_magic_cast(fm801_t, ac97->private_data, return);
850 if (ac97->num == 0) {
853 chip->ac97_sec = NULL;
857 static int __devinit snd_fm801_mixer(fm801_t *chip)
863 memset(&ac97, 0, sizeof(ac97));
864 ac97.write = snd_fm801_codec_write;
865 ac97.read = snd_fm801_codec_read;
866 ac97.private_data = chip;
867 ac97.private_free = snd_fm801_mixer_free_ac97;
868 if ((err = snd_ac97_mixer(chip->card, &ac97, &chip->ac97)) < 0)
870 if (chip->secondary) {
872 ac97.addr = chip->secondary_addr;
873 if ((err = snd_ac97_mixer(chip->card, &ac97, &chip->ac97_sec)) < 0)
876 for (i = 0; i < FM801_CONTROLS; i++)
877 snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls[i], chip));
878 if (chip->multichannel) {
879 for (i = 0; i < FM801_CONTROLS_MULTI; i++)
880 snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
886 * initialization routines
889 static int snd_fm801_free(fm801_t *chip)
896 /* interrupt setup - mask everything */
897 cmdw = inw(FM801_REG(chip, IRQ_MASK));
899 outw(cmdw, FM801_REG(chip, IRQ_MASK));
902 if (chip->res_port) {
903 release_resource(chip->res_port);
904 kfree_nocheck(chip->res_port);
907 free_irq(chip->irq, (void *)chip);
909 snd_magic_kfree(chip);
913 static int snd_fm801_dev_free(snd_device_t *device)
915 fm801_t *chip = snd_magic_cast(fm801_t, device->device_data, return -ENXIO);
916 return snd_fm801_free(chip);
919 static int __devinit snd_fm801_create(snd_card_t * card,
920 struct pci_dev * pci,
924 unsigned char rev, id;
926 unsigned long timeout;
928 static snd_device_ops_t ops = {
929 .dev_free = snd_fm801_dev_free,
933 if ((err = pci_enable_device(pci)) < 0)
935 chip = snd_magic_kcalloc(fm801_t, 0, GFP_KERNEL);
938 spin_lock_init(&chip->reg_lock);
942 chip->port = pci_resource_start(pci, 0);
943 if ((chip->res_port = request_region(chip->port, 0x80, "FM801")) == NULL) {
944 snd_printk("unable to grab region 0x%lx-0x%lx\n", chip->port, chip->port + 0x80 - 1);
945 snd_fm801_free(chip);
948 if (request_irq(pci->irq, snd_fm801_interrupt, SA_INTERRUPT|SA_SHIRQ, "FM801", (void *)chip)) {
949 snd_printk("unable to grab IRQ %d\n", chip->irq);
950 snd_fm801_free(chip);
953 chip->irq = pci->irq;
956 pci_read_config_byte(pci, PCI_REVISION_ID, &rev);
957 if (rev >= 0xb1) /* FM801-AU */
958 chip->multichannel = 1;
960 /* codec cold reset + AC'97 warm reset */
961 outw((1<<5)|(1<<6), FM801_REG(chip, CODEC_CTRL));
962 inw(FM801_REG(chip, CODEC_CTRL)); /* flush posting data */
964 outw(0, FM801_REG(chip, CODEC_CTRL));
966 timeout = (jiffies + (3 * HZ) / 4) + 1; /* min 750ms */
968 outw((1<<7) | (0 << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
971 if ((inw(FM801_REG(chip, AC97_CMD)) & (3<<8)) == (1<<8))
972 goto __ac97_secondary;
973 set_current_state(TASK_UNINTERRUPTIBLE);
975 } while (time_after(timeout, jiffies));
976 snd_printk("Primary AC'97 codec not found\n");
977 snd_fm801_free(chip);
981 if (!chip->multichannel) /* lookup is not required */
983 for (id = 3; id > 0; id--) { /* my card has the secondary codec */
984 /* at address #3, so the loop is inverted */
986 timeout = jiffies + HZ / 20;
988 outw((1<<7) | (id << FM801_AC97_ADDR_SHIFT) | AC97_VENDOR_ID1, FM801_REG(chip, AC97_CMD));
991 if ((inw(FM801_REG(chip, AC97_CMD)) & (3<<8)) == (1<<8)) {
992 cmdw = inw(FM801_REG(chip, AC97_DATA));
993 if (cmdw != 0xffff && cmdw != 0) {
995 chip->secondary_addr = id;
999 set_current_state(TASK_UNINTERRUPTIBLE);
1000 schedule_timeout(1);
1001 } while (time_after(timeout, jiffies));
1004 /* the recovery phase, it seems that probing for non-existing codec might */
1005 /* cause timeout problems */
1006 timeout = (jiffies + (3 * HZ) / 4) + 1; /* min 750ms */
1008 outw((1<<7) | (0 << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
1011 if ((inw(FM801_REG(chip, AC97_CMD)) & (3<<8)) == (1<<8))
1013 set_current_state(TASK_UNINTERRUPTIBLE);
1014 schedule_timeout(1);
1015 } while (time_after(timeout, jiffies));
1016 snd_printk("Primary AC'97 codec not responding\n");
1017 snd_fm801_free(chip);
1023 outw(0x0808, FM801_REG(chip, PCM_VOL));
1024 outw(0x9f1f, FM801_REG(chip, FM_VOL));
1025 outw(0x8808, FM801_REG(chip, I2S_VOL));
1027 /* I2S control - I2S mode */
1028 outw(0x0003, FM801_REG(chip, I2S_MODE));
1030 /* interrupt setup - unmask MPU, PLAYBACK & CAPTURE */
1031 cmdw = inw(FM801_REG(chip, IRQ_MASK));
1033 outw(cmdw, FM801_REG(chip, IRQ_MASK));
1035 /* interrupt clear */
1036 outw(FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU, FM801_REG(chip, IRQ_STATUS));
1038 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1039 snd_fm801_free(chip);
1047 static int __devinit snd_card_fm801_probe(struct pci_dev *pci,
1048 const struct pci_device_id *pci_id)
1056 if (dev >= SNDRV_CARDS)
1063 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1066 if ((err = snd_fm801_create(card, pci, &chip)) < 0) {
1067 snd_card_free(card);
1070 if ((err = snd_fm801_pcm(chip, 0, NULL)) < 0) {
1071 snd_card_free(card);
1074 if ((err = snd_fm801_mixer(chip)) < 0) {
1075 snd_card_free(card);
1078 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1079 FM801_REG(chip, MPU401_DATA), 1,
1080 chip->irq, 0, &chip->rmidi)) < 0) {
1081 snd_card_free(card);
1084 if ((err = snd_opl3_create(card, FM801_REG(chip, OPL3_BANK0),
1085 FM801_REG(chip, OPL3_BANK1),
1086 OPL3_HW_OPL3_FM801, 1, &opl3)) < 0) {
1087 snd_card_free(card);
1090 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
1091 snd_card_free(card);
1095 strcpy(card->driver, "FM801");
1096 strcpy(card->shortname, "ForteMedia FM801-");
1097 strcat(card->shortname, chip->multichannel ? "AU" : "AS");
1098 sprintf(card->longname, "%s at 0x%lx, irq %i",
1099 card->shortname, chip->port, chip->irq);
1101 if ((err = snd_card_register(card)) < 0) {
1102 snd_card_free(card);
1105 pci_set_drvdata(pci, card);
1110 static void __devexit snd_card_fm801_remove(struct pci_dev *pci)
1112 snd_card_free(pci_get_drvdata(pci));
1113 pci_set_drvdata(pci, NULL);
1116 static struct pci_driver driver = {
1118 .id_table = snd_fm801_ids,
1119 .probe = snd_card_fm801_probe,
1120 .remove = __devexit_p(snd_card_fm801_remove),
1123 static int __init alsa_card_fm801_init(void)
1127 if ((err = pci_module_init(&driver)) < 0) {
1129 printk(KERN_ERR "ForteMedia FM801 soundcard not found or device busy\n");
1136 static void __exit alsa_card_fm801_exit(void)
1138 pci_unregister_driver(&driver);
1141 module_init(alsa_card_fm801_init)
1142 module_exit(alsa_card_fm801_exit)
1146 /* format is: snd-fm801=enable,index,id */
1148 static int __init alsa_card_fm801_setup(char *str)
1150 static unsigned __initdata nr_dev = 0;
1152 if (nr_dev >= SNDRV_CARDS)
1154 (void)(get_option(&str,&enable[nr_dev]) == 2 &&
1155 get_option(&str,&index[nr_dev]) == 2 &&
1156 get_id(&str,&id[nr_dev]) == 2);
1161 __setup("snd-fm801=", alsa_card_fm801_setup);
1163 #endif /* ifndef MODULE */