1 // SPDX-License-Identifier: GPL-2.0-only
2 /*****************************************************************************
4 * Copyright (C) 2008 Cedric Bregardis <cedric.bregardis@free.fr> and
5 * Jean-Christian Hassler <jhassler@free.fr>
7 * This file is part of the Audiowerk2 ALSA driver
9 *****************************************************************************/
10 #include <linux/init.h>
11 #include <linux/pci.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/slab.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
17 #include <linux/module.h>
18 #include <sound/core.h>
19 #include <sound/initval.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/control.h>
25 #include "aw2-saa7146.h"
27 MODULE_AUTHOR("Cedric Bregardis <cedric.bregardis@free.fr>, "
28 "Jean-Christian Hassler <jhassler@free.fr>");
29 MODULE_DESCRIPTION("Emagic Audiowerk 2 sound driver");
30 MODULE_LICENSE("GPL");
32 /*********************************
34 ********************************/
35 #define CTL_ROUTE_ANALOG 0
36 #define CTL_ROUTE_DIGITAL 1
38 /*********************************
40 ********************************/
41 /* hardware definition */
42 static const struct snd_pcm_hardware snd_aw2_playback_hw = {
43 .info = (SNDRV_PCM_INFO_MMAP |
44 SNDRV_PCM_INFO_INTERLEAVED |
45 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
46 .formats = SNDRV_PCM_FMTBIT_S16_LE,
47 .rates = SNDRV_PCM_RATE_44100,
52 .buffer_bytes_max = 32768,
53 .period_bytes_min = 4096,
54 .period_bytes_max = 32768,
59 static const struct snd_pcm_hardware snd_aw2_capture_hw = {
60 .info = (SNDRV_PCM_INFO_MMAP |
61 SNDRV_PCM_INFO_INTERLEAVED |
62 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
63 .formats = SNDRV_PCM_FMTBIT_S16_LE,
64 .rates = SNDRV_PCM_RATE_44100,
69 .buffer_bytes_max = 32768,
70 .period_bytes_min = 4096,
71 .period_bytes_max = 32768,
76 struct aw2_pcm_device {
78 unsigned int stream_number;
83 struct snd_aw2_saa7146 saa7146;
90 unsigned long iobase_phys;
91 void __iomem *iobase_virt;
93 struct snd_card *card;
95 struct aw2_pcm_device device_playback[NB_STREAM_PLAYBACK];
96 struct aw2_pcm_device device_capture[NB_STREAM_CAPTURE];
99 /*********************************
100 * FUNCTION DECLARATIONS
101 ********************************/
102 static int snd_aw2_dev_free(struct snd_device *device);
103 static int snd_aw2_create(struct snd_card *card,
104 struct pci_dev *pci, struct aw2 **rchip);
105 static int snd_aw2_probe(struct pci_dev *pci,
106 const struct pci_device_id *pci_id);
107 static void snd_aw2_remove(struct pci_dev *pci);
108 static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream);
109 static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream);
110 static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream);
111 static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream);
112 static int snd_aw2_pcm_hw_params(struct snd_pcm_substream *substream,
113 struct snd_pcm_hw_params *hw_params);
114 static int snd_aw2_pcm_hw_free(struct snd_pcm_substream *substream);
115 static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream);
116 static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream);
117 static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
119 static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
121 static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
123 static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
125 static int snd_aw2_new_pcm(struct aw2 *chip);
127 static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
128 struct snd_ctl_elem_info *uinfo);
129 static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
130 struct snd_ctl_elem_value
132 static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
133 struct snd_ctl_elem_value
136 /*********************************
138 ********************************/
139 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
140 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
141 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
143 module_param_array(index, int, NULL, 0444);
144 MODULE_PARM_DESC(index, "Index value for Audiowerk2 soundcard.");
145 module_param_array(id, charp, NULL, 0444);
146 MODULE_PARM_DESC(id, "ID string for the Audiowerk2 soundcard.");
147 module_param_array(enable, bool, NULL, 0444);
148 MODULE_PARM_DESC(enable, "Enable Audiowerk2 soundcard.");
150 static const struct pci_device_id snd_aw2_ids[] = {
151 {PCI_VENDOR_ID_PHILIPS, PCI_DEVICE_ID_PHILIPS_SAA7146, 0, 0,
156 MODULE_DEVICE_TABLE(pci, snd_aw2_ids);
158 /* pci_driver definition */
159 static struct pci_driver aw2_driver = {
160 .name = KBUILD_MODNAME,
161 .id_table = snd_aw2_ids,
162 .probe = snd_aw2_probe,
163 .remove = snd_aw2_remove,
166 module_pci_driver(aw2_driver);
168 /* operators for playback PCM alsa interface */
169 static const struct snd_pcm_ops snd_aw2_playback_ops = {
170 .open = snd_aw2_pcm_playback_open,
171 .close = snd_aw2_pcm_playback_close,
172 .ioctl = snd_pcm_lib_ioctl,
173 .hw_params = snd_aw2_pcm_hw_params,
174 .hw_free = snd_aw2_pcm_hw_free,
175 .prepare = snd_aw2_pcm_prepare_playback,
176 .trigger = snd_aw2_pcm_trigger_playback,
177 .pointer = snd_aw2_pcm_pointer_playback,
180 /* operators for capture PCM alsa interface */
181 static const struct snd_pcm_ops snd_aw2_capture_ops = {
182 .open = snd_aw2_pcm_capture_open,
183 .close = snd_aw2_pcm_capture_close,
184 .ioctl = snd_pcm_lib_ioctl,
185 .hw_params = snd_aw2_pcm_hw_params,
186 .hw_free = snd_aw2_pcm_hw_free,
187 .prepare = snd_aw2_pcm_prepare_capture,
188 .trigger = snd_aw2_pcm_trigger_capture,
189 .pointer = snd_aw2_pcm_pointer_capture,
192 static const struct snd_kcontrol_new aw2_control = {
193 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
194 .name = "PCM Capture Route",
196 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
197 .private_value = 0xffff,
198 .info = snd_aw2_control_switch_capture_info,
199 .get = snd_aw2_control_switch_capture_get,
200 .put = snd_aw2_control_switch_capture_put
203 /*********************************
204 * FUNCTION IMPLEMENTATIONS
205 ********************************/
207 /* component-destructor */
208 static int snd_aw2_dev_free(struct snd_device *device)
210 struct aw2 *chip = device->device_data;
213 snd_aw2_saa7146_free(&chip->saa7146);
215 /* release the irq */
217 free_irq(chip->irq, (void *)chip);
218 /* release the i/o ports & memory */
219 iounmap(chip->iobase_virt);
220 pci_release_regions(chip->pci);
221 /* disable the PCI entry */
222 pci_disable_device(chip->pci);
223 /* release the data */
229 /* chip-specific constructor */
230 static int snd_aw2_create(struct snd_card *card,
231 struct pci_dev *pci, struct aw2 **rchip)
235 static struct snd_device_ops ops = {
236 .dev_free = snd_aw2_dev_free,
241 /* initialize the PCI entry */
242 err = pci_enable_device(pci);
247 /* check PCI availability (32bit DMA) */
248 if ((dma_set_mask(&pci->dev, DMA_BIT_MASK(32)) < 0) ||
249 (dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(32)) < 0)) {
250 dev_err(card->dev, "Impossible to set 32bit mask DMA\n");
251 pci_disable_device(pci);
254 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
256 pci_disable_device(pci);
260 /* initialize the stuff */
265 /* (1) PCI resource allocation */
266 err = pci_request_regions(pci, "Audiowerk2");
268 pci_disable_device(pci);
272 chip->iobase_phys = pci_resource_start(pci, 0);
274 ioremap_nocache(chip->iobase_phys,
275 pci_resource_len(pci, 0));
277 if (chip->iobase_virt == NULL) {
278 dev_err(card->dev, "unable to remap memory region");
279 pci_release_regions(pci);
280 pci_disable_device(pci);
285 /* (2) initialization of the chip hardware */
286 snd_aw2_saa7146_setup(&chip->saa7146, chip->iobase_virt);
288 if (request_irq(pci->irq, snd_aw2_saa7146_interrupt,
289 IRQF_SHARED, KBUILD_MODNAME, chip)) {
290 dev_err(card->dev, "Cannot grab irq %d\n", pci->irq);
292 iounmap(chip->iobase_virt);
293 pci_release_regions(chip->pci);
294 pci_disable_device(chip->pci);
298 chip->irq = pci->irq;
300 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
302 free_irq(chip->irq, (void *)chip);
303 iounmap(chip->iobase_virt);
304 pci_release_regions(chip->pci);
305 pci_disable_device(chip->pci);
313 "Audiowerk 2 sound card (saa7146 chipset) detected and managed\n");
318 static int snd_aw2_probe(struct pci_dev *pci,
319 const struct pci_device_id *pci_id)
322 struct snd_card *card;
326 /* (1) Continue if device is not enabled, else inc dev */
327 if (dev >= SNDRV_CARDS)
334 /* (2) Create card instance */
335 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
340 /* (3) Create main component */
341 err = snd_aw2_create(card, pci, &chip);
347 /* initialize mutex */
348 mutex_init(&chip->mtx);
350 spin_lock_init(&chip->reg_lock);
351 /* (4) Define driver ID and name string */
352 strcpy(card->driver, "aw2");
353 strcpy(card->shortname, "Audiowerk2");
355 sprintf(card->longname, "%s with SAA7146 irq %i",
356 card->shortname, chip->irq);
358 /* (5) Create other components */
359 snd_aw2_new_pcm(chip);
361 /* (6) Register card instance */
362 err = snd_card_register(card);
368 /* (7) Set PCI driver data */
369 pci_set_drvdata(pci, card);
376 static void snd_aw2_remove(struct pci_dev *pci)
378 snd_card_free(pci_get_drvdata(pci));
382 static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream)
384 struct snd_pcm_runtime *runtime = substream->runtime;
386 dev_dbg(substream->pcm->card->dev, "Playback_open\n");
387 runtime->hw = snd_aw2_playback_hw;
392 static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream)
398 static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream)
400 struct snd_pcm_runtime *runtime = substream->runtime;
402 dev_dbg(substream->pcm->card->dev, "Capture_open\n");
403 runtime->hw = snd_aw2_capture_hw;
408 static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream)
410 /* TODO: something to do ? */
414 /* hw_params callback */
415 static int snd_aw2_pcm_hw_params(struct snd_pcm_substream *substream,
416 struct snd_pcm_hw_params *hw_params)
418 return snd_pcm_lib_malloc_pages(substream,
419 params_buffer_bytes(hw_params));
422 /* hw_free callback */
423 static int snd_aw2_pcm_hw_free(struct snd_pcm_substream *substream)
425 return snd_pcm_lib_free_pages(substream);
428 /* prepare callback for playback */
429 static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream)
431 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
432 struct aw2 *chip = pcm_device->chip;
433 struct snd_pcm_runtime *runtime = substream->runtime;
434 unsigned long period_size, buffer_size;
436 mutex_lock(&chip->mtx);
438 period_size = snd_pcm_lib_period_bytes(substream);
439 buffer_size = snd_pcm_lib_buffer_bytes(substream);
441 snd_aw2_saa7146_pcm_init_playback(&chip->saa7146,
442 pcm_device->stream_number,
443 runtime->dma_addr, period_size,
446 /* Define Interrupt callback */
447 snd_aw2_saa7146_define_it_playback_callback(pcm_device->stream_number,
448 (snd_aw2_saa7146_it_cb)
449 snd_pcm_period_elapsed,
452 mutex_unlock(&chip->mtx);
457 /* prepare callback for capture */
458 static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream)
460 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
461 struct aw2 *chip = pcm_device->chip;
462 struct snd_pcm_runtime *runtime = substream->runtime;
463 unsigned long period_size, buffer_size;
465 mutex_lock(&chip->mtx);
467 period_size = snd_pcm_lib_period_bytes(substream);
468 buffer_size = snd_pcm_lib_buffer_bytes(substream);
470 snd_aw2_saa7146_pcm_init_capture(&chip->saa7146,
471 pcm_device->stream_number,
472 runtime->dma_addr, period_size,
475 /* Define Interrupt callback */
476 snd_aw2_saa7146_define_it_capture_callback(pcm_device->stream_number,
477 (snd_aw2_saa7146_it_cb)
478 snd_pcm_period_elapsed,
481 mutex_unlock(&chip->mtx);
486 /* playback trigger callback */
487 static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
491 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
492 struct aw2 *chip = pcm_device->chip;
493 spin_lock(&chip->reg_lock);
495 case SNDRV_PCM_TRIGGER_START:
496 snd_aw2_saa7146_pcm_trigger_start_playback(&chip->saa7146,
500 case SNDRV_PCM_TRIGGER_STOP:
501 snd_aw2_saa7146_pcm_trigger_stop_playback(&chip->saa7146,
508 spin_unlock(&chip->reg_lock);
512 /* capture trigger callback */
513 static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
517 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
518 struct aw2 *chip = pcm_device->chip;
519 spin_lock(&chip->reg_lock);
521 case SNDRV_PCM_TRIGGER_START:
522 snd_aw2_saa7146_pcm_trigger_start_capture(&chip->saa7146,
526 case SNDRV_PCM_TRIGGER_STOP:
527 snd_aw2_saa7146_pcm_trigger_stop_capture(&chip->saa7146,
534 spin_unlock(&chip->reg_lock);
538 /* playback pointer callback */
539 static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
542 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
543 struct aw2 *chip = pcm_device->chip;
544 unsigned int current_ptr;
546 /* get the current hardware pointer */
547 struct snd_pcm_runtime *runtime = substream->runtime;
549 snd_aw2_saa7146_get_hw_ptr_playback(&chip->saa7146,
550 pcm_device->stream_number,
552 runtime->buffer_size);
554 return bytes_to_frames(substream->runtime, current_ptr);
557 /* capture pointer callback */
558 static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
561 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
562 struct aw2 *chip = pcm_device->chip;
563 unsigned int current_ptr;
565 /* get the current hardware pointer */
566 struct snd_pcm_runtime *runtime = substream->runtime;
568 snd_aw2_saa7146_get_hw_ptr_capture(&chip->saa7146,
569 pcm_device->stream_number,
571 runtime->buffer_size);
573 return bytes_to_frames(substream->runtime, current_ptr);
576 /* create a pcm device */
577 static int snd_aw2_new_pcm(struct aw2 *chip)
579 struct snd_pcm *pcm_playback_ana;
580 struct snd_pcm *pcm_playback_num;
581 struct snd_pcm *pcm_capture;
582 struct aw2_pcm_device *pcm_device;
585 /* Create new Alsa PCM device */
587 err = snd_pcm_new(chip->card, "Audiowerk2 analog playback", 0, 1, 0,
590 dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
595 pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_ANA];
597 /* Set PCM device name */
598 strcpy(pcm_playback_ana->name, "Analog playback");
599 /* Associate private data to PCM device */
600 pcm_playback_ana->private_data = pcm_device;
601 /* set operators of PCM device */
602 snd_pcm_set_ops(pcm_playback_ana, SNDRV_PCM_STREAM_PLAYBACK,
603 &snd_aw2_playback_ops);
604 /* store PCM device */
605 pcm_device->pcm = pcm_playback_ana;
606 /* give base chip pointer to our internal pcm device
608 pcm_device->chip = chip;
609 /* Give stream number to PCM device */
610 pcm_device->stream_number = NUM_STREAM_PLAYBACK_ANA;
612 /* pre-allocation of buffers */
613 /* Preallocate continuous pages. */
614 snd_pcm_lib_preallocate_pages_for_all(pcm_playback_ana,
616 snd_dma_pci_data(chip->pci),
617 64 * 1024, 64 * 1024);
619 err = snd_pcm_new(chip->card, "Audiowerk2 digital playback", 1, 1, 0,
623 dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
627 pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_DIG];
629 /* Set PCM device name */
630 strcpy(pcm_playback_num->name, "Digital playback");
631 /* Associate private data to PCM device */
632 pcm_playback_num->private_data = pcm_device;
633 /* set operators of PCM device */
634 snd_pcm_set_ops(pcm_playback_num, SNDRV_PCM_STREAM_PLAYBACK,
635 &snd_aw2_playback_ops);
636 /* store PCM device */
637 pcm_device->pcm = pcm_playback_num;
638 /* give base chip pointer to our internal pcm device
640 pcm_device->chip = chip;
641 /* Give stream number to PCM device */
642 pcm_device->stream_number = NUM_STREAM_PLAYBACK_DIG;
644 /* pre-allocation of buffers */
645 /* Preallocate continuous pages. */
646 snd_pcm_lib_preallocate_pages_for_all(pcm_playback_num,
648 snd_dma_pci_data(chip->pci),
649 64 * 1024, 64 * 1024);
651 err = snd_pcm_new(chip->card, "Audiowerk2 capture", 2, 0, 1,
655 dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
660 pcm_device = &chip->device_capture[NUM_STREAM_CAPTURE_ANA];
662 /* Set PCM device name */
663 strcpy(pcm_capture->name, "Capture");
664 /* Associate private data to PCM device */
665 pcm_capture->private_data = pcm_device;
666 /* set operators of PCM device */
667 snd_pcm_set_ops(pcm_capture, SNDRV_PCM_STREAM_CAPTURE,
668 &snd_aw2_capture_ops);
669 /* store PCM device */
670 pcm_device->pcm = pcm_capture;
671 /* give base chip pointer to our internal pcm device
673 pcm_device->chip = chip;
674 /* Give stream number to PCM device */
675 pcm_device->stream_number = NUM_STREAM_CAPTURE_ANA;
677 /* pre-allocation of buffers */
678 /* Preallocate continuous pages. */
679 snd_pcm_lib_preallocate_pages_for_all(pcm_capture,
681 snd_dma_pci_data(chip->pci),
682 64 * 1024, 64 * 1024);
685 err = snd_ctl_add(chip->card, snd_ctl_new1(&aw2_control, chip));
687 dev_err(chip->card->dev, "snd_ctl_add error (0x%X)\n", err);
694 static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
695 struct snd_ctl_elem_info *uinfo)
697 static const char * const texts[2] = {
700 return snd_ctl_enum_info(uinfo, 1, 2, texts);
703 static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
704 struct snd_ctl_elem_value
707 struct aw2 *chip = snd_kcontrol_chip(kcontrol);
708 if (snd_aw2_saa7146_is_using_digital_input(&chip->saa7146))
709 ucontrol->value.enumerated.item[0] = CTL_ROUTE_DIGITAL;
711 ucontrol->value.enumerated.item[0] = CTL_ROUTE_ANALOG;
715 static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
716 struct snd_ctl_elem_value
719 struct aw2 *chip = snd_kcontrol_chip(kcontrol);
722 snd_aw2_saa7146_is_using_digital_input(&chip->saa7146);
724 if (((ucontrol->value.integer.value[0] == CTL_ROUTE_DIGITAL)
726 || ((ucontrol->value.integer.value[0] == CTL_ROUTE_ANALOG)
728 snd_aw2_saa7146_use_digital_input(&chip->saa7146, !is_disgital);