4199fbf9bc44bdfe4220596d3a5534b4cc6bd0ee
[deliverable/linux.git] / drivers / media / pci / saa7134 / saa7134-alsa.c
1 /*
2 * SAA713x ALSA support for V4L
3 *
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
7 *
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.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
18
19 #include "saa7134.h"
20 #include "saa7134-reg.h"
21
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/time.h>
25 #include <linux/wait.h>
26 #include <linux/module.h>
27 #include <sound/core.h>
28 #include <sound/control.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/initval.h>
32 #include <linux/interrupt.h>
33 #include <linux/vmalloc.h>
34
35 /*
36 * Configuration macros
37 */
38
39 /* defaults */
40 #define MIXER_ADDR_UNSELECTED -1
41 #define MIXER_ADDR_TVTUNER 0
42 #define MIXER_ADDR_LINE1 1
43 #define MIXER_ADDR_LINE2 2
44 #define MIXER_ADDR_LAST 2
45
46
47 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
48 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
49 static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
50
51 module_param_array(index, int, NULL, 0444);
52 module_param_array(enable, int, NULL, 0444);
53 MODULE_PARM_DESC(index, "Index value for SAA7134 capture interface(s).");
54 MODULE_PARM_DESC(enable, "Enable (or not) the SAA7134 capture interface(s).");
55
56 /*
57 * Main chip structure
58 */
59
60 typedef struct snd_card_saa7134 {
61 struct snd_card *card;
62 spinlock_t mixer_lock;
63 int mixer_volume[MIXER_ADDR_LAST+1][2];
64 int capture_source_addr;
65 int capture_source[2];
66 struct snd_kcontrol *capture_ctl[MIXER_ADDR_LAST+1];
67 struct pci_dev *pci;
68 struct saa7134_dev *dev;
69
70 unsigned long iobase;
71 s16 irq;
72 u16 mute_was_on;
73
74 spinlock_t lock;
75 } snd_card_saa7134_t;
76
77
78 /*
79 * PCM structure
80 */
81
82 typedef struct snd_card_saa7134_pcm {
83 struct saa7134_dev *dev;
84
85 spinlock_t lock;
86
87 struct snd_pcm_substream *substream;
88 } snd_card_saa7134_pcm_t;
89
90 static struct snd_card *snd_saa7134_cards[SNDRV_CARDS];
91
92
93 /*
94 * saa7134 DMA audio stop
95 *
96 * Called when the capture device is released or the buffer overflows
97 *
98 * - Copied verbatim from saa7134-oss's dsp_dma_stop.
99 *
100 */
101
102 static void saa7134_dma_stop(struct saa7134_dev *dev)
103 {
104 dev->dmasound.dma_blk = -1;
105 dev->dmasound.dma_running = 0;
106 saa7134_set_dmabits(dev);
107 }
108
109 /*
110 * saa7134 DMA audio start
111 *
112 * Called when preparing the capture device for use
113 *
114 * - Copied verbatim from saa7134-oss's dsp_dma_start.
115 *
116 */
117
118 static void saa7134_dma_start(struct saa7134_dev *dev)
119 {
120 dev->dmasound.dma_blk = 0;
121 dev->dmasound.dma_running = 1;
122 saa7134_set_dmabits(dev);
123 }
124
125 /*
126 * saa7134 audio DMA IRQ handler
127 *
128 * Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
129 * Handles shifting between the 2 buffers, manages the read counters,
130 * and notifies ALSA when periods elapse
131 *
132 * - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
133 *
134 */
135
136 static void saa7134_irq_alsa_done(struct saa7134_dev *dev,
137 unsigned long status)
138 {
139 int next_blk, reg = 0;
140
141 spin_lock(&dev->slock);
142 if (UNSET == dev->dmasound.dma_blk) {
143 pr_debug("irq: recording stopped\n");
144 goto done;
145 }
146 if (0 != (status & 0x0f000000))
147 pr_debug("irq: lost %ld\n", (status >> 24) & 0x0f);
148 if (0 == (status & 0x10000000)) {
149 /* odd */
150 if (0 == (dev->dmasound.dma_blk & 0x01))
151 reg = SAA7134_RS_BA1(6);
152 } else {
153 /* even */
154 if (1 == (dev->dmasound.dma_blk & 0x01))
155 reg = SAA7134_RS_BA2(6);
156 }
157 if (0 == reg) {
158 pr_debug("irq: field oops [%s]\n",
159 (status & 0x10000000) ? "even" : "odd");
160 goto done;
161 }
162
163 if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
164 pr_debug("irq: overrun [full=%d/%d] - Blocks in %d\n",dev->dmasound.read_count,
165 dev->dmasound.bufsize, dev->dmasound.blocks);
166 spin_unlock(&dev->slock);
167 snd_pcm_stop_xrun(dev->dmasound.substream);
168 return;
169 }
170
171 /* next block addr */
172 next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
173 saa_writel(reg,next_blk * dev->dmasound.blksize);
174 pr_debug("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
175 (status & 0x10000000) ? "even" : "odd ", next_blk,
176 next_blk * dev->dmasound.blksize, dev->dmasound.blocks, dev->dmasound.blksize, dev->dmasound.read_count);
177
178 /* update status & wake waiting readers */
179 dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
180 dev->dmasound.read_count += dev->dmasound.blksize;
181
182 dev->dmasound.recording_on = reg;
183
184 if (dev->dmasound.read_count >= snd_pcm_lib_period_bytes(dev->dmasound.substream)) {
185 spin_unlock(&dev->slock);
186 snd_pcm_period_elapsed(dev->dmasound.substream);
187 spin_lock(&dev->slock);
188 }
189
190 done:
191 spin_unlock(&dev->slock);
192
193 }
194
195 /*
196 * IRQ request handler
197 *
198 * Runs along with saa7134's IRQ handler, discards anything that isn't
199 * DMA sound
200 *
201 */
202
203 static irqreturn_t saa7134_alsa_irq(int irq, void *dev_id)
204 {
205 struct saa7134_dmasound *dmasound = dev_id;
206 struct saa7134_dev *dev = dmasound->priv_data;
207
208 unsigned long report, status;
209 int loop, handled = 0;
210
211 for (loop = 0; loop < 10; loop++) {
212 report = saa_readl(SAA7134_IRQ_REPORT);
213 status = saa_readl(SAA7134_IRQ_STATUS);
214
215 if (report & SAA7134_IRQ_REPORT_DONE_RA3) {
216 handled = 1;
217 saa_writel(SAA7134_IRQ_REPORT,
218 SAA7134_IRQ_REPORT_DONE_RA3);
219 saa7134_irq_alsa_done(dev, status);
220 } else {
221 goto out;
222 }
223 }
224
225 if (loop == 10) {
226 pr_debug("error! looping IRQ!");
227 }
228
229 out:
230 return IRQ_RETVAL(handled);
231 }
232
233 /*
234 * ALSA capture trigger
235 *
236 * - One of the ALSA capture callbacks.
237 *
238 * Called whenever a capture is started or stopped. Must be defined,
239 * but there's nothing we want to do here
240 *
241 */
242
243 static int snd_card_saa7134_capture_trigger(struct snd_pcm_substream * substream,
244 int cmd)
245 {
246 struct snd_pcm_runtime *runtime = substream->runtime;
247 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
248 struct saa7134_dev *dev=pcm->dev;
249 int err = 0;
250
251 spin_lock(&dev->slock);
252 if (cmd == SNDRV_PCM_TRIGGER_START) {
253 /* start dma */
254 saa7134_dma_start(dev);
255 } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
256 /* stop dma */
257 saa7134_dma_stop(dev);
258 } else {
259 err = -EINVAL;
260 }
261 spin_unlock(&dev->slock);
262
263 return err;
264 }
265
266 static int saa7134_alsa_dma_init(struct saa7134_dev *dev, int nr_pages)
267 {
268 struct saa7134_dmasound *dma = &dev->dmasound;
269 struct page *pg;
270 int i;
271
272 dma->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
273 if (NULL == dma->vaddr) {
274 pr_debug("vmalloc_32(%d pages) failed\n", nr_pages);
275 return -ENOMEM;
276 }
277
278 pr_debug("vmalloc is at addr 0x%08lx, size=%d\n",
279 (unsigned long)dma->vaddr,
280 nr_pages << PAGE_SHIFT);
281
282 memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
283 dma->nr_pages = nr_pages;
284
285 dma->sglist = vzalloc(dma->nr_pages * sizeof(*dma->sglist));
286 if (NULL == dma->sglist)
287 goto vzalloc_err;
288
289 sg_init_table(dma->sglist, dma->nr_pages);
290 for (i = 0; i < dma->nr_pages; i++) {
291 pg = vmalloc_to_page(dma->vaddr + i * PAGE_SIZE);
292 if (NULL == pg)
293 goto vmalloc_to_page_err;
294 sg_set_page(&dma->sglist[i], pg, PAGE_SIZE, 0);
295 }
296 return 0;
297
298 vmalloc_to_page_err:
299 vfree(dma->sglist);
300 dma->sglist = NULL;
301 vzalloc_err:
302 vfree(dma->vaddr);
303 dma->vaddr = NULL;
304 return -ENOMEM;
305 }
306
307 static int saa7134_alsa_dma_map(struct saa7134_dev *dev)
308 {
309 struct saa7134_dmasound *dma = &dev->dmasound;
310
311 dma->sglen = dma_map_sg(&dev->pci->dev, dma->sglist,
312 dma->nr_pages, PCI_DMA_FROMDEVICE);
313
314 if (0 == dma->sglen) {
315 pr_warn("%s: saa7134_alsa_map_sg failed\n", __func__);
316 return -ENOMEM;
317 }
318 return 0;
319 }
320
321 static int saa7134_alsa_dma_unmap(struct saa7134_dev *dev)
322 {
323 struct saa7134_dmasound *dma = &dev->dmasound;
324
325 if (!dma->sglen)
326 return 0;
327
328 dma_unmap_sg(&dev->pci->dev, dma->sglist, dma->sglen, PCI_DMA_FROMDEVICE);
329 dma->sglen = 0;
330 return 0;
331 }
332
333 static int saa7134_alsa_dma_free(struct saa7134_dmasound *dma)
334 {
335 vfree(dma->sglist);
336 dma->sglist = NULL;
337 vfree(dma->vaddr);
338 dma->vaddr = NULL;
339 return 0;
340 }
341
342 /*
343 * DMA buffer initialization
344 *
345 * Uses V4L functions to initialize the DMA. Shouldn't be necessary in
346 * ALSA, but I was unable to use ALSA's own DMA, and had to force the
347 * usage of V4L's
348 *
349 * - Copied verbatim from saa7134-oss.
350 *
351 */
352
353 static int dsp_buffer_init(struct saa7134_dev *dev)
354 {
355 int err;
356
357 BUG_ON(!dev->dmasound.bufsize);
358
359 err = saa7134_alsa_dma_init(dev,
360 (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
361 if (0 != err)
362 return err;
363 return 0;
364 }
365
366 /*
367 * DMA buffer release
368 *
369 * Called after closing the device, during snd_card_saa7134_capture_close
370 *
371 */
372
373 static int dsp_buffer_free(struct saa7134_dev *dev)
374 {
375 BUG_ON(!dev->dmasound.blksize);
376
377 saa7134_alsa_dma_free(&dev->dmasound);
378
379 dev->dmasound.blocks = 0;
380 dev->dmasound.blksize = 0;
381 dev->dmasound.bufsize = 0;
382
383 return 0;
384 }
385
386 /*
387 * Setting the capture source and updating the ALSA controls
388 */
389 static int snd_saa7134_capsrc_set(struct snd_kcontrol *kcontrol,
390 int left, int right, bool force_notify)
391 {
392 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
393 int change = 0, addr = kcontrol->private_value;
394 int active, old_addr;
395 u32 anabar, xbarin;
396 int analog_io, rate;
397 struct saa7134_dev *dev;
398
399 dev = chip->dev;
400
401 spin_lock_irq(&chip->mixer_lock);
402
403 active = left != 0 || right != 0;
404 old_addr = chip->capture_source_addr;
405
406 /* The active capture source cannot be deactivated */
407 if (active) {
408 change = old_addr != addr ||
409 chip->capture_source[0] != left ||
410 chip->capture_source[1] != right;
411
412 chip->capture_source[0] = left;
413 chip->capture_source[1] = right;
414 chip->capture_source_addr = addr;
415 dev->dmasound.input = addr;
416 }
417 spin_unlock_irq(&chip->mixer_lock);
418
419 if (change) {
420 switch (dev->pci->device) {
421
422 case PCI_DEVICE_ID_PHILIPS_SAA7134:
423 switch (addr) {
424 case MIXER_ADDR_TVTUNER:
425 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
426 0xc0, 0xc0);
427 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
428 0x03, 0x00);
429 break;
430 case MIXER_ADDR_LINE1:
431 case MIXER_ADDR_LINE2:
432 analog_io = (MIXER_ADDR_LINE1 == addr) ?
433 0x00 : 0x08;
434 rate = (32000 == dev->dmasound.rate) ?
435 0x01 : 0x03;
436 saa_andorb(SAA7134_ANALOG_IO_SELECT,
437 0x08, analog_io);
438 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
439 0xc0, 0x80);
440 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
441 0x03, rate);
442 break;
443 }
444
445 break;
446 case PCI_DEVICE_ID_PHILIPS_SAA7133:
447 case PCI_DEVICE_ID_PHILIPS_SAA7135:
448 xbarin = 0x03; /* adc */
449 anabar = 0;
450 switch (addr) {
451 case MIXER_ADDR_TVTUNER:
452 xbarin = 0; /* Demodulator */
453 anabar = 2; /* DACs */
454 break;
455 case MIXER_ADDR_LINE1:
456 anabar = 0; /* aux1, aux1 */
457 break;
458 case MIXER_ADDR_LINE2:
459 anabar = 9; /* aux2, aux2 */
460 break;
461 }
462
463 /* output xbar always main channel */
464 saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1,
465 0xbbbb10);
466
467 if (left || right) {
468 /* We've got data, turn the input on */
469 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
470 xbarin);
471 saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
472 } else {
473 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
474 0);
475 saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
476 }
477 break;
478 }
479 }
480
481 if (change) {
482 if (force_notify)
483 snd_ctl_notify(chip->card,
484 SNDRV_CTL_EVENT_MASK_VALUE,
485 &chip->capture_ctl[addr]->id);
486
487 if (old_addr != MIXER_ADDR_UNSELECTED && old_addr != addr)
488 snd_ctl_notify(chip->card,
489 SNDRV_CTL_EVENT_MASK_VALUE,
490 &chip->capture_ctl[old_addr]->id);
491 }
492
493 return change;
494 }
495
496 /*
497 * ALSA PCM preparation
498 *
499 * - One of the ALSA capture callbacks.
500 *
501 * Called right after the capture device is opened, this function configures
502 * the buffer using the previously defined functions, allocates the memory,
503 * sets up the hardware registers, and then starts the DMA. When this function
504 * returns, the audio should be flowing.
505 *
506 */
507
508 static int snd_card_saa7134_capture_prepare(struct snd_pcm_substream * substream)
509 {
510 struct snd_pcm_runtime *runtime = substream->runtime;
511 int bswap, sign;
512 u32 fmt, control;
513 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
514 struct saa7134_dev *dev;
515 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
516
517 pcm->dev->dmasound.substream = substream;
518
519 dev = saa7134->dev;
520
521 if (snd_pcm_format_width(runtime->format) == 8)
522 fmt = 0x00;
523 else
524 fmt = 0x01;
525
526 if (snd_pcm_format_signed(runtime->format))
527 sign = 1;
528 else
529 sign = 0;
530
531 if (snd_pcm_format_big_endian(runtime->format))
532 bswap = 1;
533 else
534 bswap = 0;
535
536 switch (dev->pci->device) {
537 case PCI_DEVICE_ID_PHILIPS_SAA7134:
538 if (1 == runtime->channels)
539 fmt |= (1 << 3);
540 if (2 == runtime->channels)
541 fmt |= (3 << 3);
542 if (sign)
543 fmt |= 0x04;
544
545 fmt |= (MIXER_ADDR_TVTUNER == dev->dmasound.input) ? 0xc0 : 0x80;
546 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
547 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >> 8);
548 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
549 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
550
551 break;
552 case PCI_DEVICE_ID_PHILIPS_SAA7133:
553 case PCI_DEVICE_ID_PHILIPS_SAA7135:
554 if (1 == runtime->channels)
555 fmt |= (1 << 4);
556 if (2 == runtime->channels)
557 fmt |= (2 << 4);
558 if (!sign)
559 fmt |= 0x04;
560 saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -1);
561 saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
562 break;
563 }
564
565 pr_debug("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
566 runtime->format, runtime->channels, fmt,
567 bswap ? 'b' : '-');
568 /* dma: setup channel 6 (= AUDIO) */
569 control = SAA7134_RS_CONTROL_BURST_16 |
570 SAA7134_RS_CONTROL_ME |
571 (dev->dmasound.pt.dma >> 12);
572 if (bswap)
573 control |= SAA7134_RS_CONTROL_BSWAP;
574
575 saa_writel(SAA7134_RS_BA1(6),0);
576 saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
577 saa_writel(SAA7134_RS_PITCH(6),0);
578 saa_writel(SAA7134_RS_CONTROL(6),control);
579
580 dev->dmasound.rate = runtime->rate;
581
582 /* Setup and update the card/ALSA controls */
583 snd_saa7134_capsrc_set(saa7134->capture_ctl[dev->dmasound.input], 1, 1,
584 true);
585
586 return 0;
587
588 }
589
590 /*
591 * ALSA pointer fetching
592 *
593 * - One of the ALSA capture callbacks.
594 *
595 * Called whenever a period elapses, it must return the current hardware
596 * position of the buffer.
597 * Also resets the read counter used to prevent overruns
598 *
599 */
600
601 static snd_pcm_uframes_t
602 snd_card_saa7134_capture_pointer(struct snd_pcm_substream * substream)
603 {
604 struct snd_pcm_runtime *runtime = substream->runtime;
605 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
606 struct saa7134_dev *dev=pcm->dev;
607
608 if (dev->dmasound.read_count) {
609 dev->dmasound.read_count -= snd_pcm_lib_period_bytes(substream);
610 dev->dmasound.read_offset += snd_pcm_lib_period_bytes(substream);
611 if (dev->dmasound.read_offset == dev->dmasound.bufsize)
612 dev->dmasound.read_offset = 0;
613 }
614
615 return bytes_to_frames(runtime, dev->dmasound.read_offset);
616 }
617
618 /*
619 * ALSA hardware capabilities definition
620 *
621 * Report only 32kHz for ALSA:
622 *
623 * - SAA7133/35 uses DDEP (DemDec Easy Programming mode), which works in 32kHz
624 * only
625 * - SAA7134 for TV mode uses DemDec mode (32kHz)
626 * - Radio works in 32kHz only
627 * - When recording 48kHz from Line1/Line2, switching of capture source to TV
628 * means
629 * switching to 32kHz without any frequency translation
630 */
631
632 static struct snd_pcm_hardware snd_card_saa7134_capture =
633 {
634 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
635 SNDRV_PCM_INFO_BLOCK_TRANSFER |
636 SNDRV_PCM_INFO_MMAP_VALID),
637 .formats = SNDRV_PCM_FMTBIT_S16_LE | \
638 SNDRV_PCM_FMTBIT_S16_BE | \
639 SNDRV_PCM_FMTBIT_S8 | \
640 SNDRV_PCM_FMTBIT_U8 | \
641 SNDRV_PCM_FMTBIT_U16_LE | \
642 SNDRV_PCM_FMTBIT_U16_BE,
643 .rates = SNDRV_PCM_RATE_32000,
644 .rate_min = 32000,
645 .rate_max = 32000,
646 .channels_min = 1,
647 .channels_max = 2,
648 .buffer_bytes_max = (256*1024),
649 .period_bytes_min = 64,
650 .period_bytes_max = (256*1024),
651 .periods_min = 4,
652 .periods_max = 1024,
653 };
654
655 static void snd_card_saa7134_runtime_free(struct snd_pcm_runtime *runtime)
656 {
657 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
658
659 kfree(pcm);
660 }
661
662
663 /*
664 * ALSA hardware params
665 *
666 * - One of the ALSA capture callbacks.
667 *
668 * Called on initialization, right before the PCM preparation
669 *
670 */
671
672 static int snd_card_saa7134_hw_params(struct snd_pcm_substream * substream,
673 struct snd_pcm_hw_params * hw_params)
674 {
675 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
676 struct saa7134_dev *dev;
677 unsigned int period_size, periods;
678 int err;
679
680 period_size = params_period_bytes(hw_params);
681 periods = params_periods(hw_params);
682
683 if (period_size < 0x100 || period_size > 0x10000)
684 return -EINVAL;
685 if (periods < 4)
686 return -EINVAL;
687 if (period_size * periods > 1024 * 1024)
688 return -EINVAL;
689
690 dev = saa7134->dev;
691
692 if (dev->dmasound.blocks == periods &&
693 dev->dmasound.blksize == period_size)
694 return 0;
695
696 /* release the old buffer */
697 if (substream->runtime->dma_area) {
698 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
699 saa7134_alsa_dma_unmap(dev);
700 dsp_buffer_free(dev);
701 substream->runtime->dma_area = NULL;
702 }
703 dev->dmasound.blocks = periods;
704 dev->dmasound.blksize = period_size;
705 dev->dmasound.bufsize = period_size * periods;
706
707 err = dsp_buffer_init(dev);
708 if (0 != err) {
709 dev->dmasound.blocks = 0;
710 dev->dmasound.blksize = 0;
711 dev->dmasound.bufsize = 0;
712 return err;
713 }
714
715 err = saa7134_alsa_dma_map(dev);
716 if (err) {
717 dsp_buffer_free(dev);
718 return err;
719 }
720 err = saa7134_pgtable_alloc(dev->pci, &dev->dmasound.pt);
721 if (err) {
722 saa7134_alsa_dma_unmap(dev);
723 dsp_buffer_free(dev);
724 return err;
725 }
726 err = saa7134_pgtable_build(dev->pci, &dev->dmasound.pt,
727 dev->dmasound.sglist, dev->dmasound.sglen, 0);
728 if (err) {
729 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
730 saa7134_alsa_dma_unmap(dev);
731 dsp_buffer_free(dev);
732 return err;
733 }
734
735 /* I should be able to use runtime->dma_addr in the control
736 byte, but it doesn't work. So I allocate the DMA using the
737 V4L functions, and force ALSA to use that as the DMA area */
738
739 substream->runtime->dma_area = dev->dmasound.vaddr;
740 substream->runtime->dma_bytes = dev->dmasound.bufsize;
741 substream->runtime->dma_addr = 0;
742
743 return 0;
744
745 }
746
747 /*
748 * ALSA hardware release
749 *
750 * - One of the ALSA capture callbacks.
751 *
752 * Called after closing the device, but before snd_card_saa7134_capture_close
753 * It stops the DMA audio and releases the buffers.
754 *
755 */
756
757 static int snd_card_saa7134_hw_free(struct snd_pcm_substream * substream)
758 {
759 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
760 struct saa7134_dev *dev;
761
762 dev = saa7134->dev;
763
764 if (substream->runtime->dma_area) {
765 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
766 saa7134_alsa_dma_unmap(dev);
767 dsp_buffer_free(dev);
768 substream->runtime->dma_area = NULL;
769 }
770
771 return 0;
772 }
773
774 /*
775 * ALSA capture finish
776 *
777 * - One of the ALSA capture callbacks.
778 *
779 * Called after closing the device.
780 *
781 */
782
783 static int snd_card_saa7134_capture_close(struct snd_pcm_substream * substream)
784 {
785 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
786 struct saa7134_dev *dev = saa7134->dev;
787
788 if (saa7134->mute_was_on) {
789 dev->ctl_mute = 1;
790 saa7134_tvaudio_setmute(dev);
791 }
792 return 0;
793 }
794
795 /*
796 * ALSA capture start
797 *
798 * - One of the ALSA capture callbacks.
799 *
800 * Called when opening the device. It creates and populates the PCM
801 * structure
802 *
803 */
804
805 static int snd_card_saa7134_capture_open(struct snd_pcm_substream * substream)
806 {
807 struct snd_pcm_runtime *runtime = substream->runtime;
808 snd_card_saa7134_pcm_t *pcm;
809 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
810 struct saa7134_dev *dev;
811 int amux, err;
812
813 if (!saa7134) {
814 pr_err("BUG: saa7134 can't find device struct."
815 " Can't proceed with open\n");
816 return -ENODEV;
817 }
818 dev = saa7134->dev;
819 mutex_lock(&dev->dmasound.lock);
820
821 dev->dmasound.read_count = 0;
822 dev->dmasound.read_offset = 0;
823
824 amux = dev->input->amux;
825 if ((amux < 1) || (amux > 3))
826 amux = 1;
827 dev->dmasound.input = amux - 1;
828
829 mutex_unlock(&dev->dmasound.lock);
830
831 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
832 if (pcm == NULL)
833 return -ENOMEM;
834
835 pcm->dev=saa7134->dev;
836
837 spin_lock_init(&pcm->lock);
838
839 pcm->substream = substream;
840 runtime->private_data = pcm;
841 runtime->private_free = snd_card_saa7134_runtime_free;
842 runtime->hw = snd_card_saa7134_capture;
843
844 if (dev->ctl_mute != 0) {
845 saa7134->mute_was_on = 1;
846 dev->ctl_mute = 0;
847 saa7134_tvaudio_setmute(dev);
848 }
849
850 err = snd_pcm_hw_constraint_integer(runtime,
851 SNDRV_PCM_HW_PARAM_PERIODS);
852 if (err < 0)
853 return err;
854
855 err = snd_pcm_hw_constraint_step(runtime, 0,
856 SNDRV_PCM_HW_PARAM_PERIODS, 2);
857 if (err < 0)
858 return err;
859
860 return 0;
861 }
862
863 /*
864 * page callback (needed for mmap)
865 */
866
867 static struct page *snd_card_saa7134_page(struct snd_pcm_substream *substream,
868 unsigned long offset)
869 {
870 void *pageptr = substream->runtime->dma_area + offset;
871 return vmalloc_to_page(pageptr);
872 }
873
874 /*
875 * ALSA capture callbacks definition
876 */
877
878 static struct snd_pcm_ops snd_card_saa7134_capture_ops = {
879 .open = snd_card_saa7134_capture_open,
880 .close = snd_card_saa7134_capture_close,
881 .ioctl = snd_pcm_lib_ioctl,
882 .hw_params = snd_card_saa7134_hw_params,
883 .hw_free = snd_card_saa7134_hw_free,
884 .prepare = snd_card_saa7134_capture_prepare,
885 .trigger = snd_card_saa7134_capture_trigger,
886 .pointer = snd_card_saa7134_capture_pointer,
887 .page = snd_card_saa7134_page,
888 };
889
890 /*
891 * ALSA PCM setup
892 *
893 * Called when initializing the board. Sets up the name and hooks up
894 * the callbacks
895 *
896 */
897
898 static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
899 {
900 struct snd_pcm *pcm;
901 int err;
902
903 if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
904 return err;
905 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
906 pcm->private_data = saa7134;
907 pcm->info_flags = 0;
908 strcpy(pcm->name, "SAA7134 PCM");
909 return 0;
910 }
911
912 #define SAA713x_VOLUME(xname, xindex, addr) \
913 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
914 .info = snd_saa7134_volume_info, \
915 .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
916 .private_value = addr }
917
918 static int snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,
919 struct snd_ctl_elem_info * uinfo)
920 {
921 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
922 uinfo->count = 2;
923 uinfo->value.integer.min = 0;
924 uinfo->value.integer.max = 20;
925 return 0;
926 }
927
928 static int snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,
929 struct snd_ctl_elem_value * ucontrol)
930 {
931 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
932 int addr = kcontrol->private_value;
933
934 ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
935 ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
936 return 0;
937 }
938
939 static int snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,
940 struct snd_ctl_elem_value * ucontrol)
941 {
942 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
943 struct saa7134_dev *dev = chip->dev;
944
945 int change, addr = kcontrol->private_value;
946 int left, right;
947
948 left = ucontrol->value.integer.value[0];
949 if (left < 0)
950 left = 0;
951 if (left > 20)
952 left = 20;
953 right = ucontrol->value.integer.value[1];
954 if (right < 0)
955 right = 0;
956 if (right > 20)
957 right = 20;
958 spin_lock_irq(&chip->mixer_lock);
959 change = 0;
960 if (chip->mixer_volume[addr][0] != left) {
961 change = 1;
962 right = left;
963 }
964 if (chip->mixer_volume[addr][1] != right) {
965 change = 1;
966 left = right;
967 }
968 if (change) {
969 switch (dev->pci->device) {
970 case PCI_DEVICE_ID_PHILIPS_SAA7134:
971 switch (addr) {
972 case MIXER_ADDR_TVTUNER:
973 left = 20;
974 break;
975 case MIXER_ADDR_LINE1:
976 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
977 (left > 10) ? 0x00 : 0x10);
978 break;
979 case MIXER_ADDR_LINE2:
980 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
981 (left > 10) ? 0x00 : 0x20);
982 break;
983 }
984 break;
985 case PCI_DEVICE_ID_PHILIPS_SAA7133:
986 case PCI_DEVICE_ID_PHILIPS_SAA7135:
987 switch (addr) {
988 case MIXER_ADDR_TVTUNER:
989 left = 20;
990 break;
991 case MIXER_ADDR_LINE1:
992 saa_andorb(0x0594, 0x10,
993 (left > 10) ? 0x00 : 0x10);
994 break;
995 case MIXER_ADDR_LINE2:
996 saa_andorb(0x0594, 0x20,
997 (left > 10) ? 0x00 : 0x20);
998 break;
999 }
1000 break;
1001 }
1002 chip->mixer_volume[addr][0] = left;
1003 chip->mixer_volume[addr][1] = right;
1004 }
1005 spin_unlock_irq(&chip->mixer_lock);
1006 return change;
1007 }
1008
1009 #define SAA713x_CAPSRC(xname, xindex, addr) \
1010 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1011 .info = snd_saa7134_capsrc_info, \
1012 .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
1013 .private_value = addr }
1014
1015 static int snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,
1016 struct snd_ctl_elem_info * uinfo)
1017 {
1018 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1019 uinfo->count = 2;
1020 uinfo->value.integer.min = 0;
1021 uinfo->value.integer.max = 1;
1022 return 0;
1023 }
1024
1025 static int snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,
1026 struct snd_ctl_elem_value * ucontrol)
1027 {
1028 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
1029 int addr = kcontrol->private_value;
1030
1031 spin_lock_irq(&chip->mixer_lock);
1032 if (chip->capture_source_addr == addr) {
1033 ucontrol->value.integer.value[0] = chip->capture_source[0];
1034 ucontrol->value.integer.value[1] = chip->capture_source[1];
1035 } else {
1036 ucontrol->value.integer.value[0] = 0;
1037 ucontrol->value.integer.value[1] = 0;
1038 }
1039 spin_unlock_irq(&chip->mixer_lock);
1040
1041 return 0;
1042 }
1043
1044 static int snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,
1045 struct snd_ctl_elem_value * ucontrol)
1046 {
1047 int left, right;
1048 left = ucontrol->value.integer.value[0] & 1;
1049 right = ucontrol->value.integer.value[1] & 1;
1050
1051 return snd_saa7134_capsrc_set(kcontrol, left, right, false);
1052 }
1053
1054 static struct snd_kcontrol_new snd_saa7134_volume_controls[] = {
1055 SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
1056 SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
1057 SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
1058 };
1059
1060 static struct snd_kcontrol_new snd_saa7134_capture_controls[] = {
1061 SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
1062 SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
1063 SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
1064 };
1065
1066 /*
1067 * ALSA mixer setup
1068 *
1069 * Called when initializing the board. Sets up the name and hooks up
1070 * the callbacks
1071 *
1072 */
1073
1074 static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
1075 {
1076 struct snd_card *card = chip->card;
1077 struct snd_kcontrol *kcontrol;
1078 unsigned int idx;
1079 int err, addr;
1080
1081 strcpy(card->mixername, "SAA7134 Mixer");
1082
1083 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_volume_controls); idx++) {
1084 kcontrol = snd_ctl_new1(&snd_saa7134_volume_controls[idx],
1085 chip);
1086 err = snd_ctl_add(card, kcontrol);
1087 if (err < 0)
1088 return err;
1089 }
1090
1091 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_capture_controls); idx++) {
1092 kcontrol = snd_ctl_new1(&snd_saa7134_capture_controls[idx],
1093 chip);
1094 addr = snd_saa7134_capture_controls[idx].private_value;
1095 chip->capture_ctl[addr] = kcontrol;
1096 err = snd_ctl_add(card, kcontrol);
1097 if (err < 0)
1098 return err;
1099 }
1100
1101 chip->capture_source_addr = MIXER_ADDR_UNSELECTED;
1102 return 0;
1103 }
1104
1105 static void snd_saa7134_free(struct snd_card * card)
1106 {
1107 snd_card_saa7134_t *chip = card->private_data;
1108
1109 if (chip->dev->dmasound.priv_data == NULL)
1110 return;
1111
1112 if (chip->irq >= 0)
1113 free_irq(chip->irq, &chip->dev->dmasound);
1114
1115 chip->dev->dmasound.priv_data = NULL;
1116
1117 }
1118
1119 /*
1120 * ALSA initialization
1121 *
1122 * Called by the init routine, once for each saa7134 device present,
1123 * it creates the basic structures and registers the ALSA devices
1124 *
1125 */
1126
1127 static int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
1128 {
1129
1130 struct snd_card *card;
1131 snd_card_saa7134_t *chip;
1132 int err;
1133
1134
1135 if (devnum >= SNDRV_CARDS)
1136 return -ENODEV;
1137 if (!enable[devnum])
1138 return -ENODEV;
1139
1140 err = snd_card_new(&dev->pci->dev, index[devnum], id[devnum],
1141 THIS_MODULE, sizeof(snd_card_saa7134_t), &card);
1142 if (err < 0)
1143 return err;
1144
1145 strcpy(card->driver, "SAA7134");
1146
1147 /* Card "creation" */
1148
1149 card->private_free = snd_saa7134_free;
1150 chip = card->private_data;
1151
1152 spin_lock_init(&chip->lock);
1153 spin_lock_init(&chip->mixer_lock);
1154
1155 chip->dev = dev;
1156
1157 chip->card = card;
1158
1159 chip->pci = dev->pci;
1160 chip->iobase = pci_resource_start(dev->pci, 0);
1161
1162
1163 err = request_irq(dev->pci->irq, saa7134_alsa_irq,
1164 IRQF_SHARED, dev->name,
1165 (void*) &dev->dmasound);
1166
1167 if (err < 0) {
1168 pr_err("%s: can't get IRQ %d for ALSA\n",
1169 dev->name, dev->pci->irq);
1170 goto __nodev;
1171 }
1172
1173 chip->irq = dev->pci->irq;
1174
1175 mutex_init(&dev->dmasound.lock);
1176
1177 if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
1178 goto __nodev;
1179
1180 if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
1181 goto __nodev;
1182
1183 /* End of "creation" */
1184
1185 strcpy(card->shortname, "SAA7134");
1186 sprintf(card->longname, "%s at 0x%lx irq %d",
1187 chip->dev->name, chip->iobase, chip->irq);
1188
1189 pr_info("%s/alsa: %s registered as card %d\n",dev->name,card->longname,index[devnum]);
1190
1191 if ((err = snd_card_register(card)) == 0) {
1192 snd_saa7134_cards[devnum] = card;
1193 return 0;
1194 }
1195
1196 __nodev:
1197 snd_card_free(card);
1198 return err;
1199 }
1200
1201
1202 static int alsa_device_init(struct saa7134_dev *dev)
1203 {
1204 dev->dmasound.priv_data = dev;
1205 alsa_card_saa7134_create(dev,dev->nr);
1206 return 1;
1207 }
1208
1209 static int alsa_device_exit(struct saa7134_dev *dev)
1210 {
1211
1212 snd_card_free(snd_saa7134_cards[dev->nr]);
1213 snd_saa7134_cards[dev->nr] = NULL;
1214 return 1;
1215 }
1216
1217 /*
1218 * Module initializer
1219 *
1220 * Loops through present saa7134 cards, and assigns an ALSA device
1221 * to each one
1222 *
1223 */
1224
1225 static int saa7134_alsa_init(void)
1226 {
1227 struct saa7134_dev *dev = NULL;
1228 struct list_head *list;
1229
1230 saa7134_dmasound_init = alsa_device_init;
1231 saa7134_dmasound_exit = alsa_device_exit;
1232
1233 pr_info("saa7134 ALSA driver for DMA sound loaded\n");
1234
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);
1240 else
1241 alsa_device_init(dev);
1242 }
1243
1244 if (dev == NULL)
1245 pr_info("saa7134 ALSA: no saa7134 cards found\n");
1246
1247 return 0;
1248
1249 }
1250
1251 /*
1252 * Module destructor
1253 */
1254
1255 static void saa7134_alsa_exit(void)
1256 {
1257 int idx;
1258
1259 for (idx = 0; idx < SNDRV_CARDS; idx++) {
1260 snd_card_free(snd_saa7134_cards[idx]);
1261 }
1262
1263 saa7134_dmasound_init = NULL;
1264 saa7134_dmasound_exit = NULL;
1265 pr_info("saa7134 ALSA driver for DMA sound unloaded\n");
1266
1267 return;
1268 }
1269
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");
This page took 0.06481 seconds and 4 git commands to generate.