[PATCH] v4l: (930) Alsa fixes and improvements
[linux-2.6.git] / drivers / media / video / saa7134 / saa7134-alsa.c
1 /*
2  *   SAA713x ALSA support for V4L
3  *
4  *
5  *   Caveats:
6  *        - Volume doesn't work (it's always at max)
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation, version 2
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  *
21  */
22
23 #include <sound/driver.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/time.h>
27 #include <linux/wait.h>
28 #include <linux/moduleparam.h>
29 #include <linux/module.h>
30 #include <sound/core.h>
31 #include <sound/control.h>
32 #include <sound/pcm.h>
33 #include <sound/initval.h>
34
35 #include "saa7134.h"
36 #include "saa7134-reg.h"
37
38 static unsigned int debug  = 0;
39 module_param(debug, int, 0644);
40 MODULE_PARM_DESC(debug,"enable debug messages [alsa]");
41
42 /*
43  * Configuration macros
44  */
45
46 /* defaults */
47 #define MIXER_ADDR_TVTUNER      0
48 #define MIXER_ADDR_LINE1        1
49 #define MIXER_ADDR_LINE2        2
50 #define MIXER_ADDR_LAST         2
51
52 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
53 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
54 static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
55
56 module_param_array(index, int, NULL, 0444);
57 MODULE_PARM_DESC(index, "Index value for SAA7134 capture interface(s).");
58
59 int position;
60
61 #define dprintk(fmt, arg...)    if (debug) \
62         printk(KERN_DEBUG "%s/alsa: " fmt, dev->name , ## arg)
63
64 /*
65  * Main chip structure
66  */
67 typedef struct snd_card_saa7134 {
68         snd_card_t *card;
69         spinlock_t mixer_lock;
70         int mixer_volume[MIXER_ADDR_LAST+1][2];
71         int capture_source[MIXER_ADDR_LAST+1][2];
72         struct pci_dev *pci;
73         struct saa7134_dev *saadev;
74
75         unsigned long iobase;
76         int irq;
77
78         spinlock_t lock;
79 } snd_card_saa7134_t;
80
81
82
83 /*
84  * PCM structure
85  */
86
87 typedef struct snd_card_saa7134_pcm {
88         struct saa7134_dev *saadev;
89
90         spinlock_t lock;
91         unsigned int pcm_size;          /* buffer size */
92         unsigned int pcm_count;         /* bytes per period */
93         unsigned int pcm_bps;           /* bytes per second */
94         snd_pcm_substream_t *substream;
95 } snd_card_saa7134_pcm_t;
96
97 static snd_card_t *snd_saa7134_cards[SNDRV_CARDS];
98
99
100 /*
101  * saa7134 DMA audio stop
102  *
103  *   Called when the capture device is released or the buffer overflows
104  *
105  *   - Copied verbatim from saa7134-oss's dsp_dma_stop.
106  *
107  */
108
109 static void saa7134_dma_stop(struct saa7134_dev *dev)
110 {
111         dev->dmasound.dma_blk     = -1;
112         dev->dmasound.dma_running = 0;
113         saa7134_set_dmabits(dev);
114 }
115
116 /*
117  * saa7134 DMA audio start
118  *
119  *   Called when preparing the capture device for use
120  *
121  *   - Copied verbatim from saa7134-oss's dsp_dma_start.
122  *
123  */
124
125 static void saa7134_dma_start(struct saa7134_dev *dev)
126 {
127         dev->dmasound.dma_blk     = 0;
128         dev->dmasound.dma_running = 1;
129         saa7134_set_dmabits(dev);
130 }
131
132 /*
133  * saa7134 audio DMA IRQ handler
134  *
135  *   Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
136  *   Handles shifting between the 2 buffers, manages the read counters,
137  *  and notifies ALSA when periods elapse
138  *
139  *   - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
140  *
141  */
142
143 void saa7134_irq_alsa_done(struct saa7134_dev *dev, unsigned long status)
144 {
145         int next_blk, reg = 0;
146
147         spin_lock(&dev->slock);
148         if (UNSET == dev->dmasound.dma_blk) {
149                 dprintk("irq: recording stopped\n");
150                 goto done;
151         }
152         if (0 != (status & 0x0f000000))
153                 dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
154         if (0 == (status & 0x10000000)) {
155                 /* odd */
156                 if (0 == (dev->dmasound.dma_blk & 0x01))
157                         reg = SAA7134_RS_BA1(6);
158         } else {
159                 /* even */
160                 if (1 == (dev->dmasound.dma_blk & 0x01))
161                         reg = SAA7134_RS_BA2(6);
162         }
163         if (0 == reg) {
164                 dprintk("irq: field oops [%s]\n",
165                         (status & 0x10000000) ? "even" : "odd");
166                 goto done;
167         }
168
169         if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
170                 dprintk("irq: overrun [full=%d/%d] - Blocks in %d\n",dev->dmasound.read_count,
171                         dev->dmasound.bufsize, dev->dmasound.blocks);
172                 snd_pcm_stop(dev->dmasound.substream,SNDRV_PCM_STATE_XRUN);
173                 goto done;
174         }
175
176         /* next block addr */
177         next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
178         saa_writel(reg,next_blk * dev->dmasound.blksize);
179         if (debug > 2)
180                 dprintk("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
181                         (status & 0x10000000) ? "even" : "odd ", next_blk,
182                         next_blk * dev->dmasound.blksize, dev->dmasound.blocks, dev->dmasound.blksize, dev->dmasound.read_count);
183
184         /* update status & wake waiting readers */
185         dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
186         dev->dmasound.read_count += dev->dmasound.blksize;
187
188         dev->dmasound.recording_on = reg;
189
190         if (dev->dmasound.read_count >= snd_pcm_lib_period_bytes(dev->dmasound.substream)) {
191                 spin_unlock(&dev->slock);
192                 snd_pcm_period_elapsed(dev->dmasound.substream);
193                 spin_lock(&dev->slock);
194         }
195  done:
196         spin_unlock(&dev->slock);
197
198 }
199
200 /*
201  * IRQ request handler
202  *
203  *   Runs along with saa7134's IRQ handler, discards anything that isn't
204  *   DMA sound
205  *
206  */
207
208 static irqreturn_t saa7134_alsa_irq(int irq, void *dev_id, struct pt_regs *regs)
209 {
210         snd_card_saa7134_t *saa7134 = dev_id;
211         struct saa7134_dev *dev = saa7134->saadev;
212         unsigned long report, status;
213         int loop, handled = 0;
214
215         for (loop = 0; loop < 10; loop++) {
216                 report = saa_readl(SAA7134_IRQ_REPORT);
217                 status = saa_readl(SAA7134_IRQ_STATUS);
218
219                 if (report & SAA7134_IRQ_REPORT_DONE_RA3) {
220                         handled = 1;
221                         saa_writel(SAA7134_IRQ_REPORT,report);
222                         saa7134_irq_alsa_done(dev, status);
223                 } else {
224                         goto out;
225                 }
226         }
227
228         if (loop == 10) {
229                 dprintk("error! looping IRQ!");
230         }
231
232 out:
233         return IRQ_RETVAL(handled);
234 }
235
236 /*
237  * ALSA capture trigger
238  *
239  *   - One of the ALSA capture callbacks.
240  *
241  *   Called whenever a capture is started or stopped. Must be defined,
242  *   but there's nothing we want to do here
243  *
244  */
245
246 static int snd_card_saa7134_capture_trigger(snd_pcm_substream_t * substream,
247                                           int cmd)
248 {
249         snd_pcm_runtime_t *runtime = substream->runtime;
250         snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
251         struct saa7134_dev *dev=saapcm->saadev;
252         int err = 0;
253
254         spin_lock_irq(&dev->slock);
255         if (cmd == SNDRV_PCM_TRIGGER_START) {
256                 /* start dma */
257                 saa7134_dma_start(dev);
258         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
259                 /* stop dma */
260                 saa7134_dma_stop(dev);
261         } else {
262                 err = -EINVAL;
263         }
264         spin_unlock_irq(&dev->slock);
265
266         return err;
267 }
268
269 /*
270  * DMA buffer config
271  *
272  *   Sets the values that will later be used as the size of the buffer,
273  *  size of the fragments, and total number of fragments.
274  *   Must be called during the preparation stage, before memory is
275  *  allocated
276  *
277  *   - Copied verbatim from saa7134-oss.
278  *
279  */
280
281 static int dsp_buffer_conf(struct saa7134_dev *dev, int blksize, int blocks)
282 {
283         if (blksize < 0x100)
284                 blksize = 0x100;
285         if (blksize > 0x10000)
286                 blksize = 0x10000;
287
288         if (blocks < 2)
289                 blocks = 2;
290         if ((blksize * blocks) > 1024*1024)
291                 blocks = 1024*1024 / blksize;
292
293         dev->dmasound.blocks  = blocks;
294         dev->dmasound.blksize = blksize;
295         dev->dmasound.bufsize = blksize * blocks;
296
297         dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
298                 blocks,blksize,blksize * blocks / 1024);
299         return 0;
300 }
301
302 /*
303  * DMA buffer initialization
304  *
305  *   Uses V4L functions to initialize the DMA. Shouldn't be necessary in
306  *  ALSA, but I was unable to use ALSA's own DMA, and had to force the
307  *  usage of V4L's
308  *
309  *   - Copied verbatim from saa7134-oss.
310  *
311  */
312
313 static int dsp_buffer_init(struct saa7134_dev *dev)
314 {
315         int err;
316
317         if (!dev->dmasound.bufsize)
318                 BUG();
319         videobuf_dma_init(&dev->dmasound.dma);
320         err = videobuf_dma_init_kernel(&dev->dmasound.dma, PCI_DMA_FROMDEVICE,
321                                        (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
322         if (0 != err)
323                 return err;
324         return 0;
325 }
326
327 /*
328  * ALSA PCM preparation
329  *
330  *   - One of the ALSA capture callbacks.
331  *
332  *   Called right after the capture device is opened, this function configures
333  *  the buffer using the previously defined functions, allocates the memory,
334  *  sets up the hardware registers, and then starts the DMA. When this function
335  *  returns, the audio should be flowing.
336  *
337  */
338
339 static int snd_card_saa7134_capture_prepare(snd_pcm_substream_t * substream)
340 {
341         snd_pcm_runtime_t *runtime = substream->runtime;
342         int err, bswap, sign;
343         u32 fmt, control;
344         snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
345         struct saa7134_dev *dev;
346         snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
347         unsigned int bps;
348         unsigned long size;
349         unsigned count;
350
351         size = snd_pcm_lib_buffer_bytes(substream);
352         count = snd_pcm_lib_period_bytes(substream);
353
354         saapcm->saadev->dmasound.substream = substream;
355         bps = runtime->rate * runtime->channels;
356         bps *= snd_pcm_format_width(runtime->format);
357         bps /= 8;
358         if (bps <= 0)
359                 return -EINVAL;
360         saapcm->pcm_bps = bps;
361         saapcm->pcm_size = snd_pcm_lib_buffer_bytes(substream);
362         saapcm->pcm_count = snd_pcm_lib_period_bytes(substream);
363
364
365         dev=saa7134->saadev;
366
367         dsp_buffer_conf(dev,saapcm->pcm_count,(saapcm->pcm_size/saapcm->pcm_count));
368
369         err = dsp_buffer_init(dev);
370         if (0 != err)
371                 return err;
372
373         /* prepare buffer */
374         if (0 != (err = videobuf_dma_pci_map(dev->pci,&dev->dmasound.dma)))
375                 return err;
376         if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->dmasound.pt)))
377                 goto fail1;
378         if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->dmasound.pt,
379                                               dev->dmasound.dma.sglist,
380                                               dev->dmasound.dma.sglen,
381                                               0)))
382                 goto fail2;
383
384
385
386         switch (runtime->format) {
387           case SNDRV_PCM_FORMAT_U8:
388           case SNDRV_PCM_FORMAT_S8:
389                 fmt = 0x00;
390                 break;
391           case SNDRV_PCM_FORMAT_U16_LE:
392           case SNDRV_PCM_FORMAT_U16_BE:
393           case SNDRV_PCM_FORMAT_S16_LE:
394           case SNDRV_PCM_FORMAT_S16_BE:
395                 fmt = 0x01;
396                 break;
397           default:
398                 err = -EINVAL;
399                 return 1;
400         }
401
402         switch (runtime->format) {
403           case SNDRV_PCM_FORMAT_S8:
404           case SNDRV_PCM_FORMAT_S16_LE:
405           case SNDRV_PCM_FORMAT_S16_BE:
406                 sign = 1;
407                 break;
408           default:
409                 sign = 0;
410                 break;
411         }
412
413         switch (runtime->format) {
414           case SNDRV_PCM_FORMAT_U16_BE:
415           case SNDRV_PCM_FORMAT_S16_BE:
416                 bswap = 1; break;
417           default:
418                 bswap = 0; break;
419         }
420
421         switch (dev->pci->device) {
422           case PCI_DEVICE_ID_PHILIPS_SAA7134:
423                 if (1 == runtime->channels)
424                         fmt |= (1 << 3);
425                 if (2 == runtime->channels)
426                         fmt |= (3 << 3);
427                 if (sign)
428                         fmt |= 0x04;
429
430                 fmt |= (MIXER_ADDR_TVTUNER == dev->dmasound.input) ? 0xc0 : 0x80;
431                 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
432                 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >>  8);
433                 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
434                 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
435
436                 break;
437           case PCI_DEVICE_ID_PHILIPS_SAA7133:
438           case PCI_DEVICE_ID_PHILIPS_SAA7135:
439                 if (1 == runtime->channels)
440                         fmt |= (1 << 4);
441                 if (2 == runtime->channels)
442                         fmt |= (2 << 4);
443                 if (!sign)
444                         fmt |= 0x04;
445                 saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -1);
446                 saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
447                 //saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210);
448                 break;
449         }
450
451         dprintk("rec_start: afmt=%d ch=%d  =>  fmt=0x%x swap=%c\n",
452                 runtime->format, runtime->channels, fmt,
453                 bswap ? 'b' : '-');
454         /* dma: setup channel 6 (= AUDIO) */
455         control = SAA7134_RS_CONTROL_BURST_16 |
456                 SAA7134_RS_CONTROL_ME |
457                 (dev->dmasound.pt.dma >> 12);
458         if (bswap)
459                 control |= SAA7134_RS_CONTROL_BSWAP;
460
461         /* I should be able to use runtime->dma_addr in the control
462            byte, but it doesn't work. So I allocate the DMA using the
463            V4L functions, and force ALSA to use that as the DMA area */
464
465         runtime->dma_area = dev->dmasound.dma.vmalloc;
466
467         saa_writel(SAA7134_RS_BA1(6),0);
468         saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
469         saa_writel(SAA7134_RS_PITCH(6),0);
470         saa_writel(SAA7134_RS_CONTROL(6),control);
471
472         dev->dmasound.rate = runtime->rate;
473
474         return 0;
475  fail2:
476         saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
477  fail1:
478         videobuf_dma_pci_unmap(dev->pci,&dev->dmasound.dma);
479         return err;
480
481
482 }
483
484 /*
485  * ALSA pointer fetching
486  *
487  *   - One of the ALSA capture callbacks.
488  *
489  *   Called whenever a period elapses, it must return the current hardware
490  *  position of the buffer.
491  *   Also resets the read counter used to prevent overruns
492  *
493  */
494
495 static snd_pcm_uframes_t snd_card_saa7134_capture_pointer(snd_pcm_substream_t * substream)
496 {
497         snd_pcm_runtime_t *runtime = substream->runtime;
498         snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
499         struct saa7134_dev *dev=saapcm->saadev;
500
501
502
503         if (dev->dmasound.read_count) {
504                 dev->dmasound.read_count  -= snd_pcm_lib_period_bytes(substream);
505                 dev->dmasound.read_offset += snd_pcm_lib_period_bytes(substream);
506                 if (dev->dmasound.read_offset == dev->dmasound.bufsize)
507                         dev->dmasound.read_offset = 0;
508         }
509
510         return bytes_to_frames(runtime, dev->dmasound.read_offset);
511 }
512
513 /*
514  * ALSA hardware capabilities definition
515  */
516
517 static snd_pcm_hardware_t snd_card_saa7134_capture =
518 {
519         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
520                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
521                                  SNDRV_PCM_INFO_MMAP_VALID),
522         .formats =              SNDRV_PCM_FMTBIT_S16_LE | \
523                                 SNDRV_PCM_FMTBIT_S16_BE | \
524                                 SNDRV_PCM_FMTBIT_S8 | \
525                                 SNDRV_PCM_FMTBIT_U8 | \
526                                 SNDRV_PCM_FMTBIT_U16_LE | \
527                                 SNDRV_PCM_FMTBIT_U16_BE,
528         .rates =                SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000,
529         .rate_min =             32000,
530         .rate_max =             48000,
531         .channels_min =         1,
532         .channels_max =         2,
533         .buffer_bytes_max =     (256*1024),
534         .period_bytes_min =     64,
535         .period_bytes_max =     (256*1024),
536         .periods_min =          2,
537         .periods_max =          1024,
538 };
539
540 static void snd_card_saa7134_runtime_free(snd_pcm_runtime_t *runtime)
541 {
542         snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
543
544         kfree(saapcm);
545 }
546
547
548 /*
549  * ALSA hardware params
550  *
551  *   - One of the ALSA capture callbacks.
552  *
553  *   Called on initialization, right before the PCM preparation
554  *   Usually used in ALSA to allocate the DMA, but since we don't use the
555  *  ALSA DMA it does nothing
556  *
557  */
558
559 static int snd_card_saa7134_hw_params(snd_pcm_substream_t * substream,
560                                     snd_pcm_hw_params_t * hw_params)
561 {
562         return 0;
563
564 }
565
566 /*
567  * ALSA hardware release
568  *
569  *   - One of the ALSA capture callbacks.
570  *
571  *   Called after closing the device, but before snd_card_saa7134_capture_close
572  *   Usually used in ALSA to free the DMA, but since we don't use the
573  *  ALSA DMA I'm almost sure this isn't necessary.
574  *
575  */
576
577 static int snd_card_saa7134_hw_free(snd_pcm_substream_t * substream)
578 {
579         return 0;
580 }
581
582 /*
583  * DMA buffer release
584  *
585  *   Called after closing the device, during snd_card_saa7134_capture_close
586  *
587  */
588
589 static int dsp_buffer_free(struct saa7134_dev *dev)
590 {
591         if (!dev->dmasound.blksize)
592                 BUG();
593
594         videobuf_dma_free(&dev->dmasound.dma);
595
596         dev->dmasound.blocks  = 0;
597         dev->dmasound.blksize = 0;
598         dev->dmasound.bufsize = 0;
599
600         return 0;
601 }
602
603 /*
604  * ALSA capture finish
605  *
606  *   - One of the ALSA capture callbacks.
607  *
608  *   Called after closing the device. It stops the DMA audio and releases
609  *  the buffers
610  *
611  */
612
613 static int snd_card_saa7134_capture_close(snd_pcm_substream_t * substream)
614 {
615         snd_card_saa7134_t *chip = snd_pcm_substream_chip(substream);
616         struct saa7134_dev *dev = chip->saadev;
617
618         /* unlock buffer */
619         saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
620         videobuf_dma_pci_unmap(dev->pci,&dev->dmasound.dma);
621
622         dsp_buffer_free(dev);
623         return 0;
624 }
625
626 /*
627  * ALSA capture start
628  *
629  *   - One of the ALSA capture callbacks.
630  *
631  *   Called when opening the device. It creates and populates the PCM
632  *  structure
633  *
634  */
635
636 static int snd_card_saa7134_capture_open(snd_pcm_substream_t * substream)
637 {
638         snd_pcm_runtime_t *runtime = substream->runtime;
639         snd_card_saa7134_pcm_t *saapcm;
640         snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
641         struct saa7134_dev *dev = saa7134->saadev;
642         int err;
643
644         down(&dev->dmasound.lock);
645
646         dev->dmasound.afmt        = SNDRV_PCM_FORMAT_U8;
647         dev->dmasound.channels    = 2;
648         dev->dmasound.read_count  = 0;
649         dev->dmasound.read_offset = 0;
650
651         up(&dev->dmasound.lock);
652
653         saapcm = kzalloc(sizeof(*saapcm), GFP_KERNEL);
654         if (saapcm == NULL)
655                 return -ENOMEM;
656         saapcm->saadev=saa7134->saadev;
657
658         spin_lock_init(&saapcm->lock);
659
660         saapcm->substream = substream;
661         runtime->private_data = saapcm;
662         runtime->private_free = snd_card_saa7134_runtime_free;
663         runtime->hw = snd_card_saa7134_capture;
664
665         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
666                 return err;
667
668         return 0;
669 }
670
671 /*
672  * ALSA capture callbacks definition
673  */
674
675 static snd_pcm_ops_t snd_card_saa7134_capture_ops = {
676         .open =                 snd_card_saa7134_capture_open,
677         .close =                snd_card_saa7134_capture_close,
678         .ioctl =                snd_pcm_lib_ioctl,
679         .hw_params =            snd_card_saa7134_hw_params,
680         .hw_free =              snd_card_saa7134_hw_free,
681         .prepare =              snd_card_saa7134_capture_prepare,
682         .trigger =              snd_card_saa7134_capture_trigger,
683         .pointer =              snd_card_saa7134_capture_pointer,
684 };
685
686 /*
687  * ALSA PCM setup
688  *
689  *   Called when initializing the board. Sets up the name and hooks up
690  *  the callbacks
691  *
692  */
693
694 static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
695 {
696         snd_pcm_t *pcm;
697         int err;
698
699         if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
700                 return err;
701         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
702         pcm->private_data = saa7134;
703         pcm->info_flags = 0;
704         strcpy(pcm->name, "SAA7134 PCM");
705         return 0;
706 }
707
708 #define SAA713x_VOLUME(xname, xindex, addr) \
709 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
710   .info = snd_saa7134_volume_info, \
711   .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
712   .private_value = addr }
713
714 static int snd_saa7134_volume_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
715 {
716         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
717         uinfo->count = 2;
718         uinfo->value.integer.min = 0;
719         uinfo->value.integer.max = 20;
720         return 0;
721 }
722
723 static int snd_saa7134_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
724 {
725         snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
726         int addr = kcontrol->private_value;
727
728         ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
729         ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
730         return 0;
731 }
732
733 static int snd_saa7134_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
734 {
735         snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
736         unsigned long flags;
737         int change, addr = kcontrol->private_value;
738         int left, right;
739
740         left = ucontrol->value.integer.value[0];
741         if (left < 0)
742                 left = 0;
743         if (left > 20)
744                 left = 20;
745         right = ucontrol->value.integer.value[1];
746         if (right < 0)
747                 right = 0;
748         if (right > 20)
749                 right = 20;
750         spin_lock_irqsave(&chip->mixer_lock, flags);
751         change = chip->mixer_volume[addr][0] != left ||
752                  chip->mixer_volume[addr][1] != right;
753         chip->mixer_volume[addr][0] = left;
754         chip->mixer_volume[addr][1] = right;
755         spin_unlock_irqrestore(&chip->mixer_lock, flags);
756         return change;
757 }
758
759 #define SAA713x_CAPSRC(xname, xindex, addr) \
760 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
761   .info = snd_saa7134_capsrc_info, \
762   .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
763   .private_value = addr }
764
765 static int snd_saa7134_capsrc_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
766 {
767         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
768         uinfo->count = 2;
769         uinfo->value.integer.min = 0;
770         uinfo->value.integer.max = 1;
771         return 0;
772 }
773
774 static int snd_saa7134_capsrc_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
775 {
776         snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
777         unsigned long flags;
778         int addr = kcontrol->private_value;
779
780         spin_lock_irqsave(&chip->mixer_lock, flags);
781         ucontrol->value.integer.value[0] = chip->capture_source[addr][0];
782         ucontrol->value.integer.value[1] = chip->capture_source[addr][1];
783         spin_unlock_irqrestore(&chip->mixer_lock, flags);
784         return 0;
785 }
786
787 static int snd_saa7134_capsrc_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
788 {
789         snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
790         int change, addr = kcontrol->private_value;
791         int left, right;
792         u32 anabar, xbarin;
793         int analog_io, rate;
794         struct saa7134_dev *dev;
795
796         dev = chip->saadev;
797
798         left = ucontrol->value.integer.value[0] & 1;
799         right = ucontrol->value.integer.value[1] & 1;
800         spin_lock_irq(&chip->mixer_lock);
801
802         change = chip->capture_source[addr][0] != left ||
803                  chip->capture_source[addr][1] != right;
804         chip->capture_source[addr][0] = left;
805         chip->capture_source[addr][1] = right;
806         dev->dmasound.input=addr;
807         spin_unlock_irq(&chip->mixer_lock);
808
809
810         if (change) {
811           switch (dev->pci->device) {
812
813            case PCI_DEVICE_ID_PHILIPS_SAA7134:
814                 switch (addr) {
815                         case MIXER_ADDR_TVTUNER:
816                                 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
817                                 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,   0x03, 0x00);
818                                 break;
819                         case MIXER_ADDR_LINE1:
820                         case MIXER_ADDR_LINE2:
821                                 analog_io = (MIXER_ADDR_LINE1 == addr) ? 0x00 : 0x08;
822                                 rate = (32000 == dev->dmasound.rate) ? 0x01 : 0x03;
823                                 saa_andorb(SAA7134_ANALOG_IO_SELECT,  0x08, analog_io);
824                                 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
825                                 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,   0x03, rate);
826                                 break;
827                 }
828
829                 break;
830            case PCI_DEVICE_ID_PHILIPS_SAA7133:
831            case PCI_DEVICE_ID_PHILIPS_SAA7135:
832                 xbarin = 0x03; // adc
833                 anabar = 0;
834                 switch (addr) {
835                         case MIXER_ADDR_TVTUNER:
836                                 xbarin = 0; // Demodulator
837                                 anabar = 2; // DACs
838                                 break;
839                         case MIXER_ADDR_LINE1:
840                                 anabar = 0;  // aux1, aux1
841                                 break;
842                         case MIXER_ADDR_LINE2:
843                                 anabar = 9;  // aux2, aux2
844                                 break;
845                 }
846
847                 /* output xbar always main channel */
848                 saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1, 0xbbbb10);
849
850                 if (left || right) { // We've got data, turn the input on
851                   saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, xbarin);
852                   saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
853                 } else {
854                   saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, 0);
855                   saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
856                 }
857                 break;
858           }
859         }
860
861         return change;
862 }
863
864 static snd_kcontrol_new_t snd_saa7134_controls[] = {
865 SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
866 SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
867 SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
868 SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
869 SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
870 SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
871 };
872
873 /*
874  * ALSA mixer setup
875  *
876  *   Called when initializing the board. Sets up the name and hooks up
877  *  the callbacks
878  *
879  */
880
881 static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
882 {
883         snd_card_t *card = chip->card;
884         unsigned int idx;
885         int err;
886
887         snd_assert(chip != NULL, return -EINVAL);
888         strcpy(card->mixername, "SAA7134 Mixer");
889
890         for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_controls); idx++) {
891                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_saa7134_controls[idx], chip))) < 0)
892                         return err;
893         }
894         return 0;
895 }
896
897 static void snd_saa7134_free(snd_card_t * card)
898 {
899         return;
900 }
901
902 static int snd_saa7134_dev_free(snd_device_t *device)
903 {
904         snd_card_saa7134_t *chip = device->device_data;
905
906         if (chip->irq >= 0) {
907                 synchronize_irq(chip->irq);
908                 free_irq(chip->irq, (void *) chip);
909         }
910
911         return 0;
912 }
913
914 /*
915  * ALSA initialization
916  *
917  *   Called by the init routine, once for each saa7134 device present,
918  *  it creates the basic structures and registers the ALSA devices
919  *
920  */
921
922 int alsa_card_saa7134_create(struct saa7134_dev *saadev, int dev)
923 {
924
925         snd_card_t *card;
926         snd_card_saa7134_t *chip;
927         int err;
928         static snd_device_ops_t ops = {
929                 .dev_free =     snd_saa7134_dev_free,
930         };
931
932
933         if (dev >= SNDRV_CARDS)
934                 return -ENODEV;
935         if (!enable[dev])
936                 return -ENODEV;
937
938         card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(snd_card_saa7134_t));
939
940         if (card == NULL)
941                 return -ENOMEM;
942
943         strcpy(card->driver, "SAA7134");
944
945         /* Card "creation" */
946
947         card->private_free = snd_saa7134_free;
948         chip = (snd_card_saa7134_t *) card->private_data;
949
950         spin_lock_init(&chip->lock);
951         spin_lock_init(&chip->mixer_lock);
952
953         chip->saadev = saadev;
954
955         chip->card = card;
956
957         chip->pci = saadev->pci;
958         chip->irq = saadev->pci->irq;
959         chip->iobase = pci_resource_start(saadev->pci, 0);
960
961         err = request_irq(saadev->pci->irq, saa7134_alsa_irq,
962                                 SA_SHIRQ | SA_INTERRUPT, saadev->name, (void *)chip);
963
964         if (err < 0) {
965                 printk(KERN_ERR "%s: can't get IRQ %d for ALSA\n",
966                         saadev->name, saadev->pci->irq);
967                 goto __nodev;
968         }
969
970         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
971                 goto __nodev;
972         }
973
974         if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
975                 goto __nodev;
976
977         if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
978                 goto __nodev;
979
980         snd_card_set_dev(card, &chip->pci->dev);
981
982         /* End of "creation" */
983
984         strcpy(card->shortname, "SAA7134");
985         sprintf(card->longname, "%s at 0x%lx irq %d",
986                 chip->saadev->name, chip->iobase, chip->irq);
987
988         if ((err = snd_card_register(card)) == 0) {
989                 snd_saa7134_cards[dev] = card;
990                 return 0;
991         }
992
993 __nodev:
994         snd_card_free(card);
995         return err;
996 }
997
998 /*
999  * Module initializer
1000  *
1001  * Loops through present saa7134 cards, and assigns an ALSA device
1002  * to each one
1003  *
1004  */
1005
1006 static int saa7134_alsa_init(void)
1007 {
1008         struct saa7134_dev *saadev = NULL;
1009         struct list_head *list;
1010
1011         position = 0;
1012
1013         printk(KERN_INFO "saa7134 ALSA driver for DMA sound loaded\n");
1014
1015         list_for_each(list,&saa7134_devlist) {
1016                 saadev = list_entry(list, struct saa7134_dev, devlist);
1017                 alsa_card_saa7134_create(saadev,position);
1018                 position++;
1019         }
1020
1021         if (saadev == NULL)
1022                 printk(KERN_INFO "saa7134 ALSA: no saa7134 cards found\n");
1023
1024         return 0;
1025 }
1026
1027 /*
1028  * Module destructor
1029  */
1030
1031 void saa7134_alsa_exit(void)
1032 {
1033         int idx;
1034
1035         for (idx = 0; idx < SNDRV_CARDS; idx++) {
1036                 snd_card_free(snd_saa7134_cards[idx]);
1037         }
1038
1039         printk(KERN_INFO "saa7134 ALSA driver for DMA sound unloaded\n");
1040
1041         return;
1042 }
1043
1044 module_init(saa7134_alsa_init);
1045 module_exit(saa7134_alsa_exit);
1046 MODULE_LICENSE("GPL");
1047 MODULE_AUTHOR("Ricardo Cerqueira");