ALSA: HDA: Add new revision for ALC662
[linux-2.6.git] / sound / pci / fm801.c
1 /*
2  *  The driver for the ForteMedia FM801 based soundcards
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4  *
5  *  Support FM only card by Andy Shevchenko <andy@smile.org.ua>
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 2 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  *
21  */
22
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/tlv.h>
32 #include <sound/ac97_codec.h>
33 #include <sound/mpu401.h>
34 #include <sound/opl3.h>
35 #include <sound/initval.h>
36
37 #include <asm/io.h>
38
39 #ifdef CONFIG_SND_FM801_TEA575X_BOOL
40 #include <sound/tea575x-tuner.h>
41 #endif
42
43 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
44 MODULE_DESCRIPTION("ForteMedia FM801");
45 MODULE_LICENSE("GPL");
46 MODULE_SUPPORTED_DEVICE("{{ForteMedia,FM801},"
47                 "{Genius,SoundMaker Live 5.1}}");
48
49 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
50 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
51 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable this card */
52 /*
53  *  Enable TEA575x tuner
54  *    1 = MediaForte 256-PCS
55  *    2 = MediaForte 256-PCP
56  *    3 = MediaForte 64-PCR
57  *   16 = setup tuner only (this is additional bit), i.e. SF64-PCR FM card
58  *  High 16-bits are video (radio) device number + 1
59  */
60 static int tea575x_tuner[SNDRV_CARDS];
61
62 module_param_array(index, int, NULL, 0444);
63 MODULE_PARM_DESC(index, "Index value for the FM801 soundcard.");
64 module_param_array(id, charp, NULL, 0444);
65 MODULE_PARM_DESC(id, "ID string for the FM801 soundcard.");
66 module_param_array(enable, bool, NULL, 0444);
67 MODULE_PARM_DESC(enable, "Enable FM801 soundcard.");
68 module_param_array(tea575x_tuner, int, NULL, 0444);
69 MODULE_PARM_DESC(tea575x_tuner, "TEA575x tuner access method (0 = auto, 1 = SF256-PCS, 2=SF256-PCP, 3=SF64-PCR, 8=disable, +16=tuner-only).");
70
71 #define TUNER_DISABLED          (1<<3)
72 #define TUNER_ONLY              (1<<4)
73 #define TUNER_TYPE_MASK         (~TUNER_ONLY & 0xFFFF)
74
75 /*
76  *  Direct registers
77  */
78
79 #define FM801_REG(chip, reg)    (chip->port + FM801_##reg)
80
81 #define FM801_PCM_VOL           0x00    /* PCM Output Volume */
82 #define FM801_FM_VOL            0x02    /* FM Output Volume */
83 #define FM801_I2S_VOL           0x04    /* I2S Volume */
84 #define FM801_REC_SRC           0x06    /* Record Source */
85 #define FM801_PLY_CTRL          0x08    /* Playback Control */
86 #define FM801_PLY_COUNT         0x0a    /* Playback Count */
87 #define FM801_PLY_BUF1          0x0c    /* Playback Bufer I */
88 #define FM801_PLY_BUF2          0x10    /* Playback Buffer II */
89 #define FM801_CAP_CTRL          0x14    /* Capture Control */
90 #define FM801_CAP_COUNT         0x16    /* Capture Count */
91 #define FM801_CAP_BUF1          0x18    /* Capture Buffer I */
92 #define FM801_CAP_BUF2          0x1c    /* Capture Buffer II */
93 #define FM801_CODEC_CTRL        0x22    /* Codec Control */
94 #define FM801_I2S_MODE          0x24    /* I2S Mode Control */
95 #define FM801_VOLUME            0x26    /* Volume Up/Down/Mute Status */
96 #define FM801_I2C_CTRL          0x29    /* I2C Control */
97 #define FM801_AC97_CMD          0x2a    /* AC'97 Command */
98 #define FM801_AC97_DATA         0x2c    /* AC'97 Data */
99 #define FM801_MPU401_DATA       0x30    /* MPU401 Data */
100 #define FM801_MPU401_CMD        0x31    /* MPU401 Command */
101 #define FM801_GPIO_CTRL         0x52    /* General Purpose I/O Control */
102 #define FM801_GEN_CTRL          0x54    /* General Control */
103 #define FM801_IRQ_MASK          0x56    /* Interrupt Mask */
104 #define FM801_IRQ_STATUS        0x5a    /* Interrupt Status */
105 #define FM801_OPL3_BANK0        0x68    /* OPL3 Status Read / Bank 0 Write */
106 #define FM801_OPL3_DATA0        0x69    /* OPL3 Data 0 Write */
107 #define FM801_OPL3_BANK1        0x6a    /* OPL3 Bank 1 Write */
108 #define FM801_OPL3_DATA1        0x6b    /* OPL3 Bank 1 Write */
109 #define FM801_POWERDOWN         0x70    /* Blocks Power Down Control */
110
111 /* codec access */
112 #define FM801_AC97_READ         (1<<7)  /* read=1, write=0 */
113 #define FM801_AC97_VALID        (1<<8)  /* port valid=1 */
114 #define FM801_AC97_BUSY         (1<<9)  /* busy=1 */
115 #define FM801_AC97_ADDR_SHIFT   10      /* codec id (2bit) */
116
117 /* playback and record control register bits */
118 #define FM801_BUF1_LAST         (1<<1)
119 #define FM801_BUF2_LAST         (1<<2)
120 #define FM801_START             (1<<5)
121 #define FM801_PAUSE             (1<<6)
122 #define FM801_IMMED_STOP        (1<<7)
123 #define FM801_RATE_SHIFT        8
124 #define FM801_RATE_MASK         (15 << FM801_RATE_SHIFT)
125 #define FM801_CHANNELS_4        (1<<12) /* playback only */
126 #define FM801_CHANNELS_6        (2<<12) /* playback only */
127 #define FM801_CHANNELS_6MS      (3<<12) /* playback only */
128 #define FM801_CHANNELS_MASK     (3<<12)
129 #define FM801_16BIT             (1<<14)
130 #define FM801_STEREO            (1<<15)
131
132 /* IRQ status bits */
133 #define FM801_IRQ_PLAYBACK      (1<<8)
134 #define FM801_IRQ_CAPTURE       (1<<9)
135 #define FM801_IRQ_VOLUME        (1<<14)
136 #define FM801_IRQ_MPU           (1<<15)
137
138 /* GPIO control register */
139 #define FM801_GPIO_GP0          (1<<0)  /* read/write */
140 #define FM801_GPIO_GP1          (1<<1)
141 #define FM801_GPIO_GP2          (1<<2)
142 #define FM801_GPIO_GP3          (1<<3)
143 #define FM801_GPIO_GP(x)        (1<<(0+(x)))
144 #define FM801_GPIO_GD0          (1<<8)  /* directions: 1 = input, 0 = output*/
145 #define FM801_GPIO_GD1          (1<<9)
146 #define FM801_GPIO_GD2          (1<<10)
147 #define FM801_GPIO_GD3          (1<<11)
148 #define FM801_GPIO_GD(x)        (1<<(8+(x)))
149 #define FM801_GPIO_GS0          (1<<12) /* function select: */
150 #define FM801_GPIO_GS1          (1<<13) /*    1 = GPIO */
151 #define FM801_GPIO_GS2          (1<<14) /*    0 = other (S/PDIF, VOL) */
152 #define FM801_GPIO_GS3          (1<<15)
153 #define FM801_GPIO_GS(x)        (1<<(12+(x)))
154         
155 /*
156
157  */
158
159 struct fm801 {
160         int irq;
161
162         unsigned long port;     /* I/O port number */
163         unsigned int multichannel: 1,   /* multichannel support */
164                      secondary: 1;      /* secondary codec */
165         unsigned char secondary_addr;   /* address of the secondary codec */
166         unsigned int tea575x_tuner;     /* tuner access method & flags */
167
168         unsigned short ply_ctrl; /* playback control */
169         unsigned short cap_ctrl; /* capture control */
170
171         unsigned long ply_buffer;
172         unsigned int ply_buf;
173         unsigned int ply_count;
174         unsigned int ply_size;
175         unsigned int ply_pos;
176
177         unsigned long cap_buffer;
178         unsigned int cap_buf;
179         unsigned int cap_count;
180         unsigned int cap_size;
181         unsigned int cap_pos;
182
183         struct snd_ac97_bus *ac97_bus;
184         struct snd_ac97 *ac97;
185         struct snd_ac97 *ac97_sec;
186
187         struct pci_dev *pci;
188         struct snd_card *card;
189         struct snd_pcm *pcm;
190         struct snd_rawmidi *rmidi;
191         struct snd_pcm_substream *playback_substream;
192         struct snd_pcm_substream *capture_substream;
193         unsigned int p_dma_size;
194         unsigned int c_dma_size;
195
196         spinlock_t reg_lock;
197         struct snd_info_entry *proc_entry;
198
199 #ifdef CONFIG_SND_FM801_TEA575X_BOOL
200         struct snd_tea575x tea;
201 #endif
202
203 #ifdef CONFIG_PM
204         u16 saved_regs[0x20];
205 #endif
206 };
207
208 static DEFINE_PCI_DEVICE_TABLE(snd_fm801_ids) = {
209         { 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* FM801 */
210         { 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* Gallant Odyssey Sound 4 */
211         { 0, }
212 };
213
214 MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
215
216 /*
217  *  common I/O routines
218  */
219
220 static int snd_fm801_update_bits(struct fm801 *chip, unsigned short reg,
221                                  unsigned short mask, unsigned short value)
222 {
223         int change;
224         unsigned long flags;
225         unsigned short old, new;
226
227         spin_lock_irqsave(&chip->reg_lock, flags);
228         old = inw(chip->port + reg);
229         new = (old & ~mask) | value;
230         change = old != new;
231         if (change)
232                 outw(new, chip->port + reg);
233         spin_unlock_irqrestore(&chip->reg_lock, flags);
234         return change;
235 }
236
237 static void snd_fm801_codec_write(struct snd_ac97 *ac97,
238                                   unsigned short reg,
239                                   unsigned short val)
240 {
241         struct fm801 *chip = ac97->private_data;
242         int idx;
243
244         /*
245          *  Wait until the codec interface is not ready..
246          */
247         for (idx = 0; idx < 100; idx++) {
248                 if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
249                         goto ok1;
250                 udelay(10);
251         }
252         snd_printk(KERN_ERR "AC'97 interface is busy (1)\n");
253         return;
254
255  ok1:
256         /* write data and address */
257         outw(val, FM801_REG(chip, AC97_DATA));
258         outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
259         /*
260          *  Wait until the write command is not completed..
261          */
262         for (idx = 0; idx < 1000; idx++) {
263                 if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
264                         return;
265                 udelay(10);
266         }
267         snd_printk(KERN_ERR "AC'97 interface #%d is busy (2)\n", ac97->num);
268 }
269
270 static unsigned short snd_fm801_codec_read(struct snd_ac97 *ac97, unsigned short reg)
271 {
272         struct fm801 *chip = ac97->private_data;
273         int idx;
274
275         /*
276          *  Wait until the codec interface is not ready..
277          */
278         for (idx = 0; idx < 100; idx++) {
279                 if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
280                         goto ok1;
281                 udelay(10);
282         }
283         snd_printk(KERN_ERR "AC'97 interface is busy (1)\n");
284         return 0;
285
286  ok1:
287         /* read command */
288         outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ,
289              FM801_REG(chip, AC97_CMD));
290         for (idx = 0; idx < 100; idx++) {
291                 if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
292                         goto ok2;
293                 udelay(10);
294         }
295         snd_printk(KERN_ERR "AC'97 interface #%d is busy (2)\n", ac97->num);
296         return 0;
297
298  ok2:
299         for (idx = 0; idx < 1000; idx++) {
300                 if (inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_VALID)
301                         goto ok3;
302                 udelay(10);
303         }
304         snd_printk(KERN_ERR "AC'97 interface #%d is not valid (2)\n", ac97->num);
305         return 0;
306
307  ok3:
308         return inw(FM801_REG(chip, AC97_DATA));
309 }
310
311 static unsigned int rates[] = {
312   5500,  8000,  9600, 11025,
313   16000, 19200, 22050, 32000,
314   38400, 44100, 48000
315 };
316
317 static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
318         .count = ARRAY_SIZE(rates),
319         .list = rates,
320         .mask = 0,
321 };
322
323 static unsigned int channels[] = {
324   2, 4, 6
325 };
326
327 static struct snd_pcm_hw_constraint_list hw_constraints_channels = {
328         .count = ARRAY_SIZE(channels),
329         .list = channels,
330         .mask = 0,
331 };
332
333 /*
334  *  Sample rate routines
335  */
336
337 static unsigned short snd_fm801_rate_bits(unsigned int rate)
338 {
339         unsigned int idx;
340
341         for (idx = 0; idx < ARRAY_SIZE(rates); idx++)
342                 if (rates[idx] == rate)
343                         return idx;
344         snd_BUG();
345         return ARRAY_SIZE(rates) - 1;
346 }
347
348 /*
349  *  PCM part
350  */
351
352 static int snd_fm801_playback_trigger(struct snd_pcm_substream *substream,
353                                       int cmd)
354 {
355         struct fm801 *chip = snd_pcm_substream_chip(substream);
356
357         spin_lock(&chip->reg_lock);
358         switch (cmd) {
359         case SNDRV_PCM_TRIGGER_START:
360                 chip->ply_ctrl &= ~(FM801_BUF1_LAST |
361                                      FM801_BUF2_LAST |
362                                      FM801_PAUSE);
363                 chip->ply_ctrl |= FM801_START |
364                                    FM801_IMMED_STOP;
365                 break;
366         case SNDRV_PCM_TRIGGER_STOP:
367                 chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
368                 break;
369         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
370         case SNDRV_PCM_TRIGGER_SUSPEND:
371                 chip->ply_ctrl |= FM801_PAUSE;
372                 break;
373         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
374         case SNDRV_PCM_TRIGGER_RESUME:
375                 chip->ply_ctrl &= ~FM801_PAUSE;
376                 break;
377         default:
378                 spin_unlock(&chip->reg_lock);
379                 snd_BUG();
380                 return -EINVAL;
381         }
382         outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
383         spin_unlock(&chip->reg_lock);
384         return 0;
385 }
386
387 static int snd_fm801_capture_trigger(struct snd_pcm_substream *substream,
388                                      int cmd)
389 {
390         struct fm801 *chip = snd_pcm_substream_chip(substream);
391
392         spin_lock(&chip->reg_lock);
393         switch (cmd) {
394         case SNDRV_PCM_TRIGGER_START:
395                 chip->cap_ctrl &= ~(FM801_BUF1_LAST |
396                                      FM801_BUF2_LAST |
397                                      FM801_PAUSE);
398                 chip->cap_ctrl |= FM801_START |
399                                    FM801_IMMED_STOP;
400                 break;
401         case SNDRV_PCM_TRIGGER_STOP:
402                 chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
403                 break;
404         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
405         case SNDRV_PCM_TRIGGER_SUSPEND:
406                 chip->cap_ctrl |= FM801_PAUSE;
407                 break;
408         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
409         case SNDRV_PCM_TRIGGER_RESUME:
410                 chip->cap_ctrl &= ~FM801_PAUSE;
411                 break;
412         default:
413                 spin_unlock(&chip->reg_lock);
414                 snd_BUG();
415                 return -EINVAL;
416         }
417         outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
418         spin_unlock(&chip->reg_lock);
419         return 0;
420 }
421
422 static int snd_fm801_hw_params(struct snd_pcm_substream *substream,
423                                struct snd_pcm_hw_params *hw_params)
424 {
425         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
426 }
427
428 static int snd_fm801_hw_free(struct snd_pcm_substream *substream)
429 {
430         return snd_pcm_lib_free_pages(substream);
431 }
432
433 static int snd_fm801_playback_prepare(struct snd_pcm_substream *substream)
434 {
435         struct fm801 *chip = snd_pcm_substream_chip(substream);
436         struct snd_pcm_runtime *runtime = substream->runtime;
437
438         chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
439         chip->ply_count = snd_pcm_lib_period_bytes(substream);
440         spin_lock_irq(&chip->reg_lock);
441         chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
442                              FM801_STEREO | FM801_RATE_MASK |
443                              FM801_CHANNELS_MASK);
444         if (snd_pcm_format_width(runtime->format) == 16)
445                 chip->ply_ctrl |= FM801_16BIT;
446         if (runtime->channels > 1) {
447                 chip->ply_ctrl |= FM801_STEREO;
448                 if (runtime->channels == 4)
449                         chip->ply_ctrl |= FM801_CHANNELS_4;
450                 else if (runtime->channels == 6)
451                         chip->ply_ctrl |= FM801_CHANNELS_6;
452         }
453         chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
454         chip->ply_buf = 0;
455         outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
456         outw(chip->ply_count - 1, FM801_REG(chip, PLY_COUNT));
457         chip->ply_buffer = runtime->dma_addr;
458         chip->ply_pos = 0;
459         outl(chip->ply_buffer, FM801_REG(chip, PLY_BUF1));
460         outl(chip->ply_buffer + (chip->ply_count % chip->ply_size), FM801_REG(chip, PLY_BUF2));
461         spin_unlock_irq(&chip->reg_lock);
462         return 0;
463 }
464
465 static int snd_fm801_capture_prepare(struct snd_pcm_substream *substream)
466 {
467         struct fm801 *chip = snd_pcm_substream_chip(substream);
468         struct snd_pcm_runtime *runtime = substream->runtime;
469
470         chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
471         chip->cap_count = snd_pcm_lib_period_bytes(substream);
472         spin_lock_irq(&chip->reg_lock);
473         chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
474                              FM801_STEREO | FM801_RATE_MASK);
475         if (snd_pcm_format_width(runtime->format) == 16)
476                 chip->cap_ctrl |= FM801_16BIT;
477         if (runtime->channels > 1)
478                 chip->cap_ctrl |= FM801_STEREO;
479         chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
480         chip->cap_buf = 0;
481         outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
482         outw(chip->cap_count - 1, FM801_REG(chip, CAP_COUNT));
483         chip->cap_buffer = runtime->dma_addr;
484         chip->cap_pos = 0;
485         outl(chip->cap_buffer, FM801_REG(chip, CAP_BUF1));
486         outl(chip->cap_buffer + (chip->cap_count % chip->cap_size), FM801_REG(chip, CAP_BUF2));
487         spin_unlock_irq(&chip->reg_lock);
488         return 0;
489 }
490
491 static snd_pcm_uframes_t snd_fm801_playback_pointer(struct snd_pcm_substream *substream)
492 {
493         struct fm801 *chip = snd_pcm_substream_chip(substream);
494         size_t ptr;
495
496         if (!(chip->ply_ctrl & FM801_START))
497                 return 0;
498         spin_lock(&chip->reg_lock);
499         ptr = chip->ply_pos + (chip->ply_count - 1) - inw(FM801_REG(chip, PLY_COUNT));
500         if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_PLAYBACK) {
501                 ptr += chip->ply_count;
502                 ptr %= chip->ply_size;
503         }
504         spin_unlock(&chip->reg_lock);
505         return bytes_to_frames(substream->runtime, ptr);
506 }
507
508 static snd_pcm_uframes_t snd_fm801_capture_pointer(struct snd_pcm_substream *substream)
509 {
510         struct fm801 *chip = snd_pcm_substream_chip(substream);
511         size_t ptr;
512
513         if (!(chip->cap_ctrl & FM801_START))
514                 return 0;
515         spin_lock(&chip->reg_lock);
516         ptr = chip->cap_pos + (chip->cap_count - 1) - inw(FM801_REG(chip, CAP_COUNT));
517         if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_CAPTURE) {
518                 ptr += chip->cap_count;
519                 ptr %= chip->cap_size;
520         }
521         spin_unlock(&chip->reg_lock);
522         return bytes_to_frames(substream->runtime, ptr);
523 }
524
525 static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id)
526 {
527         struct fm801 *chip = dev_id;
528         unsigned short status;
529         unsigned int tmp;
530
531         status = inw(FM801_REG(chip, IRQ_STATUS));
532         status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
533         if (! status)
534                 return IRQ_NONE;
535         /* ack first */
536         outw(status, FM801_REG(chip, IRQ_STATUS));
537         if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
538                 spin_lock(&chip->reg_lock);
539                 chip->ply_buf++;
540                 chip->ply_pos += chip->ply_count;
541                 chip->ply_pos %= chip->ply_size;
542                 tmp = chip->ply_pos + chip->ply_count;
543                 tmp %= chip->ply_size;
544                 outl(chip->ply_buffer + tmp,
545                                 (chip->ply_buf & 1) ?
546                                         FM801_REG(chip, PLY_BUF1) :
547                                         FM801_REG(chip, PLY_BUF2));
548                 spin_unlock(&chip->reg_lock);
549                 snd_pcm_period_elapsed(chip->playback_substream);
550         }
551         if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
552                 spin_lock(&chip->reg_lock);
553                 chip->cap_buf++;
554                 chip->cap_pos += chip->cap_count;
555                 chip->cap_pos %= chip->cap_size;
556                 tmp = chip->cap_pos + chip->cap_count;
557                 tmp %= chip->cap_size;
558                 outl(chip->cap_buffer + tmp,
559                                 (chip->cap_buf & 1) ?
560                                         FM801_REG(chip, CAP_BUF1) :
561                                         FM801_REG(chip, CAP_BUF2));
562                 spin_unlock(&chip->reg_lock);
563                 snd_pcm_period_elapsed(chip->capture_substream);
564         }
565         if (chip->rmidi && (status & FM801_IRQ_MPU))
566                 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
567         if (status & FM801_IRQ_VOLUME)
568                 ;/* TODO */
569
570         return IRQ_HANDLED;
571 }
572
573 static struct snd_pcm_hardware snd_fm801_playback =
574 {
575         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
576                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
577                                  SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
578                                  SNDRV_PCM_INFO_MMAP_VALID),
579         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
580         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
581         .rate_min =             5500,
582         .rate_max =             48000,
583         .channels_min =         1,
584         .channels_max =         2,
585         .buffer_bytes_max =     (128*1024),
586         .period_bytes_min =     64,
587         .period_bytes_max =     (128*1024),
588         .periods_min =          1,
589         .periods_max =          1024,
590         .fifo_size =            0,
591 };
592
593 static struct snd_pcm_hardware snd_fm801_capture =
594 {
595         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
596                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
597                                  SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
598                                  SNDRV_PCM_INFO_MMAP_VALID),
599         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
600         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
601         .rate_min =             5500,
602         .rate_max =             48000,
603         .channels_min =         1,
604         .channels_max =         2,
605         .buffer_bytes_max =     (128*1024),
606         .period_bytes_min =     64,
607         .period_bytes_max =     (128*1024),
608         .periods_min =          1,
609         .periods_max =          1024,
610         .fifo_size =            0,
611 };
612
613 static int snd_fm801_playback_open(struct snd_pcm_substream *substream)
614 {
615         struct fm801 *chip = snd_pcm_substream_chip(substream);
616         struct snd_pcm_runtime *runtime = substream->runtime;
617         int err;
618
619         chip->playback_substream = substream;
620         runtime->hw = snd_fm801_playback;
621         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
622                                    &hw_constraints_rates);
623         if (chip->multichannel) {
624                 runtime->hw.channels_max = 6;
625                 snd_pcm_hw_constraint_list(runtime, 0,
626                                            SNDRV_PCM_HW_PARAM_CHANNELS,
627                                            &hw_constraints_channels);
628         }
629         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
630                 return err;
631         return 0;
632 }
633
634 static int snd_fm801_capture_open(struct snd_pcm_substream *substream)
635 {
636         struct fm801 *chip = snd_pcm_substream_chip(substream);
637         struct snd_pcm_runtime *runtime = substream->runtime;
638         int err;
639
640         chip->capture_substream = substream;
641         runtime->hw = snd_fm801_capture;
642         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
643                                    &hw_constraints_rates);
644         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
645                 return err;
646         return 0;
647 }
648
649 static int snd_fm801_playback_close(struct snd_pcm_substream *substream)
650 {
651         struct fm801 *chip = snd_pcm_substream_chip(substream);
652
653         chip->playback_substream = NULL;
654         return 0;
655 }
656
657 static int snd_fm801_capture_close(struct snd_pcm_substream *substream)
658 {
659         struct fm801 *chip = snd_pcm_substream_chip(substream);
660
661         chip->capture_substream = NULL;
662         return 0;
663 }
664
665 static struct snd_pcm_ops snd_fm801_playback_ops = {
666         .open =         snd_fm801_playback_open,
667         .close =        snd_fm801_playback_close,
668         .ioctl =        snd_pcm_lib_ioctl,
669         .hw_params =    snd_fm801_hw_params,
670         .hw_free =      snd_fm801_hw_free,
671         .prepare =      snd_fm801_playback_prepare,
672         .trigger =      snd_fm801_playback_trigger,
673         .pointer =      snd_fm801_playback_pointer,
674 };
675
676 static struct snd_pcm_ops snd_fm801_capture_ops = {
677         .open =         snd_fm801_capture_open,
678         .close =        snd_fm801_capture_close,
679         .ioctl =        snd_pcm_lib_ioctl,
680         .hw_params =    snd_fm801_hw_params,
681         .hw_free =      snd_fm801_hw_free,
682         .prepare =      snd_fm801_capture_prepare,
683         .trigger =      snd_fm801_capture_trigger,
684         .pointer =      snd_fm801_capture_pointer,
685 };
686
687 static int __devinit snd_fm801_pcm(struct fm801 *chip, int device, struct snd_pcm ** rpcm)
688 {
689         struct snd_pcm *pcm;
690         int err;
691
692         if (rpcm)
693                 *rpcm = NULL;
694         if ((err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm)) < 0)
695                 return err;
696
697         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
698         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
699
700         pcm->private_data = chip;
701         pcm->info_flags = 0;
702         strcpy(pcm->name, "FM801");
703         chip->pcm = pcm;
704
705         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
706                                               snd_dma_pci_data(chip->pci),
707                                               chip->multichannel ? 128*1024 : 64*1024, 128*1024);
708
709         if (rpcm)
710                 *rpcm = pcm;
711         return 0;
712 }
713
714 /*
715  *  TEA5757 radio
716  */
717
718 #ifdef CONFIG_SND_FM801_TEA575X_BOOL
719
720 /* GPIO to TEA575x maps */
721 struct snd_fm801_tea575x_gpio {
722         u8 data, clk, wren, most;
723         char *name;
724 };
725
726 static struct snd_fm801_tea575x_gpio snd_fm801_tea575x_gpios[] = {
727         { .data = 1, .clk = 3, .wren = 2, .most = 0, .name = "SF256-PCS" },
728         { .data = 1, .clk = 0, .wren = 2, .most = 3, .name = "SF256-PCP" },
729         { .data = 2, .clk = 0, .wren = 1, .most = 3, .name = "SF64-PCR" },
730 };
731
732 static void snd_fm801_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
733 {
734         struct fm801 *chip = tea->private_data;
735         unsigned short reg = inw(FM801_REG(chip, GPIO_CTRL));
736         struct snd_fm801_tea575x_gpio gpio = snd_fm801_tea575x_gpios[(chip->tea575x_tuner & TUNER_TYPE_MASK) - 1];
737
738         reg &= ~(FM801_GPIO_GP(gpio.data) |
739                  FM801_GPIO_GP(gpio.clk) |
740                  FM801_GPIO_GP(gpio.wren));
741
742         reg |= (pins & TEA575X_DATA) ? FM801_GPIO_GP(gpio.data) : 0;
743         reg |= (pins & TEA575X_CLK)  ? FM801_GPIO_GP(gpio.clk) : 0;
744         /* WRITE_ENABLE is inverted */
745         reg |= (pins & TEA575X_WREN) ? 0 : FM801_GPIO_GP(gpio.wren);
746
747         outw(reg, FM801_REG(chip, GPIO_CTRL));
748 }
749
750 static u8 snd_fm801_tea575x_get_pins(struct snd_tea575x *tea)
751 {
752         struct fm801 *chip = tea->private_data;
753         unsigned short reg = inw(FM801_REG(chip, GPIO_CTRL));
754         struct snd_fm801_tea575x_gpio gpio = snd_fm801_tea575x_gpios[(chip->tea575x_tuner & TUNER_TYPE_MASK) - 1];
755
756         return  (reg & FM801_GPIO_GP(gpio.data)) ? TEA575X_DATA : 0 |
757                 (reg & FM801_GPIO_GP(gpio.most)) ? TEA575X_MOST : 0;
758 }
759
760 static void snd_fm801_tea575x_set_direction(struct snd_tea575x *tea, bool output)
761 {
762         struct fm801 *chip = tea->private_data;
763         unsigned short reg = inw(FM801_REG(chip, GPIO_CTRL));
764         struct snd_fm801_tea575x_gpio gpio = snd_fm801_tea575x_gpios[(chip->tea575x_tuner & TUNER_TYPE_MASK) - 1];
765
766         /* use GPIO lines and set write enable bit */
767         reg |= FM801_GPIO_GS(gpio.data) |
768                FM801_GPIO_GS(gpio.wren) |
769                FM801_GPIO_GS(gpio.clk) |
770                FM801_GPIO_GS(gpio.most);
771         if (output) {
772                 /* all of lines are in the write direction */
773                 /* clear data and clock lines */
774                 reg &= ~(FM801_GPIO_GD(gpio.data) |
775                          FM801_GPIO_GD(gpio.wren) |
776                          FM801_GPIO_GD(gpio.clk) |
777                          FM801_GPIO_GP(gpio.data) |
778                          FM801_GPIO_GP(gpio.clk) |
779                          FM801_GPIO_GP(gpio.wren));
780         } else {
781                 /* use GPIO lines, set data direction to input */
782                 reg |= FM801_GPIO_GD(gpio.data) |
783                        FM801_GPIO_GD(gpio.most) |
784                        FM801_GPIO_GP(gpio.data) |
785                        FM801_GPIO_GP(gpio.most) |
786                        FM801_GPIO_GP(gpio.wren);
787                 /* all of lines are in the write direction, except data */
788                 /* clear data, write enable and clock lines */
789                 reg &= ~(FM801_GPIO_GD(gpio.wren) |
790                          FM801_GPIO_GD(gpio.clk) |
791                          FM801_GPIO_GP(gpio.clk));
792         }
793
794         outw(reg, FM801_REG(chip, GPIO_CTRL));
795 }
796
797 static struct snd_tea575x_ops snd_fm801_tea_ops = {
798         .set_pins = snd_fm801_tea575x_set_pins,
799         .get_pins = snd_fm801_tea575x_get_pins,
800         .set_direction = snd_fm801_tea575x_set_direction,
801 };
802 #endif
803
804 /*
805  *  Mixer routines
806  */
807
808 #define FM801_SINGLE(xname, reg, shift, mask, invert) \
809 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
810   .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
811   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
812
813 static int snd_fm801_info_single(struct snd_kcontrol *kcontrol,
814                                  struct snd_ctl_elem_info *uinfo)
815 {
816         int mask = (kcontrol->private_value >> 16) & 0xff;
817
818         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
819         uinfo->count = 1;
820         uinfo->value.integer.min = 0;
821         uinfo->value.integer.max = mask;
822         return 0;
823 }
824
825 static int snd_fm801_get_single(struct snd_kcontrol *kcontrol,
826                                 struct snd_ctl_elem_value *ucontrol)
827 {
828         struct fm801 *chip = snd_kcontrol_chip(kcontrol);
829         int reg = kcontrol->private_value & 0xff;
830         int shift = (kcontrol->private_value >> 8) & 0xff;
831         int mask = (kcontrol->private_value >> 16) & 0xff;
832         int invert = (kcontrol->private_value >> 24) & 0xff;
833
834         ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift) & mask;
835         if (invert)
836                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
837         return 0;
838 }
839
840 static int snd_fm801_put_single(struct snd_kcontrol *kcontrol,
841                                 struct snd_ctl_elem_value *ucontrol)
842 {
843         struct fm801 *chip = snd_kcontrol_chip(kcontrol);
844         int reg = kcontrol->private_value & 0xff;
845         int shift = (kcontrol->private_value >> 8) & 0xff;
846         int mask = (kcontrol->private_value >> 16) & 0xff;
847         int invert = (kcontrol->private_value >> 24) & 0xff;
848         unsigned short val;
849
850         val = (ucontrol->value.integer.value[0] & mask);
851         if (invert)
852                 val = mask - val;
853         return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
854 }
855
856 #define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
857 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
858   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
859   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
860 #define FM801_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
861 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
862   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
863   .name = xname, .info = snd_fm801_info_double, \
864   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
865   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
866   .tlv = { .p = (xtlv) } }
867
868 static int snd_fm801_info_double(struct snd_kcontrol *kcontrol,
869                                  struct snd_ctl_elem_info *uinfo)
870 {
871         int mask = (kcontrol->private_value >> 16) & 0xff;
872
873         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
874         uinfo->count = 2;
875         uinfo->value.integer.min = 0;
876         uinfo->value.integer.max = mask;
877         return 0;
878 }
879
880 static int snd_fm801_get_double(struct snd_kcontrol *kcontrol,
881                                 struct snd_ctl_elem_value *ucontrol)
882 {
883         struct fm801 *chip = snd_kcontrol_chip(kcontrol);
884         int reg = kcontrol->private_value & 0xff;
885         int shift_left = (kcontrol->private_value >> 8) & 0x0f;
886         int shift_right = (kcontrol->private_value >> 12) & 0x0f;
887         int mask = (kcontrol->private_value >> 16) & 0xff;
888         int invert = (kcontrol->private_value >> 24) & 0xff;
889
890         spin_lock_irq(&chip->reg_lock);
891         ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift_left) & mask;
892         ucontrol->value.integer.value[1] = (inw(chip->port + reg) >> shift_right) & mask;
893         spin_unlock_irq(&chip->reg_lock);
894         if (invert) {
895                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
896                 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
897         }
898         return 0;
899 }
900
901 static int snd_fm801_put_double(struct snd_kcontrol *kcontrol,
902                                 struct snd_ctl_elem_value *ucontrol)
903 {
904         struct fm801 *chip = snd_kcontrol_chip(kcontrol);
905         int reg = kcontrol->private_value & 0xff;
906         int shift_left = (kcontrol->private_value >> 8) & 0x0f;
907         int shift_right = (kcontrol->private_value >> 12) & 0x0f;
908         int mask = (kcontrol->private_value >> 16) & 0xff;
909         int invert = (kcontrol->private_value >> 24) & 0xff;
910         unsigned short val1, val2;
911  
912         val1 = ucontrol->value.integer.value[0] & mask;
913         val2 = ucontrol->value.integer.value[1] & mask;
914         if (invert) {
915                 val1 = mask - val1;
916                 val2 = mask - val2;
917         }
918         return snd_fm801_update_bits(chip, reg,
919                                      (mask << shift_left) | (mask << shift_right),
920                                      (val1 << shift_left ) | (val2 << shift_right));
921 }
922
923 static int snd_fm801_info_mux(struct snd_kcontrol *kcontrol,
924                               struct snd_ctl_elem_info *uinfo)
925 {
926         static char *texts[5] = {
927                 "AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
928         };
929  
930         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
931         uinfo->count = 1;
932         uinfo->value.enumerated.items = 5;
933         if (uinfo->value.enumerated.item > 4)
934                 uinfo->value.enumerated.item = 4;
935         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
936         return 0;
937 }
938
939 static int snd_fm801_get_mux(struct snd_kcontrol *kcontrol,
940                              struct snd_ctl_elem_value *ucontrol)
941 {
942         struct fm801 *chip = snd_kcontrol_chip(kcontrol);
943         unsigned short val;
944  
945         val = inw(FM801_REG(chip, REC_SRC)) & 7;
946         if (val > 4)
947                 val = 4;
948         ucontrol->value.enumerated.item[0] = val;
949         return 0;
950 }
951
952 static int snd_fm801_put_mux(struct snd_kcontrol *kcontrol,
953                              struct snd_ctl_elem_value *ucontrol)
954 {
955         struct fm801 *chip = snd_kcontrol_chip(kcontrol);
956         unsigned short val;
957  
958         if ((val = ucontrol->value.enumerated.item[0]) > 4)
959                 return -EINVAL;
960         return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
961 }
962
963 static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -3450, 150, 0);
964
965 #define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
966
967 static struct snd_kcontrol_new snd_fm801_controls[] __devinitdata = {
968 FM801_DOUBLE_TLV("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1,
969                  db_scale_dsp),
970 FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
971 FM801_DOUBLE_TLV("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1,
972                  db_scale_dsp),
973 FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
974 FM801_DOUBLE_TLV("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1,
975                  db_scale_dsp),
976 FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
977 {
978         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
979         .name = "Digital Capture Source",
980         .info = snd_fm801_info_mux,
981         .get = snd_fm801_get_mux,
982         .put = snd_fm801_put_mux,
983 }
984 };
985
986 #define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
987
988 static struct snd_kcontrol_new snd_fm801_controls_multi[] __devinitdata = {
989 FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
990 FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
991 FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), FM801_I2S_MODE, 8, 1, 0),
992 FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",PLAYBACK,SWITCH), FM801_I2S_MODE, 9, 1, 0),
993 FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",CAPTURE,SWITCH), FM801_I2S_MODE, 10, 1, 0),
994 FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), FM801_GEN_CTRL, 2, 1, 0),
995 };
996
997 static void snd_fm801_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
998 {
999         struct fm801 *chip = bus->private_data;
1000         chip->ac97_bus = NULL;
1001 }
1002
1003 static void snd_fm801_mixer_free_ac97(struct snd_ac97 *ac97)
1004 {
1005         struct fm801 *chip = ac97->private_data;
1006         if (ac97->num == 0) {
1007                 chip->ac97 = NULL;
1008         } else {
1009                 chip->ac97_sec = NULL;
1010         }
1011 }
1012
1013 static int __devinit snd_fm801_mixer(struct fm801 *chip)
1014 {
1015         struct snd_ac97_template ac97;
1016         unsigned int i;
1017         int err;
1018         static struct snd_ac97_bus_ops ops = {
1019                 .write = snd_fm801_codec_write,
1020                 .read = snd_fm801_codec_read,
1021         };
1022
1023         if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1024                 return err;
1025         chip->ac97_bus->private_free = snd_fm801_mixer_free_ac97_bus;
1026
1027         memset(&ac97, 0, sizeof(ac97));
1028         ac97.private_data = chip;
1029         ac97.private_free = snd_fm801_mixer_free_ac97;
1030         if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1031                 return err;
1032         if (chip->secondary) {
1033                 ac97.num = 1;
1034                 ac97.addr = chip->secondary_addr;
1035                 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec)) < 0)
1036                         return err;
1037         }
1038         for (i = 0; i < FM801_CONTROLS; i++)
1039                 snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls[i], chip));
1040         if (chip->multichannel) {
1041                 for (i = 0; i < FM801_CONTROLS_MULTI; i++)
1042                         snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
1043         }
1044         return 0;
1045 }
1046
1047 /*
1048  *  initialization routines
1049  */
1050
1051 static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
1052                           unsigned short reg, unsigned long waits)
1053 {
1054         unsigned long timeout = jiffies + waits;
1055
1056         outw(FM801_AC97_READ | (codec_id << FM801_AC97_ADDR_SHIFT) | reg,
1057              FM801_REG(chip, AC97_CMD));
1058         udelay(5);
1059         do {
1060                 if ((inw(FM801_REG(chip, AC97_CMD)) & (FM801_AC97_VALID|FM801_AC97_BUSY))
1061                     == FM801_AC97_VALID)
1062                         return 0;
1063                 schedule_timeout_uninterruptible(1);
1064         } while (time_after(timeout, jiffies));
1065         return -EIO;
1066 }
1067
1068 static int snd_fm801_chip_init(struct fm801 *chip, int resume)
1069 {
1070         unsigned short cmdw;
1071
1072         if (chip->tea575x_tuner & TUNER_ONLY)
1073                 goto __ac97_ok;
1074
1075         /* codec cold reset + AC'97 warm reset */
1076         outw((1<<5) | (1<<6), FM801_REG(chip, CODEC_CTRL));
1077         inw(FM801_REG(chip, CODEC_CTRL)); /* flush posting data */
1078         udelay(100);
1079         outw(0, FM801_REG(chip, CODEC_CTRL));
1080
1081         if (wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750)) < 0)
1082                 if (!resume) {
1083                         snd_printk(KERN_INFO "Primary AC'97 codec not found, "
1084                                             "assume SF64-PCR (tuner-only)\n");
1085                         chip->tea575x_tuner = 3 | TUNER_ONLY;
1086                         goto __ac97_ok;
1087                 }
1088
1089         if (chip->multichannel) {
1090                 if (chip->secondary_addr) {
1091                         wait_for_codec(chip, chip->secondary_addr,
1092                                        AC97_VENDOR_ID1, msecs_to_jiffies(50));
1093                 } else {
1094                         /* my card has the secondary codec */
1095                         /* at address #3, so the loop is inverted */
1096                         int i;
1097                         for (i = 3; i > 0; i--) {
1098                                 if (!wait_for_codec(chip, i, AC97_VENDOR_ID1,
1099                                                      msecs_to_jiffies(50))) {
1100                                         cmdw = inw(FM801_REG(chip, AC97_DATA));
1101                                         if (cmdw != 0xffff && cmdw != 0) {
1102                                                 chip->secondary = 1;
1103                                                 chip->secondary_addr = i;
1104                                                 break;
1105                                         }
1106                                 }
1107                         }
1108                 }
1109
1110                 /* the recovery phase, it seems that probing for non-existing codec might */
1111                 /* cause timeout problems */
1112                 wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
1113         }
1114
1115       __ac97_ok:
1116
1117         /* init volume */
1118         outw(0x0808, FM801_REG(chip, PCM_VOL));
1119         outw(0x9f1f, FM801_REG(chip, FM_VOL));
1120         outw(0x8808, FM801_REG(chip, I2S_VOL));
1121
1122         /* I2S control - I2S mode */
1123         outw(0x0003, FM801_REG(chip, I2S_MODE));
1124
1125         /* interrupt setup */
1126         cmdw = inw(FM801_REG(chip, IRQ_MASK));
1127         if (chip->irq < 0)
1128                 cmdw |= 0x00c3;         /* mask everything, no PCM nor MPU */
1129         else
1130                 cmdw &= ~0x0083;        /* unmask MPU, PLAYBACK & CAPTURE */
1131         outw(cmdw, FM801_REG(chip, IRQ_MASK));
1132
1133         /* interrupt clear */
1134         outw(FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU, FM801_REG(chip, IRQ_STATUS));
1135
1136         return 0;
1137 }
1138
1139
1140 static int snd_fm801_free(struct fm801 *chip)
1141 {
1142         unsigned short cmdw;
1143
1144         if (chip->irq < 0)
1145                 goto __end_hw;
1146
1147         /* interrupt setup - mask everything */
1148         cmdw = inw(FM801_REG(chip, IRQ_MASK));
1149         cmdw |= 0x00c3;
1150         outw(cmdw, FM801_REG(chip, IRQ_MASK));
1151
1152       __end_hw:
1153 #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1154         if (!(chip->tea575x_tuner & TUNER_DISABLED))
1155                 snd_tea575x_exit(&chip->tea);
1156 #endif
1157         if (chip->irq >= 0)
1158                 free_irq(chip->irq, chip);
1159         pci_release_regions(chip->pci);
1160         pci_disable_device(chip->pci);
1161
1162         kfree(chip);
1163         return 0;
1164 }
1165
1166 static int snd_fm801_dev_free(struct snd_device *device)
1167 {
1168         struct fm801 *chip = device->device_data;
1169         return snd_fm801_free(chip);
1170 }
1171
1172 static int __devinit snd_fm801_create(struct snd_card *card,
1173                                       struct pci_dev * pci,
1174                                       int tea575x_tuner,
1175                                       struct fm801 ** rchip)
1176 {
1177         struct fm801 *chip;
1178         int err;
1179         static struct snd_device_ops ops = {
1180                 .dev_free =     snd_fm801_dev_free,
1181         };
1182
1183         *rchip = NULL;
1184         if ((err = pci_enable_device(pci)) < 0)
1185                 return err;
1186         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1187         if (chip == NULL) {
1188                 pci_disable_device(pci);
1189                 return -ENOMEM;
1190         }
1191         spin_lock_init(&chip->reg_lock);
1192         chip->card = card;
1193         chip->pci = pci;
1194         chip->irq = -1;
1195         chip->tea575x_tuner = tea575x_tuner;
1196         if ((err = pci_request_regions(pci, "FM801")) < 0) {
1197                 kfree(chip);
1198                 pci_disable_device(pci);
1199                 return err;
1200         }
1201         chip->port = pci_resource_start(pci, 0);
1202         if ((tea575x_tuner & TUNER_ONLY) == 0) {
1203                 if (request_irq(pci->irq, snd_fm801_interrupt, IRQF_SHARED,
1204                                 KBUILD_MODNAME, chip)) {
1205                         snd_printk(KERN_ERR "unable to grab IRQ %d\n", chip->irq);
1206                         snd_fm801_free(chip);
1207                         return -EBUSY;
1208                 }
1209                 chip->irq = pci->irq;
1210                 pci_set_master(pci);
1211         }
1212
1213         if (pci->revision >= 0xb1)      /* FM801-AU */
1214                 chip->multichannel = 1;
1215
1216         snd_fm801_chip_init(chip, 0);
1217         /* init might set tuner access method */
1218         tea575x_tuner = chip->tea575x_tuner;
1219
1220         if (chip->irq >= 0 && (tea575x_tuner & TUNER_ONLY)) {
1221                 pci_clear_master(pci);
1222                 free_irq(chip->irq, chip);
1223                 chip->irq = -1;
1224         }
1225
1226         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1227                 snd_fm801_free(chip);
1228                 return err;
1229         }
1230
1231         snd_card_set_dev(card, &pci->dev);
1232
1233 #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1234         chip->tea.private_data = chip;
1235         chip->tea.ops = &snd_fm801_tea_ops;
1236         sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
1237         if ((tea575x_tuner & TUNER_TYPE_MASK) > 0 &&
1238             (tea575x_tuner & TUNER_TYPE_MASK) < 4) {
1239                 if (snd_tea575x_init(&chip->tea)) {
1240                         snd_printk(KERN_ERR "TEA575x radio not found\n");
1241                         return -ENODEV;
1242                 }
1243         } else if ((tea575x_tuner & TUNER_TYPE_MASK) == 0) {
1244                 /* autodetect tuner connection */
1245                 for (tea575x_tuner = 1; tea575x_tuner <= 3; tea575x_tuner++) {
1246                         chip->tea575x_tuner = tea575x_tuner;
1247                         if (!snd_tea575x_init(&chip->tea)) {
1248                                 snd_printk(KERN_INFO "detected TEA575x radio type %s\n",
1249                                         snd_fm801_tea575x_gpios[tea575x_tuner - 1].name);
1250                                 break;
1251                         }
1252                 }
1253                 if (tea575x_tuner == 4) {
1254                         snd_printk(KERN_ERR "TEA575x radio not found\n");
1255                         chip->tea575x_tuner = TUNER_DISABLED;
1256                 }
1257         }
1258         if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
1259                 strlcpy(chip->tea.card,
1260                         snd_fm801_tea575x_gpios[(tea575x_tuner &
1261                                                  TUNER_TYPE_MASK) - 1].name,
1262                         sizeof(chip->tea.card));
1263         }
1264 #endif
1265
1266         *rchip = chip;
1267         return 0;
1268 }
1269
1270 static int __devinit snd_card_fm801_probe(struct pci_dev *pci,
1271                                           const struct pci_device_id *pci_id)
1272 {
1273         static int dev;
1274         struct snd_card *card;
1275         struct fm801 *chip;
1276         struct snd_opl3 *opl3;
1277         int err;
1278
1279         if (dev >= SNDRV_CARDS)
1280                 return -ENODEV;
1281         if (!enable[dev]) {
1282                 dev++;
1283                 return -ENOENT;
1284         }
1285
1286         err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
1287         if (err < 0)
1288                 return err;
1289         if ((err = snd_fm801_create(card, pci, tea575x_tuner[dev], &chip)) < 0) {
1290                 snd_card_free(card);
1291                 return err;
1292         }
1293         card->private_data = chip;
1294
1295         strcpy(card->driver, "FM801");
1296         strcpy(card->shortname, "ForteMedia FM801-");
1297         strcat(card->shortname, chip->multichannel ? "AU" : "AS");
1298         sprintf(card->longname, "%s at 0x%lx, irq %i",
1299                 card->shortname, chip->port, chip->irq);
1300
1301         if (chip->tea575x_tuner & TUNER_ONLY)
1302                 goto __fm801_tuner_only;
1303
1304         if ((err = snd_fm801_pcm(chip, 0, NULL)) < 0) {
1305                 snd_card_free(card);
1306                 return err;
1307         }
1308         if ((err = snd_fm801_mixer(chip)) < 0) {
1309                 snd_card_free(card);
1310                 return err;
1311         }
1312         if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1313                                        FM801_REG(chip, MPU401_DATA),
1314                                        MPU401_INFO_INTEGRATED,
1315                                        chip->irq, 0, &chip->rmidi)) < 0) {
1316                 snd_card_free(card);
1317                 return err;
1318         }
1319         if ((err = snd_opl3_create(card, FM801_REG(chip, OPL3_BANK0),
1320                                    FM801_REG(chip, OPL3_BANK1),
1321                                    OPL3_HW_OPL3_FM801, 1, &opl3)) < 0) {
1322                 snd_card_free(card);
1323                 return err;
1324         }
1325         if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
1326                 snd_card_free(card);
1327                 return err;
1328         }
1329
1330       __fm801_tuner_only:
1331         if ((err = snd_card_register(card)) < 0) {
1332                 snd_card_free(card);
1333                 return err;
1334         }
1335         pci_set_drvdata(pci, card);
1336         dev++;
1337         return 0;
1338 }
1339
1340 static void __devexit snd_card_fm801_remove(struct pci_dev *pci)
1341 {
1342         snd_card_free(pci_get_drvdata(pci));
1343         pci_set_drvdata(pci, NULL);
1344 }
1345
1346 #ifdef CONFIG_PM
1347 static unsigned char saved_regs[] = {
1348         FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
1349         FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
1350         FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
1351         FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
1352 };
1353
1354 static int snd_fm801_suspend(struct pci_dev *pci, pm_message_t state)
1355 {
1356         struct snd_card *card = pci_get_drvdata(pci);
1357         struct fm801 *chip = card->private_data;
1358         int i;
1359
1360         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1361         snd_pcm_suspend_all(chip->pcm);
1362         snd_ac97_suspend(chip->ac97);
1363         snd_ac97_suspend(chip->ac97_sec);
1364         for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1365                 chip->saved_regs[i] = inw(chip->port + saved_regs[i]);
1366         /* FIXME: tea575x suspend */
1367
1368         pci_disable_device(pci);
1369         pci_save_state(pci);
1370         pci_set_power_state(pci, pci_choose_state(pci, state));
1371         return 0;
1372 }
1373
1374 static int snd_fm801_resume(struct pci_dev *pci)
1375 {
1376         struct snd_card *card = pci_get_drvdata(pci);
1377         struct fm801 *chip = card->private_data;
1378         int i;
1379
1380         pci_set_power_state(pci, PCI_D0);
1381         pci_restore_state(pci);
1382         if (pci_enable_device(pci) < 0) {
1383                 printk(KERN_ERR "fm801: pci_enable_device failed, "
1384                        "disabling device\n");
1385                 snd_card_disconnect(card);
1386                 return -EIO;
1387         }
1388         pci_set_master(pci);
1389
1390         snd_fm801_chip_init(chip, 1);
1391         snd_ac97_resume(chip->ac97);
1392         snd_ac97_resume(chip->ac97_sec);
1393         for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1394                 outw(chip->saved_regs[i], chip->port + saved_regs[i]);
1395
1396         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1397         return 0;
1398 }
1399 #endif
1400
1401 static struct pci_driver driver = {
1402         .name = KBUILD_MODNAME,
1403         .id_table = snd_fm801_ids,
1404         .probe = snd_card_fm801_probe,
1405         .remove = __devexit_p(snd_card_fm801_remove),
1406 #ifdef CONFIG_PM
1407         .suspend = snd_fm801_suspend,
1408         .resume = snd_fm801_resume,
1409 #endif
1410 };
1411
1412 static int __init alsa_card_fm801_init(void)
1413 {
1414         return pci_register_driver(&driver);
1415 }
1416
1417 static void __exit alsa_card_fm801_exit(void)
1418 {
1419         pci_unregister_driver(&driver);
1420 }
1421
1422 module_init(alsa_card_fm801_init)
1423 module_exit(alsa_card_fm801_exit)