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