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