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