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_create(struct snd_card
*card
, struct pci_dev
*pci
);
103 static int snd_aw2_probe(struct pci_dev
*pci
,
104 const struct pci_device_id
*pci_id
);
105 static int snd_aw2_pcm_playback_open(struct snd_pcm_substream
*substream
);
106 static int snd_aw2_pcm_playback_close(struct snd_pcm_substream
*substream
);
107 static int snd_aw2_pcm_capture_open(struct snd_pcm_substream
*substream
);
108 static int snd_aw2_pcm_capture_close(struct snd_pcm_substream
*substream
);
109 static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream
*substream
);
110 static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream
*substream
);
111 static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream
*substream
,
113 static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream
*substream
,
115 static snd_pcm_uframes_t
snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
117 static snd_pcm_uframes_t
snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
119 static int snd_aw2_new_pcm(struct aw2
*chip
);
121 static int snd_aw2_control_switch_capture_info(struct snd_kcontrol
*kcontrol
,
122 struct snd_ctl_elem_info
*uinfo
);
123 static int snd_aw2_control_switch_capture_get(struct snd_kcontrol
*kcontrol
,
124 struct snd_ctl_elem_value
126 static int snd_aw2_control_switch_capture_put(struct snd_kcontrol
*kcontrol
,
127 struct snd_ctl_elem_value
130 /*********************************
132 ********************************/
133 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
;
134 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
;
135 static bool enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE_PNP
;
137 module_param_array(index
, int, NULL
, 0444);
138 MODULE_PARM_DESC(index
, "Index value for Audiowerk2 soundcard.");
139 module_param_array(id
, charp
, NULL
, 0444);
140 MODULE_PARM_DESC(id
, "ID string for the Audiowerk2 soundcard.");
141 module_param_array(enable
, bool, NULL
, 0444);
142 MODULE_PARM_DESC(enable
, "Enable Audiowerk2 soundcard.");
144 static const struct pci_device_id snd_aw2_ids
[] = {
145 {PCI_VENDOR_ID_PHILIPS
, PCI_DEVICE_ID_PHILIPS_SAA7146
, 0, 0,
150 MODULE_DEVICE_TABLE(pci
, snd_aw2_ids
);
152 /* pci_driver definition */
153 static struct pci_driver aw2_driver
= {
154 .name
= KBUILD_MODNAME
,
155 .id_table
= snd_aw2_ids
,
156 .probe
= snd_aw2_probe
,
159 module_pci_driver(aw2_driver
);
161 /* operators for playback PCM alsa interface */
162 static const struct snd_pcm_ops snd_aw2_playback_ops
= {
163 .open
= snd_aw2_pcm_playback_open
,
164 .close
= snd_aw2_pcm_playback_close
,
165 .prepare
= snd_aw2_pcm_prepare_playback
,
166 .trigger
= snd_aw2_pcm_trigger_playback
,
167 .pointer
= snd_aw2_pcm_pointer_playback
,
170 /* operators for capture PCM alsa interface */
171 static const struct snd_pcm_ops snd_aw2_capture_ops
= {
172 .open
= snd_aw2_pcm_capture_open
,
173 .close
= snd_aw2_pcm_capture_close
,
174 .prepare
= snd_aw2_pcm_prepare_capture
,
175 .trigger
= snd_aw2_pcm_trigger_capture
,
176 .pointer
= snd_aw2_pcm_pointer_capture
,
179 static const struct snd_kcontrol_new aw2_control
= {
180 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
181 .name
= "PCM Capture Route",
183 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
184 .private_value
= 0xffff,
185 .info
= snd_aw2_control_switch_capture_info
,
186 .get
= snd_aw2_control_switch_capture_get
,
187 .put
= snd_aw2_control_switch_capture_put
190 /*********************************
191 * FUNCTION IMPLEMENTATIONS
192 ********************************/
194 /* component-destructor */
195 static void snd_aw2_free(struct snd_card
*card
)
197 struct aw2
*chip
= card
->private_data
;
200 snd_aw2_saa7146_free(&chip
->saa7146
);
203 /* chip-specific constructor */
204 static int snd_aw2_create(struct snd_card
*card
,
207 struct aw2
*chip
= card
->private_data
;
210 /* initialize the PCI entry */
211 err
= pcim_enable_device(pci
);
216 /* check PCI availability (32bit DMA) */
217 if (dma_set_mask_and_coherent(&pci
->dev
, DMA_BIT_MASK(32))) {
218 dev_err(card
->dev
, "Impossible to set 32bit mask DMA\n");
222 /* initialize the stuff */
227 /* (1) PCI resource allocation */
228 err
= pcim_iomap_regions(pci
, 1 << 0, "Audiowerk2");
231 chip
->iobase_phys
= pci_resource_start(pci
, 0);
232 chip
->iobase_virt
= pcim_iomap_table(pci
)[0];
234 /* (2) initialization of the chip hardware */
235 snd_aw2_saa7146_setup(&chip
->saa7146
, chip
->iobase_virt
);
237 if (devm_request_irq(&pci
->dev
, pci
->irq
, snd_aw2_saa7146_interrupt
,
238 IRQF_SHARED
, KBUILD_MODNAME
, chip
)) {
239 dev_err(card
->dev
, "Cannot grab irq %d\n", pci
->irq
);
242 chip
->irq
= pci
->irq
;
243 card
->sync_irq
= chip
->irq
;
244 card
->private_free
= snd_aw2_free
;
247 "Audiowerk 2 sound card (saa7146 chipset) detected and managed\n");
252 static int snd_aw2_probe(struct pci_dev
*pci
,
253 const struct pci_device_id
*pci_id
)
256 struct snd_card
*card
;
260 /* (1) Continue if device is not enabled, else inc dev */
261 if (dev
>= SNDRV_CARDS
)
268 /* (2) Create card instance */
269 err
= snd_devm_card_new(&pci
->dev
, index
[dev
], id
[dev
], THIS_MODULE
,
270 sizeof(*chip
), &card
);
273 chip
= card
->private_data
;
275 /* (3) Create main component */
276 err
= snd_aw2_create(card
, pci
);
280 /* initialize mutex */
281 mutex_init(&chip
->mtx
);
283 spin_lock_init(&chip
->reg_lock
);
284 /* (4) Define driver ID and name string */
285 strcpy(card
->driver
, "aw2");
286 strcpy(card
->shortname
, "Audiowerk2");
288 sprintf(card
->longname
, "%s with SAA7146 irq %i",
289 card
->shortname
, chip
->irq
);
291 /* (5) Create other components */
292 snd_aw2_new_pcm(chip
);
294 /* (6) Register card instance */
295 err
= snd_card_register(card
);
299 /* (7) Set PCI driver data */
300 pci_set_drvdata(pci
, card
);
311 static int snd_aw2_pcm_playback_open(struct snd_pcm_substream
*substream
)
313 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
315 dev_dbg(substream
->pcm
->card
->dev
, "Playback_open\n");
316 runtime
->hw
= snd_aw2_playback_hw
;
321 static int snd_aw2_pcm_playback_close(struct snd_pcm_substream
*substream
)
327 static int snd_aw2_pcm_capture_open(struct snd_pcm_substream
*substream
)
329 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
331 dev_dbg(substream
->pcm
->card
->dev
, "Capture_open\n");
332 runtime
->hw
= snd_aw2_capture_hw
;
337 static int snd_aw2_pcm_capture_close(struct snd_pcm_substream
*substream
)
339 /* TODO: something to do ? */
343 /* prepare callback for playback */
344 static int snd_aw2_pcm_prepare_playback(struct snd_pcm_substream
*substream
)
346 struct aw2_pcm_device
*pcm_device
= snd_pcm_substream_chip(substream
);
347 struct aw2
*chip
= pcm_device
->chip
;
348 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
349 unsigned long period_size
, buffer_size
;
351 mutex_lock(&chip
->mtx
);
353 period_size
= snd_pcm_lib_period_bytes(substream
);
354 buffer_size
= snd_pcm_lib_buffer_bytes(substream
);
356 snd_aw2_saa7146_pcm_init_playback(&chip
->saa7146
,
357 pcm_device
->stream_number
,
358 runtime
->dma_addr
, period_size
,
361 /* Define Interrupt callback */
362 snd_aw2_saa7146_define_it_playback_callback(pcm_device
->stream_number
,
363 (snd_aw2_saa7146_it_cb
)
364 snd_pcm_period_elapsed
,
367 mutex_unlock(&chip
->mtx
);
372 /* prepare callback for capture */
373 static int snd_aw2_pcm_prepare_capture(struct snd_pcm_substream
*substream
)
375 struct aw2_pcm_device
*pcm_device
= snd_pcm_substream_chip(substream
);
376 struct aw2
*chip
= pcm_device
->chip
;
377 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
378 unsigned long period_size
, buffer_size
;
380 mutex_lock(&chip
->mtx
);
382 period_size
= snd_pcm_lib_period_bytes(substream
);
383 buffer_size
= snd_pcm_lib_buffer_bytes(substream
);
385 snd_aw2_saa7146_pcm_init_capture(&chip
->saa7146
,
386 pcm_device
->stream_number
,
387 runtime
->dma_addr
, period_size
,
390 /* Define Interrupt callback */
391 snd_aw2_saa7146_define_it_capture_callback(pcm_device
->stream_number
,
392 (snd_aw2_saa7146_it_cb
)
393 snd_pcm_period_elapsed
,
396 mutex_unlock(&chip
->mtx
);
401 /* playback trigger callback */
402 static int snd_aw2_pcm_trigger_playback(struct snd_pcm_substream
*substream
,
406 struct aw2_pcm_device
*pcm_device
= snd_pcm_substream_chip(substream
);
407 struct aw2
*chip
= pcm_device
->chip
;
408 spin_lock(&chip
->reg_lock
);
410 case SNDRV_PCM_TRIGGER_START
:
411 snd_aw2_saa7146_pcm_trigger_start_playback(&chip
->saa7146
,
415 case SNDRV_PCM_TRIGGER_STOP
:
416 snd_aw2_saa7146_pcm_trigger_stop_playback(&chip
->saa7146
,
423 spin_unlock(&chip
->reg_lock
);
427 /* capture trigger callback */
428 static int snd_aw2_pcm_trigger_capture(struct snd_pcm_substream
*substream
,
432 struct aw2_pcm_device
*pcm_device
= snd_pcm_substream_chip(substream
);
433 struct aw2
*chip
= pcm_device
->chip
;
434 spin_lock(&chip
->reg_lock
);
436 case SNDRV_PCM_TRIGGER_START
:
437 snd_aw2_saa7146_pcm_trigger_start_capture(&chip
->saa7146
,
441 case SNDRV_PCM_TRIGGER_STOP
:
442 snd_aw2_saa7146_pcm_trigger_stop_capture(&chip
->saa7146
,
449 spin_unlock(&chip
->reg_lock
);
453 /* playback pointer callback */
454 static snd_pcm_uframes_t
snd_aw2_pcm_pointer_playback(struct snd_pcm_substream
457 struct aw2_pcm_device
*pcm_device
= snd_pcm_substream_chip(substream
);
458 struct aw2
*chip
= pcm_device
->chip
;
459 unsigned int current_ptr
;
461 /* get the current hardware pointer */
462 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
464 snd_aw2_saa7146_get_hw_ptr_playback(&chip
->saa7146
,
465 pcm_device
->stream_number
,
467 runtime
->buffer_size
);
469 return bytes_to_frames(substream
->runtime
, current_ptr
);
472 /* capture pointer callback */
473 static snd_pcm_uframes_t
snd_aw2_pcm_pointer_capture(struct snd_pcm_substream
476 struct aw2_pcm_device
*pcm_device
= snd_pcm_substream_chip(substream
);
477 struct aw2
*chip
= pcm_device
->chip
;
478 unsigned int current_ptr
;
480 /* get the current hardware pointer */
481 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
483 snd_aw2_saa7146_get_hw_ptr_capture(&chip
->saa7146
,
484 pcm_device
->stream_number
,
486 runtime
->buffer_size
);
488 return bytes_to_frames(substream
->runtime
, current_ptr
);
491 /* create a pcm device */
492 static int snd_aw2_new_pcm(struct aw2
*chip
)
494 struct snd_pcm
*pcm_playback_ana
;
495 struct snd_pcm
*pcm_playback_num
;
496 struct snd_pcm
*pcm_capture
;
497 struct aw2_pcm_device
*pcm_device
;
500 /* Create new Alsa PCM device */
502 err
= snd_pcm_new(chip
->card
, "Audiowerk2 analog playback", 0, 1, 0,
505 dev_err(chip
->card
->dev
, "snd_pcm_new error (0x%X)\n", err
);
510 pcm_device
= &chip
->device_playback
[NUM_STREAM_PLAYBACK_ANA
];
512 /* Set PCM device name */
513 strcpy(pcm_playback_ana
->name
, "Analog playback");
514 /* Associate private data to PCM device */
515 pcm_playback_ana
->private_data
= pcm_device
;
516 /* set operators of PCM device */
517 snd_pcm_set_ops(pcm_playback_ana
, SNDRV_PCM_STREAM_PLAYBACK
,
518 &snd_aw2_playback_ops
);
519 /* store PCM device */
520 pcm_device
->pcm
= pcm_playback_ana
;
521 /* give base chip pointer to our internal pcm device
523 pcm_device
->chip
= chip
;
524 /* Give stream number to PCM device */
525 pcm_device
->stream_number
= NUM_STREAM_PLAYBACK_ANA
;
527 /* pre-allocation of buffers */
528 /* Preallocate continuous pages. */
529 snd_pcm_set_managed_buffer_all(pcm_playback_ana
,
532 64 * 1024, 64 * 1024);
534 err
= snd_pcm_new(chip
->card
, "Audiowerk2 digital playback", 1, 1, 0,
538 dev_err(chip
->card
->dev
, "snd_pcm_new error (0x%X)\n", err
);
542 pcm_device
= &chip
->device_playback
[NUM_STREAM_PLAYBACK_DIG
];
544 /* Set PCM device name */
545 strcpy(pcm_playback_num
->name
, "Digital playback");
546 /* Associate private data to PCM device */
547 pcm_playback_num
->private_data
= pcm_device
;
548 /* set operators of PCM device */
549 snd_pcm_set_ops(pcm_playback_num
, SNDRV_PCM_STREAM_PLAYBACK
,
550 &snd_aw2_playback_ops
);
551 /* store PCM device */
552 pcm_device
->pcm
= pcm_playback_num
;
553 /* give base chip pointer to our internal pcm device
555 pcm_device
->chip
= chip
;
556 /* Give stream number to PCM device */
557 pcm_device
->stream_number
= NUM_STREAM_PLAYBACK_DIG
;
559 /* pre-allocation of buffers */
560 /* Preallocate continuous pages. */
561 snd_pcm_set_managed_buffer_all(pcm_playback_num
,
564 64 * 1024, 64 * 1024);
566 err
= snd_pcm_new(chip
->card
, "Audiowerk2 capture", 2, 0, 1,
570 dev_err(chip
->card
->dev
, "snd_pcm_new error (0x%X)\n", err
);
575 pcm_device
= &chip
->device_capture
[NUM_STREAM_CAPTURE_ANA
];
577 /* Set PCM device name */
578 strcpy(pcm_capture
->name
, "Capture");
579 /* Associate private data to PCM device */
580 pcm_capture
->private_data
= pcm_device
;
581 /* set operators of PCM device */
582 snd_pcm_set_ops(pcm_capture
, SNDRV_PCM_STREAM_CAPTURE
,
583 &snd_aw2_capture_ops
);
584 /* store PCM device */
585 pcm_device
->pcm
= pcm_capture
;
586 /* give base chip pointer to our internal pcm device
588 pcm_device
->chip
= chip
;
589 /* Give stream number to PCM device */
590 pcm_device
->stream_number
= NUM_STREAM_CAPTURE_ANA
;
592 /* pre-allocation of buffers */
593 /* Preallocate continuous pages. */
594 snd_pcm_set_managed_buffer_all(pcm_capture
,
597 64 * 1024, 64 * 1024);
600 err
= snd_ctl_add(chip
->card
, snd_ctl_new1(&aw2_control
, chip
));
602 dev_err(chip
->card
->dev
, "snd_ctl_add error (0x%X)\n", err
);
609 static int snd_aw2_control_switch_capture_info(struct snd_kcontrol
*kcontrol
,
610 struct snd_ctl_elem_info
*uinfo
)
612 static const char * const texts
[2] = {
615 return snd_ctl_enum_info(uinfo
, 1, 2, texts
);
618 static int snd_aw2_control_switch_capture_get(struct snd_kcontrol
*kcontrol
,
619 struct snd_ctl_elem_value
622 struct aw2
*chip
= snd_kcontrol_chip(kcontrol
);
623 if (snd_aw2_saa7146_is_using_digital_input(&chip
->saa7146
))
624 ucontrol
->value
.enumerated
.item
[0] = CTL_ROUTE_DIGITAL
;
626 ucontrol
->value
.enumerated
.item
[0] = CTL_ROUTE_ANALOG
;
630 static int snd_aw2_control_switch_capture_put(struct snd_kcontrol
*kcontrol
,
631 struct snd_ctl_elem_value
634 struct aw2
*chip
= snd_kcontrol_chip(kcontrol
);
637 snd_aw2_saa7146_is_using_digital_input(&chip
->saa7146
);
639 if (((ucontrol
->value
.integer
.value
[0] == CTL_ROUTE_DIGITAL
)
641 || ((ucontrol
->value
.integer
.value
[0] == CTL_ROUTE_ANALOG
)
643 snd_aw2_saa7146_use_digital_input(&chip
->saa7146
, !is_disgital
);