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