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 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(&pci->dev, DMA_BIT_MASK(32)) < 0) ||
240 (dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(32)) < 0)) {
241 dev_err(card->dev, "Impossible to set 32bit mask DMA\n");
242 pci_disable_device(pci);
245 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
247 pci_disable_device(pci);
251 /* initialize the stuff */
256 /* (1) PCI resource allocation */
257 err = pci_request_regions(pci, "Audiowerk2");
259 pci_disable_device(pci);
263 chip->iobase_phys = pci_resource_start(pci, 0);
265 ioremap_nocache(chip->iobase_phys,
266 pci_resource_len(pci, 0));
268 if (chip->iobase_virt == NULL) {
269 dev_err(card->dev, "unable to remap memory region");
270 pci_release_regions(pci);
271 pci_disable_device(pci);
276 /* (2) initialization of the chip hardware */
277 snd_aw2_saa7146_setup(&chip->saa7146, chip->iobase_virt);
279 if (request_irq(pci->irq, snd_aw2_saa7146_interrupt,
280 IRQF_SHARED, KBUILD_MODNAME, chip)) {
281 dev_err(card->dev, "Cannot grab irq %d\n", pci->irq);
283 iounmap(chip->iobase_virt);
284 pci_release_regions(chip->pci);
285 pci_disable_device(chip->pci);
289 chip->irq = pci->irq;
290 card->sync_irq = chip->irq;
292 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
294 free_irq(chip->irq, (void *)chip);
295 iounmap(chip->iobase_virt);
296 pci_release_regions(chip->pci);
297 pci_disable_device(chip->pci);
305 "Audiowerk 2 sound card (saa7146 chipset) detected and managed\n");
310 static int snd_aw2_probe(struct pci_dev *pci,
311 const struct pci_device_id *pci_id)
314 struct snd_card *card;
318 /* (1) Continue if device is not enabled, else inc dev */
319 if (dev >= SNDRV_CARDS)
326 /* (2) Create card instance */
327 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
332 /* (3) Create main component */
333 err = snd_aw2_create(card, pci, &chip);
339 /* initialize mutex */
340 mutex_init(&chip->mtx);
342 spin_lock_init(&chip->reg_lock);
343 /* (4) Define driver ID and name string */
344 strcpy(card->driver, "aw2");
345 strcpy(card->shortname, "Audiowerk2");
347 sprintf(card->longname, "%s with SAA7146 irq %i",
348 card->shortname, chip->irq);
350 /* (5) Create other components */
351 snd_aw2_new_pcm(chip);
353 /* (6) Register card instance */
354 err = snd_card_register(card);
360 /* (7) Set PCI driver data */
361 pci_set_drvdata(pci, card);
368 static void snd_aw2_remove(struct pci_dev *pci)
370 snd_card_free(pci_get_drvdata(pci));
374 static int snd_aw2_pcm_playback_open(struct snd_pcm_substream *substream)
376 struct snd_pcm_runtime *runtime = substream->runtime;
378 dev_dbg(substream->pcm->card->dev, "Playback_open\n");
379 runtime->hw = snd_aw2_playback_hw;
384 static int snd_aw2_pcm_playback_close(struct snd_pcm_substream *substream)
390 static int snd_aw2_pcm_capture_open(struct snd_pcm_substream *substream)
392 struct snd_pcm_runtime *runtime = substream->runtime;
394 dev_dbg(substream->pcm->card->dev, "Capture_open\n");
395 runtime->hw = snd_aw2_capture_hw;
400 static int snd_aw2_pcm_capture_close(struct snd_pcm_substream *substream)
402 /* TODO: something to do ? */
406 /* prepare callback for playback */
407 static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream *substream)
409 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
410 struct aw2 *chip = pcm_device->chip;
411 struct snd_pcm_runtime *runtime = substream->runtime;
412 unsigned long period_size, buffer_size;
414 mutex_lock(&chip->mtx);
416 period_size = snd_pcm_lib_period_bytes(substream);
417 buffer_size = snd_pcm_lib_buffer_bytes(substream);
419 snd_aw2_saa7146_pcm_init_playback(&chip->saa7146,
420 pcm_device->stream_number,
421 runtime->dma_addr, period_size,
424 /* Define Interrupt callback */
425 snd_aw2_saa7146_define_it_playback_callback(pcm_device->stream_number,
426 (snd_aw2_saa7146_it_cb)
427 snd_pcm_period_elapsed,
430 mutex_unlock(&chip->mtx);
435 /* prepare callback for capture */
436 static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream *substream)
438 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
439 struct aw2 *chip = pcm_device->chip;
440 struct snd_pcm_runtime *runtime = substream->runtime;
441 unsigned long period_size, buffer_size;
443 mutex_lock(&chip->mtx);
445 period_size = snd_pcm_lib_period_bytes(substream);
446 buffer_size = snd_pcm_lib_buffer_bytes(substream);
448 snd_aw2_saa7146_pcm_init_capture(&chip->saa7146,
449 pcm_device->stream_number,
450 runtime->dma_addr, period_size,
453 /* Define Interrupt callback */
454 snd_aw2_saa7146_define_it_capture_callback(pcm_device->stream_number,
455 (snd_aw2_saa7146_it_cb)
456 snd_pcm_period_elapsed,
459 mutex_unlock(&chip->mtx);
464 /* playback trigger callback */
465 static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream *substream,
469 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
470 struct aw2 *chip = pcm_device->chip;
471 spin_lock(&chip->reg_lock);
473 case SNDRV_PCM_TRIGGER_START:
474 snd_aw2_saa7146_pcm_trigger_start_playback(&chip->saa7146,
478 case SNDRV_PCM_TRIGGER_STOP:
479 snd_aw2_saa7146_pcm_trigger_stop_playback(&chip->saa7146,
486 spin_unlock(&chip->reg_lock);
490 /* capture trigger callback */
491 static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream *substream,
495 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
496 struct aw2 *chip = pcm_device->chip;
497 spin_lock(&chip->reg_lock);
499 case SNDRV_PCM_TRIGGER_START:
500 snd_aw2_saa7146_pcm_trigger_start_capture(&chip->saa7146,
504 case SNDRV_PCM_TRIGGER_STOP:
505 snd_aw2_saa7146_pcm_trigger_stop_capture(&chip->saa7146,
512 spin_unlock(&chip->reg_lock);
516 /* playback pointer callback */
517 static snd_pcm_uframes_t snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
520 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
521 struct aw2 *chip = pcm_device->chip;
522 unsigned int current_ptr;
524 /* get the current hardware pointer */
525 struct snd_pcm_runtime *runtime = substream->runtime;
527 snd_aw2_saa7146_get_hw_ptr_playback(&chip->saa7146,
528 pcm_device->stream_number,
530 runtime->buffer_size);
532 return bytes_to_frames(substream->runtime, current_ptr);
535 /* capture pointer callback */
536 static snd_pcm_uframes_t snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
539 struct aw2_pcm_device *pcm_device = snd_pcm_substream_chip(substream);
540 struct aw2 *chip = pcm_device->chip;
541 unsigned int current_ptr;
543 /* get the current hardware pointer */
544 struct snd_pcm_runtime *runtime = substream->runtime;
546 snd_aw2_saa7146_get_hw_ptr_capture(&chip->saa7146,
547 pcm_device->stream_number,
549 runtime->buffer_size);
551 return bytes_to_frames(substream->runtime, current_ptr);
554 /* create a pcm device */
555 static int snd_aw2_new_pcm(struct aw2 *chip)
557 struct snd_pcm *pcm_playback_ana;
558 struct snd_pcm *pcm_playback_num;
559 struct snd_pcm *pcm_capture;
560 struct aw2_pcm_device *pcm_device;
563 /* Create new Alsa PCM device */
565 err = snd_pcm_new(chip->card, "Audiowerk2 analog playback", 0, 1, 0,
568 dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
573 pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_ANA];
575 /* Set PCM device name */
576 strcpy(pcm_playback_ana->name, "Analog playback");
577 /* Associate private data to PCM device */
578 pcm_playback_ana->private_data = pcm_device;
579 /* set operators of PCM device */
580 snd_pcm_set_ops(pcm_playback_ana, SNDRV_PCM_STREAM_PLAYBACK,
581 &snd_aw2_playback_ops);
582 /* store PCM device */
583 pcm_device->pcm = pcm_playback_ana;
584 /* give base chip pointer to our internal pcm device
586 pcm_device->chip = chip;
587 /* Give stream number to PCM device */
588 pcm_device->stream_number = NUM_STREAM_PLAYBACK_ANA;
590 /* pre-allocation of buffers */
591 /* Preallocate continuous pages. */
592 snd_pcm_set_managed_buffer_all(pcm_playback_ana,
595 64 * 1024, 64 * 1024);
597 err = snd_pcm_new(chip->card, "Audiowerk2 digital playback", 1, 1, 0,
601 dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
605 pcm_device = &chip->device_playback[NUM_STREAM_PLAYBACK_DIG];
607 /* Set PCM device name */
608 strcpy(pcm_playback_num->name, "Digital playback");
609 /* Associate private data to PCM device */
610 pcm_playback_num->private_data = pcm_device;
611 /* set operators of PCM device */
612 snd_pcm_set_ops(pcm_playback_num, SNDRV_PCM_STREAM_PLAYBACK,
613 &snd_aw2_playback_ops);
614 /* store PCM device */
615 pcm_device->pcm = pcm_playback_num;
616 /* give base chip pointer to our internal pcm device
618 pcm_device->chip = chip;
619 /* Give stream number to PCM device */
620 pcm_device->stream_number = NUM_STREAM_PLAYBACK_DIG;
622 /* pre-allocation of buffers */
623 /* Preallocate continuous pages. */
624 snd_pcm_set_managed_buffer_all(pcm_playback_num,
627 64 * 1024, 64 * 1024);
629 err = snd_pcm_new(chip->card, "Audiowerk2 capture", 2, 0, 1,
633 dev_err(chip->card->dev, "snd_pcm_new error (0x%X)\n", err);
638 pcm_device = &chip->device_capture[NUM_STREAM_CAPTURE_ANA];
640 /* Set PCM device name */
641 strcpy(pcm_capture->name, "Capture");
642 /* Associate private data to PCM device */
643 pcm_capture->private_data = pcm_device;
644 /* set operators of PCM device */
645 snd_pcm_set_ops(pcm_capture, SNDRV_PCM_STREAM_CAPTURE,
646 &snd_aw2_capture_ops);
647 /* store PCM device */
648 pcm_device->pcm = pcm_capture;
649 /* give base chip pointer to our internal pcm device
651 pcm_device->chip = chip;
652 /* Give stream number to PCM device */
653 pcm_device->stream_number = NUM_STREAM_CAPTURE_ANA;
655 /* pre-allocation of buffers */
656 /* Preallocate continuous pages. */
657 snd_pcm_set_managed_buffer_all(pcm_capture,
660 64 * 1024, 64 * 1024);
663 err = snd_ctl_add(chip->card, snd_ctl_new1(&aw2_control, chip));
665 dev_err(chip->card->dev, "snd_ctl_add error (0x%X)\n", err);
672 static int snd_aw2_control_switch_capture_info(struct snd_kcontrol *kcontrol,
673 struct snd_ctl_elem_info *uinfo)
675 static const char * const texts[2] = {
678 return snd_ctl_enum_info(uinfo, 1, 2, texts);
681 static int snd_aw2_control_switch_capture_get(struct snd_kcontrol *kcontrol,
682 struct snd_ctl_elem_value
685 struct aw2 *chip = snd_kcontrol_chip(kcontrol);
686 if (snd_aw2_saa7146_is_using_digital_input(&chip->saa7146))
687 ucontrol->value.enumerated.item[0] = CTL_ROUTE_DIGITAL;
689 ucontrol->value.enumerated.item[0] = CTL_ROUTE_ANALOG;
693 static int snd_aw2_control_switch_capture_put(struct snd_kcontrol *kcontrol,
694 struct snd_ctl_elem_value
697 struct aw2 *chip = snd_kcontrol_chip(kcontrol);
700 snd_aw2_saa7146_is_using_digital_input(&chip->saa7146);
702 if (((ucontrol->value.integer.value[0] == CTL_ROUTE_DIGITAL)
704 || ((ucontrol->value.integer.value[0] == CTL_ROUTE_ANALOG)
706 snd_aw2_saa7146_use_digital_input(&chip->saa7146, !is_disgital);