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 <linux/moduleparam.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/ac97_codec.h>
32 #include <sound/mpu401.h>
33 #include <sound/opl3.h>
34 #include <sound/initval.h>
38 #if (defined(CONFIG_SND_FM801_TEA575X) || defined(CONFIG_SND_FM801_TEA575X_MODULE)) && (defined(CONFIG_VIDEO_DEV) || defined(CONFIG_VIDEO_DEV_MODULE))
39 #include <sound/tea575x-tuner.h>
40 #define TEA575X_RADIO 1
43 #define chip_t fm801_t
45 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
46 MODULE_DESCRIPTION("ForteMedia FM801");
47 MODULE_LICENSE("GPL");
48 MODULE_CLASSES("{sound}");
49 MODULE_DEVICES("{{ForteMedia,FM801},"
50 "{Genius,SoundMaker Live 5.1}}");
52 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
53 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
54 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
56 * Enable TEA575x tuner
57 * 1 = MediaForte 256-PCS
58 * 2 = MediaForte 256-PCPR
59 * 3 = MediaForte 64-PCR
60 * High 16-bits are video (radio) device number + 1
62 static int tea575x_tuner[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = 0 };
65 module_param_array(index, int, boot_devs, 0444);
66 MODULE_PARM_DESC(index, "Index value for the FM801 soundcard.");
67 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
68 module_param_array(id, charp, boot_devs, 0444);
69 MODULE_PARM_DESC(id, "ID string for the FM801 soundcard.");
70 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
71 module_param_array(enable, bool, boot_devs, 0444);
72 MODULE_PARM_DESC(enable, "Enable FM801 soundcard.");
73 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
74 module_param_array(tea575x_tuner, bool, boot_devs, 0444);
75 MODULE_PARM_DESC(tea575x_tuner, "Enable TEA575x tuner.");
76 MODULE_PARM_SYNTAX(tea575x_tuner, SNDRV_ENABLE_DESC);
82 #define FM801_REG(chip, reg) (chip->port + FM801_##reg)
84 #define FM801_PCM_VOL 0x00 /* PCM Output Volume */
85 #define FM801_FM_VOL 0x02 /* FM Output Volume */
86 #define FM801_I2S_VOL 0x04 /* I2S Volume */
87 #define FM801_REC_SRC 0x06 /* Record Source */
88 #define FM801_PLY_CTRL 0x08 /* Playback Control */
89 #define FM801_PLY_COUNT 0x0a /* Playback Count */
90 #define FM801_PLY_BUF1 0x0c /* Playback Bufer I */
91 #define FM801_PLY_BUF2 0x10 /* Playback Buffer II */
92 #define FM801_CAP_CTRL 0x14 /* Capture Control */
93 #define FM801_CAP_COUNT 0x16 /* Capture Count */
94 #define FM801_CAP_BUF1 0x18 /* Capture Buffer I */
95 #define FM801_CAP_BUF2 0x1c /* Capture Buffer II */
96 #define FM801_CODEC_CTRL 0x22 /* Codec Control */
97 #define FM801_I2S_MODE 0x24 /* I2S Mode Control */
98 #define FM801_VOLUME 0x26 /* Volume Up/Down/Mute Status */
99 #define FM801_I2C_CTRL 0x29 /* I2C Control */
100 #define FM801_AC97_CMD 0x2a /* AC'97 Command */
101 #define FM801_AC97_DATA 0x2c /* AC'97 Data */
102 #define FM801_MPU401_DATA 0x30 /* MPU401 Data */
103 #define FM801_MPU401_CMD 0x31 /* MPU401 Command */
104 #define FM801_GPIO_CTRL 0x52 /* General Purpose I/O Control */
105 #define FM801_GEN_CTRL 0x54 /* General Control */
106 #define FM801_IRQ_MASK 0x56 /* Interrupt Mask */
107 #define FM801_IRQ_STATUS 0x5a /* Interrupt Status */
108 #define FM801_OPL3_BANK0 0x68 /* OPL3 Status Read / Bank 0 Write */
109 #define FM801_OPL3_DATA0 0x69 /* OPL3 Data 0 Write */
110 #define FM801_OPL3_BANK1 0x6a /* OPL3 Bank 1 Write */
111 #define FM801_OPL3_DATA1 0x6b /* OPL3 Bank 1 Write */
112 #define FM801_POWERDOWN 0x70 /* Blocks Power Down Control */
114 #define FM801_AC97_ADDR_SHIFT 10
116 /* playback and record control register bits */
117 #define FM801_BUF1_LAST (1<<1)
118 #define FM801_BUF2_LAST (1<<2)
119 #define FM801_START (1<<5)
120 #define FM801_PAUSE (1<<6)
121 #define FM801_IMMED_STOP (1<<7)
122 #define FM801_RATE_SHIFT 8
123 #define FM801_RATE_MASK (15 << FM801_RATE_SHIFT)
124 #define FM801_CHANNELS_4 (1<<12) /* playback only */
125 #define FM801_CHANNELS_6 (2<<12) /* playback only */
126 #define FM801_CHANNELS_6MS (3<<12) /* playback only */
127 #define FM801_CHANNELS_MASK (3<<12)
128 #define FM801_16BIT (1<<14)
129 #define FM801_STEREO (1<<15)
131 /* IRQ status bits */
132 #define FM801_IRQ_PLAYBACK (1<<8)
133 #define FM801_IRQ_CAPTURE (1<<9)
134 #define FM801_IRQ_VOLUME (1<<14)
135 #define FM801_IRQ_MPU (1<<15)
137 /* GPIO control register */
138 #define FM801_GPIO_GP0 (1<<0) /* read/write */
139 #define FM801_GPIO_GP1 (1<<1)
140 #define FM801_GPIO_GP2 (1<<2)
141 #define FM801_GPIO_GP3 (1<<3)
142 #define FM801_GPIO_GP(x) (1<<(0+(x)))
143 #define FM801_GPIO_GD0 (1<<8) /* directions: 1 = input, 0 = output*/
144 #define FM801_GPIO_GD1 (1<<9)
145 #define FM801_GPIO_GD2 (1<<10)
146 #define FM801_GPIO_GD3 (1<<11)
147 #define FM801_GPIO_GD(x) (1<<(8+(x)))
148 #define FM801_GPIO_GS0 (1<<12) /* function select: */
149 #define FM801_GPIO_GS1 (1<<13) /* 1 = GPIO */
150 #define FM801_GPIO_GS2 (1<<14) /* 0 = other (S/PDIF, VOL) */
151 #define FM801_GPIO_GS3 (1<<15)
152 #define FM801_GPIO_GS(x) (1<<(12+(x)))
158 typedef struct _snd_fm801 fm801_t;
163 unsigned long port; /* I/O port number */
164 struct resource *res_port;
165 unsigned int multichannel: 1, /* multichannel support */
166 secondary: 1; /* secondary codec */
167 unsigned char secondary_addr; /* address of the secondary codec */
169 unsigned short ply_ctrl; /* playback control */
170 unsigned short cap_ctrl; /* capture control */
172 unsigned long ply_buffer;
173 unsigned int ply_buf;
174 unsigned int ply_count;
175 unsigned int ply_size;
176 unsigned int ply_pos;
178 unsigned long cap_buffer;
179 unsigned int cap_buf;
180 unsigned int cap_count;
181 unsigned int cap_size;
182 unsigned int cap_pos;
184 ac97_bus_t *ac97_bus;
191 snd_rawmidi_t *rmidi;
192 snd_pcm_substream_t *playback_substream;
193 snd_pcm_substream_t *capture_substream;
194 unsigned int p_dma_size;
195 unsigned int c_dma_size;
198 snd_info_entry_t *proc_entry;
205 static struct pci_device_id snd_fm801_ids[] = {
206 { 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, }, /* FM801 */
210 MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
213 * common I/O routines
216 static int snd_fm801_update_bits(fm801_t *chip, unsigned short reg,
217 unsigned short mask, unsigned short value)
220 unsigned short old, new;
222 spin_lock(&chip->reg_lock);
223 old = inw(chip->port + reg);
224 new = (old & ~mask) | value;
227 outw(new, chip->port + reg);
228 spin_unlock(&chip->reg_lock);
232 static void snd_fm801_codec_write(ac97_t *ac97,
236 fm801_t *chip = snd_magic_cast(fm801_t, ac97->private_data, return);
240 * Wait until the codec interface is not ready..
242 for (idx = 0; idx < 100; idx++) {
243 if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
247 snd_printk("AC'97 interface is busy (1)\n");
251 /* write data and address */
252 outw(val, FM801_REG(chip, AC97_DATA));
253 outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
255 * Wait until the write command is not completed..
257 for (idx = 0; idx < 1000; idx++) {
258 if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
262 snd_printk("AC'97 interface #%d is busy (2)\n", ac97->num);
265 static unsigned short snd_fm801_codec_read(ac97_t *ac97, unsigned short reg)
267 fm801_t *chip = snd_magic_cast(fm801_t, ac97->private_data, return -ENXIO);
271 * Wait until the codec interface is not ready..
273 for (idx = 0; idx < 100; idx++) {
274 if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
278 snd_printk("AC'97 interface is busy (1)\n");
283 outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | (1<<7), FM801_REG(chip, AC97_CMD));
284 for (idx = 0; idx < 100; idx++) {
285 if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
289 snd_printk("AC'97 interface #%d is busy (2)\n", ac97->num);
293 for (idx = 0; idx < 1000; idx++) {
294 if (inw(FM801_REG(chip, AC97_CMD)) & (1<<8))
298 snd_printk("AC'97 interface #%d is not valid (2)\n", ac97->num);
302 return inw(FM801_REG(chip, AC97_DATA));
305 static unsigned int rates[] = {
306 5500, 8000, 9600, 11025,
307 16000, 19200, 22050, 32000,
311 #define RATES sizeof(rates) / sizeof(rates[0])
313 static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
319 static unsigned int channels[] = {
323 #define CHANNELS sizeof(channels) / sizeof(channels[0])
325 static snd_pcm_hw_constraint_list_t hw_constraints_channels = {
332 * Sample rate routines
335 static unsigned short snd_fm801_rate_bits(unsigned int rate)
339 for (idx = 0; idx < 11; idx++)
340 if (rates[idx] == rate)
350 static int snd_fm801_playback_trigger(snd_pcm_substream_t * substream,
353 fm801_t *chip = snd_pcm_substream_chip(substream);
355 spin_lock(&chip->reg_lock);
357 case SNDRV_PCM_TRIGGER_START:
358 chip->ply_ctrl &= ~(FM801_BUF1_LAST |
361 chip->ply_ctrl |= FM801_START |
364 case SNDRV_PCM_TRIGGER_STOP:
365 chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
367 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
368 chip->ply_ctrl |= FM801_PAUSE;
370 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
371 chip->ply_ctrl &= ~FM801_PAUSE;
374 spin_unlock(&chip->reg_lock);
378 outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
379 spin_unlock(&chip->reg_lock);
383 static int snd_fm801_capture_trigger(snd_pcm_substream_t * substream,
386 fm801_t *chip = snd_pcm_substream_chip(substream);
388 spin_lock(&chip->reg_lock);
390 case SNDRV_PCM_TRIGGER_START:
391 chip->cap_ctrl &= ~(FM801_BUF1_LAST |
394 chip->cap_ctrl |= FM801_START |
397 case SNDRV_PCM_TRIGGER_STOP:
398 chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
400 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
401 chip->cap_ctrl |= FM801_PAUSE;
403 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
404 chip->cap_ctrl &= ~FM801_PAUSE;
407 spin_unlock(&chip->reg_lock);
411 outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
412 spin_unlock(&chip->reg_lock);
416 static int snd_fm801_hw_params(snd_pcm_substream_t * substream,
417 snd_pcm_hw_params_t * hw_params)
419 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
422 static int snd_fm801_hw_free(snd_pcm_substream_t * substream)
424 return snd_pcm_lib_free_pages(substream);
427 static int snd_fm801_playback_prepare(snd_pcm_substream_t * substream)
430 fm801_t *chip = snd_pcm_substream_chip(substream);
431 snd_pcm_runtime_t *runtime = substream->runtime;
433 chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
434 chip->ply_count = snd_pcm_lib_period_bytes(substream);
435 spin_lock_irqsave(&chip->reg_lock, flags);
436 chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
437 FM801_STEREO | FM801_RATE_MASK |
438 FM801_CHANNELS_MASK);
439 if (snd_pcm_format_width(runtime->format) == 16)
440 chip->ply_ctrl |= FM801_16BIT;
441 if (runtime->channels > 1) {
442 chip->ply_ctrl |= FM801_STEREO;
443 if (runtime->channels == 4)
444 chip->ply_ctrl |= FM801_CHANNELS_4;
445 else if (runtime->channels == 6)
446 chip->ply_ctrl |= FM801_CHANNELS_6;
448 chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
450 outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
451 outw(chip->ply_count - 1, FM801_REG(chip, PLY_COUNT));
452 chip->ply_buffer = runtime->dma_addr;
454 outl(chip->ply_buffer, FM801_REG(chip, PLY_BUF1));
455 outl(chip->ply_buffer + (chip->ply_count % chip->ply_size), FM801_REG(chip, PLY_BUF2));
456 spin_unlock_irqrestore(&chip->reg_lock, flags);
460 static int snd_fm801_capture_prepare(snd_pcm_substream_t * substream)
463 fm801_t *chip = snd_pcm_substream_chip(substream);
464 snd_pcm_runtime_t *runtime = substream->runtime;
466 chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
467 chip->cap_count = snd_pcm_lib_period_bytes(substream);
468 spin_lock_irqsave(&chip->reg_lock, flags);
469 chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
470 FM801_STEREO | FM801_RATE_MASK);
471 if (snd_pcm_format_width(runtime->format) == 16)
472 chip->cap_ctrl |= FM801_16BIT;
473 if (runtime->channels > 1)
474 chip->cap_ctrl |= FM801_STEREO;
475 chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
477 outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
478 outw(chip->cap_count - 1, FM801_REG(chip, CAP_COUNT));
479 chip->cap_buffer = runtime->dma_addr;
481 outl(chip->cap_buffer, FM801_REG(chip, CAP_BUF1));
482 outl(chip->cap_buffer + (chip->cap_count % chip->cap_size), FM801_REG(chip, CAP_BUF2));
483 spin_unlock_irqrestore(&chip->reg_lock, flags);
487 static snd_pcm_uframes_t snd_fm801_playback_pointer(snd_pcm_substream_t * substream)
489 fm801_t *chip = snd_pcm_substream_chip(substream);
493 if (!(chip->ply_ctrl & FM801_START))
495 spin_lock_irqsave(&chip->reg_lock, flags);
496 ptr = chip->ply_pos + (chip->ply_count - 1) - inw(FM801_REG(chip, PLY_COUNT));
497 if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_PLAYBACK) {
498 ptr += chip->ply_count;
499 ptr %= chip->ply_size;
501 spin_unlock_irqrestore(&chip->reg_lock, flags);
502 return bytes_to_frames(substream->runtime, ptr);
505 static snd_pcm_uframes_t snd_fm801_capture_pointer(snd_pcm_substream_t * substream)
507 fm801_t *chip = snd_pcm_substream_chip(substream);
511 if (!(chip->cap_ctrl & FM801_START))
513 spin_lock_irqsave(&chip->reg_lock, flags);
514 ptr = chip->cap_pos + (chip->cap_count - 1) - inw(FM801_REG(chip, CAP_COUNT));
515 if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_CAPTURE) {
516 ptr += chip->cap_count;
517 ptr %= chip->cap_size;
519 spin_unlock_irqrestore(&chip->reg_lock, flags);
520 return bytes_to_frames(substream->runtime, ptr);
523 static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id, struct pt_regs *regs)
525 fm801_t *chip = snd_magic_cast(fm801_t, dev_id, return IRQ_NONE);
526 unsigned short status;
529 status = inw(FM801_REG(chip, IRQ_STATUS));
530 status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
534 outw(status, FM801_REG(chip, IRQ_STATUS));
535 if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
536 spin_lock(&chip->reg_lock);
538 chip->ply_pos += chip->ply_count;
539 chip->ply_pos %= chip->ply_size;
540 tmp = chip->ply_pos + chip->ply_count;
541 tmp %= chip->ply_size;
542 outl(chip->ply_buffer + tmp,
543 (chip->ply_buf & 1) ?
544 FM801_REG(chip, PLY_BUF1) :
545 FM801_REG(chip, PLY_BUF2));
546 spin_unlock(&chip->reg_lock);
547 snd_pcm_period_elapsed(chip->playback_substream);
549 if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
550 spin_lock(&chip->reg_lock);
552 chip->cap_pos += chip->cap_count;
553 chip->cap_pos %= chip->cap_size;
554 tmp = chip->cap_pos + chip->cap_count;
555 tmp %= chip->cap_size;
556 outl(chip->cap_buffer + tmp,
557 (chip->cap_buf & 1) ?
558 FM801_REG(chip, CAP_BUF1) :
559 FM801_REG(chip, CAP_BUF2));
560 spin_unlock(&chip->reg_lock);
561 snd_pcm_period_elapsed(chip->capture_substream);
563 if (chip->rmidi && (status & FM801_IRQ_MPU))
564 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
565 if (status & FM801_IRQ_VOLUME)
571 static snd_pcm_hardware_t snd_fm801_playback =
573 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
574 SNDRV_PCM_INFO_BLOCK_TRANSFER |
575 SNDRV_PCM_INFO_PAUSE |
576 SNDRV_PCM_INFO_MMAP_VALID),
577 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
578 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
583 .buffer_bytes_max = (128*1024),
584 .period_bytes_min = 64,
585 .period_bytes_max = (128*1024),
591 static snd_pcm_hardware_t snd_fm801_capture =
593 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
594 SNDRV_PCM_INFO_BLOCK_TRANSFER |
595 SNDRV_PCM_INFO_PAUSE |
596 SNDRV_PCM_INFO_MMAP_VALID),
597 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
598 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
603 .buffer_bytes_max = (128*1024),
604 .period_bytes_min = 64,
605 .period_bytes_max = (128*1024),
611 static int snd_fm801_playback_open(snd_pcm_substream_t * substream)
613 fm801_t *chip = snd_pcm_substream_chip(substream);
614 snd_pcm_runtime_t *runtime = substream->runtime;
617 chip->playback_substream = substream;
618 runtime->hw = snd_fm801_playback;
619 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
620 if (chip->multichannel) {
621 runtime->hw.channels_max = 6;
622 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels);
624 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
629 static int snd_fm801_capture_open(snd_pcm_substream_t * substream)
631 fm801_t *chip = snd_pcm_substream_chip(substream);
632 snd_pcm_runtime_t *runtime = substream->runtime;
635 chip->capture_substream = substream;
636 runtime->hw = snd_fm801_capture;
637 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
638 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
643 static int snd_fm801_playback_close(snd_pcm_substream_t * substream)
645 fm801_t *chip = snd_pcm_substream_chip(substream);
647 chip->playback_substream = NULL;
651 static int snd_fm801_capture_close(snd_pcm_substream_t * substream)
653 fm801_t *chip = snd_pcm_substream_chip(substream);
655 chip->capture_substream = NULL;
659 static snd_pcm_ops_t snd_fm801_playback_ops = {
660 .open = snd_fm801_playback_open,
661 .close = snd_fm801_playback_close,
662 .ioctl = snd_pcm_lib_ioctl,
663 .hw_params = snd_fm801_hw_params,
664 .hw_free = snd_fm801_hw_free,
665 .prepare = snd_fm801_playback_prepare,
666 .trigger = snd_fm801_playback_trigger,
667 .pointer = snd_fm801_playback_pointer,
670 static snd_pcm_ops_t snd_fm801_capture_ops = {
671 .open = snd_fm801_capture_open,
672 .close = snd_fm801_capture_close,
673 .ioctl = snd_pcm_lib_ioctl,
674 .hw_params = snd_fm801_hw_params,
675 .hw_free = snd_fm801_hw_free,
676 .prepare = snd_fm801_capture_prepare,
677 .trigger = snd_fm801_capture_trigger,
678 .pointer = snd_fm801_capture_pointer,
681 static void snd_fm801_pcm_free(snd_pcm_t *pcm)
683 fm801_t *chip = snd_magic_cast(fm801_t, pcm->private_data, return);
685 snd_pcm_lib_preallocate_free_for_all(pcm);
688 static int __devinit snd_fm801_pcm(fm801_t *chip, int device, snd_pcm_t ** rpcm)
695 if ((err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm)) < 0)
698 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
699 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
701 pcm->private_data = chip;
702 pcm->private_free = snd_fm801_pcm_free;
704 strcpy(pcm->name, "FM801");
707 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
708 snd_dma_pci_data(chip->pci),
709 chip->multichannel ? 128*1024 : 64*1024, 128*1024);
722 /* 256PCS GPIO numbers */
723 #define TEA_256PCS_DATA 1
724 #define TEA_256PCS_WRITE_ENABLE 2 /* inverted */
725 #define TEA_256PCS_BUS_CLOCK 3
727 static void snd_fm801_tea575x_256pcs_write(tea575x_t *tea, unsigned int val)
729 fm801_t *chip = tea->private_data;
733 spin_lock_irq(&chip->reg_lock);
734 reg = inw(FM801_REG(chip, GPIO_CTRL));
735 /* use GPIO lines and set write enable bit */
736 reg |= FM801_GPIO_GS(TEA_256PCS_DATA) |
737 FM801_GPIO_GS(TEA_256PCS_WRITE_ENABLE) |
738 FM801_GPIO_GS(TEA_256PCS_BUS_CLOCK);
739 /* all of lines are in the write direction */
740 /* clear data and clock lines */
741 reg &= ~(FM801_GPIO_GD(TEA_256PCS_DATA) |
742 FM801_GPIO_GD(TEA_256PCS_WRITE_ENABLE) |
743 FM801_GPIO_GD(TEA_256PCS_BUS_CLOCK) |
744 FM801_GPIO_GP(TEA_256PCS_DATA) |
745 FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK) |
746 FM801_GPIO_GP(TEA_256PCS_WRITE_ENABLE));
747 outw(reg, FM801_REG(chip, GPIO_CTRL));
752 reg |= FM801_GPIO_GP(TEA_256PCS_DATA);
754 reg &= ~FM801_GPIO_GP(TEA_256PCS_DATA);
755 outw(reg, FM801_REG(chip, GPIO_CTRL));
757 reg |= FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK);
758 outw(reg, FM801_REG(chip, GPIO_CTRL));
759 reg &= ~FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK);
760 outw(reg, FM801_REG(chip, GPIO_CTRL));
764 /* and reset the write enable bit */
765 reg |= FM801_GPIO_GP(TEA_256PCS_WRITE_ENABLE) |
766 FM801_GPIO_GP(TEA_256PCS_DATA);
767 outw(reg, FM801_REG(chip, GPIO_CTRL));
768 spin_unlock_irq(&chip->reg_lock);
771 static unsigned int snd_fm801_tea575x_256pcs_read(tea575x_t *tea)
773 fm801_t *chip = tea->private_data;
775 unsigned int val = 0;
778 spin_lock_irq(&chip->reg_lock);
779 reg = inw(FM801_REG(chip, GPIO_CTRL));
780 /* use GPIO lines, set data direction to input */
781 reg |= FM801_GPIO_GS(TEA_256PCS_DATA) |
782 FM801_GPIO_GS(TEA_256PCS_WRITE_ENABLE) |
783 FM801_GPIO_GS(TEA_256PCS_BUS_CLOCK) |
784 FM801_GPIO_GD(TEA_256PCS_DATA) |
785 FM801_GPIO_GP(TEA_256PCS_DATA) |
786 FM801_GPIO_GP(TEA_256PCS_WRITE_ENABLE);
787 /* all of lines are in the write direction, except data */
788 /* clear data, write enable and clock lines */
789 reg &= ~(FM801_GPIO_GD(TEA_256PCS_WRITE_ENABLE) |
790 FM801_GPIO_GD(TEA_256PCS_BUS_CLOCK) |
791 FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK));
793 for (i = 0; i < 24; i++) {
794 reg &= ~FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK);
795 outw(reg, FM801_REG(chip, GPIO_CTRL));
797 reg |= FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK);
798 outw(reg, FM801_REG(chip, GPIO_CTRL));
801 if (inw(FM801_REG(chip, GPIO_CTRL)) & FM801_GPIO_GP(TEA_256PCS_DATA))
805 spin_unlock_irq(&chip->reg_lock);
810 /* 256PCPR GPIO numbers */
811 #define TEA_256PCPR_BUS_CLOCK 0
812 #define TEA_256PCPR_DATA 1
813 #define TEA_256PCPR_WRITE_ENABLE 2 /* inverted */
815 static void snd_fm801_tea575x_256pcpr_write(tea575x_t *tea, unsigned int val)
817 fm801_t *chip = tea->private_data;
821 spin_lock_irq(&chip->reg_lock);
822 reg = inw(FM801_REG(chip, GPIO_CTRL));
823 /* use GPIO lines and set write enable bit */
824 reg |= FM801_GPIO_GS(TEA_256PCPR_DATA) |
825 FM801_GPIO_GS(TEA_256PCPR_WRITE_ENABLE) |
826 FM801_GPIO_GS(TEA_256PCPR_BUS_CLOCK);
827 /* all of lines are in the write direction */
828 /* clear data and clock lines */
829 reg &= ~(FM801_GPIO_GD(TEA_256PCPR_DATA) |
830 FM801_GPIO_GD(TEA_256PCPR_WRITE_ENABLE) |
831 FM801_GPIO_GD(TEA_256PCPR_BUS_CLOCK) |
832 FM801_GPIO_GP(TEA_256PCPR_DATA) |
833 FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK) |
834 FM801_GPIO_GP(TEA_256PCPR_WRITE_ENABLE));
835 outw(reg, FM801_REG(chip, GPIO_CTRL));
840 reg |= FM801_GPIO_GP(TEA_256PCPR_DATA);
842 reg &= ~FM801_GPIO_GP(TEA_256PCPR_DATA);
843 outw(reg, FM801_REG(chip, GPIO_CTRL));
845 reg |= FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK);
846 outw(reg, FM801_REG(chip, GPIO_CTRL));
847 reg &= ~FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK);
848 outw(reg, FM801_REG(chip, GPIO_CTRL));
852 /* and reset the write enable bit */
853 reg |= FM801_GPIO_GP(TEA_256PCPR_WRITE_ENABLE) |
854 FM801_GPIO_GP(TEA_256PCPR_DATA);
855 outw(reg, FM801_REG(chip, GPIO_CTRL));
856 spin_unlock_irq(&chip->reg_lock);
859 static unsigned int snd_fm801_tea575x_256pcpr_read(tea575x_t *tea)
861 fm801_t *chip = tea->private_data;
863 unsigned int val = 0;
866 spin_lock_irq(&chip->reg_lock);
867 reg = inw(FM801_REG(chip, GPIO_CTRL));
868 /* use GPIO lines, set data direction to input */
869 reg |= FM801_GPIO_GS(TEA_256PCPR_DATA) |
870 FM801_GPIO_GS(TEA_256PCPR_WRITE_ENABLE) |
871 FM801_GPIO_GS(TEA_256PCPR_BUS_CLOCK) |
872 FM801_GPIO_GD(TEA_256PCPR_DATA) |
873 FM801_GPIO_GP(TEA_256PCPR_DATA) |
874 FM801_GPIO_GP(TEA_256PCPR_WRITE_ENABLE);
875 /* all of lines are in the write direction, except data */
876 /* clear data, write enable and clock lines */
877 reg &= ~(FM801_GPIO_GD(TEA_256PCPR_WRITE_ENABLE) |
878 FM801_GPIO_GD(TEA_256PCPR_BUS_CLOCK) |
879 FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK));
881 for (i = 0; i < 24; i++) {
882 reg &= ~FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK);
883 outw(reg, FM801_REG(chip, GPIO_CTRL));
885 reg |= FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK);
886 outw(reg, FM801_REG(chip, GPIO_CTRL));
889 if (inw(FM801_REG(chip, GPIO_CTRL)) & FM801_GPIO_GP(TEA_256PCPR_DATA))
893 spin_unlock_irq(&chip->reg_lock);
898 /* 64PCR GPIO numbers */
899 #define TEA_64PCR_BUS_CLOCK 0
900 #define TEA_64PCR_WRITE_ENABLE 1 /* inverted */
901 #define TEA_64PCR_DATA 2
903 static void snd_fm801_tea575x_64pcr_write(tea575x_t *tea, unsigned int val)
905 fm801_t *chip = tea->private_data;
909 spin_lock_irq(&chip->reg_lock);
910 reg = inw(FM801_REG(chip, GPIO_CTRL));
911 /* use GPIO lines and set write enable bit */
912 reg |= FM801_GPIO_GS(TEA_64PCR_DATA) |
913 FM801_GPIO_GS(TEA_64PCR_WRITE_ENABLE) |
914 FM801_GPIO_GS(TEA_64PCR_BUS_CLOCK);
915 /* all of lines are in the write direction */
916 /* clear data and clock lines */
917 reg &= ~(FM801_GPIO_GD(TEA_64PCR_DATA) |
918 FM801_GPIO_GD(TEA_64PCR_WRITE_ENABLE) |
919 FM801_GPIO_GD(TEA_64PCR_BUS_CLOCK) |
920 FM801_GPIO_GP(TEA_64PCR_DATA) |
921 FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK) |
922 FM801_GPIO_GP(TEA_64PCR_WRITE_ENABLE));
923 outw(reg, FM801_REG(chip, GPIO_CTRL));
928 reg |= FM801_GPIO_GP(TEA_64PCR_DATA);
930 reg &= ~FM801_GPIO_GP(TEA_64PCR_DATA);
931 outw(reg, FM801_REG(chip, GPIO_CTRL));
933 reg |= FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK);
934 outw(reg, FM801_REG(chip, GPIO_CTRL));
935 reg &= ~FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK);
936 outw(reg, FM801_REG(chip, GPIO_CTRL));
940 /* and reset the write enable bit */
941 reg |= FM801_GPIO_GP(TEA_64PCR_WRITE_ENABLE) |
942 FM801_GPIO_GP(TEA_64PCR_DATA);
943 outw(reg, FM801_REG(chip, GPIO_CTRL));
944 spin_unlock_irq(&chip->reg_lock);
947 static unsigned int snd_fm801_tea575x_64pcr_read(tea575x_t *tea)
949 fm801_t *chip = tea->private_data;
951 unsigned int val = 0;
954 spin_lock_irq(&chip->reg_lock);
955 reg = inw(FM801_REG(chip, GPIO_CTRL));
956 /* use GPIO lines, set data direction to input */
957 reg |= FM801_GPIO_GS(TEA_64PCR_DATA) |
958 FM801_GPIO_GS(TEA_64PCR_WRITE_ENABLE) |
959 FM801_GPIO_GS(TEA_64PCR_BUS_CLOCK) |
960 FM801_GPIO_GD(TEA_64PCR_DATA) |
961 FM801_GPIO_GP(TEA_64PCR_DATA) |
962 FM801_GPIO_GP(TEA_64PCR_WRITE_ENABLE);
963 /* all of lines are in the write direction, except data */
964 /* clear data, write enable and clock lines */
965 reg &= ~(FM801_GPIO_GD(TEA_64PCR_WRITE_ENABLE) |
966 FM801_GPIO_GD(TEA_64PCR_BUS_CLOCK) |
967 FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK));
969 for (i = 0; i < 24; i++) {
970 reg &= ~FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK);
971 outw(reg, FM801_REG(chip, GPIO_CTRL));
973 reg |= FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK);
974 outw(reg, FM801_REG(chip, GPIO_CTRL));
977 if (inw(FM801_REG(chip, GPIO_CTRL)) & FM801_GPIO_GP(TEA_64PCR_DATA))
981 spin_unlock_irq(&chip->reg_lock);
986 static struct snd_tea575x_ops snd_fm801_tea_ops[3] = {
988 /* 1 = MediaForte 256-PCS */
989 .write = snd_fm801_tea575x_256pcs_write,
990 .read = snd_fm801_tea575x_256pcs_read,
993 /* 2 = MediaForte 256-PCPR */
994 .write = snd_fm801_tea575x_256pcpr_write,
995 .read = snd_fm801_tea575x_256pcpr_read,
998 /* 3 = MediaForte 64-PCR */
999 .write = snd_fm801_tea575x_64pcr_write,
1000 .read = snd_fm801_tea575x_64pcr_read,
1009 #define FM801_SINGLE(xname, reg, shift, mask, invert) \
1010 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
1011 .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
1012 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1014 static int snd_fm801_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1016 int mask = (kcontrol->private_value >> 16) & 0xff;
1018 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1020 uinfo->value.integer.min = 0;
1021 uinfo->value.integer.max = mask;
1025 static int snd_fm801_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1027 fm801_t *chip = snd_kcontrol_chip(kcontrol);
1028 int reg = kcontrol->private_value & 0xff;
1029 int shift = (kcontrol->private_value >> 8) & 0xff;
1030 int mask = (kcontrol->private_value >> 16) & 0xff;
1031 int invert = (kcontrol->private_value >> 24) & 0xff;
1033 ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift) & mask;
1035 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1039 static int snd_fm801_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1041 fm801_t *chip = snd_kcontrol_chip(kcontrol);
1042 int reg = kcontrol->private_value & 0xff;
1043 int shift = (kcontrol->private_value >> 8) & 0xff;
1044 int mask = (kcontrol->private_value >> 16) & 0xff;
1045 int invert = (kcontrol->private_value >> 24) & 0xff;
1048 val = (ucontrol->value.integer.value[0] & mask);
1051 return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
1054 #define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
1055 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
1056 .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
1057 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
1059 static int snd_fm801_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1061 int mask = (kcontrol->private_value >> 16) & 0xff;
1063 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1065 uinfo->value.integer.min = 0;
1066 uinfo->value.integer.max = mask;
1070 static int snd_fm801_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1072 fm801_t *chip = snd_kcontrol_chip(kcontrol);
1073 int reg = kcontrol->private_value & 0xff;
1074 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
1075 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
1076 int mask = (kcontrol->private_value >> 16) & 0xff;
1077 int invert = (kcontrol->private_value >> 24) & 0xff;
1079 spin_lock(&chip->reg_lock);
1080 ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift_left) & mask;
1081 ucontrol->value.integer.value[1] = (inw(chip->port + reg) >> shift_right) & mask;
1082 spin_unlock(&chip->reg_lock);
1084 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1085 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1090 static int snd_fm801_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1092 fm801_t *chip = snd_kcontrol_chip(kcontrol);
1093 int reg = kcontrol->private_value & 0xff;
1094 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
1095 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
1096 int mask = (kcontrol->private_value >> 16) & 0xff;
1097 int invert = (kcontrol->private_value >> 24) & 0xff;
1098 unsigned short val1, val2;
1100 val1 = ucontrol->value.integer.value[0] & mask;
1101 val2 = ucontrol->value.integer.value[1] & mask;
1106 return snd_fm801_update_bits(chip, reg,
1107 (mask << shift_left) | (mask << shift_right),
1108 (val1 << shift_left ) | (val2 << shift_right));
1111 static int snd_fm801_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1113 static char *texts[5] = {
1114 "AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
1117 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1119 uinfo->value.enumerated.items = 5;
1120 if (uinfo->value.enumerated.item > 4)
1121 uinfo->value.enumerated.item = 4;
1122 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1126 static int snd_fm801_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1128 fm801_t *chip = snd_kcontrol_chip(kcontrol);
1131 val = inw(FM801_REG(chip, REC_SRC)) & 7;
1134 ucontrol->value.enumerated.item[0] = val;
1138 static int snd_fm801_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1140 fm801_t *chip = snd_kcontrol_chip(kcontrol);
1143 if ((val = ucontrol->value.enumerated.item[0]) > 4)
1145 return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
1148 #define FM801_CONTROLS (sizeof(snd_fm801_controls)/sizeof(snd_kcontrol_new_t))
1150 static snd_kcontrol_new_t snd_fm801_controls[] __devinitdata = {
1151 FM801_DOUBLE("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1),
1152 FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
1153 FM801_DOUBLE("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1),
1154 FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
1155 FM801_DOUBLE("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1),
1156 FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
1158 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1159 .name = "Digital Capture Source",
1160 .info = snd_fm801_info_mux,
1161 .get = snd_fm801_get_mux,
1162 .put = snd_fm801_put_mux,
1166 #define FM801_CONTROLS_MULTI (sizeof(snd_fm801_controls_multi)/sizeof(snd_kcontrol_new_t))
1168 static snd_kcontrol_new_t snd_fm801_controls_multi[] __devinitdata = {
1169 FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
1170 FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
1171 FM801_SINGLE("IEC958 Capture Switch", FM801_I2S_MODE, 8, 1, 0),
1172 FM801_SINGLE("IEC958 Raw Data Playback Switch", FM801_I2S_MODE, 9, 1, 0),
1173 FM801_SINGLE("IEC958 Raw Data Capture Switch", FM801_I2S_MODE, 10, 1, 0),
1174 FM801_SINGLE("IEC958 Playback Switch", FM801_GEN_CTRL, 2, 1, 0),
1177 static void snd_fm801_mixer_free_ac97_bus(ac97_bus_t *bus)
1179 fm801_t *chip = snd_magic_cast(fm801_t, bus->private_data, return);
1180 chip->ac97_bus = NULL;
1183 static void snd_fm801_mixer_free_ac97(ac97_t *ac97)
1185 fm801_t *chip = snd_magic_cast(fm801_t, ac97->private_data, return);
1186 if (ac97->num == 0) {
1189 chip->ac97_sec = NULL;
1193 static int __devinit snd_fm801_mixer(fm801_t *chip)
1200 memset(&bus, 0, sizeof(bus));
1201 bus.write = snd_fm801_codec_write;
1202 bus.read = snd_fm801_codec_read;
1203 bus.private_data = chip;
1204 bus.private_free = snd_fm801_mixer_free_ac97_bus;
1205 if ((err = snd_ac97_bus(chip->card, &bus, &chip->ac97_bus)) < 0)
1208 memset(&ac97, 0, sizeof(ac97));
1209 ac97.private_data = chip;
1210 ac97.private_free = snd_fm801_mixer_free_ac97;
1211 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1213 if (chip->secondary) {
1215 ac97.addr = chip->secondary_addr;
1216 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec)) < 0)
1219 for (i = 0; i < FM801_CONTROLS; i++)
1220 snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls[i], chip));
1221 if (chip->multichannel) {
1222 for (i = 0; i < FM801_CONTROLS_MULTI; i++)
1223 snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
1229 * initialization routines
1232 static int snd_fm801_free(fm801_t *chip)
1234 unsigned short cmdw;
1239 /* interrupt setup - mask everything */
1240 cmdw = inw(FM801_REG(chip, IRQ_MASK));
1242 outw(cmdw, FM801_REG(chip, IRQ_MASK));
1245 #ifdef TEA575X_RADIO
1246 snd_tea575x_exit(&chip->tea);
1248 if (chip->res_port) {
1249 release_resource(chip->res_port);
1250 kfree_nocheck(chip->res_port);
1253 free_irq(chip->irq, (void *)chip);
1255 snd_magic_kfree(chip);
1259 static int snd_fm801_dev_free(snd_device_t *device)
1261 fm801_t *chip = snd_magic_cast(fm801_t, device->device_data, return -ENXIO);
1262 return snd_fm801_free(chip);
1265 static int __devinit snd_fm801_create(snd_card_t * card,
1266 struct pci_dev * pci,
1271 unsigned char rev, id;
1272 unsigned short cmdw;
1273 unsigned long timeout;
1275 static snd_device_ops_t ops = {
1276 .dev_free = snd_fm801_dev_free,
1280 if ((err = pci_enable_device(pci)) < 0)
1282 chip = snd_magic_kcalloc(fm801_t, 0, GFP_KERNEL);
1285 spin_lock_init(&chip->reg_lock);
1289 chip->port = pci_resource_start(pci, 0);
1290 if ((chip->res_port = request_region(chip->port, 0x80, "FM801")) == NULL) {
1291 snd_printk("unable to grab region 0x%lx-0x%lx\n", chip->port, chip->port + 0x80 - 1);
1292 snd_fm801_free(chip);
1295 if (request_irq(pci->irq, snd_fm801_interrupt, SA_INTERRUPT|SA_SHIRQ, "FM801", (void *)chip)) {
1296 snd_printk("unable to grab IRQ %d\n", chip->irq);
1297 snd_fm801_free(chip);
1300 chip->irq = pci->irq;
1301 pci_set_master(pci);
1303 pci_read_config_byte(pci, PCI_REVISION_ID, &rev);
1304 if (rev >= 0xb1) /* FM801-AU */
1305 chip->multichannel = 1;
1307 /* codec cold reset + AC'97 warm reset */
1308 outw((1<<5)|(1<<6), FM801_REG(chip, CODEC_CTRL));
1309 inw(FM801_REG(chip, CODEC_CTRL)); /* flush posting data */
1311 outw(0, FM801_REG(chip, CODEC_CTRL));
1313 timeout = (jiffies + (3 * HZ) / 4) + 1; /* min 750ms */
1315 outw((1<<7) | (0 << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
1318 if ((inw(FM801_REG(chip, AC97_CMD)) & (3<<8)) == (1<<8))
1319 goto __ac97_secondary;
1320 set_current_state(TASK_UNINTERRUPTIBLE);
1321 schedule_timeout(1);
1322 } while (time_after(timeout, jiffies));
1323 snd_printk("Primary AC'97 codec not found\n");
1324 snd_fm801_free(chip);
1328 if (!chip->multichannel) /* lookup is not required */
1330 for (id = 3; id > 0; id--) { /* my card has the secondary codec */
1331 /* at address #3, so the loop is inverted */
1333 timeout = jiffies + HZ / 20;
1335 outw((1<<7) | (id << FM801_AC97_ADDR_SHIFT) | AC97_VENDOR_ID1, FM801_REG(chip, AC97_CMD));
1338 if ((inw(FM801_REG(chip, AC97_CMD)) & (3<<8)) == (1<<8)) {
1339 cmdw = inw(FM801_REG(chip, AC97_DATA));
1340 if (cmdw != 0xffff && cmdw != 0) {
1341 chip->secondary = 1;
1342 chip->secondary_addr = id;
1346 set_current_state(TASK_UNINTERRUPTIBLE);
1347 schedule_timeout(1);
1348 } while (time_after(timeout, jiffies));
1351 /* the recovery phase, it seems that probing for non-existing codec might */
1352 /* cause timeout problems */
1353 timeout = (jiffies + (3 * HZ) / 4) + 1; /* min 750ms */
1355 outw((1<<7) | (0 << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
1358 if ((inw(FM801_REG(chip, AC97_CMD)) & (3<<8)) == (1<<8))
1360 set_current_state(TASK_UNINTERRUPTIBLE);
1361 schedule_timeout(1);
1362 } while (time_after(timeout, jiffies));
1363 snd_printk("Primary AC'97 codec not responding\n");
1364 snd_fm801_free(chip);
1370 outw(0x0808, FM801_REG(chip, PCM_VOL));
1371 outw(0x9f1f, FM801_REG(chip, FM_VOL));
1372 outw(0x8808, FM801_REG(chip, I2S_VOL));
1374 /* I2S control - I2S mode */
1375 outw(0x0003, FM801_REG(chip, I2S_MODE));
1377 /* interrupt setup - unmask MPU, PLAYBACK & CAPTURE */
1378 cmdw = inw(FM801_REG(chip, IRQ_MASK));
1380 outw(cmdw, FM801_REG(chip, IRQ_MASK));
1382 /* interrupt clear */
1383 outw(FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU, FM801_REG(chip, IRQ_STATUS));
1385 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1386 snd_fm801_free(chip);
1390 snd_card_set_dev(card, &pci->dev);
1392 #ifdef TEA575X_RADIO
1393 if (tea575x_tuner > 0 && (tea575x_tuner & 0xffff) < 4) {
1394 chip->tea.dev_nr = tea575x_tuner >> 16;
1395 chip->tea.card = card;
1396 chip->tea.freq_fixup = 10700;
1397 chip->tea.private_data = chip;
1398 chip->tea.ops = &snd_fm801_tea_ops[(tea575x_tuner & 0xffff) - 1];
1399 snd_tea575x_init(&chip->tea);
1407 static int __devinit snd_card_fm801_probe(struct pci_dev *pci,
1408 const struct pci_device_id *pci_id)
1416 if (dev >= SNDRV_CARDS)
1423 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1426 if ((err = snd_fm801_create(card, pci, tea575x_tuner[dev], &chip)) < 0) {
1427 snd_card_free(card);
1431 strcpy(card->driver, "FM801");
1432 strcpy(card->shortname, "ForteMedia FM801-");
1433 strcat(card->shortname, chip->multichannel ? "AU" : "AS");
1434 sprintf(card->longname, "%s at 0x%lx, irq %i",
1435 card->shortname, chip->port, chip->irq);
1437 if ((err = snd_fm801_pcm(chip, 0, NULL)) < 0) {
1438 snd_card_free(card);
1441 if ((err = snd_fm801_mixer(chip)) < 0) {
1442 snd_card_free(card);
1445 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1446 FM801_REG(chip, MPU401_DATA), 1,
1447 chip->irq, 0, &chip->rmidi)) < 0) {
1448 snd_card_free(card);
1451 if ((err = snd_opl3_create(card, FM801_REG(chip, OPL3_BANK0),
1452 FM801_REG(chip, OPL3_BANK1),
1453 OPL3_HW_OPL3_FM801, 1, &opl3)) < 0) {
1454 snd_card_free(card);
1457 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
1458 snd_card_free(card);
1462 if ((err = snd_card_register(card)) < 0) {
1463 snd_card_free(card);
1466 pci_set_drvdata(pci, card);
1471 static void __devexit snd_card_fm801_remove(struct pci_dev *pci)
1473 snd_card_free(pci_get_drvdata(pci));
1474 pci_set_drvdata(pci, NULL);
1477 static struct pci_driver driver = {
1479 .id_table = snd_fm801_ids,
1480 .probe = snd_card_fm801_probe,
1481 .remove = __devexit_p(snd_card_fm801_remove),
1484 static int __init alsa_card_fm801_init(void)
1486 return pci_module_init(&driver);
1489 static void __exit alsa_card_fm801_exit(void)
1491 pci_unregister_driver(&driver);
1494 module_init(alsa_card_fm801_init)
1495 module_exit(alsa_card_fm801_exit)