Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / pci / cx25821 / cx25821-alsa.c
bloba45bf0331eebf7f8181635e1df6ffe133ed50fde
1 /*
2 * Driver for the Conexant CX25821 PCIe bridge
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 * Based on SAA713x ALSA driver and CX88 driver
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, version 2
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/device.h>
24 #include <linux/interrupt.h>
25 #include <linux/vmalloc.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/pci.h>
28 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <sound/core.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/control.h>
35 #include <sound/initval.h>
36 #include <sound/tlv.h>
38 #include "cx25821.h"
39 #include "cx25821-reg.h"
41 #define AUDIO_SRAM_CHANNEL SRAM_CH08
43 #define dprintk(level, fmt, arg...) \
44 do { \
45 if (debug >= level) \
46 pr_info("%s/1: " fmt, chip->dev->name, ##arg); \
47 } while (0)
48 #define dprintk_core(level, fmt, arg...) \
49 do { \
50 if (debug >= level) \
51 printk(KERN_DEBUG "%s/1: " fmt, chip->dev->name, ##arg); \
52 } while (0)
54 /****************************************************************************
55 Data type declarations - Can be moded to a header file later
56 ****************************************************************************/
58 static int devno;
60 struct cx25821_audio_buffer {
61 unsigned int bpl;
62 struct cx25821_riscmem risc;
63 void *vaddr;
64 struct scatterlist *sglist;
65 int sglen;
66 int nr_pages;
69 struct cx25821_audio_dev {
70 struct cx25821_dev *dev;
71 struct cx25821_dmaqueue q;
73 /* pci i/o */
74 struct pci_dev *pci;
76 /* audio controls */
77 int irq;
79 struct snd_card *card;
81 unsigned long iobase;
82 spinlock_t reg_lock;
83 atomic_t count;
85 unsigned int dma_size;
86 unsigned int period_size;
87 unsigned int num_periods;
89 struct cx25821_audio_buffer *buf;
91 struct snd_pcm_substream *substream;
95 /****************************************************************************
96 Module global static vars
97 ****************************************************************************/
99 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
100 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
101 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
103 module_param_array(enable, bool, NULL, 0444);
104 MODULE_PARM_DESC(enable, "Enable cx25821 soundcard. default enabled.");
106 module_param_array(index, int, NULL, 0444);
107 MODULE_PARM_DESC(index, "Index value for cx25821 capture interface(s).");
109 /****************************************************************************
110 Module macros
111 ****************************************************************************/
113 MODULE_DESCRIPTION("ALSA driver module for cx25821 based capture cards");
114 MODULE_AUTHOR("Hiep Huynh");
115 MODULE_LICENSE("GPL");
116 MODULE_SUPPORTED_DEVICE("{{Conexant,25821}"); /* "{{Conexant,23881}," */
118 static unsigned int debug;
119 module_param(debug, int, 0644);
120 MODULE_PARM_DESC(debug, "enable debug messages");
122 /****************************************************************************
123 Module specific funtions
124 ****************************************************************************/
125 /* Constants taken from cx88-reg.h */
126 #define AUD_INT_DN_RISCI1 (1 << 0)
127 #define AUD_INT_UP_RISCI1 (1 << 1)
128 #define AUD_INT_RDS_DN_RISCI1 (1 << 2)
129 #define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */
130 #define AUD_INT_UP_RISCI2 (1 << 5)
131 #define AUD_INT_RDS_DN_RISCI2 (1 << 6)
132 #define AUD_INT_DN_SYNC (1 << 12)
133 #define AUD_INT_UP_SYNC (1 << 13)
134 #define AUD_INT_RDS_DN_SYNC (1 << 14)
135 #define AUD_INT_OPC_ERR (1 << 16)
136 #define AUD_INT_BER_IRQ (1 << 20)
137 #define AUD_INT_MCHG_IRQ (1 << 21)
138 #define GP_COUNT_CONTROL_RESET 0x3
140 #define PCI_MSK_AUD_EXT (1 << 4)
141 #define PCI_MSK_AUD_INT (1 << 3)
143 static int cx25821_alsa_dma_init(struct cx25821_audio_dev *chip, int nr_pages)
145 struct cx25821_audio_buffer *buf = chip->buf;
146 struct page *pg;
147 int i;
149 buf->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
150 if (NULL == buf->vaddr) {
151 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
152 return -ENOMEM;
155 dprintk(1, "vmalloc is at addr 0x%p, size=%d\n",
156 buf->vaddr,
157 nr_pages << PAGE_SHIFT);
159 memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT);
160 buf->nr_pages = nr_pages;
162 buf->sglist = vzalloc(buf->nr_pages * sizeof(*buf->sglist));
163 if (NULL == buf->sglist)
164 goto vzalloc_err;
166 sg_init_table(buf->sglist, buf->nr_pages);
167 for (i = 0; i < buf->nr_pages; i++) {
168 pg = vmalloc_to_page(buf->vaddr + i * PAGE_SIZE);
169 if (NULL == pg)
170 goto vmalloc_to_page_err;
171 sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0);
173 return 0;
175 vmalloc_to_page_err:
176 vfree(buf->sglist);
177 buf->sglist = NULL;
178 vzalloc_err:
179 vfree(buf->vaddr);
180 buf->vaddr = NULL;
181 return -ENOMEM;
184 static int cx25821_alsa_dma_map(struct cx25821_audio_dev *dev)
186 struct cx25821_audio_buffer *buf = dev->buf;
188 buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist,
189 buf->nr_pages, PCI_DMA_FROMDEVICE);
191 if (0 == buf->sglen) {
192 pr_warn("%s: cx25821_alsa_map_sg failed\n", __func__);
193 return -ENOMEM;
195 return 0;
198 static int cx25821_alsa_dma_unmap(struct cx25821_audio_dev *dev)
200 struct cx25821_audio_buffer *buf = dev->buf;
202 if (!buf->sglen)
203 return 0;
205 dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->sglen, PCI_DMA_FROMDEVICE);
206 buf->sglen = 0;
207 return 0;
210 static int cx25821_alsa_dma_free(struct cx25821_audio_buffer *buf)
212 vfree(buf->sglist);
213 buf->sglist = NULL;
214 vfree(buf->vaddr);
215 buf->vaddr = NULL;
216 return 0;
220 * BOARD Specific: Sets audio DMA
223 static int _cx25821_start_audio_dma(struct cx25821_audio_dev *chip)
225 struct cx25821_audio_buffer *buf = chip->buf;
226 struct cx25821_dev *dev = chip->dev;
227 const struct sram_channel *audio_ch =
228 &cx25821_sram_channels[AUDIO_SRAM_CHANNEL];
229 u32 tmp = 0;
231 /* enable output on the GPIO 0 for the MCLK ADC (Audio) */
232 cx25821_set_gpiopin_direction(chip->dev, 0, 0);
234 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
235 cx_clear(AUD_INT_DMA_CTL,
236 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
238 /* setup fifo + format - out channel */
239 cx25821_sram_channel_setup_audio(chip->dev, audio_ch, buf->bpl,
240 buf->risc.dma);
242 /* sets bpl size */
243 cx_write(AUD_A_LNGTH, buf->bpl);
245 /* reset counter */
246 /* GP_COUNT_CONTROL_RESET = 0x3 */
247 cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
248 atomic_set(&chip->count, 0);
250 /* Set the input mode to 16-bit */
251 tmp = cx_read(AUD_A_CFG);
252 cx_write(AUD_A_CFG, tmp | FLD_AUD_DST_PK_MODE | FLD_AUD_DST_ENABLE |
253 FLD_AUD_CLK_ENABLE);
256 pr_info("DEBUG: Start audio DMA, %d B/line, cmds_start(0x%x)= %d lines/FIFO, %d periods, %d byte buffer\n",
257 buf->bpl, audio_ch->cmds_start,
258 cx_read(audio_ch->cmds_start + 12)>>1,
259 chip->num_periods, buf->bpl * chip->num_periods);
262 /* Enables corresponding bits at AUD_INT_STAT */
263 cx_write(AUD_A_INT_MSK, FLD_AUD_DST_RISCI1 | FLD_AUD_DST_OF |
264 FLD_AUD_DST_SYNC | FLD_AUD_DST_OPC_ERR);
266 /* Clean any pending interrupt bits already set */
267 cx_write(AUD_A_INT_STAT, ~0);
269 /* enable audio irqs */
270 cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
272 /* Turn on audio downstream fifo and risc enable 0x101 */
273 tmp = cx_read(AUD_INT_DMA_CTL);
274 cx_set(AUD_INT_DMA_CTL, tmp |
275 (FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN));
277 mdelay(100);
278 return 0;
282 * BOARD Specific: Resets audio DMA
284 static int _cx25821_stop_audio_dma(struct cx25821_audio_dev *chip)
286 struct cx25821_dev *dev = chip->dev;
288 /* stop dma */
289 cx_clear(AUD_INT_DMA_CTL,
290 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
292 /* disable irqs */
293 cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
294 cx_clear(AUD_A_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
295 AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
297 return 0;
300 #define MAX_IRQ_LOOP 50
303 * BOARD Specific: IRQ dma bits
305 static char *cx25821_aud_irqs[32] = {
306 "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */
307 NULL, /* reserved */
308 "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */
309 NULL, /* reserved */
310 "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */
311 NULL, /* reserved */
312 "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */
313 NULL, /* reserved */
314 "opc_err", "par_err", "rip_err", /* 16-18 */
315 "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */
319 * BOARD Specific: Threats IRQ audio specific calls
321 static void cx25821_aud_irq(struct cx25821_audio_dev *chip, u32 status,
322 u32 mask)
324 struct cx25821_dev *dev = chip->dev;
326 if (0 == (status & mask))
327 return;
329 cx_write(AUD_A_INT_STAT, status);
330 if (debug > 1 || (status & mask & ~0xff))
331 cx25821_print_irqbits(dev->name, "irq aud", cx25821_aud_irqs,
332 ARRAY_SIZE(cx25821_aud_irqs), status, mask);
334 /* risc op code error */
335 if (status & AUD_INT_OPC_ERR) {
336 pr_warn("WARNING %s/1: Audio risc op code error\n", dev->name);
338 cx_clear(AUD_INT_DMA_CTL,
339 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
340 cx25821_sram_channel_dump_audio(dev,
341 &cx25821_sram_channels[AUDIO_SRAM_CHANNEL]);
343 if (status & AUD_INT_DN_SYNC) {
344 pr_warn("WARNING %s: Downstream sync error!\n", dev->name);
345 cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
346 return;
349 /* risc1 downstream */
350 if (status & AUD_INT_DN_RISCI1) {
351 atomic_set(&chip->count, cx_read(AUD_A_GPCNT));
352 snd_pcm_period_elapsed(chip->substream);
357 * BOARD Specific: Handles IRQ calls
359 static irqreturn_t cx25821_irq(int irq, void *dev_id)
361 struct cx25821_audio_dev *chip = dev_id;
362 struct cx25821_dev *dev = chip->dev;
363 u32 status, pci_status;
364 u32 audint_status, audint_mask;
365 int loop, handled = 0;
367 audint_status = cx_read(AUD_A_INT_STAT);
368 audint_mask = cx_read(AUD_A_INT_MSK);
369 status = cx_read(PCI_INT_STAT);
371 for (loop = 0; loop < 1; loop++) {
372 status = cx_read(PCI_INT_STAT);
373 if (0 == status) {
374 status = cx_read(PCI_INT_STAT);
375 audint_status = cx_read(AUD_A_INT_STAT);
376 audint_mask = cx_read(AUD_A_INT_MSK);
378 if (status) {
379 handled = 1;
380 cx_write(PCI_INT_STAT, status);
382 cx25821_aud_irq(chip, audint_status,
383 audint_mask);
384 break;
385 } else {
386 goto out;
390 handled = 1;
391 cx_write(PCI_INT_STAT, status);
393 cx25821_aud_irq(chip, audint_status, audint_mask);
396 pci_status = cx_read(PCI_INT_STAT);
398 if (handled)
399 cx_write(PCI_INT_STAT, pci_status);
401 out:
402 return IRQ_RETVAL(handled);
405 static int dsp_buffer_free(struct cx25821_audio_dev *chip)
407 struct cx25821_riscmem *risc = &chip->buf->risc;
409 BUG_ON(!chip->dma_size);
411 dprintk(2, "Freeing buffer\n");
412 cx25821_alsa_dma_unmap(chip);
413 cx25821_alsa_dma_free(chip->buf);
414 pci_free_consistent(chip->pci, risc->size, risc->cpu, risc->dma);
415 kfree(chip->buf);
417 chip->buf = NULL;
418 chip->dma_size = 0;
420 return 0;
423 /****************************************************************************
424 ALSA PCM Interface
425 ****************************************************************************/
428 * Digital hardware definition
430 #define DEFAULT_FIFO_SIZE 384
431 static const struct snd_pcm_hardware snd_cx25821_digital_hw = {
432 .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
433 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID,
434 .formats = SNDRV_PCM_FMTBIT_S16_LE,
436 .rates = SNDRV_PCM_RATE_48000,
437 .rate_min = 48000,
438 .rate_max = 48000,
439 .channels_min = 2,
440 .channels_max = 2,
441 /* Analog audio output will be full of clicks and pops if there
442 are not exactly four lines in the SRAM FIFO buffer. */
443 .period_bytes_min = DEFAULT_FIFO_SIZE / 3,
444 .period_bytes_max = DEFAULT_FIFO_SIZE / 3,
445 .periods_min = 1,
446 .periods_max = AUDIO_LINE_SIZE,
447 /* 128 * 128 = 16384 = 1024 * 16 */
448 .buffer_bytes_max = (AUDIO_LINE_SIZE * AUDIO_LINE_SIZE),
452 * audio pcm capture open callback
454 static int snd_cx25821_pcm_open(struct snd_pcm_substream *substream)
456 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
457 struct snd_pcm_runtime *runtime = substream->runtime;
458 int err;
459 unsigned int bpl = 0;
461 if (!chip) {
462 pr_err("DEBUG: cx25821 can't find device struct. Can't proceed with open\n");
463 return -ENODEV;
466 err = snd_pcm_hw_constraint_pow2(runtime, 0,
467 SNDRV_PCM_HW_PARAM_PERIODS);
468 if (err < 0)
469 goto _error;
471 chip->substream = substream;
473 runtime->hw = snd_cx25821_digital_hw;
475 if (cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
476 DEFAULT_FIFO_SIZE) {
477 /* since there are 3 audio Clusters */
478 bpl = cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 3;
479 bpl &= ~7; /* must be multiple of 8 */
481 if (bpl > AUDIO_LINE_SIZE)
482 bpl = AUDIO_LINE_SIZE;
484 runtime->hw.period_bytes_min = bpl;
485 runtime->hw.period_bytes_max = bpl;
488 return 0;
489 _error:
490 dprintk(1, "Error opening PCM!\n");
491 return err;
495 * audio close callback
497 static int snd_cx25821_close(struct snd_pcm_substream *substream)
499 return 0;
503 * hw_params callback
505 static int snd_cx25821_hw_params(struct snd_pcm_substream *substream,
506 struct snd_pcm_hw_params *hw_params)
508 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
509 struct cx25821_audio_buffer *buf;
510 int ret;
512 if (substream->runtime->dma_area) {
513 dsp_buffer_free(chip);
514 substream->runtime->dma_area = NULL;
517 chip->period_size = params_period_bytes(hw_params);
518 chip->num_periods = params_periods(hw_params);
519 chip->dma_size = chip->period_size * params_periods(hw_params);
521 BUG_ON(!chip->dma_size);
522 BUG_ON(chip->num_periods & (chip->num_periods - 1));
524 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
525 if (NULL == buf)
526 return -ENOMEM;
528 if (chip->period_size > AUDIO_LINE_SIZE)
529 chip->period_size = AUDIO_LINE_SIZE;
531 buf->bpl = chip->period_size;
532 chip->buf = buf;
534 ret = cx25821_alsa_dma_init(chip,
535 (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
536 if (ret < 0)
537 goto error;
539 ret = cx25821_alsa_dma_map(chip);
540 if (ret < 0)
541 goto error;
543 ret = cx25821_risc_databuffer_audio(chip->pci, &buf->risc, buf->sglist,
544 chip->period_size, chip->num_periods, 1);
545 if (ret < 0) {
546 pr_info("DEBUG: ERROR after cx25821_risc_databuffer_audio()\n");
547 goto error;
550 /* Loop back to start of program */
551 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
552 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
553 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
555 substream->runtime->dma_area = chip->buf->vaddr;
556 substream->runtime->dma_bytes = chip->dma_size;
557 substream->runtime->dma_addr = 0;
559 return 0;
561 error:
562 chip->buf = NULL;
563 kfree(buf);
564 return ret;
568 * hw free callback
570 static int snd_cx25821_hw_free(struct snd_pcm_substream *substream)
572 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
574 if (substream->runtime->dma_area) {
575 dsp_buffer_free(chip);
576 substream->runtime->dma_area = NULL;
579 return 0;
583 * prepare callback
585 static int snd_cx25821_prepare(struct snd_pcm_substream *substream)
587 return 0;
591 * trigger callback
593 static int snd_cx25821_card_trigger(struct snd_pcm_substream *substream,
594 int cmd)
596 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
597 int err = 0;
599 /* Local interrupts are already disabled by ALSA */
600 spin_lock(&chip->reg_lock);
602 switch (cmd) {
603 case SNDRV_PCM_TRIGGER_START:
604 err = _cx25821_start_audio_dma(chip);
605 break;
606 case SNDRV_PCM_TRIGGER_STOP:
607 err = _cx25821_stop_audio_dma(chip);
608 break;
609 default:
610 err = -EINVAL;
611 break;
614 spin_unlock(&chip->reg_lock);
616 return err;
620 * pointer callback
622 static snd_pcm_uframes_t snd_cx25821_pointer(struct snd_pcm_substream
623 *substream)
625 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
626 struct snd_pcm_runtime *runtime = substream->runtime;
627 u16 count;
629 count = atomic_read(&chip->count);
631 return runtime->period_size * (count & (runtime->periods - 1));
635 * page callback (needed for mmap)
637 static struct page *snd_cx25821_page(struct snd_pcm_substream *substream,
638 unsigned long offset)
640 void *pageptr = substream->runtime->dma_area + offset;
642 return vmalloc_to_page(pageptr);
646 * operators
648 static const struct snd_pcm_ops snd_cx25821_pcm_ops = {
649 .open = snd_cx25821_pcm_open,
650 .close = snd_cx25821_close,
651 .ioctl = snd_pcm_lib_ioctl,
652 .hw_params = snd_cx25821_hw_params,
653 .hw_free = snd_cx25821_hw_free,
654 .prepare = snd_cx25821_prepare,
655 .trigger = snd_cx25821_card_trigger,
656 .pointer = snd_cx25821_pointer,
657 .page = snd_cx25821_page,
661 * ALSA create a PCM device: Called when initializing the board.
662 * Sets up the name and hooks up the callbacks
664 static int snd_cx25821_pcm(struct cx25821_audio_dev *chip, int device,
665 char *name)
667 struct snd_pcm *pcm;
668 int err;
670 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
671 if (err < 0) {
672 pr_info("ERROR: FAILED snd_pcm_new() in %s\n", __func__);
673 return err;
675 pcm->private_data = chip;
676 pcm->info_flags = 0;
677 strcpy(pcm->name, name);
678 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx25821_pcm_ops);
680 return 0;
683 /****************************************************************************
684 Basic Flow for Sound Devices
685 ****************************************************************************/
688 * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
689 * Only boards with eeprom and byte 1 at eeprom=1 have it
692 static const struct pci_device_id __maybe_unused cx25821_audio_pci_tbl[] = {
693 {0x14f1, 0x0920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
694 {0,}
697 MODULE_DEVICE_TABLE(pci, cx25821_audio_pci_tbl);
700 * Alsa Constructor - Component probe
702 static int cx25821_audio_initdev(struct cx25821_dev *dev)
704 struct snd_card *card;
705 struct cx25821_audio_dev *chip;
706 int err;
708 if (devno >= SNDRV_CARDS) {
709 pr_info("DEBUG ERROR: devno >= SNDRV_CARDS %s\n", __func__);
710 return -ENODEV;
713 if (!enable[devno]) {
714 ++devno;
715 pr_info("DEBUG ERROR: !enable[devno] %s\n", __func__);
716 return -ENOENT;
719 err = snd_card_new(&dev->pci->dev, index[devno], id[devno],
720 THIS_MODULE,
721 sizeof(struct cx25821_audio_dev), &card);
722 if (err < 0) {
723 pr_info("DEBUG ERROR: cannot create snd_card_new in %s\n",
724 __func__);
725 return err;
728 strcpy(card->driver, "cx25821");
730 /* Card "creation" */
731 chip = card->private_data;
732 spin_lock_init(&chip->reg_lock);
734 chip->dev = dev;
735 chip->card = card;
736 chip->pci = dev->pci;
737 chip->iobase = pci_resource_start(dev->pci, 0);
739 chip->irq = dev->pci->irq;
741 err = request_irq(dev->pci->irq, cx25821_irq,
742 IRQF_SHARED, chip->dev->name, chip);
744 if (err < 0) {
745 pr_err("ERROR %s: can't get IRQ %d for ALSA\n", chip->dev->name,
746 dev->pci->irq);
747 goto error;
750 err = snd_cx25821_pcm(chip, 0, "cx25821 Digital");
751 if (err < 0) {
752 pr_info("DEBUG ERROR: cannot create snd_cx25821_pcm %s\n",
753 __func__);
754 goto error;
757 strcpy(card->shortname, "cx25821");
758 sprintf(card->longname, "%s at 0x%lx irq %d", chip->dev->name,
759 chip->iobase, chip->irq);
760 strcpy(card->mixername, "CX25821");
762 pr_info("%s/%i: ALSA support for cx25821 boards\n", card->driver,
763 devno);
765 err = snd_card_register(card);
766 if (err < 0) {
767 pr_info("DEBUG ERROR: cannot register sound card %s\n",
768 __func__);
769 goto error;
772 dev->card = card;
773 devno++;
774 return 0;
776 error:
777 snd_card_free(card);
778 return err;
781 /****************************************************************************
782 LINUX MODULE INIT
783 ****************************************************************************/
785 static int cx25821_alsa_exit_callback(struct device *dev, void *data)
787 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
788 struct cx25821_dev *cxdev = get_cx25821(v4l2_dev);
790 snd_card_free(cxdev->card);
791 return 0;
794 static void cx25821_audio_fini(void)
796 struct device_driver *drv = driver_find("cx25821", &pci_bus_type);
797 int ret;
799 ret = driver_for_each_device(drv, NULL, NULL, cx25821_alsa_exit_callback);
800 if (ret)
801 pr_err("%s failed to find a cx25821 driver.\n", __func__);
804 static int cx25821_alsa_init_callback(struct device *dev, void *data)
806 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
807 struct cx25821_dev *cxdev = get_cx25821(v4l2_dev);
809 cx25821_audio_initdev(cxdev);
810 return 0;
814 * Module initializer
816 * Loops through present saa7134 cards, and assigns an ALSA device
817 * to each one
820 static int cx25821_alsa_init(void)
822 struct device_driver *drv = driver_find("cx25821", &pci_bus_type);
824 return driver_for_each_device(drv, NULL, NULL, cx25821_alsa_init_callback);
828 late_initcall(cx25821_alsa_init);
829 module_exit(cx25821_audio_fini);