1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * als300.c - driver for Avance Logic ALS300/ALS300+ soundcards.
7 * 4 channel playback for ALS300+
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.
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>
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>
37 /* snd_als300_set_irq_flag */
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)
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 */
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
76 #define DEBUG_PLAY_REC 0
79 #define snd_als300_dbgplay(format, args...) printk(KERN_ERR format, ##args)
81 #define snd_als300_dbgplay(format, args...)
84 enum {DEVICE_ALS300, DEVICE_ALS300_PLUS};
87 MODULE_DESCRIPTION("Avance Logic ALS300");
88 MODULE_LICENSE("GPL");
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;
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.");
104 struct snd_card *card;
108 struct snd_pcm_substream *playback_substream;
109 struct snd_pcm_substream *capture_substream;
111 struct snd_ac97 *ac97;
112 struct snd_opl3 *opl3;
114 struct resource *res_port;
118 int chip_type; /* ALS300 or ALS300+ */
123 struct snd_als300_substream_data {
125 int control_register;
126 int block_counter_register;
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 },
135 MODULE_DEVICE_TABLE(pci, snd_als300_ids);
137 static inline u32 snd_als300_gcr_read(unsigned long port, unsigned short reg)
139 outb(reg, port+GCR_INDEX);
140 return inl(port+GCR_DATA);
143 static inline void snd_als300_gcr_write(unsigned long port,
144 unsigned short reg, u32 val)
146 outb(reg, port+GCR_INDEX);
147 outl(val, port+GCR_DATA);
150 /* Enable/Disable Interrupts */
151 static void snd_als300_set_irq_flag(struct snd_als300 *chip, int cmd)
153 u32 tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
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)
163 snd_als300_gcr_write(chip->port, MISC_CONTROL, tmp);
166 static int snd_als300_free(struct snd_als300 *chip)
168 snd_als300_set_irq_flag(chip, IRQ_DISABLE);
170 free_irq(chip->irq, chip);
171 pci_release_regions(chip->pci);
172 pci_disable_device(chip->pci);
177 static int snd_als300_dev_free(struct snd_device *device)
179 struct snd_als300 *chip = device->device_data;
180 return snd_als300_free(chip);
183 static irqreturn_t snd_als300_interrupt(int irq, void *dev_id)
186 struct snd_als300 *chip = dev_id;
187 struct snd_als300_substream_data *data;
189 status = inb(chip->port+ALS300_IRQ_STATUS);
190 if (!status) /* shared IRQ, for different device?? Exit ASAP! */
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");
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");
214 static irqreturn_t snd_als300plus_interrupt(int irq, void *dev_id)
216 u8 general, mpu, dram;
217 struct snd_als300 *chip = dev_id;
218 struct snd_als300_substream_data *data;
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);
224 /* shared IRQ, for different device?? Exit ASAP! */
225 if ((general == 0) && ((mpu & 0x80) == 0) && ((dram & 0x01) == 0))
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");
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");
246 /* FIXME: Ack other interrupt types. Not important right now as
247 * those other devices aren't enabled. */
251 static void snd_als300_remove(struct pci_dev *pci)
253 snd_card_free(pci_get_drvdata(pci));
256 static unsigned short snd_als300_ac97_read(struct snd_ac97 *ac97,
260 struct snd_als300 *chip = ac97->private_data;
262 for (i = 0; i < 1000; i++) {
263 if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
267 outl((reg << 24) | (1 << 31), chip->port+AC97_ACCESS);
269 for (i = 0; i < 1000; i++) {
270 if ((inb(chip->port+AC97_STATUS) & (AC97_DATA_AVAIL)) != 0)
274 return inw(chip->port+AC97_READ);
277 static void snd_als300_ac97_write(struct snd_ac97 *ac97,
278 unsigned short reg, unsigned short val)
281 struct snd_als300 *chip = ac97->private_data;
283 for (i = 0; i < 1000; i++) {
284 if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
288 outl((reg << 24) | val, chip->port+AC97_ACCESS);
291 static int snd_als300_ac97(struct snd_als300 *chip)
293 struct snd_ac97_bus *bus;
294 struct snd_ac97_template ac97;
296 static const struct snd_ac97_bus_ops ops = {
297 .write = snd_als300_ac97_write,
298 .read = snd_als300_ac97_read,
301 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus)) < 0)
304 memset(&ac97, 0, sizeof(ac97));
305 ac97.private_data = chip;
307 return snd_ac97_mixer(bus, &ac97, &chip->ac97);
310 /* hardware definition
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
317 static const struct snd_pcm_hardware snd_als300_playback_hw =
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,
329 .buffer_bytes_max = 64 * 1024,
330 .period_bytes_min = 64,
331 .period_bytes_max = 32 * 1024,
336 static const struct snd_pcm_hardware snd_als300_capture_hw =
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,
348 .buffer_bytes_max = 64 * 1024,
349 .period_bytes_min = 64,
350 .period_bytes_max = 32 * 1024,
355 static int snd_als300_playback_open(struct snd_pcm_substream *substream)
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),
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;
372 static int snd_als300_playback_close(struct snd_pcm_substream *substream)
374 struct snd_als300 *chip = snd_pcm_substream_chip(substream);
375 struct snd_als300_substream_data *data;
377 data = substream->runtime->private_data;
379 chip->playback_substream = NULL;
383 static int snd_als300_capture_open(struct snd_pcm_substream *substream)
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),
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;
400 static int snd_als300_capture_close(struct snd_pcm_substream *substream)
402 struct snd_als300 *chip = snd_pcm_substream_chip(substream);
403 struct snd_als300_substream_data *data;
405 data = substream->runtime->private_data;
407 chip->capture_substream = NULL;
411 static int snd_als300_playback_prepare(struct snd_pcm_substream *substream)
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);
419 spin_lock_irq(&chip->reg_lock);
420 tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
421 tmp &= ~TRANSFER_START;
423 snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n",
424 period_bytes, buffer_bytes);
428 tmp |= period_bytes - 1;
429 snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL, tmp);
432 snd_als300_gcr_write(chip->port, PLAYBACK_START,
434 snd_als300_gcr_write(chip->port, PLAYBACK_END,
435 runtime->dma_addr + buffer_bytes - 1);
436 spin_unlock_irq(&chip->reg_lock);
440 static int snd_als300_capture_prepare(struct snd_pcm_substream *substream)
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);
448 spin_lock_irq(&chip->reg_lock);
449 tmp = snd_als300_gcr_read(chip->port, RECORD_CONTROL);
450 tmp &= ~TRANSFER_START;
452 snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n", period_bytes,
457 tmp |= period_bytes - 1;
460 snd_als300_gcr_write(chip->port, RECORD_CONTROL, tmp);
461 snd_als300_gcr_write(chip->port, RECORD_START,
463 snd_als300_gcr_write(chip->port, RECORD_END,
464 runtime->dma_addr + buffer_bytes - 1);
465 spin_unlock_irq(&chip->reg_lock);
469 static int snd_als300_trigger(struct snd_pcm_substream *substream, int cmd)
471 struct snd_als300 *chip = snd_pcm_substream_chip(substream);
473 struct snd_als300_substream_data *data;
477 data = substream->runtime->private_data;
478 reg = data->control_register;
480 spin_lock(&chip->reg_lock);
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");
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");
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");
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");
506 snd_als300_dbgplay("TRIGGER INVALID\n");
509 spin_unlock(&chip->reg_lock);
513 static snd_pcm_uframes_t snd_als300_pointer(struct snd_pcm_substream *substream)
516 struct snd_als300 *chip = snd_pcm_substream_chip(substream);
517 struct snd_als300_substream_data *data;
518 unsigned short period_bytes;
520 data = substream->runtime->private_data;
521 period_bytes = snd_pcm_lib_period_bytes(substream);
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)
530 current_ptr = period_bytes - current_ptr;
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);
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,
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,
554 static int snd_als300_new_pcm(struct snd_als300 *chip)
559 err = snd_pcm_new(chip->card, "ALS300", 0, 1, 1, &pcm);
562 pcm->private_data = chip;
563 strcpy(pcm->name, "ALS300");
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);
572 /* pre-allocation of buffers */
573 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
578 static void snd_als300_init(struct snd_als300 *chip)
583 spin_lock_irqsave(&chip->reg_lock, flags);
584 chip->revision = (snd_als300_gcr_read(chip->port, MISC_CONTROL) >> 16)
587 tmp = snd_als300_gcr_read(chip->port, DRAM_WRITE_CONTROL);
588 snd_als300_gcr_write(chip->port, DRAM_WRITE_CONTROL,
590 & ~WRITE_TRANS_START);
592 /* Enable IRQ output */
593 snd_als300_set_irq_flag(chip, IRQ_ENABLE);
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);
602 snd_als300_gcr_write(chip->port, MUS_VOC_VOL, 0);
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);
611 static int snd_als300_create(struct snd_card *card,
612 struct pci_dev *pci, int chip_type,
613 struct snd_als300 **rchip)
615 struct snd_als300 *chip;
619 static const struct snd_device_ops ops = {
620 .dev_free = snd_als300_dev_free,
624 if ((err = pci_enable_device(pci)) < 0)
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);
634 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
636 pci_disable_device(pci);
643 chip->chip_type = chip_type;
644 spin_lock_init(&chip->reg_lock);
646 if ((err = pci_request_regions(pci, "ALS300")) < 0) {
648 pci_disable_device(pci);
651 chip->port = pci_resource_start(pci, 0);
653 if (chip->chip_type == DEVICE_ALS300_PLUS)
654 irq_handler = snd_als300plus_interrupt;
656 irq_handler = snd_als300_interrupt;
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);
664 chip->irq = pci->irq;
665 card->sync_irq = chip->irq;
667 snd_als300_init(chip);
669 err = snd_als300_ac97(chip);
671 dev_err(card->dev, "Could not create ac97\n");
672 snd_als300_free(chip);
676 if ((err = snd_als300_new_pcm(chip)) < 0) {
677 dev_err(card->dev, "Could not create PCM\n");
678 snd_als300_free(chip);
682 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
684 snd_als300_free(chip);
692 #ifdef CONFIG_PM_SLEEP
693 static int snd_als300_suspend(struct device *dev)
695 struct snd_card *card = dev_get_drvdata(dev);
696 struct snd_als300 *chip = card->private_data;
698 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
699 snd_ac97_suspend(chip->ac97);
703 static int snd_als300_resume(struct device *dev)
705 struct snd_card *card = dev_get_drvdata(dev);
706 struct snd_als300 *chip = card->private_data;
708 snd_als300_init(chip);
709 snd_ac97_resume(chip->ac97);
711 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
715 static SIMPLE_DEV_PM_OPS(snd_als300_pm, snd_als300_suspend, snd_als300_resume);
716 #define SND_ALS300_PM_OPS &snd_als300_pm
718 #define SND_ALS300_PM_OPS NULL
721 static int snd_als300_probe(struct pci_dev *pci,
722 const struct pci_device_id *pci_id)
725 struct snd_card *card;
726 struct snd_als300 *chip;
729 if (dev >= SNDRV_CARDS)
736 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
742 chip_type = pci_id->driver_data;
744 if ((err = snd_als300_create(card, pci, chip_type, &chip)) < 0) {
748 card->private_data = chip;
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);
756 sprintf(card->shortname, "ALS300 (Rev. %c)", 'A' +
758 sprintf(card->longname, "%s at 0x%lx irq %i",
759 card->shortname, chip->port, chip->irq);
761 if ((err = snd_card_register(card)) < 0) {
765 pci_set_drvdata(pci, card);
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,
776 .pm = SND_ALS300_PM_OPS,
780 module_pci_driver(als300_driver);