Merge tag 'for-linus' of git://git.armlinux.org.uk/~rmk/linux-arm
[linux-2.6-microblaze.git] / sound / pci / als300.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  als300.c - driver for Avance Logic ALS300/ALS300+ soundcards.
4  *  Copyright (C) 2005 by Ash Willis <ashwillis@programmer.net>
5  *
6  *  TODO
7  *  4 channel playback for ALS300+
8  *  gameport
9  *  mpu401
10  *  opl3
11  *
12  *  NOTES
13  *  The BLOCK_COUNTER registers for the ALS300(+) return a figure related to
14  *  the position in the current period, NOT the whole buffer. It is important
15  *  to know which period we are in so we can calculate the correct pointer.
16  *  This is why we always use 2 periods. We can then use a flip-flop variable
17  *  to keep track of what period we are in.
18  */
19
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/pci.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/interrupt.h>
26 #include <linux/slab.h>
27 #include <linux/io.h>
28
29 #include <sound/core.h>
30 #include <sound/control.h>
31 #include <sound/initval.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/ac97_codec.h>
35 #include <sound/opl3.h>
36
37 /* snd_als300_set_irq_flag */
38 #define IRQ_DISABLE             0
39 #define IRQ_ENABLE              1
40
41 /* I/O port layout */
42 #define AC97_ACCESS             0x00
43 #define AC97_READ               0x04
44 #define AC97_STATUS             0x06
45 #define   AC97_DATA_AVAIL               (1<<6)
46 #define   AC97_BUSY                     (1<<7)
47 #define ALS300_IRQ_STATUS       0x07            /* ALS300 Only */
48 #define   IRQ_PLAYBACK                  (1<<3)
49 #define   IRQ_CAPTURE                   (1<<2)
50 #define GCR_DATA                0x08
51 #define GCR_INDEX               0x0C
52 #define ALS300P_DRAM_IRQ_STATUS 0x0D            /* ALS300+ Only */
53 #define MPU_IRQ_STATUS          0x0E            /* ALS300 Rev. E+, ALS300+ */
54 #define ALS300P_IRQ_STATUS      0x0F            /* ALS300+ Only */
55
56 /* General Control Registers */
57 #define PLAYBACK_START          0x80
58 #define PLAYBACK_END            0x81
59 #define PLAYBACK_CONTROL        0x82
60 #define   TRANSFER_START                (1<<16)
61 #define   FIFO_PAUSE                    (1<<17)
62 #define RECORD_START            0x83
63 #define RECORD_END              0x84
64 #define RECORD_CONTROL          0x85
65 #define DRAM_WRITE_CONTROL      0x8B
66 #define   WRITE_TRANS_START             (1<<16)
67 #define   DRAM_MODE_2                   (1<<17)
68 #define MISC_CONTROL            0x8C
69 #define   IRQ_SET_BIT                   (1<<15)
70 #define   VMUTE_NORMAL                  (1<<20)
71 #define   MMUTE_NORMAL                  (1<<21)
72 #define MUS_VOC_VOL             0x8E
73 #define PLAYBACK_BLOCK_COUNTER  0x9A
74 #define RECORD_BLOCK_COUNTER    0x9B
75
76 #define DEBUG_PLAY_REC  0
77
78 #if DEBUG_PLAY_REC
79 #define snd_als300_dbgplay(format, args...) printk(KERN_ERR format, ##args)
80 #else
81 #define snd_als300_dbgplay(format, args...)
82 #endif          
83
84 enum {DEVICE_ALS300, DEVICE_ALS300_PLUS};
85
86 MODULE_AUTHOR("Ash Willis <ashwillis@programmer.net>");
87 MODULE_DESCRIPTION("Avance Logic ALS300");
88 MODULE_LICENSE("GPL");
89
90 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
91 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
92 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
93
94 module_param_array(index, int, NULL, 0444);
95 MODULE_PARM_DESC(index, "Index value for ALS300 sound card.");
96 module_param_array(id, charp, NULL, 0444);
97 MODULE_PARM_DESC(id, "ID string for ALS300 sound card.");
98 module_param_array(enable, bool, NULL, 0444);
99 MODULE_PARM_DESC(enable, "Enable ALS300 sound card.");
100
101 struct snd_als300 {
102         unsigned long port;
103         spinlock_t reg_lock;
104         struct snd_card *card;
105         struct pci_dev *pci;
106
107         struct snd_pcm *pcm;
108         struct snd_pcm_substream *playback_substream;
109         struct snd_pcm_substream *capture_substream;
110
111         struct snd_ac97 *ac97;
112         struct snd_opl3 *opl3;
113
114         struct resource *res_port;
115
116         int irq;
117
118         int chip_type; /* ALS300 or ALS300+ */
119
120         char revision;  
121 };
122
123 struct snd_als300_substream_data {
124         int period_flipflop;
125         int control_register;
126         int block_counter_register;
127 };
128
129 static const struct pci_device_id snd_als300_ids[] = {
130         { 0x4005, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300 },
131         { 0x4005, 0x0308, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300_PLUS },
132         { 0, }
133 };
134
135 MODULE_DEVICE_TABLE(pci, snd_als300_ids);
136
137 static inline u32 snd_als300_gcr_read(unsigned long port, unsigned short reg)
138 {
139         outb(reg, port+GCR_INDEX);
140         return inl(port+GCR_DATA);
141 }
142
143 static inline void snd_als300_gcr_write(unsigned long port,
144                                                 unsigned short reg, u32 val)
145 {
146         outb(reg, port+GCR_INDEX);
147         outl(val, port+GCR_DATA);
148 }
149
150 /* Enable/Disable Interrupts */
151 static void snd_als300_set_irq_flag(struct snd_als300 *chip, int cmd)
152 {
153         u32 tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
154
155         /* boolean XOR check, since old vs. new hardware have
156            directly reversed bit setting for ENABLE and DISABLE.
157            ALS300+ acts like newer versions of ALS300 */
158         if (((chip->revision > 5 || chip->chip_type == DEVICE_ALS300_PLUS) ^
159                                                 (cmd == IRQ_ENABLE)) == 0)
160                 tmp |= IRQ_SET_BIT;
161         else
162                 tmp &= ~IRQ_SET_BIT;
163         snd_als300_gcr_write(chip->port, MISC_CONTROL, tmp);
164 }
165
166 static int snd_als300_free(struct snd_als300 *chip)
167 {
168         snd_als300_set_irq_flag(chip, IRQ_DISABLE);
169         if (chip->irq >= 0)
170                 free_irq(chip->irq, chip);
171         pci_release_regions(chip->pci);
172         pci_disable_device(chip->pci);
173         kfree(chip);
174         return 0;
175 }
176
177 static int snd_als300_dev_free(struct snd_device *device)
178 {
179         struct snd_als300 *chip = device->device_data;
180         return snd_als300_free(chip);
181 }
182
183 static irqreturn_t snd_als300_interrupt(int irq, void *dev_id)
184 {
185         u8 status;
186         struct snd_als300 *chip = dev_id;
187         struct snd_als300_substream_data *data;
188
189         status = inb(chip->port+ALS300_IRQ_STATUS);
190         if (!status) /* shared IRQ, for different device?? Exit ASAP! */
191                 return IRQ_NONE;
192
193         /* ACK everything ASAP */
194         outb(status, chip->port+ALS300_IRQ_STATUS);
195         if (status & IRQ_PLAYBACK) {
196                 if (chip->pcm && chip->playback_substream) {
197                         data = chip->playback_substream->runtime->private_data;
198                         data->period_flipflop ^= 1;
199                         snd_pcm_period_elapsed(chip->playback_substream);
200                         snd_als300_dbgplay("IRQ_PLAYBACK\n");
201                 }
202         }
203         if (status & IRQ_CAPTURE) {
204                 if (chip->pcm && chip->capture_substream) {
205                         data = chip->capture_substream->runtime->private_data;
206                         data->period_flipflop ^= 1;
207                         snd_pcm_period_elapsed(chip->capture_substream);
208                         snd_als300_dbgplay("IRQ_CAPTURE\n");
209                 }
210         }
211         return IRQ_HANDLED;
212 }
213
214 static irqreturn_t snd_als300plus_interrupt(int irq, void *dev_id)
215 {
216         u8 general, mpu, dram;
217         struct snd_als300 *chip = dev_id;
218         struct snd_als300_substream_data *data;
219         
220         general = inb(chip->port+ALS300P_IRQ_STATUS);
221         mpu = inb(chip->port+MPU_IRQ_STATUS);
222         dram = inb(chip->port+ALS300P_DRAM_IRQ_STATUS);
223
224         /* shared IRQ, for different device?? Exit ASAP! */
225         if ((general == 0) && ((mpu & 0x80) == 0) && ((dram & 0x01) == 0))
226                 return IRQ_NONE;
227
228         if (general & IRQ_PLAYBACK) {
229                 if (chip->pcm && chip->playback_substream) {
230                         outb(IRQ_PLAYBACK, chip->port+ALS300P_IRQ_STATUS);
231                         data = chip->playback_substream->runtime->private_data;
232                         data->period_flipflop ^= 1;
233                         snd_pcm_period_elapsed(chip->playback_substream);
234                         snd_als300_dbgplay("IRQ_PLAYBACK\n");
235                 }
236         }
237         if (general & IRQ_CAPTURE) {
238                 if (chip->pcm && chip->capture_substream) {
239                         outb(IRQ_CAPTURE, chip->port+ALS300P_IRQ_STATUS);
240                         data = chip->capture_substream->runtime->private_data;
241                         data->period_flipflop ^= 1;
242                         snd_pcm_period_elapsed(chip->capture_substream);
243                         snd_als300_dbgplay("IRQ_CAPTURE\n");
244                 }
245         }
246         /* FIXME: Ack other interrupt types. Not important right now as
247          * those other devices aren't enabled. */
248         return IRQ_HANDLED;
249 }
250
251 static void snd_als300_remove(struct pci_dev *pci)
252 {
253         snd_card_free(pci_get_drvdata(pci));
254 }
255
256 static unsigned short snd_als300_ac97_read(struct snd_ac97 *ac97,
257                                                         unsigned short reg)
258 {
259         int i;
260         struct snd_als300 *chip = ac97->private_data;
261
262         for (i = 0; i < 1000; i++) {
263                 if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
264                         break;
265                 udelay(10);
266         }
267         outl((reg << 24) | (1 << 31), chip->port+AC97_ACCESS);
268
269         for (i = 0; i < 1000; i++) {
270                 if ((inb(chip->port+AC97_STATUS) & (AC97_DATA_AVAIL)) != 0)
271                         break;
272                 udelay(10);
273         }
274         return inw(chip->port+AC97_READ);
275 }
276
277 static void snd_als300_ac97_write(struct snd_ac97 *ac97,
278                                 unsigned short reg, unsigned short val)
279 {
280         int i;
281         struct snd_als300 *chip = ac97->private_data;
282
283         for (i = 0; i < 1000; i++) {
284                 if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
285                         break;
286                 udelay(10);
287         }
288         outl((reg << 24) | val, chip->port+AC97_ACCESS);
289 }
290
291 static int snd_als300_ac97(struct snd_als300 *chip)
292 {
293         struct snd_ac97_bus *bus;
294         struct snd_ac97_template ac97;
295         int err;
296         static const struct snd_ac97_bus_ops ops = {
297                 .write = snd_als300_ac97_write,
298                 .read = snd_als300_ac97_read,
299         };
300
301         if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus)) < 0)
302                 return err;
303
304         memset(&ac97, 0, sizeof(ac97));
305         ac97.private_data = chip;
306
307         return snd_ac97_mixer(bus, &ac97, &chip->ac97);
308 }
309
310 /* hardware definition
311  *
312  * In AC97 mode, we always use 48k/16bit/stereo.
313  * Any request to change data type is ignored by
314  * the card when it is running outside of legacy
315  * mode.
316  */
317 static const struct snd_pcm_hardware snd_als300_playback_hw =
318 {
319         .info =                 (SNDRV_PCM_INFO_MMAP |
320                                 SNDRV_PCM_INFO_INTERLEAVED |
321                                 SNDRV_PCM_INFO_PAUSE |
322                                 SNDRV_PCM_INFO_MMAP_VALID),
323         .formats =              SNDRV_PCM_FMTBIT_S16,
324         .rates =                SNDRV_PCM_RATE_48000,
325         .rate_min =             48000,
326         .rate_max =             48000,
327         .channels_min =         2,
328         .channels_max =         2,
329         .buffer_bytes_max =     64 * 1024,
330         .period_bytes_min =     64,
331         .period_bytes_max =     32 * 1024,
332         .periods_min =          2,
333         .periods_max =          2,
334 };
335
336 static const struct snd_pcm_hardware snd_als300_capture_hw =
337 {
338         .info =                 (SNDRV_PCM_INFO_MMAP |
339                                 SNDRV_PCM_INFO_INTERLEAVED |
340                                 SNDRV_PCM_INFO_PAUSE |
341                                 SNDRV_PCM_INFO_MMAP_VALID),
342         .formats =              SNDRV_PCM_FMTBIT_S16,
343         .rates =                SNDRV_PCM_RATE_48000,
344         .rate_min =             48000,
345         .rate_max =             48000,
346         .channels_min =         2,
347         .channels_max =         2,
348         .buffer_bytes_max =     64 * 1024,
349         .period_bytes_min =     64,
350         .period_bytes_max =     32 * 1024,
351         .periods_min =          2,
352         .periods_max =          2,
353 };
354
355 static int snd_als300_playback_open(struct snd_pcm_substream *substream)
356 {
357         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
358         struct snd_pcm_runtime *runtime = substream->runtime;
359         struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
360                                                                 GFP_KERNEL);
361
362         if (!data)
363                 return -ENOMEM;
364         chip->playback_substream = substream;
365         runtime->hw = snd_als300_playback_hw;
366         runtime->private_data = data;
367         data->control_register = PLAYBACK_CONTROL;
368         data->block_counter_register = PLAYBACK_BLOCK_COUNTER;
369         return 0;
370 }
371
372 static int snd_als300_playback_close(struct snd_pcm_substream *substream)
373 {
374         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
375         struct snd_als300_substream_data *data;
376
377         data = substream->runtime->private_data;
378         kfree(data);
379         chip->playback_substream = NULL;
380         return 0;
381 }
382
383 static int snd_als300_capture_open(struct snd_pcm_substream *substream)
384 {
385         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
386         struct snd_pcm_runtime *runtime = substream->runtime;
387         struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
388                                                                 GFP_KERNEL);
389
390         if (!data)
391                 return -ENOMEM;
392         chip->capture_substream = substream;
393         runtime->hw = snd_als300_capture_hw;
394         runtime->private_data = data;
395         data->control_register = RECORD_CONTROL;
396         data->block_counter_register = RECORD_BLOCK_COUNTER;
397         return 0;
398 }
399
400 static int snd_als300_capture_close(struct snd_pcm_substream *substream)
401 {
402         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
403         struct snd_als300_substream_data *data;
404
405         data = substream->runtime->private_data;
406         kfree(data);
407         chip->capture_substream = NULL;
408         return 0;
409 }
410
411 static int snd_als300_playback_prepare(struct snd_pcm_substream *substream)
412 {
413         u32 tmp;
414         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
415         struct snd_pcm_runtime *runtime = substream->runtime;
416         unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
417         unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
418         
419         spin_lock_irq(&chip->reg_lock);
420         tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
421         tmp &= ~TRANSFER_START;
422
423         snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n",
424                                                 period_bytes, buffer_bytes);
425         
426         /* set block size */
427         tmp &= 0xffff0000;
428         tmp |= period_bytes - 1;
429         snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL, tmp);
430
431         /* set dma area */
432         snd_als300_gcr_write(chip->port, PLAYBACK_START,
433                                         runtime->dma_addr);
434         snd_als300_gcr_write(chip->port, PLAYBACK_END,
435                                         runtime->dma_addr + buffer_bytes - 1);
436         spin_unlock_irq(&chip->reg_lock);
437         return 0;
438 }
439
440 static int snd_als300_capture_prepare(struct snd_pcm_substream *substream)
441 {
442         u32 tmp;
443         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
444         struct snd_pcm_runtime *runtime = substream->runtime;
445         unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
446         unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
447
448         spin_lock_irq(&chip->reg_lock);
449         tmp = snd_als300_gcr_read(chip->port, RECORD_CONTROL);
450         tmp &= ~TRANSFER_START;
451
452         snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n", period_bytes,
453                                                         buffer_bytes);
454
455         /* set block size */
456         tmp &= 0xffff0000;
457         tmp |= period_bytes - 1;
458
459         /* set dma area */
460         snd_als300_gcr_write(chip->port, RECORD_CONTROL, tmp);
461         snd_als300_gcr_write(chip->port, RECORD_START,
462                                         runtime->dma_addr);
463         snd_als300_gcr_write(chip->port, RECORD_END,
464                                         runtime->dma_addr + buffer_bytes - 1);
465         spin_unlock_irq(&chip->reg_lock);
466         return 0;
467 }
468
469 static int snd_als300_trigger(struct snd_pcm_substream *substream, int cmd)
470 {
471         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
472         u32 tmp;
473         struct snd_als300_substream_data *data;
474         unsigned short reg;
475         int ret = 0;
476
477         data = substream->runtime->private_data;
478         reg = data->control_register;
479
480         spin_lock(&chip->reg_lock);
481         switch (cmd) {
482         case SNDRV_PCM_TRIGGER_START:
483         case SNDRV_PCM_TRIGGER_RESUME:
484                 tmp = snd_als300_gcr_read(chip->port, reg);
485                 data->period_flipflop = 1;
486                 snd_als300_gcr_write(chip->port, reg, tmp | TRANSFER_START);
487                 snd_als300_dbgplay("TRIGGER START\n");
488                 break;
489         case SNDRV_PCM_TRIGGER_STOP:
490         case SNDRV_PCM_TRIGGER_SUSPEND:
491                 tmp = snd_als300_gcr_read(chip->port, reg);
492                 snd_als300_gcr_write(chip->port, reg, tmp & ~TRANSFER_START);
493                 snd_als300_dbgplay("TRIGGER STOP\n");
494                 break;
495         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
496                 tmp = snd_als300_gcr_read(chip->port, reg);
497                 snd_als300_gcr_write(chip->port, reg, tmp | FIFO_PAUSE);
498                 snd_als300_dbgplay("TRIGGER PAUSE\n");
499                 break;
500         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
501                 tmp = snd_als300_gcr_read(chip->port, reg);
502                 snd_als300_gcr_write(chip->port, reg, tmp & ~FIFO_PAUSE);
503                 snd_als300_dbgplay("TRIGGER RELEASE\n");
504                 break;
505         default:
506                 snd_als300_dbgplay("TRIGGER INVALID\n");
507                 ret = -EINVAL;
508         }
509         spin_unlock(&chip->reg_lock);
510         return ret;
511 }
512
513 static snd_pcm_uframes_t snd_als300_pointer(struct snd_pcm_substream *substream)
514 {
515         u16 current_ptr;
516         struct snd_als300 *chip = snd_pcm_substream_chip(substream);
517         struct snd_als300_substream_data *data;
518         unsigned short period_bytes;
519
520         data = substream->runtime->private_data;
521         period_bytes = snd_pcm_lib_period_bytes(substream);
522         
523         spin_lock(&chip->reg_lock);
524         current_ptr = (u16) snd_als300_gcr_read(chip->port,
525                                         data->block_counter_register) + 4;
526         spin_unlock(&chip->reg_lock);
527         if (current_ptr > period_bytes)
528                 current_ptr = 0;
529         else
530                 current_ptr = period_bytes - current_ptr;
531
532         if (data->period_flipflop == 0)
533                 current_ptr += period_bytes;
534         snd_als300_dbgplay("Pointer (bytes): %d\n", current_ptr);
535         return bytes_to_frames(substream->runtime, current_ptr);
536 }
537
538 static const struct snd_pcm_ops snd_als300_playback_ops = {
539         .open =         snd_als300_playback_open,
540         .close =        snd_als300_playback_close,
541         .prepare =      snd_als300_playback_prepare,
542         .trigger =      snd_als300_trigger,
543         .pointer =      snd_als300_pointer,
544 };
545
546 static const struct snd_pcm_ops snd_als300_capture_ops = {
547         .open =         snd_als300_capture_open,
548         .close =        snd_als300_capture_close,
549         .prepare =      snd_als300_capture_prepare,
550         .trigger =      snd_als300_trigger,
551         .pointer =      snd_als300_pointer,
552 };
553
554 static int snd_als300_new_pcm(struct snd_als300 *chip)
555 {
556         struct snd_pcm *pcm;
557         int err;
558
559         err = snd_pcm_new(chip->card, "ALS300", 0, 1, 1, &pcm);
560         if (err < 0)
561                 return err;
562         pcm->private_data = chip;
563         strcpy(pcm->name, "ALS300");
564         chip->pcm = pcm;
565
566         /* set operators */
567         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
568                                 &snd_als300_playback_ops);
569         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
570                                 &snd_als300_capture_ops);
571
572         /* pre-allocation of buffers */
573         snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
574                                        64*1024, 64*1024);
575         return 0;
576 }
577
578 static void snd_als300_init(struct snd_als300 *chip)
579 {
580         unsigned long flags;
581         u32 tmp;
582         
583         spin_lock_irqsave(&chip->reg_lock, flags);
584         chip->revision = (snd_als300_gcr_read(chip->port, MISC_CONTROL) >> 16)
585                                                                 & 0x0000000F;
586         /* Setup DRAM */
587         tmp = snd_als300_gcr_read(chip->port, DRAM_WRITE_CONTROL);
588         snd_als300_gcr_write(chip->port, DRAM_WRITE_CONTROL,
589                                                 (tmp | DRAM_MODE_2)
590                                                 & ~WRITE_TRANS_START);
591
592         /* Enable IRQ output */
593         snd_als300_set_irq_flag(chip, IRQ_ENABLE);
594
595         /* Unmute hardware devices so their outputs get routed to
596          * the onboard mixer */
597         tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
598         snd_als300_gcr_write(chip->port, MISC_CONTROL,
599                         tmp | VMUTE_NORMAL | MMUTE_NORMAL);
600
601         /* Reset volumes */
602         snd_als300_gcr_write(chip->port, MUS_VOC_VOL, 0);
603
604         /* Make sure playback transfer is stopped */
605         tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
606         snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL,
607                         tmp & ~TRANSFER_START);
608         spin_unlock_irqrestore(&chip->reg_lock, flags);
609 }
610
611 static int snd_als300_create(struct snd_card *card,
612                              struct pci_dev *pci, int chip_type,
613                              struct snd_als300 **rchip)
614 {
615         struct snd_als300 *chip;
616         void *irq_handler;
617         int err;
618
619         static const struct snd_device_ops ops = {
620                 .dev_free = snd_als300_dev_free,
621         };
622         *rchip = NULL;
623
624         if ((err = pci_enable_device(pci)) < 0)
625                 return err;
626
627         if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(28))) {
628                 dev_err(card->dev, "error setting 28bit DMA mask\n");
629                 pci_disable_device(pci);
630                 return -ENXIO;
631         }
632         pci_set_master(pci);
633
634         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
635         if (chip == NULL) {
636                 pci_disable_device(pci);
637                 return -ENOMEM;
638         }
639
640         chip->card = card;
641         chip->pci = pci;
642         chip->irq = -1;
643         chip->chip_type = chip_type;
644         spin_lock_init(&chip->reg_lock);
645
646         if ((err = pci_request_regions(pci, "ALS300")) < 0) {
647                 kfree(chip);
648                 pci_disable_device(pci);
649                 return err;
650         }
651         chip->port = pci_resource_start(pci, 0);
652
653         if (chip->chip_type == DEVICE_ALS300_PLUS)
654                 irq_handler = snd_als300plus_interrupt;
655         else
656                 irq_handler = snd_als300_interrupt;
657
658         if (request_irq(pci->irq, irq_handler, IRQF_SHARED,
659                         KBUILD_MODNAME, chip)) {
660                 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
661                 snd_als300_free(chip);
662                 return -EBUSY;
663         }
664         chip->irq = pci->irq;
665         card->sync_irq = chip->irq;
666
667         snd_als300_init(chip);
668
669         err = snd_als300_ac97(chip);
670         if (err < 0) {
671                 dev_err(card->dev, "Could not create ac97\n");
672                 snd_als300_free(chip);
673                 return err;
674         }
675
676         if ((err = snd_als300_new_pcm(chip)) < 0) {
677                 dev_err(card->dev, "Could not create PCM\n");
678                 snd_als300_free(chip);
679                 return err;
680         }
681
682         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
683                                                 chip, &ops)) < 0) {
684                 snd_als300_free(chip);
685                 return err;
686         }
687
688         *rchip = chip;
689         return 0;
690 }
691
692 #ifdef CONFIG_PM_SLEEP
693 static int snd_als300_suspend(struct device *dev)
694 {
695         struct snd_card *card = dev_get_drvdata(dev);
696         struct snd_als300 *chip = card->private_data;
697
698         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
699         snd_ac97_suspend(chip->ac97);
700         return 0;
701 }
702
703 static int snd_als300_resume(struct device *dev)
704 {
705         struct snd_card *card = dev_get_drvdata(dev);
706         struct snd_als300 *chip = card->private_data;
707
708         snd_als300_init(chip);
709         snd_ac97_resume(chip->ac97);
710
711         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
712         return 0;
713 }
714
715 static SIMPLE_DEV_PM_OPS(snd_als300_pm, snd_als300_suspend, snd_als300_resume);
716 #define SND_ALS300_PM_OPS       &snd_als300_pm
717 #else
718 #define SND_ALS300_PM_OPS       NULL
719 #endif
720
721 static int snd_als300_probe(struct pci_dev *pci,
722                              const struct pci_device_id *pci_id)
723 {
724         static int dev;
725         struct snd_card *card;
726         struct snd_als300 *chip;
727         int err, chip_type;
728
729         if (dev >= SNDRV_CARDS)
730                 return -ENODEV;
731         if (!enable[dev]) {
732                 dev++;
733                 return -ENOENT;
734         }
735
736         err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
737                            0, &card);
738
739         if (err < 0)
740                 return err;
741
742         chip_type = pci_id->driver_data;
743
744         if ((err = snd_als300_create(card, pci, chip_type, &chip)) < 0) {
745                 snd_card_free(card);
746                 return err;
747         }
748         card->private_data = chip;
749
750         strcpy(card->driver, "ALS300");
751         if (chip->chip_type == DEVICE_ALS300_PLUS)
752                 /* don't know much about ALS300+ yet
753                  * print revision number for now */
754                 sprintf(card->shortname, "ALS300+ (Rev. %d)", chip->revision);
755         else
756                 sprintf(card->shortname, "ALS300 (Rev. %c)", 'A' +
757                                                         chip->revision - 1);
758         sprintf(card->longname, "%s at 0x%lx irq %i",
759                                 card->shortname, chip->port, chip->irq);
760
761         if ((err = snd_card_register(card)) < 0) {
762                 snd_card_free(card);
763                 return err;
764         }
765         pci_set_drvdata(pci, card);
766         dev++;
767         return 0;
768 }
769
770 static struct pci_driver als300_driver = {
771         .name = KBUILD_MODNAME,
772         .id_table = snd_als300_ids,
773         .probe = snd_als300_probe,
774         .remove = snd_als300_remove,
775         .driver = {
776                 .pm = SND_ALS300_PM_OPS,
777         },
778 };
779
780 module_pci_driver(als300_driver);