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