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