1 /*****************************************************************************
3 * Copyright (C) 2008 Cedric Bregardis <cedric.bregardis@free.fr> and
4 * Jean-Christian Hassler <jhassler@free.fr>
6 * This file is part of the Audiowerk2 ALSA driver
8 * The Audiowerk2 ALSA driver is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2.
12 * The Audiowerk2 ALSA driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with the Audiowerk2 ALSA driver; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 *****************************************************************************/
23 #include <linux/init.h>
24 #include <linux/pci.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/delay.h>
29 #include <sound/core.h>
30 #include <sound/initval.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/control.h>
36 #include "aw2-saa7146.h"
38 MODULE_LICENSE("GPL");
39 MODULE_AUTHOR("Cedric Bregardis <cedric.bregardis@free.fr>, "
40 "Jean-Christian Hassler <jhassler@free.fr>");
41 MODULE_DESCRIPTION("Emagic Audiowerk 2 sound driver");
42 MODULE_LICENSE("GPL");
44 /*********************************
46 ********************************/
47 #define PCI_VENDOR_ID_SAA7146 0x1131
48 #define PCI_DEVICE_ID_SAA7146 0x7146
50 #define CTL_ROUTE_ANALOG 0
51 #define CTL_ROUTE_DIGITAL 1
53 /*********************************
55 ********************************/
56 /* hardware definition */
57 static struct snd_pcm_hardware snd_aw2_playback_hw = {
58 .info = (SNDRV_PCM_INFO_MMAP |
59 SNDRV_PCM_INFO_INTERLEAVED |
60 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
61 .formats = SNDRV_PCM_FMTBIT_S16_LE,
62 .rates = SNDRV_PCM_RATE_44100,
67 .buffer_bytes_max = 32768,
68 .period_bytes_min = 4096,
69 .period_bytes_max = 32768,
74 static struct snd_pcm_hardware snd_aw2_capture_hw = {
75 .info = (SNDRV_PCM_INFO_MMAP |
76 SNDRV_PCM_INFO_INTERLEAVED |
77 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
78 .formats = SNDRV_PCM_FMTBIT_S16_LE,
79 .rates = SNDRV_PCM_RATE_44100,
84 .buffer_bytes_max = 32768,
85 .period_bytes_min = 4096,
86 .period_bytes_max = 32768,
91 struct aw2_pcm_device {
93 unsigned int stream_number;
98 struct snd_aw2_saa7146 saa7146;
105 unsigned long iobase_phys;
106 void __iomem *iobase_virt;
108 struct snd_card *card;
110 struct aw2_pcm_device device_playback[NB_STREAM_PLAYBACK];
111 struct aw2_pcm_device device_capture[NB_STREAM_CAPTURE];
114 /*********************************
115 * FUNCTION DECLARATIONS
116 ********************************/
117 static int __init alsa_card_aw2_init(void);
118 static void __exit alsa_card_aw2_exit(void);
119 static int snd_aw2_dev_free(struct snd_device *device);
120 static int __devinit snd_aw2_create(struct snd_card *card,
121 struct pci_dev *pci, struct aw2 **rchip);
122 static int __devinit snd_aw2_probe(struct pci_dev *pci,
123 const struct pci_device_id *pci_id);
124 static void __devexit snd_aw2_remove(struct pci_dev *pci);
125 static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream);
126 static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream);
127 static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream);
128 static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream);
129 static int snd_aw2_pcm_hw_params(struct snd_pcm_substream *substream,
130 struct snd_pcm_hw_params *hw_params);
131 static int snd_aw2_pcm_hw_free(struct snd_pcm_substream *substream);
132 static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream);
133 static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream);
134 static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
136 static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
138 static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
140 static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
142 static int __devinit snd_aw2_new_pcm(struct aw2 *chip);
144 static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
145 struct snd_ctl_elem_info *uinfo);
146 static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
147 struct snd_ctl_elem_value
149 static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
150 struct snd_ctl_elem_value
153 /*********************************
155 ********************************/
156 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
157 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
158 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
160 static struct pci_device_id snd_aw2_ids[] = {
161 {PCI_VENDOR_ID_SAA7146, PCI_DEVICE_ID_SAA7146, PCI_ANY_ID, PCI_ANY_ID,
166 MODULE_DEVICE_TABLE(pci, snd_aw2_ids);
168 /* pci_driver definition */
169 static struct pci_driver driver = {
170 .name = "Emagic Audiowerk 2",
171 .id_table = snd_aw2_ids,
172 .probe = snd_aw2_probe,
173 .remove = __devexit_p(snd_aw2_remove),
176 /* operators for playback PCM alsa interface */
177 static struct snd_pcm_ops snd_aw2_playback_ops = {
178 .open = snd_aw2_pcm_playback_open,
179 .close = snd_aw2_pcm_playback_close,
180 .ioctl = snd_pcm_lib_ioctl,
181 .hw_params = snd_aw2_pcm_hw_params,
182 .hw_free = snd_aw2_pcm_hw_free,
183 .prepare = snd_aw2_pcm_prepare_playback,
184 .trigger = snd_aw2_pcm_trigger_playback,
185 .pointer = snd_aw2_pcm_pointer_playback,
188 /* operators for capture PCM alsa interface */
189 static struct snd_pcm_ops snd_aw2_capture_ops = {
190 .open = snd_aw2_pcm_capture_open,
191 .close = snd_aw2_pcm_capture_close,
192 .ioctl = snd_pcm_lib_ioctl,
193 .hw_params = snd_aw2_pcm_hw_params,
194 .hw_free = snd_aw2_pcm_hw_free,
195 .prepare = snd_aw2_pcm_prepare_capture,
196 .trigger = snd_aw2_pcm_trigger_capture,
197 .pointer = snd_aw2_pcm_pointer_capture,
200 static struct snd_kcontrol_new aw2_control __devinitdata = {
201 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
202 .name = "PCM Capture Route",
204 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
205 .private_value = 0xffff,
206 .info = snd_aw2_control_switch_capture_info,
207 .get = snd_aw2_control_switch_capture_get,
208 .put = snd_aw2_control_switch_capture_put
211 /*********************************
212 * FUNCTION IMPLEMENTATIONS
213 ********************************/
215 /* initialization of the module */
216 static int __init alsa_card_aw2_init(void)
218 snd_printdd(KERN_DEBUG "aw2: Load aw2 module\n");
219 return pci_register_driver(&driver);
222 /* clean up the module */
223 static void __exit alsa_card_aw2_exit(void)
225 snd_printdd(KERN_DEBUG "aw2: Unload aw2 module\n");
226 pci_unregister_driver(&driver);
229 module_init(alsa_card_aw2_init);
230 module_exit(alsa_card_aw2_exit);
232 /* component-destructor */
233 static int snd_aw2_dev_free(struct snd_device *device)
235 struct aw2 *chip = device->device_data;
238 snd_aw2_saa7146_free(&chip->saa7146);
240 /* release the irq */
242 free_irq(chip->irq, (void *)chip);
243 /* release the i/o ports & memory */
244 if (chip->iobase_virt)
245 iounmap(chip->iobase_virt);
247 pci_release_regions(chip->pci);
248 /* disable the PCI entry */
249 pci_disable_device(chip->pci);
250 /* release the data */
256 /* chip-specific constructor */
257 static int __devinit snd_aw2_create(struct snd_card *card,
258 struct pci_dev *pci, struct aw2 **rchip)
262 static struct snd_device_ops ops = {
263 .dev_free = snd_aw2_dev_free,
268 /* initialize the PCI entry */
269 err = pci_enable_device(pci);
274 /* check PCI availability (32bit DMA) */
275 if ((pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0) ||
276 (pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0)) {
277 printk(KERN_ERR "aw2: Impossible to set 32bit mask DMA\n");
278 pci_disable_device(pci);
281 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
283 pci_disable_device(pci);
287 /* initialize the stuff */
292 /* (1) PCI resource allocation */
293 err = pci_request_regions(pci, "Audiowerk2");
295 pci_disable_device(pci);
299 chip->iobase_phys = pci_resource_start(pci, 0);
301 ioremap_nocache(chip->iobase_phys,
302 pci_resource_len(pci, 0));
304 if (chip->iobase_virt == NULL) {
305 printk(KERN_ERR "aw2: unable to remap memory region");
306 pci_release_regions(pci);
307 pci_disable_device(pci);
313 if (request_irq(pci->irq, snd_aw2_saa7146_interrupt,
314 IRQF_SHARED, "Audiowerk2", chip)) {
315 printk(KERN_ERR "aw2: Cannot grab irq %d\n", pci->irq);
317 iounmap(chip->iobase_virt);
318 pci_release_regions(chip->pci);
319 pci_disable_device(chip->pci);
323 chip->irq = pci->irq;
325 /* (2) initialization of the chip hardware */
326 snd_aw2_saa7146_setup(&chip->saa7146, chip->iobase_virt);
327 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
329 free_irq(chip->irq, (void *)chip);
330 iounmap(chip->iobase_virt);
331 pci_release_regions(chip->pci);
332 pci_disable_device(chip->pci);
337 snd_card_set_dev(card, &pci->dev);
341 "Audiowerk 2 sound card (saa7146 chipset) detected and "
347 static int __devinit snd_aw2_probe(struct pci_dev *pci,
348 const struct pci_device_id *pci_id)
351 struct snd_card *card;
355 /* (1) Continue if device is not enabled, else inc dev */
356 if (dev >= SNDRV_CARDS)
363 /* (2) Create card instance */
364 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
368 /* (3) Create main component */
369 err = snd_aw2_create(card, pci, &chip);
375 /* initialize mutex */
376 mutex_init(&chip->mtx);
378 spin_lock_init(&chip->reg_lock);
379 /* (4) Define driver ID and name string */
380 strcpy(card->driver, "aw2");
381 strcpy(card->shortname, "Audiowerk2");
383 sprintf(card->longname, "%s with SAA7146 irq %i",
384 card->shortname, chip->irq);
386 /* (5) Create other components */
387 snd_aw2_new_pcm(chip);
389 /* (6) Register card instance */
390 err = snd_card_register(card);
396 /* (7) Set PCI driver data */
397 pci_set_drvdata(pci, card);
404 static void __devexit snd_aw2_remove(struct pci_dev *pci)
406 snd_card_free(pci_get_drvdata(pci));
407 pci_set_drvdata(pci, NULL);
411 static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream)
413 struct snd_pcm_runtime *runtime = substream->runtime;
415 snd_printdd(KERN_DEBUG "aw2: Playback_open \n");
416 runtime->hw = snd_aw2_playback_hw;
421 static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream)
427 static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream)
429 struct snd_pcm_runtime *runtime = substream->runtime;
431 snd_printdd(KERN_DEBUG "aw2: Capture_open \n");
432 runtime->hw = snd_aw2_capture_hw;
437 static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream)
439 /* TODO: something to do ? */
443 /* hw_params callback */
444 static int snd_aw2_pcm_hw_params(struct snd_pcm_substream *substream,
445 struct snd_pcm_hw_params *hw_params)
447 return snd_pcm_lib_malloc_pages(substream,
448 params_buffer_bytes(hw_params));
451 /* hw_free callback */
452 static int snd_aw2_pcm_hw_free(struct snd_pcm_substream *substream)
454 return snd_pcm_lib_free_pages(substream);
457 /* prepare callback for playback */
458 static int snd_aw2_pcm_prepare_playback(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_playback(&chip->saa7146,
471 pcm_device->stream_number,
472 runtime->dma_addr, period_size,
475 /* Define Interrupt callback */
476 snd_aw2_saa7146_define_it_playback_callback(pcm_device->stream_number,
477 (snd_aw2_saa7146_it_cb)
478 snd_pcm_period_elapsed,
481 mutex_unlock(&chip->mtx);
486 /* prepare callback for capture */
487 static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream)
489 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
490 struct aw2 *chip = pcm_device->chip;
491 struct snd_pcm_runtime *runtime = substream->runtime;
492 unsigned long period_size, buffer_size;
494 mutex_lock(&chip->mtx);
496 period_size = snd_pcm_lib_period_bytes(substream);
497 buffer_size = snd_pcm_lib_buffer_bytes(substream);
499 snd_aw2_saa7146_pcm_init_capture(&chip->saa7146,
500 pcm_device->stream_number,
501 runtime->dma_addr, period_size,
504 /* Define Interrupt callback */
505 snd_aw2_saa7146_define_it_capture_callback(pcm_device->stream_number,
506 (snd_aw2_saa7146_it_cb)
507 snd_pcm_period_elapsed,
510 mutex_unlock(&chip->mtx);
515 /* playback trigger callback */
516 static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
520 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
521 struct aw2 *chip = pcm_device->chip;
522 spin_lock(&chip->reg_lock);
524 case SNDRV_PCM_TRIGGER_START:
525 snd_aw2_saa7146_pcm_trigger_start_playback(&chip->saa7146,
529 case SNDRV_PCM_TRIGGER_STOP:
530 snd_aw2_saa7146_pcm_trigger_stop_playback(&chip->saa7146,
537 spin_unlock(&chip->reg_lock);
541 /* capture trigger callback */
542 static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
546 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
547 struct aw2 *chip = pcm_device->chip;
548 spin_lock(&chip->reg_lock);
550 case SNDRV_PCM_TRIGGER_START:
551 snd_aw2_saa7146_pcm_trigger_start_capture(&chip->saa7146,
555 case SNDRV_PCM_TRIGGER_STOP:
556 snd_aw2_saa7146_pcm_trigger_stop_capture(&chip->saa7146,
563 spin_unlock(&chip->reg_lock);
567 /* playback pointer callback */
568 static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
571 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
572 struct aw2 *chip = pcm_device->chip;
573 unsigned int current_ptr;
575 /* get the current hardware pointer */
576 struct snd_pcm_runtime *runtime = substream->runtime;
578 snd_aw2_saa7146_get_hw_ptr_playback(&chip->saa7146,
579 pcm_device->stream_number,
581 runtime->buffer_size);
583 return bytes_to_frames(substream->runtime, current_ptr);
586 /* capture pointer callback */
587 static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
590 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
591 struct aw2 *chip = pcm_device->chip;
592 unsigned int current_ptr;
594 /* get the current hardware pointer */
595 struct snd_pcm_runtime *runtime = substream->runtime;
597 snd_aw2_saa7146_get_hw_ptr_capture(&chip->saa7146,
598 pcm_device->stream_number,
600 runtime->buffer_size);
602 return bytes_to_frames(substream->runtime, current_ptr);
605 /* create a pcm device */
606 static int __devinit snd_aw2_new_pcm(struct aw2 *chip)
608 struct snd_pcm *pcm_playback_ana;
609 struct snd_pcm *pcm_playback_num;
610 struct snd_pcm *pcm_capture;
611 struct aw2_pcm_device *pcm_device;
614 /* Create new Alsa PCM device */
616 err = snd_pcm_new(chip->card, "Audiowerk2 analog playback", 0, 1, 0,
619 printk(KERN_ERR "aw2: snd_pcm_new error (0x%X)\n", err);
624 pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_ANA];
626 /* Set PCM device name */
627 strcpy(pcm_playback_ana->name, "Analog playback");
628 /* Associate private data to PCM device */
629 pcm_playback_ana->private_data = pcm_device;
630 /* set operators of PCM device */
631 snd_pcm_set_ops(pcm_playback_ana, SNDRV_PCM_STREAM_PLAYBACK,
632 &snd_aw2_playback_ops);
633 /* store PCM device */
634 pcm_device->pcm = pcm_playback_ana;
635 /* give base chip pointer to our internal pcm device
637 pcm_device->chip = chip;
638 /* Give stream number to PCM device */
639 pcm_device->stream_number = NUM_STREAM_PLAYBACK_ANA;
641 /* pre-allocation of buffers */
642 /* Preallocate continuous pages. */
643 err = snd_pcm_lib_preallocate_pages_for_all(pcm_playback_ana,
647 64 * 1024, 64 * 1024);
649 printk(KERN_ERR "aw2: snd_pcm_lib_preallocate_pages_for_all "
650 "error (0x%X)\n", err);
652 err = snd_pcm_new(chip->card, "Audiowerk2 digital playback", 1, 1, 0,
656 printk(KERN_ERR "aw2: snd_pcm_new error (0x%X)\n", err);
660 pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_DIG];
662 /* Set PCM device name */
663 strcpy(pcm_playback_num->name, "Digital playback");
664 /* Associate private data to PCM device */
665 pcm_playback_num->private_data = pcm_device;
666 /* set operators of PCM device */
667 snd_pcm_set_ops(pcm_playback_num, SNDRV_PCM_STREAM_PLAYBACK,
668 &snd_aw2_playback_ops);
669 /* store PCM device */
670 pcm_device->pcm = pcm_playback_num;
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_PLAYBACK_DIG;
677 /* pre-allocation of buffers */
678 /* Preallocate continuous pages. */
679 err = snd_pcm_lib_preallocate_pages_for_all(pcm_playback_num,
683 64 * 1024, 64 * 1024);
686 "aw2: snd_pcm_lib_preallocate_pages_for_all error "
691 err = snd_pcm_new(chip->card, "Audiowerk2 capture", 2, 0, 1,
695 printk(KERN_ERR "aw2: snd_pcm_new error (0x%X)\n", err);
700 pcm_device = &chip->device_capture[NUM_STREAM_CAPTURE_ANA];
702 /* Set PCM device name */
703 strcpy(pcm_capture->name, "Capture");
704 /* Associate private data to PCM device */
705 pcm_capture->private_data = pcm_device;
706 /* set operators of PCM device */
707 snd_pcm_set_ops(pcm_capture, SNDRV_PCM_STREAM_CAPTURE,
708 &snd_aw2_capture_ops);
709 /* store PCM device */
710 pcm_device->pcm = pcm_capture;
711 /* give base chip pointer to our internal pcm device
713 pcm_device->chip = chip;
714 /* Give stream number to PCM device */
715 pcm_device->stream_number = NUM_STREAM_CAPTURE_ANA;
717 /* pre-allocation of buffers */
718 /* Preallocate continuous pages. */
719 err = snd_pcm_lib_preallocate_pages_for_all(pcm_capture,
723 64 * 1024, 64 * 1024);
726 "aw2: snd_pcm_lib_preallocate_pages_for_all error "
731 err = snd_ctl_add(chip->card, snd_ctl_new1(&aw2_control, chip));
733 printk(KERN_ERR "aw2: snd_ctl_add error (0x%X)\n", err);
740 static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
741 struct snd_ctl_elem_info *uinfo)
743 static char *texts[2] = {
746 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
748 uinfo->value.enumerated.items = 2;
749 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) {
750 uinfo->value.enumerated.item =
751 uinfo->value.enumerated.items - 1;
753 strcpy(uinfo->value.enumerated.name,
754 texts[uinfo->value.enumerated.item]);
758 static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
759 struct snd_ctl_elem_value
762 struct aw2 *chip = snd_kcontrol_chip(kcontrol);
763 if (snd_aw2_saa7146_is_using_digital_input(&chip->saa7146))
764 ucontrol->value.enumerated.item[0] = CTL_ROUTE_DIGITAL;
766 ucontrol->value.enumerated.item[0] = CTL_ROUTE_ANALOG;
770 static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
771 struct snd_ctl_elem_value
774 struct aw2 *chip = snd_kcontrol_chip(kcontrol);
777 snd_aw2_saa7146_is_using_digital_input(&chip->saa7146);
779 if (((ucontrol->value.integer.value[0] == CTL_ROUTE_DIGITAL)
781 || ((ucontrol->value.integer.value[0] == CTL_ROUTE_ANALOG)
783 snd_aw2_saa7146_use_digital_input(&chip->saa7146, !is_disgital);