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_prepare_playback(struct snd_pcm_substream *substream);
113 static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream);
114 static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
116 static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
118 static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
120 static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
122 static int snd_aw2_new_pcm(struct aw2 *chip);
124 static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
125 struct snd_ctl_elem_info *uinfo);
126 static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
127 struct snd_ctl_elem_value
129 static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
130 struct snd_ctl_elem_value
133 /*********************************
135 ********************************/
136 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
137 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
138 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
140 module_param_array(index, int, NULL, 0444);
141 MODULE_PARM_DESC(index, "Index value for Audiowerk2 soundcard.");
142 module_param_array(id, charp, NULL, 0444);
143 MODULE_PARM_DESC(id, "ID string for the Audiowerk2 soundcard.");
144 module_param_array(enable, bool, NULL, 0444);
145 MODULE_PARM_DESC(enable, "Enable Audiowerk2 soundcard.");
147 static const struct pci_device_id snd_aw2_ids[] = {
148 {PCI_VENDOR_ID_PHILIPS, PCI_DEVICE_ID_PHILIPS_SAA7146, 0, 0,
153 MODULE_DEVICE_TABLE(pci, snd_aw2_ids);
155 /* pci_driver definition */
156 static struct pci_driver aw2_driver = {
157 .name = KBUILD_MODNAME,
158 .id_table = snd_aw2_ids,
159 .probe = snd_aw2_probe,
160 .remove = snd_aw2_remove,
163 module_pci_driver(aw2_driver);
165 /* operators for playback PCM alsa interface */
166 static const struct snd_pcm_ops snd_aw2_playback_ops = {
167 .open = snd_aw2_pcm_playback_open,
168 .close = snd_aw2_pcm_playback_close,
169 .prepare = snd_aw2_pcm_prepare_playback,
170 .trigger = snd_aw2_pcm_trigger_playback,
171 .pointer = snd_aw2_pcm_pointer_playback,
174 /* operators for capture PCM alsa interface */
175 static const struct snd_pcm_ops snd_aw2_capture_ops = {
176 .open = snd_aw2_pcm_capture_open,
177 .close = snd_aw2_pcm_capture_close,
178 .prepare = snd_aw2_pcm_prepare_capture,
179 .trigger = snd_aw2_pcm_trigger_capture,
180 .pointer = snd_aw2_pcm_pointer_capture,
183 static const struct snd_kcontrol_new aw2_control = {
184 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
185 .name = "PCM Capture Route",
187 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
188 .private_value = 0xffff,
189 .info = snd_aw2_control_switch_capture_info,
190 .get = snd_aw2_control_switch_capture_get,
191 .put = snd_aw2_control_switch_capture_put
194 /*********************************
195 * FUNCTION IMPLEMENTATIONS
196 ********************************/
198 /* component-destructor */
199 static int snd_aw2_dev_free(struct snd_device *device)
201 struct aw2 *chip = device->device_data;
204 snd_aw2_saa7146_free(&chip->saa7146);
206 /* release the irq */
208 free_irq(chip->irq, (void *)chip);
209 /* release the i/o ports & memory */
210 iounmap(chip->iobase_virt);
211 pci_release_regions(chip->pci);
212 /* disable the PCI entry */
213 pci_disable_device(chip->pci);
214 /* release the data */
220 /* chip-specific constructor */
221 static int snd_aw2_create(struct snd_card *card,
222 struct pci_dev *pci, struct aw2 **rchip)
226 static const struct snd_device_ops ops = {
227 .dev_free = snd_aw2_dev_free,
232 /* initialize the PCI entry */
233 err = pci_enable_device(pci);
238 /* check PCI availability (32bit DMA) */
239 if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32))) {
240 dev_err(card->dev, "Impossible to set 32bit mask DMA\n");
241 pci_disable_device(pci);
244 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
246 pci_disable_device(pci);
250 /* initialize the stuff */
255 /* (1) PCI resource allocation */
256 err = pci_request_regions(pci, "Audiowerk2");
258 pci_disable_device(pci);
262 chip->iobase_phys = pci_resource_start(pci, 0);
264 ioremap(chip->iobase_phys,
265 pci_resource_len(pci, 0));
267 if (chip->iobase_virt == NULL) {
268 dev_err(card->dev, "unable to remap memory region");
269 pci_release_regions(pci);
270 pci_disable_device(pci);
275 /* (2) initialization of the chip hardware */
276 snd_aw2_saa7146_setup(&chip->saa7146, chip->iobase_virt);
278 if (request_irq(pci->irq, snd_aw2_saa7146_interrupt,
279 IRQF_SHARED, KBUILD_MODNAME, chip)) {
280 dev_err(card->dev, "Cannot grab irq %d\n", pci->irq);
282 iounmap(chip->iobase_virt);
283 pci_release_regions(chip->pci);
284 pci_disable_device(chip->pci);
288 chip->irq = pci->irq;
289 card->sync_irq = chip->irq;
291 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
293 free_irq(chip->irq, (void *)chip);
294 iounmap(chip->iobase_virt);
295 pci_release_regions(chip->pci);
296 pci_disable_device(chip->pci);
304 "Audiowerk 2 sound card (saa7146 chipset) detected and managed\n");
309 static int snd_aw2_probe(struct pci_dev *pci,
310 const struct pci_device_id *pci_id)
313 struct snd_card *card;
317 /* (1) Continue if device is not enabled, else inc dev */
318 if (dev >= SNDRV_CARDS)
325 /* (2) Create card instance */
326 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
331 /* (3) Create main component */
332 err = snd_aw2_create(card, pci, &chip);
338 /* initialize mutex */
339 mutex_init(&chip->mtx);
341 spin_lock_init(&chip->reg_lock);
342 /* (4) Define driver ID and name string */
343 strcpy(card->driver, "aw2");
344 strcpy(card->shortname, "Audiowerk2");
346 sprintf(card->longname, "%s with SAA7146 irq %i",
347 card->shortname, chip->irq);
349 /* (5) Create other components */
350 snd_aw2_new_pcm(chip);
352 /* (6) Register card instance */
353 err = snd_card_register(card);
359 /* (7) Set PCI driver data */
360 pci_set_drvdata(pci, card);
367 static void snd_aw2_remove(struct pci_dev *pci)
369 snd_card_free(pci_get_drvdata(pci));
373 static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream)
375 struct snd_pcm_runtime *runtime = substream->runtime;
377 dev_dbg(substream->pcm->card->dev, "Playback_open\n");
378 runtime->hw = snd_aw2_playback_hw;
383 static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream)
389 static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream)
391 struct snd_pcm_runtime *runtime = substream->runtime;
393 dev_dbg(substream->pcm->card->dev, "Capture_open\n");
394 runtime->hw = snd_aw2_capture_hw;
399 static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream)
401 /* TODO: something to do ? */
405 /* prepare callback for playback */
406 static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream)
408 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
409 struct aw2 *chip = pcm_device->chip;
410 struct snd_pcm_runtime *runtime = substream->runtime;
411 unsigned long period_size, buffer_size;
413 mutex_lock(&chip->mtx);
415 period_size = snd_pcm_lib_period_bytes(substream);
416 buffer_size = snd_pcm_lib_buffer_bytes(substream);
418 snd_aw2_saa7146_pcm_init_playback(&chip->saa7146,
419 pcm_device->stream_number,
420 runtime->dma_addr, period_size,
423 /* Define Interrupt callback */
424 snd_aw2_saa7146_define_it_playback_callback(pcm_device->stream_number,
425 (snd_aw2_saa7146_it_cb)
426 snd_pcm_period_elapsed,
429 mutex_unlock(&chip->mtx);
434 /* prepare callback for capture */
435 static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream)
437 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
438 struct aw2 *chip = pcm_device->chip;
439 struct snd_pcm_runtime *runtime = substream->runtime;
440 unsigned long period_size, buffer_size;
442 mutex_lock(&chip->mtx);
444 period_size = snd_pcm_lib_period_bytes(substream);
445 buffer_size = snd_pcm_lib_buffer_bytes(substream);
447 snd_aw2_saa7146_pcm_init_capture(&chip->saa7146,
448 pcm_device->stream_number,
449 runtime->dma_addr, period_size,
452 /* Define Interrupt callback */
453 snd_aw2_saa7146_define_it_capture_callback(pcm_device->stream_number,
454 (snd_aw2_saa7146_it_cb)
455 snd_pcm_period_elapsed,
458 mutex_unlock(&chip->mtx);
463 /* playback trigger callback */
464 static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
468 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
469 struct aw2 *chip = pcm_device->chip;
470 spin_lock(&chip->reg_lock);
472 case SNDRV_PCM_TRIGGER_START:
473 snd_aw2_saa7146_pcm_trigger_start_playback(&chip->saa7146,
477 case SNDRV_PCM_TRIGGER_STOP:
478 snd_aw2_saa7146_pcm_trigger_stop_playback(&chip->saa7146,
485 spin_unlock(&chip->reg_lock);
489 /* capture trigger callback */
490 static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
494 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
495 struct aw2 *chip = pcm_device->chip;
496 spin_lock(&chip->reg_lock);
498 case SNDRV_PCM_TRIGGER_START:
499 snd_aw2_saa7146_pcm_trigger_start_capture(&chip->saa7146,
503 case SNDRV_PCM_TRIGGER_STOP:
504 snd_aw2_saa7146_pcm_trigger_stop_capture(&chip->saa7146,
511 spin_unlock(&chip->reg_lock);
515 /* playback pointer callback */
516 static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
519 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
520 struct aw2 *chip = pcm_device->chip;
521 unsigned int current_ptr;
523 /* get the current hardware pointer */
524 struct snd_pcm_runtime *runtime = substream->runtime;
526 snd_aw2_saa7146_get_hw_ptr_playback(&chip->saa7146,
527 pcm_device->stream_number,
529 runtime->buffer_size);
531 return bytes_to_frames(substream->runtime, current_ptr);
534 /* capture pointer callback */
535 static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
538 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
539 struct aw2 *chip = pcm_device->chip;
540 unsigned int current_ptr;
542 /* get the current hardware pointer */
543 struct snd_pcm_runtime *runtime = substream->runtime;
545 snd_aw2_saa7146_get_hw_ptr_capture(&chip->saa7146,
546 pcm_device->stream_number,
548 runtime->buffer_size);
550 return bytes_to_frames(substream->runtime, current_ptr);
553 /* create a pcm device */
554 static int snd_aw2_new_pcm(struct aw2 *chip)
556 struct snd_pcm *pcm_playback_ana;
557 struct snd_pcm *pcm_playback_num;
558 struct snd_pcm *pcm_capture;
559 struct aw2_pcm_device *pcm_device;
562 /* Create new Alsa PCM device */
564 err = snd_pcm_new(chip->card, "Audiowerk2 analog playback", 0, 1, 0,
567 dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
572 pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_ANA];
574 /* Set PCM device name */
575 strcpy(pcm_playback_ana->name, "Analog playback");
576 /* Associate private data to PCM device */
577 pcm_playback_ana->private_data = pcm_device;
578 /* set operators of PCM device */
579 snd_pcm_set_ops(pcm_playback_ana, SNDRV_PCM_STREAM_PLAYBACK,
580 &snd_aw2_playback_ops);
581 /* store PCM device */
582 pcm_device->pcm = pcm_playback_ana;
583 /* give base chip pointer to our internal pcm device
585 pcm_device->chip = chip;
586 /* Give stream number to PCM device */
587 pcm_device->stream_number = NUM_STREAM_PLAYBACK_ANA;
589 /* pre-allocation of buffers */
590 /* Preallocate continuous pages. */
591 snd_pcm_set_managed_buffer_all(pcm_playback_ana,
594 64 * 1024, 64 * 1024);
596 err = snd_pcm_new(chip->card, "Audiowerk2 digital playback", 1, 1, 0,
600 dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
604 pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_DIG];
606 /* Set PCM device name */
607 strcpy(pcm_playback_num->name, "Digital playback");
608 /* Associate private data to PCM device */
609 pcm_playback_num->private_data = pcm_device;
610 /* set operators of PCM device */
611 snd_pcm_set_ops(pcm_playback_num, SNDRV_PCM_STREAM_PLAYBACK,
612 &snd_aw2_playback_ops);
613 /* store PCM device */
614 pcm_device->pcm = pcm_playback_num;
615 /* give base chip pointer to our internal pcm device
617 pcm_device->chip = chip;
618 /* Give stream number to PCM device */
619 pcm_device->stream_number = NUM_STREAM_PLAYBACK_DIG;
621 /* pre-allocation of buffers */
622 /* Preallocate continuous pages. */
623 snd_pcm_set_managed_buffer_all(pcm_playback_num,
626 64 * 1024, 64 * 1024);
628 err = snd_pcm_new(chip->card, "Audiowerk2 capture", 2, 0, 1,
632 dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
637 pcm_device = &chip->device_capture[NUM_STREAM_CAPTURE_ANA];
639 /* Set PCM device name */
640 strcpy(pcm_capture->name, "Capture");
641 /* Associate private data to PCM device */
642 pcm_capture->private_data = pcm_device;
643 /* set operators of PCM device */
644 snd_pcm_set_ops(pcm_capture, SNDRV_PCM_STREAM_CAPTURE,
645 &snd_aw2_capture_ops);
646 /* store PCM device */
647 pcm_device->pcm = pcm_capture;
648 /* give base chip pointer to our internal pcm device
650 pcm_device->chip = chip;
651 /* Give stream number to PCM device */
652 pcm_device->stream_number = NUM_STREAM_CAPTURE_ANA;
654 /* pre-allocation of buffers */
655 /* Preallocate continuous pages. */
656 snd_pcm_set_managed_buffer_all(pcm_capture,
659 64 * 1024, 64 * 1024);
662 err = snd_ctl_add(chip->card, snd_ctl_new1(&aw2_control, chip));
664 dev_err(chip->card->dev, "snd_ctl_add error (0x%X)\n", err);
671 static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
672 struct snd_ctl_elem_info *uinfo)
674 static const char * const texts[2] = {
677 return snd_ctl_enum_info(uinfo, 1, 2, texts);
680 static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
681 struct snd_ctl_elem_value
684 struct aw2 *chip = snd_kcontrol_chip(kcontrol);
685 if (snd_aw2_saa7146_is_using_digital_input(&chip->saa7146))
686 ucontrol->value.enumerated.item[0] = CTL_ROUTE_DIGITAL;
688 ucontrol->value.enumerated.item[0] = CTL_ROUTE_ANALOG;
692 static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
693 struct snd_ctl_elem_value
696 struct aw2 *chip = snd_kcontrol_chip(kcontrol);
699 snd_aw2_saa7146_is_using_digital_input(&chip->saa7146);
701 if (((ucontrol->value.integer.value[0] == CTL_ROUTE_DIGITAL)
703 || ((ucontrol->value.integer.value[0] == CTL_ROUTE_ANALOG)
705 snd_aw2_saa7146_use_digital_input(&chip->saa7146, !is_disgital);