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