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