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