2 * SAA713x ALSA support for V4L
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 2
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
16 #include "saa7134-reg.h"
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/time.h>
21 #include <linux/wait.h>
22 #include <linux/module.h>
23 #include <sound/core.h>
24 #include <sound/control.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/initval.h>
28 #include <linux/interrupt.h>
29 #include <linux/vmalloc.h>
32 * Configuration macros
36 #define MIXER_ADDR_UNSELECTED -1
37 #define MIXER_ADDR_TVTUNER 0
38 #define MIXER_ADDR_LINE1 1
39 #define MIXER_ADDR_LINE2 2
40 #define MIXER_ADDR_LAST 2
43 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
; /* Index 0-MAX */
44 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
; /* ID for this card */
45 static int enable
[SNDRV_CARDS
] = {1, [1 ... (SNDRV_CARDS
- 1)] = 1};
47 module_param_array(index
, int, NULL
, 0444);
48 module_param_array(enable
, int, NULL
, 0444);
49 MODULE_PARM_DESC(index
, "Index value for SAA7134 capture interface(s).");
50 MODULE_PARM_DESC(enable
, "Enable (or not) the SAA7134 capture interface(s).");
56 typedef struct snd_card_saa7134
{
57 struct snd_card
*card
;
58 spinlock_t mixer_lock
;
59 int mixer_volume
[MIXER_ADDR_LAST
+1][2];
60 int capture_source_addr
;
61 int capture_source
[2];
62 struct snd_kcontrol
*capture_ctl
[MIXER_ADDR_LAST
+1];
64 struct saa7134_dev
*dev
;
78 typedef struct snd_card_saa7134_pcm
{
79 struct saa7134_dev
*dev
;
83 struct snd_pcm_substream
*substream
;
84 } snd_card_saa7134_pcm_t
;
86 static struct snd_card
*snd_saa7134_cards
[SNDRV_CARDS
];
90 * saa7134 DMA audio stop
92 * Called when the capture device is released or the buffer overflows
94 * - Copied verbatim from saa7134-oss's dsp_dma_stop.
98 static void saa7134_dma_stop(struct saa7134_dev
*dev
)
100 dev
->dmasound
.dma_blk
= -1;
101 dev
->dmasound
.dma_running
= 0;
102 saa7134_set_dmabits(dev
);
106 * saa7134 DMA audio start
108 * Called when preparing the capture device for use
110 * - Copied verbatim from saa7134-oss's dsp_dma_start.
114 static void saa7134_dma_start(struct saa7134_dev
*dev
)
116 dev
->dmasound
.dma_blk
= 0;
117 dev
->dmasound
.dma_running
= 1;
118 saa7134_set_dmabits(dev
);
122 * saa7134 audio DMA IRQ handler
124 * Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
125 * Handles shifting between the 2 buffers, manages the read counters,
126 * and notifies ALSA when periods elapse
128 * - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
132 static void saa7134_irq_alsa_done(struct saa7134_dev
*dev
,
133 unsigned long status
)
135 int next_blk
, reg
= 0;
137 spin_lock(&dev
->slock
);
138 if (UNSET
== dev
->dmasound
.dma_blk
) {
139 pr_debug("irq: recording stopped\n");
142 if (0 != (status
& 0x0f000000))
143 pr_debug("irq: lost %ld\n", (status
>> 24) & 0x0f);
144 if (0 == (status
& 0x10000000)) {
146 if (0 == (dev
->dmasound
.dma_blk
& 0x01))
147 reg
= SAA7134_RS_BA1(6);
150 if (1 == (dev
->dmasound
.dma_blk
& 0x01))
151 reg
= SAA7134_RS_BA2(6);
154 pr_debug("irq: field oops [%s]\n",
155 (status
& 0x10000000) ? "even" : "odd");
159 if (dev
->dmasound
.read_count
>= dev
->dmasound
.blksize
* (dev
->dmasound
.blocks
-2)) {
160 pr_debug("irq: overrun [full=%d/%d] - Blocks in %d\n",
161 dev
->dmasound
.read_count
,
162 dev
->dmasound
.bufsize
, dev
->dmasound
.blocks
);
163 spin_unlock(&dev
->slock
);
164 snd_pcm_stop_xrun(dev
->dmasound
.substream
);
168 /* next block addr */
169 next_blk
= (dev
->dmasound
.dma_blk
+ 2) % dev
->dmasound
.blocks
;
170 saa_writel(reg
,next_blk
* dev
->dmasound
.blksize
);
171 pr_debug("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
172 (status
& 0x10000000) ? "even" : "odd ", next_blk
,
173 next_blk
* dev
->dmasound
.blksize
, dev
->dmasound
.blocks
,
174 dev
->dmasound
.blksize
, dev
->dmasound
.read_count
);
176 /* update status & wake waiting readers */
177 dev
->dmasound
.dma_blk
= (dev
->dmasound
.dma_blk
+ 1) % dev
->dmasound
.blocks
;
178 dev
->dmasound
.read_count
+= dev
->dmasound
.blksize
;
180 dev
->dmasound
.recording_on
= reg
;
182 if (dev
->dmasound
.read_count
>= snd_pcm_lib_period_bytes(dev
->dmasound
.substream
)) {
183 spin_unlock(&dev
->slock
);
184 snd_pcm_period_elapsed(dev
->dmasound
.substream
);
185 spin_lock(&dev
->slock
);
189 spin_unlock(&dev
->slock
);
194 * IRQ request handler
196 * Runs along with saa7134's IRQ handler, discards anything that isn't
201 static irqreturn_t
saa7134_alsa_irq(int irq
, void *dev_id
)
203 struct saa7134_dmasound
*dmasound
= dev_id
;
204 struct saa7134_dev
*dev
= dmasound
->priv_data
;
206 unsigned long report
, status
;
207 int loop
, handled
= 0;
209 for (loop
= 0; loop
< 10; loop
++) {
210 report
= saa_readl(SAA7134_IRQ_REPORT
);
211 status
= saa_readl(SAA7134_IRQ_STATUS
);
213 if (report
& SAA7134_IRQ_REPORT_DONE_RA3
) {
215 saa_writel(SAA7134_IRQ_REPORT
,
216 SAA7134_IRQ_REPORT_DONE_RA3
);
217 saa7134_irq_alsa_done(dev
, status
);
224 pr_debug("error! looping IRQ!");
228 return IRQ_RETVAL(handled
);
232 * ALSA capture trigger
234 * - One of the ALSA capture callbacks.
236 * Called whenever a capture is started or stopped. Must be defined,
237 * but there's nothing we want to do here
241 static int snd_card_saa7134_capture_trigger(struct snd_pcm_substream
* substream
,
244 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
245 snd_card_saa7134_pcm_t
*pcm
= runtime
->private_data
;
246 struct saa7134_dev
*dev
=pcm
->dev
;
249 spin_lock(&dev
->slock
);
250 if (cmd
== SNDRV_PCM_TRIGGER_START
) {
252 saa7134_dma_start(dev
);
253 } else if (cmd
== SNDRV_PCM_TRIGGER_STOP
) {
255 saa7134_dma_stop(dev
);
259 spin_unlock(&dev
->slock
);
264 static int saa7134_alsa_dma_init(struct saa7134_dev
*dev
, int nr_pages
)
266 struct saa7134_dmasound
*dma
= &dev
->dmasound
;
270 dma
->vaddr
= vmalloc_32(nr_pages
<< PAGE_SHIFT
);
271 if (NULL
== dma
->vaddr
) {
272 pr_debug("vmalloc_32(%d pages) failed\n", nr_pages
);
276 pr_debug("vmalloc is at addr 0x%08lx, size=%d\n",
277 (unsigned long)dma
->vaddr
,
278 nr_pages
<< PAGE_SHIFT
);
280 memset(dma
->vaddr
, 0, nr_pages
<< PAGE_SHIFT
);
281 dma
->nr_pages
= nr_pages
;
283 dma
->sglist
= vzalloc(dma
->nr_pages
* sizeof(*dma
->sglist
));
284 if (NULL
== dma
->sglist
)
287 sg_init_table(dma
->sglist
, dma
->nr_pages
);
288 for (i
= 0; i
< dma
->nr_pages
; i
++) {
289 pg
= vmalloc_to_page(dma
->vaddr
+ i
* PAGE_SIZE
);
291 goto vmalloc_to_page_err
;
292 sg_set_page(&dma
->sglist
[i
], pg
, PAGE_SIZE
, 0);
305 static int saa7134_alsa_dma_map(struct saa7134_dev
*dev
)
307 struct saa7134_dmasound
*dma
= &dev
->dmasound
;
309 dma
->sglen
= dma_map_sg(&dev
->pci
->dev
, dma
->sglist
,
310 dma
->nr_pages
, PCI_DMA_FROMDEVICE
);
312 if (0 == dma
->sglen
) {
313 pr_warn("%s: saa7134_alsa_map_sg failed\n", __func__
);
319 static int saa7134_alsa_dma_unmap(struct saa7134_dev
*dev
)
321 struct saa7134_dmasound
*dma
= &dev
->dmasound
;
326 dma_unmap_sg(&dev
->pci
->dev
, dma
->sglist
, dma
->sglen
, PCI_DMA_FROMDEVICE
);
331 static int saa7134_alsa_dma_free(struct saa7134_dmasound
*dma
)
341 * DMA buffer initialization
343 * Uses V4L functions to initialize the DMA. Shouldn't be necessary in
344 * ALSA, but I was unable to use ALSA's own DMA, and had to force the
347 * - Copied verbatim from saa7134-oss.
351 static int dsp_buffer_init(struct saa7134_dev
*dev
)
355 BUG_ON(!dev
->dmasound
.bufsize
);
357 err
= saa7134_alsa_dma_init(dev
,
358 (dev
->dmasound
.bufsize
+ PAGE_SIZE
) >> PAGE_SHIFT
);
367 * Called after closing the device, during snd_card_saa7134_capture_close
371 static int dsp_buffer_free(struct saa7134_dev
*dev
)
373 BUG_ON(!dev
->dmasound
.blksize
);
375 saa7134_alsa_dma_free(&dev
->dmasound
);
377 dev
->dmasound
.blocks
= 0;
378 dev
->dmasound
.blksize
= 0;
379 dev
->dmasound
.bufsize
= 0;
385 * Setting the capture source and updating the ALSA controls
387 static int snd_saa7134_capsrc_set(struct snd_kcontrol
*kcontrol
,
388 int left
, int right
, bool force_notify
)
390 snd_card_saa7134_t
*chip
= snd_kcontrol_chip(kcontrol
);
391 int change
= 0, addr
= kcontrol
->private_value
;
392 int active
, old_addr
;
395 struct saa7134_dev
*dev
;
399 spin_lock_irq(&chip
->mixer_lock
);
401 active
= left
!= 0 || right
!= 0;
402 old_addr
= chip
->capture_source_addr
;
404 /* The active capture source cannot be deactivated */
406 change
= old_addr
!= addr
||
407 chip
->capture_source
[0] != left
||
408 chip
->capture_source
[1] != right
;
410 chip
->capture_source
[0] = left
;
411 chip
->capture_source
[1] = right
;
412 chip
->capture_source_addr
= addr
;
413 dev
->dmasound
.input
= addr
;
415 spin_unlock_irq(&chip
->mixer_lock
);
418 switch (dev
->pci
->device
) {
420 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
422 case MIXER_ADDR_TVTUNER
:
423 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL
,
425 saa_andorb(SAA7134_SIF_SAMPLE_FREQ
,
428 case MIXER_ADDR_LINE1
:
429 case MIXER_ADDR_LINE2
:
430 analog_io
= (MIXER_ADDR_LINE1
== addr
) ?
432 rate
= (32000 == dev
->dmasound
.rate
) ?
434 saa_andorb(SAA7134_ANALOG_IO_SELECT
,
436 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL
,
438 saa_andorb(SAA7134_SIF_SAMPLE_FREQ
,
444 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
445 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
446 xbarin
= 0x03; /* adc */
449 case MIXER_ADDR_TVTUNER
:
450 xbarin
= 0; /* Demodulator */
451 anabar
= 2; /* DACs */
453 case MIXER_ADDR_LINE1
:
454 anabar
= 0; /* aux1, aux1 */
456 case MIXER_ADDR_LINE2
:
457 anabar
= 9; /* aux2, aux2 */
461 /* output xbar always main channel */
462 saa_dsp_writel(dev
, SAA7133_DIGITAL_OUTPUT_SEL1
,
466 /* We've got data, turn the input on */
467 saa_dsp_writel(dev
, SAA7133_DIGITAL_INPUT_XBAR1
,
469 saa_writel(SAA7133_ANALOG_IO_SELECT
, anabar
);
471 saa_dsp_writel(dev
, SAA7133_DIGITAL_INPUT_XBAR1
,
473 saa_writel(SAA7133_ANALOG_IO_SELECT
, 0);
481 snd_ctl_notify(chip
->card
,
482 SNDRV_CTL_EVENT_MASK_VALUE
,
483 &chip
->capture_ctl
[addr
]->id
);
485 if (old_addr
!= MIXER_ADDR_UNSELECTED
&& old_addr
!= addr
)
486 snd_ctl_notify(chip
->card
,
487 SNDRV_CTL_EVENT_MASK_VALUE
,
488 &chip
->capture_ctl
[old_addr
]->id
);
495 * ALSA PCM preparation
497 * - One of the ALSA capture callbacks.
499 * Called right after the capture device is opened, this function configures
500 * the buffer using the previously defined functions, allocates the memory,
501 * sets up the hardware registers, and then starts the DMA. When this function
502 * returns, the audio should be flowing.
506 static int snd_card_saa7134_capture_prepare(struct snd_pcm_substream
* substream
)
508 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
511 snd_card_saa7134_t
*saa7134
= snd_pcm_substream_chip(substream
);
512 struct saa7134_dev
*dev
;
513 snd_card_saa7134_pcm_t
*pcm
= runtime
->private_data
;
515 pcm
->dev
->dmasound
.substream
= substream
;
519 if (snd_pcm_format_width(runtime
->format
) == 8)
524 if (snd_pcm_format_signed(runtime
->format
))
529 if (snd_pcm_format_big_endian(runtime
->format
))
534 switch (dev
->pci
->device
) {
535 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
536 if (1 == runtime
->channels
)
538 if (2 == runtime
->channels
)
543 fmt
|= (MIXER_ADDR_TVTUNER
== dev
->dmasound
.input
) ? 0xc0 : 0x80;
544 saa_writeb(SAA7134_NUM_SAMPLES0
, ((dev
->dmasound
.blksize
- 1) & 0x0000ff));
545 saa_writeb(SAA7134_NUM_SAMPLES1
, ((dev
->dmasound
.blksize
- 1) & 0x00ff00) >> 8);
546 saa_writeb(SAA7134_NUM_SAMPLES2
, ((dev
->dmasound
.blksize
- 1) & 0xff0000) >> 16);
547 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL
, fmt
);
550 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
551 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
552 if (1 == runtime
->channels
)
554 if (2 == runtime
->channels
)
558 saa_writel(SAA7133_NUM_SAMPLES
, dev
->dmasound
.blksize
-1);
559 saa_writel(SAA7133_AUDIO_CHANNEL
, 0x543210 | (fmt
<< 24));
563 pr_debug("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
564 runtime
->format
, runtime
->channels
, fmt
,
566 /* dma: setup channel 6 (= AUDIO) */
567 control
= SAA7134_RS_CONTROL_BURST_16
|
568 SAA7134_RS_CONTROL_ME
|
569 (dev
->dmasound
.pt
.dma
>> 12);
571 control
|= SAA7134_RS_CONTROL_BSWAP
;
573 saa_writel(SAA7134_RS_BA1(6),0);
574 saa_writel(SAA7134_RS_BA2(6),dev
->dmasound
.blksize
);
575 saa_writel(SAA7134_RS_PITCH(6),0);
576 saa_writel(SAA7134_RS_CONTROL(6),control
);
578 dev
->dmasound
.rate
= runtime
->rate
;
580 /* Setup and update the card/ALSA controls */
581 snd_saa7134_capsrc_set(saa7134
->capture_ctl
[dev
->dmasound
.input
], 1, 1,
589 * ALSA pointer fetching
591 * - One of the ALSA capture callbacks.
593 * Called whenever a period elapses, it must return the current hardware
594 * position of the buffer.
595 * Also resets the read counter used to prevent overruns
599 static snd_pcm_uframes_t
600 snd_card_saa7134_capture_pointer(struct snd_pcm_substream
* substream
)
602 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
603 snd_card_saa7134_pcm_t
*pcm
= runtime
->private_data
;
604 struct saa7134_dev
*dev
=pcm
->dev
;
606 if (dev
->dmasound
.read_count
) {
607 dev
->dmasound
.read_count
-= snd_pcm_lib_period_bytes(substream
);
608 dev
->dmasound
.read_offset
+= snd_pcm_lib_period_bytes(substream
);
609 if (dev
->dmasound
.read_offset
== dev
->dmasound
.bufsize
)
610 dev
->dmasound
.read_offset
= 0;
613 return bytes_to_frames(runtime
, dev
->dmasound
.read_offset
);
617 * ALSA hardware capabilities definition
619 * Report only 32kHz for ALSA:
621 * - SAA7133/35 uses DDEP (DemDec Easy Programming mode), which works in 32kHz
623 * - SAA7134 for TV mode uses DemDec mode (32kHz)
624 * - Radio works in 32kHz only
625 * - When recording 48kHz from Line1/Line2, switching of capture source to TV
627 * switching to 32kHz without any frequency translation
630 static const struct snd_pcm_hardware snd_card_saa7134_capture
=
632 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
633 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
634 SNDRV_PCM_INFO_MMAP_VALID
),
635 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| \
636 SNDRV_PCM_FMTBIT_S16_BE
| \
637 SNDRV_PCM_FMTBIT_S8
| \
638 SNDRV_PCM_FMTBIT_U8
| \
639 SNDRV_PCM_FMTBIT_U16_LE
| \
640 SNDRV_PCM_FMTBIT_U16_BE
,
641 .rates
= SNDRV_PCM_RATE_32000
,
646 .buffer_bytes_max
= (256*1024),
647 .period_bytes_min
= 64,
648 .period_bytes_max
= (256*1024),
653 static void snd_card_saa7134_runtime_free(struct snd_pcm_runtime
*runtime
)
655 snd_card_saa7134_pcm_t
*pcm
= runtime
->private_data
;
662 * ALSA hardware params
664 * - One of the ALSA capture callbacks.
666 * Called on initialization, right before the PCM preparation
670 static int snd_card_saa7134_hw_params(struct snd_pcm_substream
* substream
,
671 struct snd_pcm_hw_params
* hw_params
)
673 snd_card_saa7134_t
*saa7134
= snd_pcm_substream_chip(substream
);
674 struct saa7134_dev
*dev
;
675 unsigned int period_size
, periods
;
678 period_size
= params_period_bytes(hw_params
);
679 periods
= params_periods(hw_params
);
681 if (period_size
< 0x100 || period_size
> 0x10000)
685 if (period_size
* periods
> 1024 * 1024)
690 if (dev
->dmasound
.blocks
== periods
&&
691 dev
->dmasound
.blksize
== period_size
)
694 /* release the old buffer */
695 if (substream
->runtime
->dma_area
) {
696 saa7134_pgtable_free(dev
->pci
, &dev
->dmasound
.pt
);
697 saa7134_alsa_dma_unmap(dev
);
698 dsp_buffer_free(dev
);
699 substream
->runtime
->dma_area
= NULL
;
701 dev
->dmasound
.blocks
= periods
;
702 dev
->dmasound
.blksize
= period_size
;
703 dev
->dmasound
.bufsize
= period_size
* periods
;
705 err
= dsp_buffer_init(dev
);
707 dev
->dmasound
.blocks
= 0;
708 dev
->dmasound
.blksize
= 0;
709 dev
->dmasound
.bufsize
= 0;
713 err
= saa7134_alsa_dma_map(dev
);
715 dsp_buffer_free(dev
);
718 err
= saa7134_pgtable_alloc(dev
->pci
, &dev
->dmasound
.pt
);
720 saa7134_alsa_dma_unmap(dev
);
721 dsp_buffer_free(dev
);
724 err
= saa7134_pgtable_build(dev
->pci
, &dev
->dmasound
.pt
,
725 dev
->dmasound
.sglist
, dev
->dmasound
.sglen
, 0);
727 saa7134_pgtable_free(dev
->pci
, &dev
->dmasound
.pt
);
728 saa7134_alsa_dma_unmap(dev
);
729 dsp_buffer_free(dev
);
733 /* I should be able to use runtime->dma_addr in the control
734 byte, but it doesn't work. So I allocate the DMA using the
735 V4L functions, and force ALSA to use that as the DMA area */
737 substream
->runtime
->dma_area
= dev
->dmasound
.vaddr
;
738 substream
->runtime
->dma_bytes
= dev
->dmasound
.bufsize
;
739 substream
->runtime
->dma_addr
= 0;
746 * ALSA hardware release
748 * - One of the ALSA capture callbacks.
750 * Called after closing the device, but before snd_card_saa7134_capture_close
751 * It stops the DMA audio and releases the buffers.
755 static int snd_card_saa7134_hw_free(struct snd_pcm_substream
* substream
)
757 snd_card_saa7134_t
*saa7134
= snd_pcm_substream_chip(substream
);
758 struct saa7134_dev
*dev
;
762 if (substream
->runtime
->dma_area
) {
763 saa7134_pgtable_free(dev
->pci
, &dev
->dmasound
.pt
);
764 saa7134_alsa_dma_unmap(dev
);
765 dsp_buffer_free(dev
);
766 substream
->runtime
->dma_area
= NULL
;
773 * ALSA capture finish
775 * - One of the ALSA capture callbacks.
777 * Called after closing the device.
781 static int snd_card_saa7134_capture_close(struct snd_pcm_substream
* substream
)
783 snd_card_saa7134_t
*saa7134
= snd_pcm_substream_chip(substream
);
784 struct saa7134_dev
*dev
= saa7134
->dev
;
786 if (saa7134
->mute_was_on
) {
788 saa7134_tvaudio_setmute(dev
);
796 * - One of the ALSA capture callbacks.
798 * Called when opening the device. It creates and populates the PCM
803 static int snd_card_saa7134_capture_open(struct snd_pcm_substream
* substream
)
805 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
806 snd_card_saa7134_pcm_t
*pcm
;
807 snd_card_saa7134_t
*saa7134
= snd_pcm_substream_chip(substream
);
808 struct saa7134_dev
*dev
;
812 pr_err("BUG: saa7134 can't find device struct. Can't proceed with open\n");
816 mutex_lock(&dev
->dmasound
.lock
);
818 dev
->dmasound
.read_count
= 0;
819 dev
->dmasound
.read_offset
= 0;
821 amux
= dev
->input
->amux
;
822 if ((amux
< 1) || (amux
> 3))
824 dev
->dmasound
.input
= amux
- 1;
826 mutex_unlock(&dev
->dmasound
.lock
);
828 pcm
= kzalloc(sizeof(*pcm
), GFP_KERNEL
);
832 pcm
->dev
=saa7134
->dev
;
834 spin_lock_init(&pcm
->lock
);
836 pcm
->substream
= substream
;
837 runtime
->private_data
= pcm
;
838 runtime
->private_free
= snd_card_saa7134_runtime_free
;
839 runtime
->hw
= snd_card_saa7134_capture
;
841 if (dev
->ctl_mute
!= 0) {
842 saa7134
->mute_was_on
= 1;
844 saa7134_tvaudio_setmute(dev
);
847 err
= snd_pcm_hw_constraint_integer(runtime
,
848 SNDRV_PCM_HW_PARAM_PERIODS
);
852 err
= snd_pcm_hw_constraint_step(runtime
, 0,
853 SNDRV_PCM_HW_PARAM_PERIODS
, 2);
861 * page callback (needed for mmap)
864 static struct page
*snd_card_saa7134_page(struct snd_pcm_substream
*substream
,
865 unsigned long offset
)
867 void *pageptr
= substream
->runtime
->dma_area
+ offset
;
868 return vmalloc_to_page(pageptr
);
872 * ALSA capture callbacks definition
875 static const struct snd_pcm_ops snd_card_saa7134_capture_ops
= {
876 .open
= snd_card_saa7134_capture_open
,
877 .close
= snd_card_saa7134_capture_close
,
878 .ioctl
= snd_pcm_lib_ioctl
,
879 .hw_params
= snd_card_saa7134_hw_params
,
880 .hw_free
= snd_card_saa7134_hw_free
,
881 .prepare
= snd_card_saa7134_capture_prepare
,
882 .trigger
= snd_card_saa7134_capture_trigger
,
883 .pointer
= snd_card_saa7134_capture_pointer
,
884 .page
= snd_card_saa7134_page
,
890 * Called when initializing the board. Sets up the name and hooks up
895 static int snd_card_saa7134_pcm(snd_card_saa7134_t
*saa7134
, int device
)
900 if ((err
= snd_pcm_new(saa7134
->card
, "SAA7134 PCM", device
, 0, 1, &pcm
)) < 0)
902 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_card_saa7134_capture_ops
);
903 pcm
->private_data
= saa7134
;
905 strcpy(pcm
->name
, "SAA7134 PCM");
909 #define SAA713x_VOLUME(xname, xindex, addr) \
910 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
911 .info = snd_saa7134_volume_info, \
912 .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
913 .private_value = addr }
915 static int snd_saa7134_volume_info(struct snd_kcontrol
* kcontrol
,
916 struct snd_ctl_elem_info
* uinfo
)
918 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
920 uinfo
->value
.integer
.min
= 0;
921 uinfo
->value
.integer
.max
= 20;
925 static int snd_saa7134_volume_get(struct snd_kcontrol
* kcontrol
,
926 struct snd_ctl_elem_value
* ucontrol
)
928 snd_card_saa7134_t
*chip
= snd_kcontrol_chip(kcontrol
);
929 int addr
= kcontrol
->private_value
;
931 ucontrol
->value
.integer
.value
[0] = chip
->mixer_volume
[addr
][0];
932 ucontrol
->value
.integer
.value
[1] = chip
->mixer_volume
[addr
][1];
936 static int snd_saa7134_volume_put(struct snd_kcontrol
* kcontrol
,
937 struct snd_ctl_elem_value
* ucontrol
)
939 snd_card_saa7134_t
*chip
= snd_kcontrol_chip(kcontrol
);
940 struct saa7134_dev
*dev
= chip
->dev
;
942 int change
, addr
= kcontrol
->private_value
;
945 left
= ucontrol
->value
.integer
.value
[0];
950 right
= ucontrol
->value
.integer
.value
[1];
955 spin_lock_irq(&chip
->mixer_lock
);
957 if (chip
->mixer_volume
[addr
][0] != left
) {
961 if (chip
->mixer_volume
[addr
][1] != right
) {
966 switch (dev
->pci
->device
) {
967 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
969 case MIXER_ADDR_TVTUNER
:
972 case MIXER_ADDR_LINE1
:
973 saa_andorb(SAA7134_ANALOG_IO_SELECT
, 0x10,
974 (left
> 10) ? 0x00 : 0x10);
976 case MIXER_ADDR_LINE2
:
977 saa_andorb(SAA7134_ANALOG_IO_SELECT
, 0x20,
978 (left
> 10) ? 0x00 : 0x20);
982 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
983 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
985 case MIXER_ADDR_TVTUNER
:
988 case MIXER_ADDR_LINE1
:
989 saa_andorb(0x0594, 0x10,
990 (left
> 10) ? 0x00 : 0x10);
992 case MIXER_ADDR_LINE2
:
993 saa_andorb(0x0594, 0x20,
994 (left
> 10) ? 0x00 : 0x20);
999 chip
->mixer_volume
[addr
][0] = left
;
1000 chip
->mixer_volume
[addr
][1] = right
;
1002 spin_unlock_irq(&chip
->mixer_lock
);
1006 #define SAA713x_CAPSRC(xname, xindex, addr) \
1007 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1008 .info = snd_saa7134_capsrc_info, \
1009 .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
1010 .private_value = addr }
1012 static int snd_saa7134_capsrc_info(struct snd_kcontrol
* kcontrol
,
1013 struct snd_ctl_elem_info
* uinfo
)
1015 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1017 uinfo
->value
.integer
.min
= 0;
1018 uinfo
->value
.integer
.max
= 1;
1022 static int snd_saa7134_capsrc_get(struct snd_kcontrol
* kcontrol
,
1023 struct snd_ctl_elem_value
* ucontrol
)
1025 snd_card_saa7134_t
*chip
= snd_kcontrol_chip(kcontrol
);
1026 int addr
= kcontrol
->private_value
;
1028 spin_lock_irq(&chip
->mixer_lock
);
1029 if (chip
->capture_source_addr
== addr
) {
1030 ucontrol
->value
.integer
.value
[0] = chip
->capture_source
[0];
1031 ucontrol
->value
.integer
.value
[1] = chip
->capture_source
[1];
1033 ucontrol
->value
.integer
.value
[0] = 0;
1034 ucontrol
->value
.integer
.value
[1] = 0;
1036 spin_unlock_irq(&chip
->mixer_lock
);
1041 static int snd_saa7134_capsrc_put(struct snd_kcontrol
* kcontrol
,
1042 struct snd_ctl_elem_value
* ucontrol
)
1045 left
= ucontrol
->value
.integer
.value
[0] & 1;
1046 right
= ucontrol
->value
.integer
.value
[1] & 1;
1048 return snd_saa7134_capsrc_set(kcontrol
, left
, right
, false);
1051 static struct snd_kcontrol_new snd_saa7134_volume_controls
[] = {
1052 SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER
),
1053 SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1
),
1054 SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2
),
1057 static struct snd_kcontrol_new snd_saa7134_capture_controls
[] = {
1058 SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER
),
1059 SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1
),
1060 SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2
),
1066 * Called when initializing the board. Sets up the name and hooks up
1071 static int snd_card_saa7134_new_mixer(snd_card_saa7134_t
* chip
)
1073 struct snd_card
*card
= chip
->card
;
1074 struct snd_kcontrol
*kcontrol
;
1078 strcpy(card
->mixername
, "SAA7134 Mixer");
1080 for (idx
= 0; idx
< ARRAY_SIZE(snd_saa7134_volume_controls
); idx
++) {
1081 kcontrol
= snd_ctl_new1(&snd_saa7134_volume_controls
[idx
],
1083 err
= snd_ctl_add(card
, kcontrol
);
1088 for (idx
= 0; idx
< ARRAY_SIZE(snd_saa7134_capture_controls
); idx
++) {
1089 kcontrol
= snd_ctl_new1(&snd_saa7134_capture_controls
[idx
],
1091 addr
= snd_saa7134_capture_controls
[idx
].private_value
;
1092 chip
->capture_ctl
[addr
] = kcontrol
;
1093 err
= snd_ctl_add(card
, kcontrol
);
1098 chip
->capture_source_addr
= MIXER_ADDR_UNSELECTED
;
1102 static void snd_saa7134_free(struct snd_card
* card
)
1104 snd_card_saa7134_t
*chip
= card
->private_data
;
1106 if (chip
->dev
->dmasound
.priv_data
== NULL
)
1110 free_irq(chip
->irq
, &chip
->dev
->dmasound
);
1112 chip
->dev
->dmasound
.priv_data
= NULL
;
1117 * ALSA initialization
1119 * Called by the init routine, once for each saa7134 device present,
1120 * it creates the basic structures and registers the ALSA devices
1124 static int alsa_card_saa7134_create(struct saa7134_dev
*dev
, int devnum
)
1127 struct snd_card
*card
;
1128 snd_card_saa7134_t
*chip
;
1132 if (devnum
>= SNDRV_CARDS
)
1134 if (!enable
[devnum
])
1137 err
= snd_card_new(&dev
->pci
->dev
, index
[devnum
], id
[devnum
],
1138 THIS_MODULE
, sizeof(snd_card_saa7134_t
), &card
);
1142 strcpy(card
->driver
, "SAA7134");
1144 /* Card "creation" */
1146 card
->private_free
= snd_saa7134_free
;
1147 chip
= card
->private_data
;
1149 spin_lock_init(&chip
->lock
);
1150 spin_lock_init(&chip
->mixer_lock
);
1156 chip
->pci
= dev
->pci
;
1157 chip
->iobase
= pci_resource_start(dev
->pci
, 0);
1160 err
= request_irq(dev
->pci
->irq
, saa7134_alsa_irq
,
1161 IRQF_SHARED
, dev
->name
,
1162 (void*) &dev
->dmasound
);
1165 pr_err("%s: can't get IRQ %d for ALSA\n",
1166 dev
->name
, dev
->pci
->irq
);
1170 chip
->irq
= dev
->pci
->irq
;
1172 mutex_init(&dev
->dmasound
.lock
);
1174 if ((err
= snd_card_saa7134_new_mixer(chip
)) < 0)
1177 if ((err
= snd_card_saa7134_pcm(chip
, 0)) < 0)
1180 /* End of "creation" */
1182 strcpy(card
->shortname
, "SAA7134");
1183 sprintf(card
->longname
, "%s at 0x%lx irq %d",
1184 chip
->dev
->name
, chip
->iobase
, chip
->irq
);
1186 pr_info("%s/alsa: %s registered as card %d\n",
1187 dev
->name
, card
->longname
, index
[devnum
]);
1189 if ((err
= snd_card_register(card
)) == 0) {
1190 snd_saa7134_cards
[devnum
] = card
;
1195 snd_card_free(card
);
1200 static int alsa_device_init(struct saa7134_dev
*dev
)
1202 dev
->dmasound
.priv_data
= dev
;
1203 alsa_card_saa7134_create(dev
,dev
->nr
);
1207 static int alsa_device_exit(struct saa7134_dev
*dev
)
1209 if (!snd_saa7134_cards
[dev
->nr
])
1212 snd_card_free(snd_saa7134_cards
[dev
->nr
]);
1213 snd_saa7134_cards
[dev
->nr
] = NULL
;
1218 * Module initializer
1220 * Loops through present saa7134 cards, and assigns an ALSA device
1225 static int saa7134_alsa_init(void)
1227 struct saa7134_dev
*dev
= NULL
;
1228 struct list_head
*list
;
1230 saa7134_dmasound_init
= alsa_device_init
;
1231 saa7134_dmasound_exit
= alsa_device_exit
;
1233 pr_info("saa7134 ALSA driver for DMA sound loaded\n");
1235 list_for_each(list
,&saa7134_devlist
) {
1236 dev
= list_entry(list
, struct saa7134_dev
, devlist
);
1237 if (dev
->pci
->device
== PCI_DEVICE_ID_PHILIPS_SAA7130
)
1238 pr_info("%s/alsa: %s doesn't support digital audio\n",
1239 dev
->name
, saa7134_boards
[dev
->board
].name
);
1241 alsa_device_init(dev
);
1245 pr_info("saa7134 ALSA: no saa7134 cards found\n");
1255 static void saa7134_alsa_exit(void)
1259 for (idx
= 0; idx
< SNDRV_CARDS
; idx
++) {
1260 if (snd_saa7134_cards
[idx
])
1261 snd_card_free(snd_saa7134_cards
[idx
]);
1264 saa7134_dmasound_init
= NULL
;
1265 saa7134_dmasound_exit
= NULL
;
1266 pr_info("saa7134 ALSA driver for DMA sound unloaded\n");
1271 /* We initialize this late, to make sure the sound system is up and running */
1272 late_initcall(saa7134_alsa_init
);
1273 module_exit(saa7134_alsa_exit
);
1274 MODULE_LICENSE("GPL");
1275 MODULE_AUTHOR("Ricardo Cerqueira");