2 * SAA713x ALSA support for V4L
3 * Ricardo Cerqueira <v4l@cerqueira.org>
7 * - Volume doesn't work (it's always at max)
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, version 2
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sound/driver.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <linux/wait.h>
29 #include <linux/moduleparam.h>
30 #include <linux/module.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/pcm.h>
34 #include <sound/rawmidi.h>
35 #include <sound/initval.h>
38 #include "saa7134-reg.h"
40 static unsigned int alsa_debug = 0;
41 module_param(alsa_debug, int, 0644);
42 MODULE_PARM_DESC(alsa_debug,"enable debug messages [alsa]");
45 * Configuration macros
48 #define MAX_PCM_DEVICES 1
49 #define MAX_PCM_SUBSTREAMS 1
52 #define MAX_BUFFER_SIZE (256*1024)
53 #define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE
54 #define USE_RATE SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000
55 #define USE_RATE_MIN 32000
56 #define USE_RATE_MAX 48000
57 #define USE_CHANNELS_MIN 1
58 #define USE_CHANNELS_MAX 2
59 #ifndef USE_PERIODS_MIN
60 #define USE_PERIODS_MIN 2
62 #ifndef USE_PERIODS_MAX
63 #define USE_PERIODS_MAX 1024
66 #define MIXER_ADDR_TVTUNER 0
67 #define MIXER_ADDR_LINE1 1
68 #define MIXER_ADDR_LINE2 2
69 #define MIXER_ADDR_LAST 2
71 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
72 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
73 static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
75 #define dprintk(fmt, arg...) if (alsa_debug) \
76 printk(KERN_DEBUG "%s/alsa: " fmt, dev->name , ## arg)
81 typedef struct snd_card_saa7134 {
83 spinlock_t mixer_lock;
84 int mixer_volume[MIXER_ADDR_LAST+1][2];
85 int capture_source[MIXER_ADDR_LAST+1][2];
87 struct saa7134_dev *saadev;
99 typedef struct snd_card_saa7134_pcm {
100 struct saa7134_dev *saadev;
103 unsigned int pcm_size; /* buffer size */
104 unsigned int pcm_count; /* bytes per period */
105 unsigned int pcm_bps; /* bytes per second */
106 snd_pcm_substream_t *substream;
107 } snd_card_saa7134_pcm_t;
109 static snd_card_t *snd_saa7134_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
112 * saa7134 DMA audio stop
114 * Called when the capture device is released or the buffer overflows
116 * - Copied verbatim from saa7134-oss's dsp_dma_stop. Can be dropped
117 * if we just share dsp_dma_stop and use it here
121 static void saa7134_dma_stop(struct saa7134_dev *dev)
124 dev->oss.dma_blk = -1;
125 dev->oss.dma_running = 0;
126 saa7134_set_dmabits(dev);
130 * saa7134 DMA audio start
132 * Called when preparing the capture device for use
134 * - Copied verbatim from saa7134-oss's dsp_dma_start. Can be dropped
135 * if we just share dsp_dma_start and use it here
139 static void saa7134_dma_start(struct saa7134_dev *dev)
141 dev->oss.dma_blk = 0;
142 dev->oss.dma_running = 1;
143 saa7134_set_dmabits(dev);
147 * saa7134 audio DMA IRQ handler
149 * Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
150 * Handles shifting between the 2 buffers, manages the read counters,
151 * and notifies ALSA when periods elapse
153 * - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
157 void saa7134_irq_alsa_done(struct saa7134_dev *dev, unsigned long status)
159 int next_blk, reg = 0;
161 spin_lock(&dev->slock);
162 if (UNSET == dev->oss.dma_blk) {
163 dprintk("irq: recording stopped\n");
166 if (0 != (status & 0x0f000000))
167 dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
168 if (0 == (status & 0x10000000)) {
170 if (0 == (dev->oss.dma_blk & 0x01))
171 reg = SAA7134_RS_BA1(6);
174 if (1 == (dev->oss.dma_blk & 0x01))
175 reg = SAA7134_RS_BA2(6);
178 dprintk("irq: field oops [%s]\n",
179 (status & 0x10000000) ? "even" : "odd");
183 if (dev->oss.read_count >= dev->oss.blksize * (dev->oss.blocks-2)) {
184 dprintk("irq: overrun [full=%d/%d] - Blocks in %d\n",dev->oss.read_count,
185 dev->oss.bufsize, dev->oss.blocks);
186 saa7134_dma_stop(dev);
190 /* next block addr */
191 next_blk = (dev->oss.dma_blk + 2) % dev->oss.blocks;
192 saa_writel(reg,next_blk * dev->oss.blksize);
194 dprintk("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
195 (status & 0x10000000) ? "even" : "odd ", next_blk,
196 next_blk * dev->oss.blksize, dev->oss.blocks, dev->oss.blksize, dev->oss.read_count);
199 /* update status & wake waiting readers */
200 dev->oss.dma_blk = (dev->oss.dma_blk + 1) % dev->oss.blocks;
201 dev->oss.read_count += dev->oss.blksize;
203 dev->oss.recording_on = reg;
205 if (dev->oss.read_count >= snd_pcm_lib_period_bytes(dev->oss.substream)) {
206 spin_unlock(&dev->slock);
207 snd_pcm_period_elapsed(dev->oss.substream);
208 spin_lock(&dev->slock);
211 spin_unlock(&dev->slock);
216 * ALSA capture trigger
218 * - One of the ALSA capture callbacks.
220 * Called whenever a capture is started or stopped. Must be defined,
221 * but there's nothing we want to do here
225 static int snd_card_saa7134_capture_trigger(snd_pcm_substream_t * substream,
234 * Sets the values that will later be used as the size of the buffer,
235 * size of the fragments, and total number of fragments.
236 * Must be called during the preparation stage, before memory is
239 * - Copied verbatim from saa7134-oss. Can be dropped
240 * if we just share dsp_buffer_conf from OSS.
243 static int dsp_buffer_conf(struct saa7134_dev *dev, int blksize, int blocks)
247 if (blksize > 0x10000)
252 if ((blksize * blocks) > 1024*1024)
253 blocks = 1024*1024 / blksize;
255 dev->oss.blocks = blocks;
256 dev->oss.blksize = blksize;
257 dev->oss.bufsize = blksize * blocks;
259 dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
260 blocks,blksize,blksize * blocks / 1024);
265 * DMA buffer initialization
267 * Uses V4L functions to initialize the DMA. Shouldn't be necessary in
268 * ALSA, but I was unable to use ALSA's own DMA, and had to force the
271 * - Copied verbatim from saa7134-oss. Can be dropped
272 * if we just share dsp_buffer_init from OSS.
275 static int dsp_buffer_init(struct saa7134_dev *dev)
279 if (!dev->oss.bufsize)
281 videobuf_dma_init(&dev->oss.dma);
282 err = videobuf_dma_init_kernel(&dev->oss.dma, PCI_DMA_FROMDEVICE,
283 (dev->oss.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
290 * ALSA PCM preparation
292 * - One of the ALSA capture callbacks.
294 * Called right after the capture device is opened, this function configures
295 * the buffer using the previously defined functions, allocates the memory,
296 * sets up the hardware registers, and then starts the DMA. When this function
297 * returns, the audio should be flowing.
301 static int snd_card_saa7134_capture_prepare(snd_pcm_substream_t * substream)
303 snd_pcm_runtime_t *runtime = substream->runtime;
304 int err, bswap, sign;
307 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
308 struct saa7134_dev *dev;
309 snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
314 size = snd_pcm_lib_buffer_bytes(substream);
315 count = snd_pcm_lib_period_bytes(substream);
317 saapcm->saadev->oss.substream = substream;
318 bps = runtime->rate * runtime->channels;
319 bps *= snd_pcm_format_width(runtime->format);
323 saapcm->pcm_bps = bps;
324 saapcm->pcm_size = snd_pcm_lib_buffer_bytes(substream);
325 saapcm->pcm_count = snd_pcm_lib_period_bytes(substream);
330 dsp_buffer_conf(dev,saapcm->pcm_count,(saapcm->pcm_size/saapcm->pcm_count));
332 err = dsp_buffer_init(dev);
337 if (0 != (err = videobuf_dma_pci_map(dev->pci,&dev->oss.dma)))
339 if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->oss.pt)))
341 if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->oss.pt,
349 switch (runtime->format) {
350 case SNDRV_PCM_FORMAT_U8:
351 case SNDRV_PCM_FORMAT_S8:
354 case SNDRV_PCM_FORMAT_U16_LE:
355 case SNDRV_PCM_FORMAT_U16_BE:
356 case SNDRV_PCM_FORMAT_S16_LE:
357 case SNDRV_PCM_FORMAT_S16_BE:
365 switch (runtime->format) {
366 case SNDRV_PCM_FORMAT_S8:
367 case SNDRV_PCM_FORMAT_S16_LE:
368 case SNDRV_PCM_FORMAT_S16_BE:
376 switch (runtime->format) {
377 case SNDRV_PCM_FORMAT_U16_BE:
378 case SNDRV_PCM_FORMAT_S16_BE:
384 switch (dev->pci->device) {
385 case PCI_DEVICE_ID_PHILIPS_SAA7134:
386 if (1 == runtime->channels)
388 if (2 == runtime->channels)
393 fmt |= (MIXER_ADDR_TVTUNER == dev->oss.input) ? 0xc0 : 0x80;
394 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->oss.blksize - 1) & 0x0000ff));
395 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->oss.blksize - 1) & 0x00ff00) >> 8);
396 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->oss.blksize - 1) & 0xff0000) >> 16);
397 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
400 case PCI_DEVICE_ID_PHILIPS_SAA7133:
401 case PCI_DEVICE_ID_PHILIPS_SAA7135:
402 if (1 == runtime->channels)
404 if (2 == runtime->channels)
408 saa_writel(SAA7133_NUM_SAMPLES, dev->oss.blksize -1);
409 saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
410 //saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210);
414 dprintk("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
415 runtime->format, runtime->channels, fmt,
417 /* dma: setup channel 6 (= AUDIO) */
418 control = SAA7134_RS_CONTROL_BURST_16 |
419 SAA7134_RS_CONTROL_ME |
420 (dev->oss.pt.dma >> 12);
422 control |= SAA7134_RS_CONTROL_BSWAP;
424 /* I should be able to use runtime->dma_addr in the control
425 byte, but it doesn't work. So I allocate the DMA using the
426 V4L functions, and force ALSA to use that as the DMA area */
428 runtime->dma_area = dev->oss.dma.vmalloc;
430 saa_writel(SAA7134_RS_BA1(6),0);
431 saa_writel(SAA7134_RS_BA2(6),dev->oss.blksize);
432 saa_writel(SAA7134_RS_PITCH(6),0);
433 saa_writel(SAA7134_RS_CONTROL(6),control);
435 dev->oss.rate = runtime->rate;
437 spin_lock_irqsave(&dev->slock,flags);
438 saa7134_dma_start(dev);
439 spin_unlock_irqrestore(&dev->slock,flags);
443 saa7134_pgtable_free(dev->pci,&dev->oss.pt);
445 videobuf_dma_pci_unmap(dev->pci,&dev->oss.dma);
452 * ALSA pointer fetching
454 * - One of the ALSA capture callbacks.
456 * Called whenever a period elapses, it must return the current hardware
457 * position of the buffer.
458 * Also resets the read counter used to prevent overruns
462 static snd_pcm_uframes_t snd_card_saa7134_capture_pointer(snd_pcm_substream_t * substream)
464 snd_pcm_runtime_t *runtime = substream->runtime;
465 snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
466 struct saa7134_dev *dev=saapcm->saadev;
470 if (dev->oss.read_count) {
471 dev->oss.read_count -= snd_pcm_lib_period_bytes(substream);
472 dev->oss.read_offset += snd_pcm_lib_period_bytes(substream);
473 if (dev->oss.read_offset == dev->oss.bufsize)
474 dev->oss.read_offset = 0;
477 return bytes_to_frames(runtime, dev->oss.read_offset);
481 * ALSA hardware capabilities definition
484 static snd_pcm_hardware_t snd_card_saa7134_capture =
486 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
487 SNDRV_PCM_INFO_BLOCK_TRANSFER |
488 SNDRV_PCM_INFO_MMAP_VALID),
489 .formats = USE_FORMATS,
491 .rate_min = USE_RATE_MIN,
492 .rate_max = USE_RATE_MAX,
493 .channels_min = USE_CHANNELS_MIN,
494 .channels_max = USE_CHANNELS_MAX,
495 .buffer_bytes_max = MAX_BUFFER_SIZE,
496 .period_bytes_min = 64,
497 .period_bytes_max = MAX_BUFFER_SIZE,
498 .periods_min = USE_PERIODS_MIN,
499 .periods_max = USE_PERIODS_MAX,
500 .fifo_size = 0x08070503,
503 static void snd_card_saa7134_runtime_free(snd_pcm_runtime_t *runtime)
505 snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
512 * ALSA hardware params
514 * - One of the ALSA capture callbacks.
516 * Called on initialization, right before the PCM preparation
517 * Usually used in ALSA to allocate the DMA, but since we don't use the
518 * ALSA DMA I'm almost sure this isn't necessary.
522 static int snd_card_saa7134_hw_params(snd_pcm_substream_t * substream,
523 snd_pcm_hw_params_t * hw_params)
526 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
532 * ALSA hardware release
534 * - One of the ALSA capture callbacks.
536 * Called after closing the device, but before snd_card_saa7134_capture_close
537 * Usually used in ALSA to free the DMA, but since we don't use the
538 * ALSA DMA I'm almost sure this isn't necessary.
542 static int snd_card_saa7134_hw_free(snd_pcm_substream_t * substream)
544 return snd_pcm_lib_free_pages(substream);
550 * Called after closing the device, during snd_card_saa7134_capture_close
554 static int dsp_buffer_free(struct saa7134_dev *dev)
556 if (!dev->oss.blksize)
559 videobuf_dma_free(&dev->oss.dma);
562 dev->oss.blksize = 0;
563 dev->oss.bufsize = 0;
569 * ALSA capture finish
571 * - One of the ALSA capture callbacks.
573 * Called after closing the device. It stops the DMA audio and releases
578 static int snd_card_saa7134_capture_close(snd_pcm_substream_t * substream)
580 snd_card_saa7134_t *chip = snd_pcm_substream_chip(substream);
581 struct saa7134_dev *dev = chip->saadev;
585 spin_lock_irqsave(&dev->slock,flags);
586 saa7134_dma_stop(dev);
587 spin_unlock_irqrestore(&dev->slock,flags);
590 saa7134_pgtable_free(dev->pci,&dev->oss.pt);
591 videobuf_dma_pci_unmap(dev->pci,&dev->oss.dma);
593 dsp_buffer_free(dev);
600 * - One of the ALSA capture callbacks.
602 * Called when opening the device. It creates and populates the PCM
607 static int snd_card_saa7134_capture_open(snd_pcm_substream_t * substream)
609 snd_pcm_runtime_t *runtime = substream->runtime;
610 snd_card_saa7134_pcm_t *saapcm;
611 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
612 struct saa7134_dev *dev = saa7134->saadev;
615 down(&dev->oss.lock);
617 dev->oss.afmt = SNDRV_PCM_FORMAT_U8;
618 dev->oss.channels = 2;
619 dev->oss.read_count = 0;
620 dev->oss.read_offset = 0;
624 saapcm = kcalloc(1, sizeof(*saapcm), GFP_KERNEL);
627 saapcm->saadev=saa7134->saadev;
629 spin_lock_init(&saapcm->lock);
631 saapcm->substream = substream;
632 runtime->private_data = saapcm;
633 runtime->private_free = snd_card_saa7134_runtime_free;
634 runtime->hw = snd_card_saa7134_capture;
636 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
643 * ALSA capture callbacks definition
646 static snd_pcm_ops_t snd_card_saa7134_capture_ops = {
647 .open = snd_card_saa7134_capture_open,
648 .close = snd_card_saa7134_capture_close,
649 .ioctl = snd_pcm_lib_ioctl,
650 .hw_params = snd_card_saa7134_hw_params,
651 .hw_free = snd_card_saa7134_hw_free,
652 .prepare = snd_card_saa7134_capture_prepare,
653 .trigger = snd_card_saa7134_capture_trigger,
654 .pointer = snd_card_saa7134_capture_pointer,
660 * Called when initializing the board. Sets up the name and hooks up
665 static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
670 if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
672 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
673 pcm->private_data = saa7134;
675 strcpy(pcm->name, "SAA7134 PCM");
676 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
677 snd_dma_pci_data(saa7134->pci),
682 #define SAA713x_VOLUME(xname, xindex, addr) \
683 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
684 .info = snd_saa7134_volume_info, \
685 .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
686 .private_value = addr }
688 static int snd_saa7134_volume_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
690 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
692 uinfo->value.integer.min = 0;
693 uinfo->value.integer.max = 20;
697 static int snd_saa7134_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
699 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
701 int addr = kcontrol->private_value;
703 spin_lock_irqsave(&chip->mixer_lock, flags);
704 ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
705 ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
706 spin_unlock_irqrestore(&chip->mixer_lock, flags);
710 static int snd_saa7134_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
712 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
714 int change, addr = kcontrol->private_value;
717 left = ucontrol->value.integer.value[0];
722 right = ucontrol->value.integer.value[1];
727 spin_lock_irqsave(&chip->mixer_lock, flags);
728 change = chip->mixer_volume[addr][0] != left ||
729 chip->mixer_volume[addr][1] != right;
730 chip->mixer_volume[addr][0] = left;
731 chip->mixer_volume[addr][1] = right;
732 spin_unlock_irqrestore(&chip->mixer_lock, flags);
736 #define SAA713x_CAPSRC(xname, xindex, addr) \
737 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
738 .info = snd_saa7134_capsrc_info, \
739 .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
740 .private_value = addr }
742 static int snd_saa7134_capsrc_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
744 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
746 uinfo->value.integer.min = 0;
747 uinfo->value.integer.max = 1;
751 static int snd_saa7134_capsrc_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
753 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
755 int addr = kcontrol->private_value;
757 spin_lock_irqsave(&chip->mixer_lock, flags);
758 ucontrol->value.integer.value[0] = chip->capture_source[addr][0];
759 ucontrol->value.integer.value[1] = chip->capture_source[addr][1];
760 spin_unlock_irqrestore(&chip->mixer_lock, flags);
764 static int snd_saa7134_capsrc_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
766 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
768 int change, addr = kcontrol->private_value;
772 struct saa7134_dev *dev;
776 left = ucontrol->value.integer.value[0] & 1;
777 right = ucontrol->value.integer.value[1] & 1;
778 spin_lock_irqsave(&chip->mixer_lock, flags);
780 change = chip->capture_source[addr][0] != left ||
781 chip->capture_source[addr][1] != right;
782 chip->capture_source[addr][0] = left;
783 chip->capture_source[addr][1] = right;
785 spin_unlock_irqrestore(&chip->mixer_lock, flags);
789 switch (dev->pci->device) {
791 case PCI_DEVICE_ID_PHILIPS_SAA7134:
793 case MIXER_ADDR_TVTUNER:
794 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
795 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
797 case MIXER_ADDR_LINE1:
798 case MIXER_ADDR_LINE2:
799 analog_io = (MIXER_ADDR_LINE1 == addr) ? 0x00 : 0x08;
800 rate = (32000 == dev->oss.rate) ? 0x01 : 0x03;
801 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, analog_io);
802 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
803 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, rate);
807 case PCI_DEVICE_ID_PHILIPS_SAA7133:
808 case PCI_DEVICE_ID_PHILIPS_SAA7135:
809 xbarin = 0x03; // adc
812 case MIXER_ADDR_TVTUNER:
813 xbarin = 0; // Demodulator
816 case MIXER_ADDR_LINE1:
817 anabar = 0; // aux1, aux1
819 case MIXER_ADDR_LINE2:
820 anabar = 9; // aux2, aux2
824 /* output xbar always main channel */
825 saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1, 0xbbbb10);
827 if (left || right) { // We've got data, turn the input on
828 //saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL2, 0x101010);
829 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, xbarin);
830 saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
832 //saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL2, 0x101010);
833 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, 0);
834 saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
842 static snd_kcontrol_new_t snd_saa7134_controls[] = {
843 SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
844 SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
845 SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
846 SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
847 SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
848 SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
854 * Called when initializing the board. Sets up the name and hooks up
859 static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
861 snd_card_t *card = chip->card;
865 snd_assert(chip != NULL, return -EINVAL);
866 strcpy(card->mixername, "SAA7134 Mixer");
868 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_controls); idx++) {
869 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_saa7134_controls[idx], chip))) < 0)
875 static int snd_saa7134_free(snd_card_saa7134_t *chip)
880 static int snd_saa7134_dev_free(snd_device_t *device)
882 snd_card_saa7134_t *chip = device->device_data;
883 return snd_saa7134_free(chip);
887 * ALSA initialization
889 * Called by saa7134-core, it creates the basic structures and registers
894 int alsa_card_saa7134_create(struct saa7134_dev *saadev)
898 snd_card_saa7134_t *chip;
900 static snd_device_ops_t ops = {
901 .dev_free = snd_saa7134_dev_free,
904 if (dev >= SNDRV_CARDS)
909 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
914 strcpy(card->driver, "SAA7134");
916 /* Card "creation" */
918 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
923 spin_lock_init(&chip->lock);
925 chip->saadev = saadev;
929 chip->pci = saadev->pci;
930 chip->irq = saadev->pci->irq;
931 chip->iobase = pci_resource_start(saadev->pci, 0);
933 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
934 snd_saa7134_free(chip);
938 if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
941 if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
944 spin_lock_init(&chip->mixer_lock);
946 snd_card_set_dev(card, &chip->pci->dev);
948 /* End of "creation" */
950 strcpy(card->shortname, "SAA7134");
951 sprintf(card->longname, "%s at 0x%lx irq %d",
952 card->shortname, chip->iobase, chip->irq);
954 if ((err = snd_card_register(card)) == 0) {
955 snd_saa7134_cards[dev] = card;
965 void alsa_card_saa7134_exit(void)
968 for (idx = 0; idx < SNDRV_CARDS; idx++) {
969 snd_card_free(snd_saa7134_cards[idx]);