ASoC: es8316: Add jack-detect support
[linux/fpc-iii.git] / drivers / media / pci / saa7134 / saa7134-alsa.c
blobdc9cdaaee1fb80923b04e89d6ef25c185601acb9
1 /*
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.
15 #include "saa7134.h"
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
35 /* defaults */
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).");
53 * Main chip structure
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];
63 struct pci_dev *pci;
64 struct saa7134_dev *dev;
66 unsigned long iobase;
67 s16 irq;
68 u16 mute_was_on;
70 spinlock_t lock;
71 } snd_card_saa7134_t;
75 * PCM structure
78 typedef struct snd_card_saa7134_pcm {
79 struct saa7134_dev *dev;
81 spinlock_t lock;
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");
140 goto done;
142 if (0 != (status & 0x0f000000))
143 pr_debug("irq: lost %ld\n", (status >> 24) & 0x0f);
144 if (0 == (status & 0x10000000)) {
145 /* odd */
146 if (0 == (dev->dmasound.dma_blk & 0x01))
147 reg = SAA7134_RS_BA1(6);
148 } else {
149 /* even */
150 if (1 == (dev->dmasound.dma_blk & 0x01))
151 reg = SAA7134_RS_BA2(6);
153 if (0 == reg) {
154 pr_debug("irq: field oops [%s]\n",
155 (status & 0x10000000) ? "even" : "odd");
156 goto done;
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);
165 return;
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);
188 done:
189 spin_unlock(&dev->slock);
194 * IRQ request handler
196 * Runs along with saa7134's IRQ handler, discards anything that isn't
197 * DMA sound
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) {
214 handled = 1;
215 saa_writel(SAA7134_IRQ_REPORT,
216 SAA7134_IRQ_REPORT_DONE_RA3);
217 saa7134_irq_alsa_done(dev, status);
218 } else {
219 goto out;
223 if (loop == 10) {
224 pr_debug("error! looping IRQ!");
227 out:
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,
242 int cmd)
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;
247 int err = 0;
249 spin_lock(&dev->slock);
250 if (cmd == SNDRV_PCM_TRIGGER_START) {
251 /* start dma */
252 saa7134_dma_start(dev);
253 } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
254 /* stop dma */
255 saa7134_dma_stop(dev);
256 } else {
257 err = -EINVAL;
259 spin_unlock(&dev->slock);
261 return err;
264 static int saa7134_alsa_dma_init(struct saa7134_dev *dev, int nr_pages)
266 struct saa7134_dmasound *dma = &dev->dmasound;
267 struct page *pg;
268 int i;
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);
273 return -ENOMEM;
276 pr_debug("vmalloc is at addr %p, size=%d\n",
277 dma->vaddr, nr_pages << PAGE_SHIFT);
279 memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
280 dma->nr_pages = nr_pages;
282 dma->sglist = vzalloc(array_size(sizeof(*dma->sglist), dma->nr_pages));
283 if (NULL == dma->sglist)
284 goto vzalloc_err;
286 sg_init_table(dma->sglist, dma->nr_pages);
287 for (i = 0; i < dma->nr_pages; i++) {
288 pg = vmalloc_to_page(dma->vaddr + i * PAGE_SIZE);
289 if (NULL == pg)
290 goto vmalloc_to_page_err;
291 sg_set_page(&dma->sglist[i], pg, PAGE_SIZE, 0);
293 return 0;
295 vmalloc_to_page_err:
296 vfree(dma->sglist);
297 dma->sglist = NULL;
298 vzalloc_err:
299 vfree(dma->vaddr);
300 dma->vaddr = NULL;
301 return -ENOMEM;
304 static int saa7134_alsa_dma_map(struct saa7134_dev *dev)
306 struct saa7134_dmasound *dma = &dev->dmasound;
308 dma->sglen = dma_map_sg(&dev->pci->dev, dma->sglist,
309 dma->nr_pages, PCI_DMA_FROMDEVICE);
311 if (0 == dma->sglen) {
312 pr_warn("%s: saa7134_alsa_map_sg failed\n", __func__);
313 return -ENOMEM;
315 return 0;
318 static int saa7134_alsa_dma_unmap(struct saa7134_dev *dev)
320 struct saa7134_dmasound *dma = &dev->dmasound;
322 if (!dma->sglen)
323 return 0;
325 dma_unmap_sg(&dev->pci->dev, dma->sglist, dma->sglen, PCI_DMA_FROMDEVICE);
326 dma->sglen = 0;
327 return 0;
330 static int saa7134_alsa_dma_free(struct saa7134_dmasound *dma)
332 vfree(dma->sglist);
333 dma->sglist = NULL;
334 vfree(dma->vaddr);
335 dma->vaddr = NULL;
336 return 0;
340 * DMA buffer initialization
342 * Uses V4L functions to initialize the DMA. Shouldn't be necessary in
343 * ALSA, but I was unable to use ALSA's own DMA, and had to force the
344 * usage of V4L's
346 * - Copied verbatim from saa7134-oss.
350 static int dsp_buffer_init(struct saa7134_dev *dev)
352 int err;
354 BUG_ON(!dev->dmasound.bufsize);
356 err = saa7134_alsa_dma_init(dev,
357 (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
358 if (0 != err)
359 return err;
360 return 0;
364 * DMA buffer release
366 * Called after closing the device, during snd_card_saa7134_capture_close
370 static int dsp_buffer_free(struct saa7134_dev *dev)
372 BUG_ON(!dev->dmasound.blksize);
374 saa7134_alsa_dma_free(&dev->dmasound);
376 dev->dmasound.blocks = 0;
377 dev->dmasound.blksize = 0;
378 dev->dmasound.bufsize = 0;
380 return 0;
384 * Setting the capture source and updating the ALSA controls
386 static int snd_saa7134_capsrc_set(struct snd_kcontrol *kcontrol,
387 int left, int right, bool force_notify)
389 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
390 int change = 0, addr = kcontrol->private_value;
391 int active, old_addr;
392 u32 anabar, xbarin;
393 int analog_io, rate;
394 struct saa7134_dev *dev;
396 dev = chip->dev;
398 spin_lock_irq(&chip->mixer_lock);
400 active = left != 0 || right != 0;
401 old_addr = chip->capture_source_addr;
403 /* The active capture source cannot be deactivated */
404 if (active) {
405 change = old_addr != addr ||
406 chip->capture_source[0] != left ||
407 chip->capture_source[1] != right;
409 chip->capture_source[0] = left;
410 chip->capture_source[1] = right;
411 chip->capture_source_addr = addr;
412 dev->dmasound.input = addr;
414 spin_unlock_irq(&chip->mixer_lock);
416 if (change) {
417 switch (dev->pci->device) {
419 case PCI_DEVICE_ID_PHILIPS_SAA7134:
420 switch (addr) {
421 case MIXER_ADDR_TVTUNER:
422 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
423 0xc0, 0xc0);
424 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
425 0x03, 0x00);
426 break;
427 case MIXER_ADDR_LINE1:
428 case MIXER_ADDR_LINE2:
429 analog_io = (MIXER_ADDR_LINE1 == addr) ?
430 0x00 : 0x08;
431 rate = (32000 == dev->dmasound.rate) ?
432 0x01 : 0x03;
433 saa_andorb(SAA7134_ANALOG_IO_SELECT,
434 0x08, analog_io);
435 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
436 0xc0, 0x80);
437 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
438 0x03, rate);
439 break;
442 break;
443 case PCI_DEVICE_ID_PHILIPS_SAA7133:
444 case PCI_DEVICE_ID_PHILIPS_SAA7135:
445 xbarin = 0x03; /* adc */
446 anabar = 0;
447 switch (addr) {
448 case MIXER_ADDR_TVTUNER:
449 xbarin = 0; /* Demodulator */
450 anabar = 2; /* DACs */
451 break;
452 case MIXER_ADDR_LINE1:
453 anabar = 0; /* aux1, aux1 */
454 break;
455 case MIXER_ADDR_LINE2:
456 anabar = 9; /* aux2, aux2 */
457 break;
460 /* output xbar always main channel */
461 saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1,
462 0xbbbb10);
464 if (left || right) {
465 /* We've got data, turn the input on */
466 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
467 xbarin);
468 saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
469 } else {
470 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
472 saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
474 break;
478 if (change) {
479 if (force_notify)
480 snd_ctl_notify(chip->card,
481 SNDRV_CTL_EVENT_MASK_VALUE,
482 &chip->capture_ctl[addr]->id);
484 if (old_addr != MIXER_ADDR_UNSELECTED && old_addr != addr)
485 snd_ctl_notify(chip->card,
486 SNDRV_CTL_EVENT_MASK_VALUE,
487 &chip->capture_ctl[old_addr]->id);
490 return change;
494 * ALSA PCM preparation
496 * - One of the ALSA capture callbacks.
498 * Called right after the capture device is opened, this function configures
499 * the buffer using the previously defined functions, allocates the memory,
500 * sets up the hardware registers, and then starts the DMA. When this function
501 * returns, the audio should be flowing.
505 static int snd_card_saa7134_capture_prepare(struct snd_pcm_substream * substream)
507 struct snd_pcm_runtime *runtime = substream->runtime;
508 int bswap, sign;
509 u32 fmt, control;
510 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
511 struct saa7134_dev *dev;
512 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
514 pcm->dev->dmasound.substream = substream;
516 dev = saa7134->dev;
518 if (snd_pcm_format_width(runtime->format) == 8)
519 fmt = 0x00;
520 else
521 fmt = 0x01;
523 if (snd_pcm_format_signed(runtime->format))
524 sign = 1;
525 else
526 sign = 0;
528 if (snd_pcm_format_big_endian(runtime->format))
529 bswap = 1;
530 else
531 bswap = 0;
533 switch (dev->pci->device) {
534 case PCI_DEVICE_ID_PHILIPS_SAA7134:
535 if (1 == runtime->channels)
536 fmt |= (1 << 3);
537 if (2 == runtime->channels)
538 fmt |= (3 << 3);
539 if (sign)
540 fmt |= 0x04;
542 fmt |= (MIXER_ADDR_TVTUNER == dev->dmasound.input) ? 0xc0 : 0x80;
543 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
544 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >> 8);
545 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
546 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
548 break;
549 case PCI_DEVICE_ID_PHILIPS_SAA7133:
550 case PCI_DEVICE_ID_PHILIPS_SAA7135:
551 if (1 == runtime->channels)
552 fmt |= (1 << 4);
553 if (2 == runtime->channels)
554 fmt |= (2 << 4);
555 if (!sign)
556 fmt |= 0x04;
557 saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -1);
558 saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
559 break;
562 pr_debug("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
563 runtime->format, runtime->channels, fmt,
564 bswap ? 'b' : '-');
565 /* dma: setup channel 6 (= AUDIO) */
566 control = SAA7134_RS_CONTROL_BURST_16 |
567 SAA7134_RS_CONTROL_ME |
568 (dev->dmasound.pt.dma >> 12);
569 if (bswap)
570 control |= SAA7134_RS_CONTROL_BSWAP;
572 saa_writel(SAA7134_RS_BA1(6),0);
573 saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
574 saa_writel(SAA7134_RS_PITCH(6),0);
575 saa_writel(SAA7134_RS_CONTROL(6),control);
577 dev->dmasound.rate = runtime->rate;
579 /* Setup and update the card/ALSA controls */
580 snd_saa7134_capsrc_set(saa7134->capture_ctl[dev->dmasound.input], 1, 1,
581 true);
583 return 0;
588 * ALSA pointer fetching
590 * - One of the ALSA capture callbacks.
592 * Called whenever a period elapses, it must return the current hardware
593 * position of the buffer.
594 * Also resets the read counter used to prevent overruns
598 static snd_pcm_uframes_t
599 snd_card_saa7134_capture_pointer(struct snd_pcm_substream * substream)
601 struct snd_pcm_runtime *runtime = substream->runtime;
602 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
603 struct saa7134_dev *dev=pcm->dev;
605 if (dev->dmasound.read_count) {
606 dev->dmasound.read_count -= snd_pcm_lib_period_bytes(substream);
607 dev->dmasound.read_offset += snd_pcm_lib_period_bytes(substream);
608 if (dev->dmasound.read_offset == dev->dmasound.bufsize)
609 dev->dmasound.read_offset = 0;
612 return bytes_to_frames(runtime, dev->dmasound.read_offset);
616 * ALSA hardware capabilities definition
618 * Report only 32kHz for ALSA:
620 * - SAA7133/35 uses DDEP (DemDec Easy Programming mode), which works in 32kHz
621 * only
622 * - SAA7134 for TV mode uses DemDec mode (32kHz)
623 * - Radio works in 32kHz only
624 * - When recording 48kHz from Line1/Line2, switching of capture source to TV
625 * means
626 * switching to 32kHz without any frequency translation
629 static const struct snd_pcm_hardware snd_card_saa7134_capture =
631 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
632 SNDRV_PCM_INFO_BLOCK_TRANSFER |
633 SNDRV_PCM_INFO_MMAP_VALID),
634 .formats = SNDRV_PCM_FMTBIT_S16_LE | \
635 SNDRV_PCM_FMTBIT_S16_BE | \
636 SNDRV_PCM_FMTBIT_S8 | \
637 SNDRV_PCM_FMTBIT_U8 | \
638 SNDRV_PCM_FMTBIT_U16_LE | \
639 SNDRV_PCM_FMTBIT_U16_BE,
640 .rates = SNDRV_PCM_RATE_32000,
641 .rate_min = 32000,
642 .rate_max = 32000,
643 .channels_min = 1,
644 .channels_max = 2,
645 .buffer_bytes_max = (256*1024),
646 .period_bytes_min = 64,
647 .period_bytes_max = (256*1024),
648 .periods_min = 4,
649 .periods_max = 1024,
652 static void snd_card_saa7134_runtime_free(struct snd_pcm_runtime *runtime)
654 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
656 kfree(pcm);
661 * ALSA hardware params
663 * - One of the ALSA capture callbacks.
665 * Called on initialization, right before the PCM preparation
669 static int snd_card_saa7134_hw_params(struct snd_pcm_substream * substream,
670 struct snd_pcm_hw_params * hw_params)
672 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
673 struct saa7134_dev *dev;
674 unsigned int period_size, periods;
675 int err;
677 period_size = params_period_bytes(hw_params);
678 periods = params_periods(hw_params);
680 if (period_size < 0x100 || period_size > 0x10000)
681 return -EINVAL;
682 if (periods < 4)
683 return -EINVAL;
684 if (period_size * periods > 1024 * 1024)
685 return -EINVAL;
687 dev = saa7134->dev;
689 if (dev->dmasound.blocks == periods &&
690 dev->dmasound.blksize == period_size)
691 return 0;
693 /* release the old buffer */
694 if (substream->runtime->dma_area) {
695 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
696 saa7134_alsa_dma_unmap(dev);
697 dsp_buffer_free(dev);
698 substream->runtime->dma_area = NULL;
700 dev->dmasound.blocks = periods;
701 dev->dmasound.blksize = period_size;
702 dev->dmasound.bufsize = period_size * periods;
704 err = dsp_buffer_init(dev);
705 if (0 != err) {
706 dev->dmasound.blocks = 0;
707 dev->dmasound.blksize = 0;
708 dev->dmasound.bufsize = 0;
709 return err;
712 err = saa7134_alsa_dma_map(dev);
713 if (err) {
714 dsp_buffer_free(dev);
715 return err;
717 err = saa7134_pgtable_alloc(dev->pci, &dev->dmasound.pt);
718 if (err) {
719 saa7134_alsa_dma_unmap(dev);
720 dsp_buffer_free(dev);
721 return err;
723 err = saa7134_pgtable_build(dev->pci, &dev->dmasound.pt,
724 dev->dmasound.sglist, dev->dmasound.sglen, 0);
725 if (err) {
726 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
727 saa7134_alsa_dma_unmap(dev);
728 dsp_buffer_free(dev);
729 return err;
732 /* I should be able to use runtime->dma_addr in the control
733 byte, but it doesn't work. So I allocate the DMA using the
734 V4L functions, and force ALSA to use that as the DMA area */
736 substream->runtime->dma_area = dev->dmasound.vaddr;
737 substream->runtime->dma_bytes = dev->dmasound.bufsize;
738 substream->runtime->dma_addr = 0;
740 return 0;
745 * ALSA hardware release
747 * - One of the ALSA capture callbacks.
749 * Called after closing the device, but before snd_card_saa7134_capture_close
750 * It stops the DMA audio and releases the buffers.
754 static int snd_card_saa7134_hw_free(struct snd_pcm_substream * substream)
756 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
757 struct saa7134_dev *dev;
759 dev = saa7134->dev;
761 if (substream->runtime->dma_area) {
762 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
763 saa7134_alsa_dma_unmap(dev);
764 dsp_buffer_free(dev);
765 substream->runtime->dma_area = NULL;
768 return 0;
772 * ALSA capture finish
774 * - One of the ALSA capture callbacks.
776 * Called after closing the device.
780 static int snd_card_saa7134_capture_close(struct snd_pcm_substream * substream)
782 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
783 struct saa7134_dev *dev = saa7134->dev;
785 if (saa7134->mute_was_on) {
786 dev->ctl_mute = 1;
787 saa7134_tvaudio_setmute(dev);
789 return 0;
793 * ALSA capture start
795 * - One of the ALSA capture callbacks.
797 * Called when opening the device. It creates and populates the PCM
798 * structure
802 static int snd_card_saa7134_capture_open(struct snd_pcm_substream * substream)
804 struct snd_pcm_runtime *runtime = substream->runtime;
805 snd_card_saa7134_pcm_t *pcm;
806 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
807 struct saa7134_dev *dev;
808 int amux, err;
810 if (!saa7134) {
811 pr_err("BUG: saa7134 can't find device struct. Can't proceed with open\n");
812 return -ENODEV;
814 dev = saa7134->dev;
815 mutex_lock(&dev->dmasound.lock);
817 dev->dmasound.read_count = 0;
818 dev->dmasound.read_offset = 0;
820 amux = dev->input->amux;
821 if ((amux < 1) || (amux > 3))
822 amux = 1;
823 dev->dmasound.input = amux - 1;
825 mutex_unlock(&dev->dmasound.lock);
827 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
828 if (pcm == NULL)
829 return -ENOMEM;
831 pcm->dev=saa7134->dev;
833 spin_lock_init(&pcm->lock);
835 pcm->substream = substream;
836 runtime->private_data = pcm;
837 runtime->private_free = snd_card_saa7134_runtime_free;
838 runtime->hw = snd_card_saa7134_capture;
840 if (dev->ctl_mute != 0) {
841 saa7134->mute_was_on = 1;
842 dev->ctl_mute = 0;
843 saa7134_tvaudio_setmute(dev);
846 err = snd_pcm_hw_constraint_integer(runtime,
847 SNDRV_PCM_HW_PARAM_PERIODS);
848 if (err < 0)
849 return err;
851 err = snd_pcm_hw_constraint_step(runtime, 0,
852 SNDRV_PCM_HW_PARAM_PERIODS, 2);
853 if (err < 0)
854 return err;
856 return 0;
860 * page callback (needed for mmap)
863 static struct page *snd_card_saa7134_page(struct snd_pcm_substream *substream,
864 unsigned long offset)
866 void *pageptr = substream->runtime->dma_area + offset;
867 return vmalloc_to_page(pageptr);
871 * ALSA capture callbacks definition
874 static const struct snd_pcm_ops snd_card_saa7134_capture_ops = {
875 .open = snd_card_saa7134_capture_open,
876 .close = snd_card_saa7134_capture_close,
877 .ioctl = snd_pcm_lib_ioctl,
878 .hw_params = snd_card_saa7134_hw_params,
879 .hw_free = snd_card_saa7134_hw_free,
880 .prepare = snd_card_saa7134_capture_prepare,
881 .trigger = snd_card_saa7134_capture_trigger,
882 .pointer = snd_card_saa7134_capture_pointer,
883 .page = snd_card_saa7134_page,
887 * ALSA PCM setup
889 * Called when initializing the board. Sets up the name and hooks up
890 * the callbacks
894 static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
896 struct snd_pcm *pcm;
897 int err;
899 if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
900 return err;
901 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
902 pcm->private_data = saa7134;
903 pcm->info_flags = 0;
904 strscpy(pcm->name, "SAA7134 PCM", sizeof(pcm->name));
905 return 0;
908 #define SAA713x_VOLUME(xname, xindex, addr) \
909 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
910 .info = snd_saa7134_volume_info, \
911 .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
912 .private_value = addr }
914 static int snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,
915 struct snd_ctl_elem_info * uinfo)
917 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
918 uinfo->count = 2;
919 uinfo->value.integer.min = 0;
920 uinfo->value.integer.max = 20;
921 return 0;
924 static int snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,
925 struct snd_ctl_elem_value * ucontrol)
927 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
928 int addr = kcontrol->private_value;
930 ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
931 ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
932 return 0;
935 static int snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,
936 struct snd_ctl_elem_value * ucontrol)
938 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
939 struct saa7134_dev *dev = chip->dev;
941 int change, addr = kcontrol->private_value;
942 int left, right;
944 left = ucontrol->value.integer.value[0];
945 if (left < 0)
946 left = 0;
947 if (left > 20)
948 left = 20;
949 right = ucontrol->value.integer.value[1];
950 if (right < 0)
951 right = 0;
952 if (right > 20)
953 right = 20;
954 spin_lock_irq(&chip->mixer_lock);
955 change = 0;
956 if (chip->mixer_volume[addr][0] != left) {
957 change = 1;
958 right = left;
960 if (chip->mixer_volume[addr][1] != right) {
961 change = 1;
962 left = right;
964 if (change) {
965 switch (dev->pci->device) {
966 case PCI_DEVICE_ID_PHILIPS_SAA7134:
967 switch (addr) {
968 case MIXER_ADDR_TVTUNER:
969 left = 20;
970 break;
971 case MIXER_ADDR_LINE1:
972 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
973 (left > 10) ? 0x00 : 0x10);
974 break;
975 case MIXER_ADDR_LINE2:
976 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
977 (left > 10) ? 0x00 : 0x20);
978 break;
980 break;
981 case PCI_DEVICE_ID_PHILIPS_SAA7133:
982 case PCI_DEVICE_ID_PHILIPS_SAA7135:
983 switch (addr) {
984 case MIXER_ADDR_TVTUNER:
985 left = 20;
986 break;
987 case MIXER_ADDR_LINE1:
988 saa_andorb(0x0594, 0x10,
989 (left > 10) ? 0x00 : 0x10);
990 break;
991 case MIXER_ADDR_LINE2:
992 saa_andorb(0x0594, 0x20,
993 (left > 10) ? 0x00 : 0x20);
994 break;
996 break;
998 chip->mixer_volume[addr][0] = left;
999 chip->mixer_volume[addr][1] = right;
1001 spin_unlock_irq(&chip->mixer_lock);
1002 return change;
1005 #define SAA713x_CAPSRC(xname, xindex, addr) \
1006 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1007 .info = snd_saa7134_capsrc_info, \
1008 .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
1009 .private_value = addr }
1011 static int snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,
1012 struct snd_ctl_elem_info * uinfo)
1014 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1015 uinfo->count = 2;
1016 uinfo->value.integer.min = 0;
1017 uinfo->value.integer.max = 1;
1018 return 0;
1021 static int snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,
1022 struct snd_ctl_elem_value * ucontrol)
1024 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
1025 int addr = kcontrol->private_value;
1027 spin_lock_irq(&chip->mixer_lock);
1028 if (chip->capture_source_addr == addr) {
1029 ucontrol->value.integer.value[0] = chip->capture_source[0];
1030 ucontrol->value.integer.value[1] = chip->capture_source[1];
1031 } else {
1032 ucontrol->value.integer.value[0] = 0;
1033 ucontrol->value.integer.value[1] = 0;
1035 spin_unlock_irq(&chip->mixer_lock);
1037 return 0;
1040 static int snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,
1041 struct snd_ctl_elem_value * ucontrol)
1043 int left, right;
1044 left = ucontrol->value.integer.value[0] & 1;
1045 right = ucontrol->value.integer.value[1] & 1;
1047 return snd_saa7134_capsrc_set(kcontrol, left, right, false);
1050 static struct snd_kcontrol_new snd_saa7134_volume_controls[] = {
1051 SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
1052 SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
1053 SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
1056 static struct snd_kcontrol_new snd_saa7134_capture_controls[] = {
1057 SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
1058 SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
1059 SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
1063 * ALSA mixer setup
1065 * Called when initializing the board. Sets up the name and hooks up
1066 * the callbacks
1070 static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
1072 struct snd_card *card = chip->card;
1073 struct snd_kcontrol *kcontrol;
1074 unsigned int idx;
1075 int err, addr;
1077 strscpy(card->mixername, "SAA7134 Mixer", sizeof(card->mixername));
1079 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_volume_controls); idx++) {
1080 kcontrol = snd_ctl_new1(&snd_saa7134_volume_controls[idx],
1081 chip);
1082 err = snd_ctl_add(card, kcontrol);
1083 if (err < 0)
1084 return err;
1087 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_capture_controls); idx++) {
1088 kcontrol = snd_ctl_new1(&snd_saa7134_capture_controls[idx],
1089 chip);
1090 addr = snd_saa7134_capture_controls[idx].private_value;
1091 chip->capture_ctl[addr] = kcontrol;
1092 err = snd_ctl_add(card, kcontrol);
1093 if (err < 0)
1094 return err;
1097 chip->capture_source_addr = MIXER_ADDR_UNSELECTED;
1098 return 0;
1101 static void snd_saa7134_free(struct snd_card * card)
1103 snd_card_saa7134_t *chip = card->private_data;
1105 if (chip->dev->dmasound.priv_data == NULL)
1106 return;
1108 if (chip->irq >= 0)
1109 free_irq(chip->irq, &chip->dev->dmasound);
1111 chip->dev->dmasound.priv_data = NULL;
1116 * ALSA initialization
1118 * Called by the init routine, once for each saa7134 device present,
1119 * it creates the basic structures and registers the ALSA devices
1123 static int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
1126 struct snd_card *card;
1127 snd_card_saa7134_t *chip;
1128 int err;
1131 if (devnum >= SNDRV_CARDS)
1132 return -ENODEV;
1133 if (!enable[devnum])
1134 return -ENODEV;
1136 err = snd_card_new(&dev->pci->dev, index[devnum], id[devnum],
1137 THIS_MODULE, sizeof(snd_card_saa7134_t), &card);
1138 if (err < 0)
1139 return err;
1141 strscpy(card->driver, "SAA7134", sizeof(card->driver));
1143 /* Card "creation" */
1145 card->private_free = snd_saa7134_free;
1146 chip = card->private_data;
1148 spin_lock_init(&chip->lock);
1149 spin_lock_init(&chip->mixer_lock);
1151 chip->dev = dev;
1153 chip->card = card;
1155 chip->pci = dev->pci;
1156 chip->iobase = pci_resource_start(dev->pci, 0);
1159 err = request_irq(dev->pci->irq, saa7134_alsa_irq,
1160 IRQF_SHARED, dev->name,
1161 (void*) &dev->dmasound);
1163 if (err < 0) {
1164 pr_err("%s: can't get IRQ %d for ALSA\n",
1165 dev->name, dev->pci->irq);
1166 goto __nodev;
1169 chip->irq = dev->pci->irq;
1171 mutex_init(&dev->dmasound.lock);
1173 if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
1174 goto __nodev;
1176 if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
1177 goto __nodev;
1179 /* End of "creation" */
1181 strscpy(card->shortname, "SAA7134", sizeof(card->shortname));
1182 sprintf(card->longname, "%s at 0x%lx irq %d",
1183 chip->dev->name, chip->iobase, chip->irq);
1185 pr_info("%s/alsa: %s registered as card %d\n",
1186 dev->name, card->longname, index[devnum]);
1188 if ((err = snd_card_register(card)) == 0) {
1189 snd_saa7134_cards[devnum] = card;
1190 return 0;
1193 __nodev:
1194 snd_card_free(card);
1195 return err;
1199 static int alsa_device_init(struct saa7134_dev *dev)
1201 dev->dmasound.priv_data = dev;
1202 alsa_card_saa7134_create(dev,dev->nr);
1203 return 1;
1206 static int alsa_device_exit(struct saa7134_dev *dev)
1208 if (!snd_saa7134_cards[dev->nr])
1209 return 1;
1211 snd_card_free(snd_saa7134_cards[dev->nr]);
1212 snd_saa7134_cards[dev->nr] = NULL;
1213 return 1;
1217 * Module initializer
1219 * Loops through present saa7134 cards, and assigns an ALSA device
1220 * to each one
1224 static int saa7134_alsa_init(void)
1226 struct saa7134_dev *dev = NULL;
1227 struct list_head *list;
1229 saa7134_dmasound_init = alsa_device_init;
1230 saa7134_dmasound_exit = alsa_device_exit;
1232 pr_info("saa7134 ALSA driver for DMA sound loaded\n");
1234 list_for_each(list,&saa7134_devlist) {
1235 dev = list_entry(list, struct saa7134_dev, devlist);
1236 if (dev->pci->device == PCI_DEVICE_ID_PHILIPS_SAA7130)
1237 pr_info("%s/alsa: %s doesn't support digital audio\n",
1238 dev->name, saa7134_boards[dev->board].name);
1239 else
1240 alsa_device_init(dev);
1243 if (dev == NULL)
1244 pr_info("saa7134 ALSA: no saa7134 cards found\n");
1246 return 0;
1251 * Module destructor
1254 static void saa7134_alsa_exit(void)
1256 int idx;
1258 for (idx = 0; idx < SNDRV_CARDS; idx++) {
1259 if (snd_saa7134_cards[idx])
1260 snd_card_free(snd_saa7134_cards[idx]);
1263 saa7134_dmasound_init = NULL;
1264 saa7134_dmasound_exit = NULL;
1265 pr_info("saa7134 ALSA driver for DMA sound unloaded\n");
1267 return;
1270 /* We initialize this late, to make sure the sound system is up and running */
1271 late_initcall(saa7134_alsa_init);
1272 module_exit(saa7134_alsa_exit);
1273 MODULE_LICENSE("GPL");
1274 MODULE_AUTHOR("Ricardo Cerqueira");