Merge tag 'modules-next-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / drivers / media / pci / cx25821 / cx25821-alsa.c
1 /*
2  *  Driver for the Conexant CX25821 PCIe bridge
3  *
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
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation, version 2
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  *
21  */
22
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/device.h>
28 #include <linux/interrupt.h>
29 #include <linux/vmalloc.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/pci.h>
32 #include <linux/slab.h>
33
34 #include <linux/delay.h>
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/control.h>
39 #include <sound/initval.h>
40 #include <sound/tlv.h>
41
42 #include "cx25821.h"
43 #include "cx25821-reg.h"
44
45 #define AUDIO_SRAM_CHANNEL      SRAM_CH08
46
47 #define dprintk(level, fmt, arg...)                             \
48 do {                                                            \
49         if (debug >= level)                                     \
50                 pr_info("%s/1: " fmt, chip->dev->name, ##arg);  \
51 } while (0)
52 #define dprintk_core(level, fmt, arg...)                                \
53 do {                                                                    \
54         if (debug >= level)                                             \
55                 printk(KERN_DEBUG "%s/1: " fmt, chip->dev->name, ##arg); \
56 } while (0)
57
58 /****************************************************************************
59         Data type declarations - Can be moded to a header file later
60  ****************************************************************************/
61
62 static int devno;
63
64 struct cx25821_audio_buffer {
65         unsigned int bpl;
66         struct btcx_riscmem risc;
67         struct videobuf_dmabuf dma;
68 };
69
70 struct cx25821_audio_dev {
71         struct cx25821_dev *dev;
72         struct cx25821_dmaqueue q;
73
74         /* pci i/o */
75         struct pci_dev *pci;
76
77         /* audio controls */
78         int irq;
79
80         struct snd_card *card;
81
82         unsigned long iobase;
83         spinlock_t reg_lock;
84         atomic_t count;
85
86         unsigned int dma_size;
87         unsigned int period_size;
88         unsigned int num_periods;
89
90         struct videobuf_dmabuf *dma_risc;
91
92         struct cx25821_audio_buffer *buf;
93
94         struct snd_pcm_substream *substream;
95 };
96
97
98 /****************************************************************************
99                         Module global static vars
100  ****************************************************************************/
101
102 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
103 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
104 static bool enable[SNDRV_CARDS] = { 1, [1 ... (SNDRV_CARDS - 1)] = 1 };
105
106 module_param_array(enable, bool, NULL, 0444);
107 MODULE_PARM_DESC(enable, "Enable cx25821 soundcard. default enabled.");
108
109 module_param_array(index, int, NULL, 0444);
110 MODULE_PARM_DESC(index, "Index value for cx25821 capture interface(s).");
111
112 /****************************************************************************
113                                 Module macros
114  ****************************************************************************/
115
116 MODULE_DESCRIPTION("ALSA driver module for cx25821 based capture cards");
117 MODULE_AUTHOR("Hiep Huynh");
118 MODULE_LICENSE("GPL");
119 MODULE_SUPPORTED_DEVICE("{{Conexant,25821}");   /* "{{Conexant,23881}," */
120
121 static unsigned int debug;
122 module_param(debug, int, 0644);
123 MODULE_PARM_DESC(debug, "enable debug messages");
124
125 /****************************************************************************
126                         Module specific funtions
127  ****************************************************************************/
128 /* Constants taken from cx88-reg.h */
129 #define AUD_INT_DN_RISCI1       (1 <<  0)
130 #define AUD_INT_UP_RISCI1       (1 <<  1)
131 #define AUD_INT_RDS_DN_RISCI1   (1 <<  2)
132 #define AUD_INT_DN_RISCI2       (1 <<  4)       /* yes, 3 is skipped */
133 #define AUD_INT_UP_RISCI2       (1 <<  5)
134 #define AUD_INT_RDS_DN_RISCI2   (1 <<  6)
135 #define AUD_INT_DN_SYNC         (1 << 12)
136 #define AUD_INT_UP_SYNC         (1 << 13)
137 #define AUD_INT_RDS_DN_SYNC     (1 << 14)
138 #define AUD_INT_OPC_ERR         (1 << 16)
139 #define AUD_INT_BER_IRQ         (1 << 20)
140 #define AUD_INT_MCHG_IRQ        (1 << 21)
141 #define GP_COUNT_CONTROL_RESET  0x3
142
143 #define PCI_MSK_AUD_EXT   (1 <<  4)
144 #define PCI_MSK_AUD_INT   (1 <<  3)
145 /*
146  * BOARD Specific: Sets audio DMA
147  */
148
149 static int _cx25821_start_audio_dma(struct cx25821_audio_dev *chip)
150 {
151         struct cx25821_audio_buffer *buf = chip->buf;
152         struct cx25821_dev *dev = chip->dev;
153         const struct sram_channel *audio_ch =
154             &cx25821_sram_channels[AUDIO_SRAM_CHANNEL];
155         u32 tmp = 0;
156
157         /* enable output on the GPIO 0 for the MCLK ADC (Audio) */
158         cx25821_set_gpiopin_direction(chip->dev, 0, 0);
159
160         /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
161         cx_clear(AUD_INT_DMA_CTL,
162                  FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
163
164         /* setup fifo + format - out channel */
165         cx25821_sram_channel_setup_audio(chip->dev, audio_ch, buf->bpl,
166                                          buf->risc.dma);
167
168         /* sets bpl size */
169         cx_write(AUD_A_LNGTH, buf->bpl);
170
171         /* reset counter */
172         /* GP_COUNT_CONTROL_RESET = 0x3 */
173         cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
174         atomic_set(&chip->count, 0);
175
176         /* Set the input mode to 16-bit */
177         tmp = cx_read(AUD_A_CFG);
178         cx_write(AUD_A_CFG, tmp | FLD_AUD_DST_PK_MODE | FLD_AUD_DST_ENABLE |
179                  FLD_AUD_CLK_ENABLE);
180
181         /*
182         pr_info("DEBUG: Start audio DMA, %d B/line, cmds_start(0x%x)= %d lines/FIFO, %d periods, %d byte buffer\n",
183                 buf->bpl, audio_ch->cmds_start,
184                 cx_read(audio_ch->cmds_start + 12)>>1,
185                 chip->num_periods, buf->bpl * chip->num_periods);
186         */
187
188         /* Enables corresponding bits at AUD_INT_STAT */
189         cx_write(AUD_A_INT_MSK, FLD_AUD_DST_RISCI1 | FLD_AUD_DST_OF |
190                  FLD_AUD_DST_SYNC | FLD_AUD_DST_OPC_ERR);
191
192         /* Clean any pending interrupt bits already set */
193         cx_write(AUD_A_INT_STAT, ~0);
194
195         /* enable audio irqs */
196         cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
197
198         /* Turn on audio downstream fifo and risc enable 0x101 */
199         tmp = cx_read(AUD_INT_DMA_CTL);
200         cx_set(AUD_INT_DMA_CTL, tmp |
201                (FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN));
202
203         mdelay(100);
204         return 0;
205 }
206
207 /*
208  * BOARD Specific: Resets audio DMA
209  */
210 static int _cx25821_stop_audio_dma(struct cx25821_audio_dev *chip)
211 {
212         struct cx25821_dev *dev = chip->dev;
213
214         /* stop dma */
215         cx_clear(AUD_INT_DMA_CTL,
216                  FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
217
218         /* disable irqs */
219         cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
220         cx_clear(AUD_A_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
221                  AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
222
223         return 0;
224 }
225
226 #define MAX_IRQ_LOOP 50
227
228 /*
229  * BOARD Specific: IRQ dma bits
230  */
231 static char *cx25821_aud_irqs[32] = {
232         "dn_risci1", "up_risci1", "rds_dn_risc1",       /* 0-2 */
233         NULL,                                           /* reserved */
234         "dn_risci2", "up_risci2", "rds_dn_risc2",       /* 4-6 */
235         NULL,                                           /* reserved */
236         "dnf_of", "upf_uf", "rds_dnf_uf",               /* 8-10 */
237         NULL,                                           /* reserved */
238         "dn_sync", "up_sync", "rds_dn_sync",            /* 12-14 */
239         NULL,                                           /* reserved */
240         "opc_err", "par_err", "rip_err",                /* 16-18 */
241         "pci_abort", "ber_irq", "mchg_irq"              /* 19-21 */
242 };
243
244 /*
245  * BOARD Specific: Threats IRQ audio specific calls
246  */
247 static void cx25821_aud_irq(struct cx25821_audio_dev *chip, u32 status,
248                             u32 mask)
249 {
250         struct cx25821_dev *dev = chip->dev;
251
252         if (0 == (status & mask))
253                 return;
254
255         cx_write(AUD_A_INT_STAT, status);
256         if (debug > 1 || (status & mask & ~0xff))
257                 cx25821_print_irqbits(dev->name, "irq aud", cx25821_aud_irqs,
258                                 ARRAY_SIZE(cx25821_aud_irqs), status, mask);
259
260         /* risc op code error */
261         if (status & AUD_INT_OPC_ERR) {
262                 pr_warn("WARNING %s/1: Audio risc op code error\n", dev->name);
263
264                 cx_clear(AUD_INT_DMA_CTL,
265                          FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
266                 cx25821_sram_channel_dump_audio(dev,
267                                 &cx25821_sram_channels[AUDIO_SRAM_CHANNEL]);
268         }
269         if (status & AUD_INT_DN_SYNC) {
270                 pr_warn("WARNING %s: Downstream sync error!\n", dev->name);
271                 cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
272                 return;
273         }
274
275         /* risc1 downstream */
276         if (status & AUD_INT_DN_RISCI1) {
277                 atomic_set(&chip->count, cx_read(AUD_A_GPCNT));
278                 snd_pcm_period_elapsed(chip->substream);
279         }
280 }
281
282 /*
283  * BOARD Specific: Handles IRQ calls
284  */
285 static irqreturn_t cx25821_irq(int irq, void *dev_id)
286 {
287         struct cx25821_audio_dev *chip = dev_id;
288         struct cx25821_dev *dev = chip->dev;
289         u32 status, pci_status;
290         u32 audint_status, audint_mask;
291         int loop, handled = 0;
292
293         audint_status = cx_read(AUD_A_INT_STAT);
294         audint_mask = cx_read(AUD_A_INT_MSK);
295         status = cx_read(PCI_INT_STAT);
296
297         for (loop = 0; loop < 1; loop++) {
298                 status = cx_read(PCI_INT_STAT);
299                 if (0 == status) {
300                         status = cx_read(PCI_INT_STAT);
301                         audint_status = cx_read(AUD_A_INT_STAT);
302                         audint_mask = cx_read(AUD_A_INT_MSK);
303
304                         if (status) {
305                                 handled = 1;
306                                 cx_write(PCI_INT_STAT, status);
307
308                                 cx25821_aud_irq(chip, audint_status,
309                                                 audint_mask);
310                                 break;
311                         } else {
312                                 goto out;
313                         }
314                 }
315
316                 handled = 1;
317                 cx_write(PCI_INT_STAT, status);
318
319                 cx25821_aud_irq(chip, audint_status, audint_mask);
320         }
321
322         pci_status = cx_read(PCI_INT_STAT);
323
324         if (handled)
325                 cx_write(PCI_INT_STAT, pci_status);
326
327 out:
328         return IRQ_RETVAL(handled);
329 }
330
331 static int dsp_buffer_free(struct cx25821_audio_dev *chip)
332 {
333         BUG_ON(!chip->dma_size);
334
335         dprintk(2, "Freeing buffer\n");
336         videobuf_dma_unmap(&chip->pci->dev, chip->dma_risc);
337         videobuf_dma_free(chip->dma_risc);
338         btcx_riscmem_free(chip->pci, &chip->buf->risc);
339         kfree(chip->buf);
340
341         chip->dma_risc = NULL;
342         chip->dma_size = 0;
343
344         return 0;
345 }
346
347 /****************************************************************************
348                                 ALSA PCM Interface
349  ****************************************************************************/
350
351 /*
352  * Digital hardware definition
353  */
354 #define DEFAULT_FIFO_SIZE       384
355 static struct snd_pcm_hardware snd_cx25821_digital_hw = {
356         .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
357                 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID,
358         .formats = SNDRV_PCM_FMTBIT_S16_LE,
359
360         .rates = SNDRV_PCM_RATE_48000,
361         .rate_min = 48000,
362         .rate_max = 48000,
363         .channels_min = 2,
364         .channels_max = 2,
365         /* Analog audio output will be full of clicks and pops if there
366            are not exactly four lines in the SRAM FIFO buffer.  */
367         .period_bytes_min = DEFAULT_FIFO_SIZE / 3,
368         .period_bytes_max = DEFAULT_FIFO_SIZE / 3,
369         .periods_min = 1,
370         .periods_max = AUDIO_LINE_SIZE,
371         /* 128 * 128 = 16384 = 1024 * 16 */
372         .buffer_bytes_max = (AUDIO_LINE_SIZE * AUDIO_LINE_SIZE),
373 };
374
375 /*
376  * audio pcm capture open callback
377  */
378 static int snd_cx25821_pcm_open(struct snd_pcm_substream *substream)
379 {
380         struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
381         struct snd_pcm_runtime *runtime = substream->runtime;
382         int err;
383         unsigned int bpl = 0;
384
385         if (!chip) {
386                 pr_err("DEBUG: cx25821 can't find device struct. Can't proceed with open\n");
387                 return -ENODEV;
388         }
389
390         err = snd_pcm_hw_constraint_pow2(runtime, 0,
391                                          SNDRV_PCM_HW_PARAM_PERIODS);
392         if (err < 0)
393                 goto _error;
394
395         chip->substream = substream;
396
397         runtime->hw = snd_cx25821_digital_hw;
398
399         if (cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
400             DEFAULT_FIFO_SIZE) {
401                 /* since there are 3 audio Clusters */
402                 bpl = cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 3;
403                 bpl &= ~7;      /* must be multiple of 8 */
404
405                 if (bpl > AUDIO_LINE_SIZE)
406                         bpl = AUDIO_LINE_SIZE;
407
408                 runtime->hw.period_bytes_min = bpl;
409                 runtime->hw.period_bytes_max = bpl;
410         }
411
412         return 0;
413 _error:
414         dprintk(1, "Error opening PCM!\n");
415         return err;
416 }
417
418 /*
419  * audio close callback
420  */
421 static int snd_cx25821_close(struct snd_pcm_substream *substream)
422 {
423         return 0;
424 }
425
426 /*
427  * hw_params callback
428  */
429 static int snd_cx25821_hw_params(struct snd_pcm_substream *substream,
430                                  struct snd_pcm_hw_params *hw_params)
431 {
432         struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
433         struct videobuf_dmabuf *dma;
434
435         struct cx25821_audio_buffer *buf;
436         int ret;
437
438         if (substream->runtime->dma_area) {
439                 dsp_buffer_free(chip);
440                 substream->runtime->dma_area = NULL;
441         }
442
443         chip->period_size = params_period_bytes(hw_params);
444         chip->num_periods = params_periods(hw_params);
445         chip->dma_size = chip->period_size * params_periods(hw_params);
446
447         BUG_ON(!chip->dma_size);
448         BUG_ON(chip->num_periods & (chip->num_periods - 1));
449
450         buf = kzalloc(sizeof(*buf), GFP_KERNEL);
451         if (NULL == buf)
452                 return -ENOMEM;
453
454         if (chip->period_size > AUDIO_LINE_SIZE)
455                 chip->period_size = AUDIO_LINE_SIZE;
456
457         buf->bpl = chip->period_size;
458
459         dma = &buf->dma;
460         videobuf_dma_init(dma);
461         ret = videobuf_dma_init_kernel(dma, PCI_DMA_FROMDEVICE,
462                         (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
463         if (ret < 0)
464                 goto error;
465
466         ret = videobuf_dma_map(&chip->pci->dev, dma);
467         if (ret < 0)
468                 goto error;
469
470         ret = cx25821_risc_databuffer_audio(chip->pci, &buf->risc, dma->sglist,
471                         chip->period_size, chip->num_periods, 1);
472         if (ret < 0) {
473                 pr_info("DEBUG: ERROR after cx25821_risc_databuffer_audio()\n");
474                 goto error;
475         }
476
477         /* Loop back to start of program */
478         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
479         buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
480         buf->risc.jmp[2] = cpu_to_le32(0);      /* bits 63-32 */
481
482         chip->buf = buf;
483         chip->dma_risc = dma;
484
485         substream->runtime->dma_area = chip->dma_risc->vaddr;
486         substream->runtime->dma_bytes = chip->dma_size;
487         substream->runtime->dma_addr = 0;
488
489         return 0;
490
491 error:
492         kfree(buf);
493         return ret;
494 }
495
496 /*
497  * hw free callback
498  */
499 static int snd_cx25821_hw_free(struct snd_pcm_substream *substream)
500 {
501         struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
502
503         if (substream->runtime->dma_area) {
504                 dsp_buffer_free(chip);
505                 substream->runtime->dma_area = NULL;
506         }
507
508         return 0;
509 }
510
511 /*
512  * prepare callback
513  */
514 static int snd_cx25821_prepare(struct snd_pcm_substream *substream)
515 {
516         return 0;
517 }
518
519 /*
520  * trigger callback
521  */
522 static int snd_cx25821_card_trigger(struct snd_pcm_substream *substream,
523                                     int cmd)
524 {
525         struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
526         int err = 0;
527
528         /* Local interrupts are already disabled by ALSA */
529         spin_lock(&chip->reg_lock);
530
531         switch (cmd) {
532         case SNDRV_PCM_TRIGGER_START:
533                 err = _cx25821_start_audio_dma(chip);
534                 break;
535         case SNDRV_PCM_TRIGGER_STOP:
536                 err = _cx25821_stop_audio_dma(chip);
537                 break;
538         default:
539                 err = -EINVAL;
540                 break;
541         }
542
543         spin_unlock(&chip->reg_lock);
544
545         return err;
546 }
547
548 /*
549  * pointer callback
550  */
551 static snd_pcm_uframes_t snd_cx25821_pointer(struct snd_pcm_substream
552                                              *substream)
553 {
554         struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
555         struct snd_pcm_runtime *runtime = substream->runtime;
556         u16 count;
557
558         count = atomic_read(&chip->count);
559
560         return runtime->period_size * (count & (runtime->periods - 1));
561 }
562
563 /*
564  * page callback (needed for mmap)
565  */
566 static struct page *snd_cx25821_page(struct snd_pcm_substream *substream,
567                                      unsigned long offset)
568 {
569         void *pageptr = substream->runtime->dma_area + offset;
570
571         return vmalloc_to_page(pageptr);
572 }
573
574 /*
575  * operators
576  */
577 static struct snd_pcm_ops snd_cx25821_pcm_ops = {
578         .open = snd_cx25821_pcm_open,
579         .close = snd_cx25821_close,
580         .ioctl = snd_pcm_lib_ioctl,
581         .hw_params = snd_cx25821_hw_params,
582         .hw_free = snd_cx25821_hw_free,
583         .prepare = snd_cx25821_prepare,
584         .trigger = snd_cx25821_card_trigger,
585         .pointer = snd_cx25821_pointer,
586         .page = snd_cx25821_page,
587 };
588
589 /*
590  * ALSA create a PCM device:  Called when initializing the board.
591  * Sets up the name and hooks up the callbacks
592  */
593 static int snd_cx25821_pcm(struct cx25821_audio_dev *chip, int device,
594                            char *name)
595 {
596         struct snd_pcm *pcm;
597         int err;
598
599         err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
600         if (err < 0) {
601                 pr_info("ERROR: FAILED snd_pcm_new() in %s\n", __func__);
602                 return err;
603         }
604         pcm->private_data = chip;
605         pcm->info_flags = 0;
606         strcpy(pcm->name, name);
607         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx25821_pcm_ops);
608
609         return 0;
610 }
611
612 /****************************************************************************
613                         Basic Flow for Sound Devices
614  ****************************************************************************/
615
616 /*
617  * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
618  * Only boards with eeprom and byte 1 at eeprom=1 have it
619  */
620
621 static const struct pci_device_id cx25821_audio_pci_tbl[] = {
622         {0x14f1, 0x0920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
623         {0,}
624 };
625
626 MODULE_DEVICE_TABLE(pci, cx25821_audio_pci_tbl);
627
628 /*
629  * Alsa Constructor - Component probe
630  */
631 static int cx25821_audio_initdev(struct cx25821_dev *dev)
632 {
633         struct snd_card *card;
634         struct cx25821_audio_dev *chip;
635         int err;
636
637         if (devno >= SNDRV_CARDS) {
638                 pr_info("DEBUG ERROR: devno >= SNDRV_CARDS %s\n", __func__);
639                 return -ENODEV;
640         }
641
642         if (!enable[devno]) {
643                 ++devno;
644                 pr_info("DEBUG ERROR: !enable[devno] %s\n", __func__);
645                 return -ENOENT;
646         }
647
648         err = snd_card_new(&dev->pci->dev, index[devno], id[devno],
649                            THIS_MODULE,
650                            sizeof(struct cx25821_audio_dev), &card);
651         if (err < 0) {
652                 pr_info("DEBUG ERROR: cannot create snd_card_new in %s\n",
653                         __func__);
654                 return err;
655         }
656
657         strcpy(card->driver, "cx25821");
658
659         /* Card "creation" */
660         chip = card->private_data;
661         spin_lock_init(&chip->reg_lock);
662
663         chip->dev = dev;
664         chip->card = card;
665         chip->pci = dev->pci;
666         chip->iobase = pci_resource_start(dev->pci, 0);
667
668         chip->irq = dev->pci->irq;
669
670         err = request_irq(dev->pci->irq, cx25821_irq,
671                           IRQF_SHARED, chip->dev->name, chip);
672
673         if (err < 0) {
674                 pr_err("ERROR %s: can't get IRQ %d for ALSA\n", chip->dev->name,
675                         dev->pci->irq);
676                 goto error;
677         }
678
679         err = snd_cx25821_pcm(chip, 0, "cx25821 Digital");
680         if (err < 0) {
681                 pr_info("DEBUG ERROR: cannot create snd_cx25821_pcm %s\n",
682                         __func__);
683                 goto error;
684         }
685
686         strcpy(card->shortname, "cx25821");
687         sprintf(card->longname, "%s at 0x%lx irq %d", chip->dev->name,
688                 chip->iobase, chip->irq);
689         strcpy(card->mixername, "CX25821");
690
691         pr_info("%s/%i: ALSA support for cx25821 boards\n", card->driver,
692                 devno);
693
694         err = snd_card_register(card);
695         if (err < 0) {
696                 pr_info("DEBUG ERROR: cannot register sound card %s\n",
697                         __func__);
698                 goto error;
699         }
700
701         dev->card = card;
702         devno++;
703         return 0;
704
705 error:
706         snd_card_free(card);
707         return err;
708 }
709
710 /****************************************************************************
711                                 LINUX MODULE INIT
712  ****************************************************************************/
713
714 static int cx25821_alsa_exit_callback(struct device *dev, void *data)
715 {
716         struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
717         struct cx25821_dev *cxdev = get_cx25821(v4l2_dev);
718
719         snd_card_free(cxdev->card);
720         return 0;
721 }
722
723 static void cx25821_audio_fini(void)
724 {
725         struct device_driver *drv = driver_find("cx25821", &pci_bus_type);
726         int ret;
727
728         ret = driver_for_each_device(drv, NULL, NULL, cx25821_alsa_exit_callback);
729         if (ret)
730                 pr_err("%s failed to find a cx25821 driver.\n", __func__);
731 }
732
733 static int cx25821_alsa_init_callback(struct device *dev, void *data)
734 {
735         struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
736         struct cx25821_dev *cxdev = get_cx25821(v4l2_dev);
737
738         cx25821_audio_initdev(cxdev);
739         return 0;
740 }
741
742 /*
743  * Module initializer
744  *
745  * Loops through present saa7134 cards, and assigns an ALSA device
746  * to each one
747  *
748  */
749 static int cx25821_alsa_init(void)
750 {
751         struct device_driver *drv = driver_find("cx25821", &pci_bus_type);
752
753         return driver_for_each_device(drv, NULL, NULL, cx25821_alsa_init_callback);
754
755 }
756
757 late_initcall(cx25821_alsa_init);
758 module_exit(cx25821_audio_fini);