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