150acfb2bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b285192aSMauro Carvalho Chehab /*
3b285192aSMauro Carvalho Chehab  *  Driver for the Conexant CX25821 PCIe bridge
4b285192aSMauro Carvalho Chehab  *
5b285192aSMauro Carvalho Chehab  *  Copyright (C) 2009 Conexant Systems Inc.
6b285192aSMauro Carvalho Chehab  *  Authors  <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
7b285192aSMauro Carvalho Chehab  *	Based on SAA713x ALSA driver and CX88 driver
8b285192aSMauro Carvalho Chehab  */
9b285192aSMauro Carvalho Chehab 
10b285192aSMauro Carvalho Chehab #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11b285192aSMauro Carvalho Chehab 
12b285192aSMauro Carvalho Chehab #include <linux/module.h>
13b285192aSMauro Carvalho Chehab #include <linux/init.h>
14b285192aSMauro Carvalho Chehab #include <linux/device.h>
15b285192aSMauro Carvalho Chehab #include <linux/interrupt.h>
16b285192aSMauro Carvalho Chehab #include <linux/vmalloc.h>
17b285192aSMauro Carvalho Chehab #include <linux/dma-mapping.h>
18b285192aSMauro Carvalho Chehab #include <linux/pci.h>
19b285192aSMauro Carvalho Chehab #include <linux/slab.h>
20b285192aSMauro Carvalho Chehab 
21b285192aSMauro Carvalho Chehab #include <linux/delay.h>
22b285192aSMauro Carvalho Chehab #include <sound/core.h>
23b285192aSMauro Carvalho Chehab #include <sound/pcm.h>
24b285192aSMauro Carvalho Chehab #include <sound/pcm_params.h>
25b285192aSMauro Carvalho Chehab #include <sound/control.h>
26b285192aSMauro Carvalho Chehab #include <sound/initval.h>
27b285192aSMauro Carvalho Chehab #include <sound/tlv.h>
28b285192aSMauro Carvalho Chehab 
29b285192aSMauro Carvalho Chehab #include "cx25821.h"
30b285192aSMauro Carvalho Chehab #include "cx25821-reg.h"
31b285192aSMauro Carvalho Chehab 
32b285192aSMauro Carvalho Chehab #define AUDIO_SRAM_CHANNEL	SRAM_CH08
33b285192aSMauro Carvalho Chehab 
34b285192aSMauro Carvalho Chehab #define dprintk(level, fmt, arg...)				\
35b285192aSMauro Carvalho Chehab do {								\
36b285192aSMauro Carvalho Chehab 	if (debug >= level)					\
37b285192aSMauro Carvalho Chehab 		pr_info("%s/1: " fmt, chip->dev->name, ##arg);	\
38b285192aSMauro Carvalho Chehab } while (0)
39b285192aSMauro Carvalho Chehab #define dprintk_core(level, fmt, arg...)				\
40b285192aSMauro Carvalho Chehab do {									\
41b285192aSMauro Carvalho Chehab 	if (debug >= level)						\
42b285192aSMauro Carvalho Chehab 		printk(KERN_DEBUG "%s/1: " fmt, chip->dev->name, ##arg); \
43b285192aSMauro Carvalho Chehab } while (0)
44b285192aSMauro Carvalho Chehab 
45b285192aSMauro Carvalho Chehab /****************************************************************************
46b285192aSMauro Carvalho Chehab 	Data type declarations - Can be moded to a header file later
47b285192aSMauro Carvalho Chehab  ****************************************************************************/
48b285192aSMauro Carvalho Chehab 
49b285192aSMauro Carvalho Chehab static int devno;
50b285192aSMauro Carvalho Chehab 
51b285192aSMauro Carvalho Chehab struct cx25821_audio_buffer {
52b285192aSMauro Carvalho Chehab 	unsigned int bpl;
535ede94c7SHans Verkuil 	struct cx25821_riscmem risc;
548d8e6d60SHans Verkuil 	void			*vaddr;
558d8e6d60SHans Verkuil 	struct scatterlist	*sglist;
568d8e6d60SHans Verkuil 	int			sglen;
5799c2ec04SMauro Carvalho Chehab 	unsigned long		nr_pages;
58b285192aSMauro Carvalho Chehab };
59b285192aSMauro Carvalho Chehab 
60b285192aSMauro Carvalho Chehab struct cx25821_audio_dev {
61b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev;
62b285192aSMauro Carvalho Chehab 	struct cx25821_dmaqueue q;
63b285192aSMauro Carvalho Chehab 
64b285192aSMauro Carvalho Chehab 	/* pci i/o */
65b285192aSMauro Carvalho Chehab 	struct pci_dev *pci;
66b285192aSMauro Carvalho Chehab 
67b285192aSMauro Carvalho Chehab 	/* audio controls */
68b285192aSMauro Carvalho Chehab 	int irq;
69b285192aSMauro Carvalho Chehab 
70b285192aSMauro Carvalho Chehab 	struct snd_card *card;
71b285192aSMauro Carvalho Chehab 
72b285192aSMauro Carvalho Chehab 	unsigned long iobase;
73b285192aSMauro Carvalho Chehab 	spinlock_t reg_lock;
74b285192aSMauro Carvalho Chehab 	atomic_t count;
75b285192aSMauro Carvalho Chehab 
76b285192aSMauro Carvalho Chehab 	unsigned int dma_size;
77b285192aSMauro Carvalho Chehab 	unsigned int period_size;
78b285192aSMauro Carvalho Chehab 	unsigned int num_periods;
79b285192aSMauro Carvalho Chehab 
80b285192aSMauro Carvalho Chehab 	struct cx25821_audio_buffer *buf;
81b285192aSMauro Carvalho Chehab 
82b285192aSMauro Carvalho Chehab 	struct snd_pcm_substream *substream;
83b285192aSMauro Carvalho Chehab };
84b285192aSMauro Carvalho Chehab 
85b285192aSMauro Carvalho Chehab 
86b285192aSMauro Carvalho Chehab /****************************************************************************
87b285192aSMauro Carvalho Chehab 			Module global static vars
88b285192aSMauro Carvalho Chehab  ****************************************************************************/
89b285192aSMauro Carvalho Chehab 
90b285192aSMauro Carvalho Chehab static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
91b285192aSMauro Carvalho Chehab static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
92d275d935SLuis de Bethencourt static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
93b285192aSMauro Carvalho Chehab 
94b285192aSMauro Carvalho Chehab module_param_array(enable, bool, NULL, 0444);
95b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enable, "Enable cx25821 soundcard. default enabled.");
96b285192aSMauro Carvalho Chehab 
97b285192aSMauro Carvalho Chehab module_param_array(index, int, NULL, 0444);
98b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(index, "Index value for cx25821 capture interface(s).");
99b285192aSMauro Carvalho Chehab 
100b285192aSMauro Carvalho Chehab /****************************************************************************
101b285192aSMauro Carvalho Chehab 				Module macros
102b285192aSMauro Carvalho Chehab  ****************************************************************************/
103b285192aSMauro Carvalho Chehab 
104b285192aSMauro Carvalho Chehab MODULE_DESCRIPTION("ALSA driver module for cx25821 based capture cards");
105b285192aSMauro Carvalho Chehab MODULE_AUTHOR("Hiep Huynh");
106b285192aSMauro Carvalho Chehab MODULE_LICENSE("GPL");
107b285192aSMauro Carvalho Chehab MODULE_SUPPORTED_DEVICE("{{Conexant,25821}");	/* "{{Conexant,23881}," */
108b285192aSMauro Carvalho Chehab 
109b285192aSMauro Carvalho Chehab static unsigned int debug;
110b285192aSMauro Carvalho Chehab module_param(debug, int, 0644);
111b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(debug, "enable debug messages");
112b285192aSMauro Carvalho Chehab 
113b285192aSMauro Carvalho Chehab /****************************************************************************
11416790554SMauro Carvalho Chehab 			Module specific functions
115b285192aSMauro Carvalho Chehab  ****************************************************************************/
116b285192aSMauro Carvalho Chehab /* Constants taken from cx88-reg.h */
117b285192aSMauro Carvalho Chehab #define AUD_INT_DN_RISCI1       (1 <<  0)
118b285192aSMauro Carvalho Chehab #define AUD_INT_UP_RISCI1       (1 <<  1)
119b285192aSMauro Carvalho Chehab #define AUD_INT_RDS_DN_RISCI1   (1 <<  2)
120b285192aSMauro Carvalho Chehab #define AUD_INT_DN_RISCI2       (1 <<  4)	/* yes, 3 is skipped */
121b285192aSMauro Carvalho Chehab #define AUD_INT_UP_RISCI2       (1 <<  5)
122b285192aSMauro Carvalho Chehab #define AUD_INT_RDS_DN_RISCI2   (1 <<  6)
123b285192aSMauro Carvalho Chehab #define AUD_INT_DN_SYNC         (1 << 12)
124b285192aSMauro Carvalho Chehab #define AUD_INT_UP_SYNC         (1 << 13)
125b285192aSMauro Carvalho Chehab #define AUD_INT_RDS_DN_SYNC     (1 << 14)
126b285192aSMauro Carvalho Chehab #define AUD_INT_OPC_ERR         (1 << 16)
127b285192aSMauro Carvalho Chehab #define AUD_INT_BER_IRQ         (1 << 20)
128b285192aSMauro Carvalho Chehab #define AUD_INT_MCHG_IRQ        (1 << 21)
129b285192aSMauro Carvalho Chehab #define GP_COUNT_CONTROL_RESET	0x3
130b285192aSMauro Carvalho Chehab 
131b285192aSMauro Carvalho Chehab #define PCI_MSK_AUD_EXT   (1 <<  4)
132b285192aSMauro Carvalho Chehab #define PCI_MSK_AUD_INT   (1 <<  3)
1338d8e6d60SHans Verkuil 
13499c2ec04SMauro Carvalho Chehab static int cx25821_alsa_dma_init(struct cx25821_audio_dev *chip,
13599c2ec04SMauro Carvalho Chehab 				 unsigned long nr_pages)
1368d8e6d60SHans Verkuil {
1378d8e6d60SHans Verkuil 	struct cx25821_audio_buffer *buf = chip->buf;
1388d8e6d60SHans Verkuil 	struct page *pg;
1398d8e6d60SHans Verkuil 	int i;
1408d8e6d60SHans Verkuil 
1418d8e6d60SHans Verkuil 	buf->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
1428d8e6d60SHans Verkuil 	if (NULL == buf->vaddr) {
14399c2ec04SMauro Carvalho Chehab 		dprintk(1, "vmalloc_32(%lu pages) failed\n", nr_pages);
1448d8e6d60SHans Verkuil 		return -ENOMEM;
1458d8e6d60SHans Verkuil 	}
1468d8e6d60SHans Verkuil 
14799c2ec04SMauro Carvalho Chehab 	dprintk(1, "vmalloc is at addr 0x%p, size=%lu\n",
14804f849c0SMauro Carvalho Chehab 				buf->vaddr,
1498d8e6d60SHans Verkuil 				nr_pages << PAGE_SHIFT);
1508d8e6d60SHans Verkuil 
1518d8e6d60SHans Verkuil 	memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT);
1528d8e6d60SHans Verkuil 	buf->nr_pages = nr_pages;
1538d8e6d60SHans Verkuil 
154fad953ceSKees Cook 	buf->sglist = vzalloc(array_size(sizeof(*buf->sglist), buf->nr_pages));
1558d8e6d60SHans Verkuil 	if (NULL == buf->sglist)
1568d8e6d60SHans Verkuil 		goto vzalloc_err;
1578d8e6d60SHans Verkuil 
1588d8e6d60SHans Verkuil 	sg_init_table(buf->sglist, buf->nr_pages);
1598d8e6d60SHans Verkuil 	for (i = 0; i < buf->nr_pages; i++) {
1608d8e6d60SHans Verkuil 		pg = vmalloc_to_page(buf->vaddr + i * PAGE_SIZE);
1618d8e6d60SHans Verkuil 		if (NULL == pg)
1628d8e6d60SHans Verkuil 			goto vmalloc_to_page_err;
1638d8e6d60SHans Verkuil 		sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0);
1648d8e6d60SHans Verkuil 	}
1658d8e6d60SHans Verkuil 	return 0;
1668d8e6d60SHans Verkuil 
1678d8e6d60SHans Verkuil vmalloc_to_page_err:
1688d8e6d60SHans Verkuil 	vfree(buf->sglist);
1698d8e6d60SHans Verkuil 	buf->sglist = NULL;
1708d8e6d60SHans Verkuil vzalloc_err:
1718d8e6d60SHans Verkuil 	vfree(buf->vaddr);
1728d8e6d60SHans Verkuil 	buf->vaddr = NULL;
1738d8e6d60SHans Verkuil 	return -ENOMEM;
1748d8e6d60SHans Verkuil }
1758d8e6d60SHans Verkuil 
1768d8e6d60SHans Verkuil static int cx25821_alsa_dma_map(struct cx25821_audio_dev *dev)
1778d8e6d60SHans Verkuil {
1788d8e6d60SHans Verkuil 	struct cx25821_audio_buffer *buf = dev->buf;
1798d8e6d60SHans Verkuil 
1808d8e6d60SHans Verkuil 	buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist,
181f95fc014SMarek Szyprowski 			buf->nr_pages, DMA_FROM_DEVICE);
1828d8e6d60SHans Verkuil 
1838d8e6d60SHans Verkuil 	if (0 == buf->sglen) {
1848d8e6d60SHans Verkuil 		pr_warn("%s: cx25821_alsa_map_sg failed\n", __func__);
1858d8e6d60SHans Verkuil 		return -ENOMEM;
1868d8e6d60SHans Verkuil 	}
1878d8e6d60SHans Verkuil 	return 0;
1888d8e6d60SHans Verkuil }
1898d8e6d60SHans Verkuil 
1908d8e6d60SHans Verkuil static int cx25821_alsa_dma_unmap(struct cx25821_audio_dev *dev)
1918d8e6d60SHans Verkuil {
1928d8e6d60SHans Verkuil 	struct cx25821_audio_buffer *buf = dev->buf;
1938d8e6d60SHans Verkuil 
1948d8e6d60SHans Verkuil 	if (!buf->sglen)
1958d8e6d60SHans Verkuil 		return 0;
1968d8e6d60SHans Verkuil 
197f95fc014SMarek Szyprowski 	dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->nr_pages, DMA_FROM_DEVICE);
1988d8e6d60SHans Verkuil 	buf->sglen = 0;
1998d8e6d60SHans Verkuil 	return 0;
2008d8e6d60SHans Verkuil }
2018d8e6d60SHans Verkuil 
2028d8e6d60SHans Verkuil static int cx25821_alsa_dma_free(struct cx25821_audio_buffer *buf)
2038d8e6d60SHans Verkuil {
2048d8e6d60SHans Verkuil 	vfree(buf->sglist);
2058d8e6d60SHans Verkuil 	buf->sglist = NULL;
2068d8e6d60SHans Verkuil 	vfree(buf->vaddr);
2078d8e6d60SHans Verkuil 	buf->vaddr = NULL;
2088d8e6d60SHans Verkuil 	return 0;
2098d8e6d60SHans Verkuil }
2108d8e6d60SHans Verkuil 
211b285192aSMauro Carvalho Chehab /*
212b285192aSMauro Carvalho Chehab  * BOARD Specific: Sets audio DMA
213b285192aSMauro Carvalho Chehab  */
214b285192aSMauro Carvalho Chehab 
215b285192aSMauro Carvalho Chehab static int _cx25821_start_audio_dma(struct cx25821_audio_dev *chip)
216b285192aSMauro Carvalho Chehab {
217b285192aSMauro Carvalho Chehab 	struct cx25821_audio_buffer *buf = chip->buf;
218b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = chip->dev;
219bfef0d35SHans Verkuil 	const struct sram_channel *audio_ch =
220b285192aSMauro Carvalho Chehab 	    &cx25821_sram_channels[AUDIO_SRAM_CHANNEL];
221b285192aSMauro Carvalho Chehab 	u32 tmp = 0;
222b285192aSMauro Carvalho Chehab 
223b285192aSMauro Carvalho Chehab 	/* enable output on the GPIO 0 for the MCLK ADC (Audio) */
224b285192aSMauro Carvalho Chehab 	cx25821_set_gpiopin_direction(chip->dev, 0, 0);
225b285192aSMauro Carvalho Chehab 
226b285192aSMauro Carvalho Chehab 	/* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
227b285192aSMauro Carvalho Chehab 	cx_clear(AUD_INT_DMA_CTL,
228b285192aSMauro Carvalho Chehab 		 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
229b285192aSMauro Carvalho Chehab 
230b285192aSMauro Carvalho Chehab 	/* setup fifo + format - out channel */
231b285192aSMauro Carvalho Chehab 	cx25821_sram_channel_setup_audio(chip->dev, audio_ch, buf->bpl,
232b285192aSMauro Carvalho Chehab 					 buf->risc.dma);
233b285192aSMauro Carvalho Chehab 
234b285192aSMauro Carvalho Chehab 	/* sets bpl size */
235b285192aSMauro Carvalho Chehab 	cx_write(AUD_A_LNGTH, buf->bpl);
236b285192aSMauro Carvalho Chehab 
237b285192aSMauro Carvalho Chehab 	/* reset counter */
238b285192aSMauro Carvalho Chehab 	/* GP_COUNT_CONTROL_RESET = 0x3 */
239b285192aSMauro Carvalho Chehab 	cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
240b285192aSMauro Carvalho Chehab 	atomic_set(&chip->count, 0);
241b285192aSMauro Carvalho Chehab 
242b285192aSMauro Carvalho Chehab 	/* Set the input mode to 16-bit */
243b285192aSMauro Carvalho Chehab 	tmp = cx_read(AUD_A_CFG);
244b285192aSMauro Carvalho Chehab 	cx_write(AUD_A_CFG, tmp | FLD_AUD_DST_PK_MODE | FLD_AUD_DST_ENABLE |
245b285192aSMauro Carvalho Chehab 		 FLD_AUD_CLK_ENABLE);
246b285192aSMauro Carvalho Chehab 
247b285192aSMauro Carvalho Chehab 	/*
248b285192aSMauro Carvalho Chehab 	pr_info("DEBUG: Start audio DMA, %d B/line, cmds_start(0x%x)= %d lines/FIFO, %d periods, %d byte buffer\n",
249b285192aSMauro Carvalho Chehab 		buf->bpl, audio_ch->cmds_start,
250b285192aSMauro Carvalho Chehab 		cx_read(audio_ch->cmds_start + 12)>>1,
251b285192aSMauro Carvalho Chehab 		chip->num_periods, buf->bpl * chip->num_periods);
252b285192aSMauro Carvalho Chehab 	*/
253b285192aSMauro Carvalho Chehab 
254b285192aSMauro Carvalho Chehab 	/* Enables corresponding bits at AUD_INT_STAT */
255b285192aSMauro Carvalho Chehab 	cx_write(AUD_A_INT_MSK, FLD_AUD_DST_RISCI1 | FLD_AUD_DST_OF |
256b285192aSMauro Carvalho Chehab 		 FLD_AUD_DST_SYNC | FLD_AUD_DST_OPC_ERR);
257b285192aSMauro Carvalho Chehab 
258b285192aSMauro Carvalho Chehab 	/* Clean any pending interrupt bits already set */
259b285192aSMauro Carvalho Chehab 	cx_write(AUD_A_INT_STAT, ~0);
260b285192aSMauro Carvalho Chehab 
261b285192aSMauro Carvalho Chehab 	/* enable audio irqs */
262b285192aSMauro Carvalho Chehab 	cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
263b285192aSMauro Carvalho Chehab 
264b285192aSMauro Carvalho Chehab 	/* Turn on audio downstream fifo and risc enable 0x101 */
265b285192aSMauro Carvalho Chehab 	tmp = cx_read(AUD_INT_DMA_CTL);
266b285192aSMauro Carvalho Chehab 	cx_set(AUD_INT_DMA_CTL, tmp |
267b285192aSMauro Carvalho Chehab 	       (FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN));
268b285192aSMauro Carvalho Chehab 
269b285192aSMauro Carvalho Chehab 	mdelay(100);
270b285192aSMauro Carvalho Chehab 	return 0;
271b285192aSMauro Carvalho Chehab }
272b285192aSMauro Carvalho Chehab 
273b285192aSMauro Carvalho Chehab /*
274b285192aSMauro Carvalho Chehab  * BOARD Specific: Resets audio DMA
275b285192aSMauro Carvalho Chehab  */
276b285192aSMauro Carvalho Chehab static int _cx25821_stop_audio_dma(struct cx25821_audio_dev *chip)
277b285192aSMauro Carvalho Chehab {
278b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = chip->dev;
279b285192aSMauro Carvalho Chehab 
280b285192aSMauro Carvalho Chehab 	/* stop dma */
281b285192aSMauro Carvalho Chehab 	cx_clear(AUD_INT_DMA_CTL,
282b285192aSMauro Carvalho Chehab 		 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
283b285192aSMauro Carvalho Chehab 
284b285192aSMauro Carvalho Chehab 	/* disable irqs */
285b285192aSMauro Carvalho Chehab 	cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
286b285192aSMauro Carvalho Chehab 	cx_clear(AUD_A_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
287b285192aSMauro Carvalho Chehab 		 AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
288b285192aSMauro Carvalho Chehab 
289b285192aSMauro Carvalho Chehab 	return 0;
290b285192aSMauro Carvalho Chehab }
291b285192aSMauro Carvalho Chehab 
292b285192aSMauro Carvalho Chehab #define MAX_IRQ_LOOP 50
293b285192aSMauro Carvalho Chehab 
294b285192aSMauro Carvalho Chehab /*
295b285192aSMauro Carvalho Chehab  * BOARD Specific: IRQ dma bits
296b285192aSMauro Carvalho Chehab  */
297b285192aSMauro Carvalho Chehab static char *cx25821_aud_irqs[32] = {
298b285192aSMauro Carvalho Chehab 	"dn_risci1", "up_risci1", "rds_dn_risc1",	/* 0-2 */
299b285192aSMauro Carvalho Chehab 	NULL,						/* reserved */
300b285192aSMauro Carvalho Chehab 	"dn_risci2", "up_risci2", "rds_dn_risc2",	/* 4-6 */
301b285192aSMauro Carvalho Chehab 	NULL,						/* reserved */
302b285192aSMauro Carvalho Chehab 	"dnf_of", "upf_uf", "rds_dnf_uf",		/* 8-10 */
303b285192aSMauro Carvalho Chehab 	NULL,						/* reserved */
304b285192aSMauro Carvalho Chehab 	"dn_sync", "up_sync", "rds_dn_sync",		/* 12-14 */
305b285192aSMauro Carvalho Chehab 	NULL,						/* reserved */
306b285192aSMauro Carvalho Chehab 	"opc_err", "par_err", "rip_err",		/* 16-18 */
307b285192aSMauro Carvalho Chehab 	"pci_abort", "ber_irq", "mchg_irq"		/* 19-21 */
308b285192aSMauro Carvalho Chehab };
309b285192aSMauro Carvalho Chehab 
310b285192aSMauro Carvalho Chehab /*
311b285192aSMauro Carvalho Chehab  * BOARD Specific: Threats IRQ audio specific calls
312b285192aSMauro Carvalho Chehab  */
313b285192aSMauro Carvalho Chehab static void cx25821_aud_irq(struct cx25821_audio_dev *chip, u32 status,
314b285192aSMauro Carvalho Chehab 			    u32 mask)
315b285192aSMauro Carvalho Chehab {
316b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = chip->dev;
317b285192aSMauro Carvalho Chehab 
318b285192aSMauro Carvalho Chehab 	if (0 == (status & mask))
319b285192aSMauro Carvalho Chehab 		return;
320b285192aSMauro Carvalho Chehab 
321b285192aSMauro Carvalho Chehab 	cx_write(AUD_A_INT_STAT, status);
322b285192aSMauro Carvalho Chehab 	if (debug > 1 || (status & mask & ~0xff))
323b285192aSMauro Carvalho Chehab 		cx25821_print_irqbits(dev->name, "irq aud", cx25821_aud_irqs,
324b285192aSMauro Carvalho Chehab 				ARRAY_SIZE(cx25821_aud_irqs), status, mask);
325b285192aSMauro Carvalho Chehab 
326b285192aSMauro Carvalho Chehab 	/* risc op code error */
327b285192aSMauro Carvalho Chehab 	if (status & AUD_INT_OPC_ERR) {
328b285192aSMauro Carvalho Chehab 		pr_warn("WARNING %s/1: Audio risc op code error\n", dev->name);
329b285192aSMauro Carvalho Chehab 
330b285192aSMauro Carvalho Chehab 		cx_clear(AUD_INT_DMA_CTL,
331b285192aSMauro Carvalho Chehab 			 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
332b285192aSMauro Carvalho Chehab 		cx25821_sram_channel_dump_audio(dev,
333b285192aSMauro Carvalho Chehab 				&cx25821_sram_channels[AUDIO_SRAM_CHANNEL]);
334b285192aSMauro Carvalho Chehab 	}
335b285192aSMauro Carvalho Chehab 	if (status & AUD_INT_DN_SYNC) {
336b285192aSMauro Carvalho Chehab 		pr_warn("WARNING %s: Downstream sync error!\n", dev->name);
337b285192aSMauro Carvalho Chehab 		cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
338b285192aSMauro Carvalho Chehab 		return;
339b285192aSMauro Carvalho Chehab 	}
340b285192aSMauro Carvalho Chehab 
341b285192aSMauro Carvalho Chehab 	/* risc1 downstream */
342b285192aSMauro Carvalho Chehab 	if (status & AUD_INT_DN_RISCI1) {
343b285192aSMauro Carvalho Chehab 		atomic_set(&chip->count, cx_read(AUD_A_GPCNT));
344b285192aSMauro Carvalho Chehab 		snd_pcm_period_elapsed(chip->substream);
345b285192aSMauro Carvalho Chehab 	}
346b285192aSMauro Carvalho Chehab }
347b285192aSMauro Carvalho Chehab 
348b285192aSMauro Carvalho Chehab /*
349b285192aSMauro Carvalho Chehab  * BOARD Specific: Handles IRQ calls
350b285192aSMauro Carvalho Chehab  */
351b285192aSMauro Carvalho Chehab static irqreturn_t cx25821_irq(int irq, void *dev_id)
352b285192aSMauro Carvalho Chehab {
353b285192aSMauro Carvalho Chehab 	struct cx25821_audio_dev *chip = dev_id;
354b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = chip->dev;
355b285192aSMauro Carvalho Chehab 	u32 status, pci_status;
356b285192aSMauro Carvalho Chehab 	u32 audint_status, audint_mask;
357b285192aSMauro Carvalho Chehab 	int loop, handled = 0;
358b285192aSMauro Carvalho Chehab 
359b285192aSMauro Carvalho Chehab 	audint_status = cx_read(AUD_A_INT_STAT);
360b285192aSMauro Carvalho Chehab 	audint_mask = cx_read(AUD_A_INT_MSK);
361b285192aSMauro Carvalho Chehab 	status = cx_read(PCI_INT_STAT);
362b285192aSMauro Carvalho Chehab 
363b285192aSMauro Carvalho Chehab 	for (loop = 0; loop < 1; loop++) {
364b285192aSMauro Carvalho Chehab 		status = cx_read(PCI_INT_STAT);
365b285192aSMauro Carvalho Chehab 		if (0 == status) {
366b285192aSMauro Carvalho Chehab 			status = cx_read(PCI_INT_STAT);
367b285192aSMauro Carvalho Chehab 			audint_status = cx_read(AUD_A_INT_STAT);
368b285192aSMauro Carvalho Chehab 			audint_mask = cx_read(AUD_A_INT_MSK);
369b285192aSMauro Carvalho Chehab 
370b285192aSMauro Carvalho Chehab 			if (status) {
371b285192aSMauro Carvalho Chehab 				handled = 1;
372b285192aSMauro Carvalho Chehab 				cx_write(PCI_INT_STAT, status);
373b285192aSMauro Carvalho Chehab 
374b285192aSMauro Carvalho Chehab 				cx25821_aud_irq(chip, audint_status,
375b285192aSMauro Carvalho Chehab 						audint_mask);
376b285192aSMauro Carvalho Chehab 				break;
377b285192aSMauro Carvalho Chehab 			} else {
378b285192aSMauro Carvalho Chehab 				goto out;
379b285192aSMauro Carvalho Chehab 			}
380b285192aSMauro Carvalho Chehab 		}
381b285192aSMauro Carvalho Chehab 
382b285192aSMauro Carvalho Chehab 		handled = 1;
383b285192aSMauro Carvalho Chehab 		cx_write(PCI_INT_STAT, status);
384b285192aSMauro Carvalho Chehab 
385b285192aSMauro Carvalho Chehab 		cx25821_aud_irq(chip, audint_status, audint_mask);
386b285192aSMauro Carvalho Chehab 	}
387b285192aSMauro Carvalho Chehab 
388b285192aSMauro Carvalho Chehab 	pci_status = cx_read(PCI_INT_STAT);
389b285192aSMauro Carvalho Chehab 
390b285192aSMauro Carvalho Chehab 	if (handled)
391b285192aSMauro Carvalho Chehab 		cx_write(PCI_INT_STAT, pci_status);
392b285192aSMauro Carvalho Chehab 
393b285192aSMauro Carvalho Chehab out:
394b285192aSMauro Carvalho Chehab 	return IRQ_RETVAL(handled);
395b285192aSMauro Carvalho Chehab }
396b285192aSMauro Carvalho Chehab 
397b285192aSMauro Carvalho Chehab static int dsp_buffer_free(struct cx25821_audio_dev *chip)
398b285192aSMauro Carvalho Chehab {
3995ede94c7SHans Verkuil 	struct cx25821_riscmem *risc = &chip->buf->risc;
4005ede94c7SHans Verkuil 
401b285192aSMauro Carvalho Chehab 	BUG_ON(!chip->dma_size);
402b285192aSMauro Carvalho Chehab 
403b285192aSMauro Carvalho Chehab 	dprintk(2, "Freeing buffer\n");
4048d8e6d60SHans Verkuil 	cx25821_alsa_dma_unmap(chip);
4058d8e6d60SHans Verkuil 	cx25821_alsa_dma_free(chip->buf);
4065ede94c7SHans Verkuil 	pci_free_consistent(chip->pci, risc->size, risc->cpu, risc->dma);
407b285192aSMauro Carvalho Chehab 	kfree(chip->buf);
408b285192aSMauro Carvalho Chehab 
4098d8e6d60SHans Verkuil 	chip->buf = NULL;
410b285192aSMauro Carvalho Chehab 	chip->dma_size = 0;
411b285192aSMauro Carvalho Chehab 
412b285192aSMauro Carvalho Chehab 	return 0;
413b285192aSMauro Carvalho Chehab }
414b285192aSMauro Carvalho Chehab 
415b285192aSMauro Carvalho Chehab /****************************************************************************
416b285192aSMauro Carvalho Chehab 				ALSA PCM Interface
417b285192aSMauro Carvalho Chehab  ****************************************************************************/
418b285192aSMauro Carvalho Chehab 
419b285192aSMauro Carvalho Chehab /*
420b285192aSMauro Carvalho Chehab  * Digital hardware definition
421b285192aSMauro Carvalho Chehab  */
422b285192aSMauro Carvalho Chehab #define DEFAULT_FIFO_SIZE	384
42347f14314SBhumika Goyal static const struct snd_pcm_hardware snd_cx25821_digital_hw = {
424b285192aSMauro Carvalho Chehab 	.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
425b285192aSMauro Carvalho Chehab 		SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID,
426b285192aSMauro Carvalho Chehab 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
427b285192aSMauro Carvalho Chehab 
428b285192aSMauro Carvalho Chehab 	.rates = SNDRV_PCM_RATE_48000,
429b285192aSMauro Carvalho Chehab 	.rate_min = 48000,
430b285192aSMauro Carvalho Chehab 	.rate_max = 48000,
431b285192aSMauro Carvalho Chehab 	.channels_min = 2,
432b285192aSMauro Carvalho Chehab 	.channels_max = 2,
433b285192aSMauro Carvalho Chehab 	/* Analog audio output will be full of clicks and pops if there
434b285192aSMauro Carvalho Chehab 	   are not exactly four lines in the SRAM FIFO buffer.  */
435b285192aSMauro Carvalho Chehab 	.period_bytes_min = DEFAULT_FIFO_SIZE / 3,
436b285192aSMauro Carvalho Chehab 	.period_bytes_max = DEFAULT_FIFO_SIZE / 3,
437b285192aSMauro Carvalho Chehab 	.periods_min = 1,
438b285192aSMauro Carvalho Chehab 	.periods_max = AUDIO_LINE_SIZE,
439b285192aSMauro Carvalho Chehab 	/* 128 * 128 = 16384 = 1024 * 16 */
440b285192aSMauro Carvalho Chehab 	.buffer_bytes_max = (AUDIO_LINE_SIZE * AUDIO_LINE_SIZE),
441b285192aSMauro Carvalho Chehab };
442b285192aSMauro Carvalho Chehab 
443b285192aSMauro Carvalho Chehab /*
444b285192aSMauro Carvalho Chehab  * audio pcm capture open callback
445b285192aSMauro Carvalho Chehab  */
446b285192aSMauro Carvalho Chehab static int snd_cx25821_pcm_open(struct snd_pcm_substream *substream)
447b285192aSMauro Carvalho Chehab {
448b285192aSMauro Carvalho Chehab 	struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
449b285192aSMauro Carvalho Chehab 	struct snd_pcm_runtime *runtime = substream->runtime;
450b285192aSMauro Carvalho Chehab 	int err;
451b285192aSMauro Carvalho Chehab 	unsigned int bpl = 0;
452b285192aSMauro Carvalho Chehab 
453b285192aSMauro Carvalho Chehab 	if (!chip) {
454b285192aSMauro Carvalho Chehab 		pr_err("DEBUG: cx25821 can't find device struct. Can't proceed with open\n");
455b285192aSMauro Carvalho Chehab 		return -ENODEV;
456b285192aSMauro Carvalho Chehab 	}
457b285192aSMauro Carvalho Chehab 
458b285192aSMauro Carvalho Chehab 	err = snd_pcm_hw_constraint_pow2(runtime, 0,
459b285192aSMauro Carvalho Chehab 					 SNDRV_PCM_HW_PARAM_PERIODS);
460b285192aSMauro Carvalho Chehab 	if (err < 0)
461b285192aSMauro Carvalho Chehab 		goto _error;
462b285192aSMauro Carvalho Chehab 
463b285192aSMauro Carvalho Chehab 	chip->substream = substream;
464b285192aSMauro Carvalho Chehab 
465b285192aSMauro Carvalho Chehab 	runtime->hw = snd_cx25821_digital_hw;
466b285192aSMauro Carvalho Chehab 
467b285192aSMauro Carvalho Chehab 	if (cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
468b285192aSMauro Carvalho Chehab 	    DEFAULT_FIFO_SIZE) {
469b285192aSMauro Carvalho Chehab 		/* since there are 3 audio Clusters */
470b285192aSMauro Carvalho Chehab 		bpl = cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 3;
471b285192aSMauro Carvalho Chehab 		bpl &= ~7;	/* must be multiple of 8 */
472b285192aSMauro Carvalho Chehab 
473b285192aSMauro Carvalho Chehab 		if (bpl > AUDIO_LINE_SIZE)
474b285192aSMauro Carvalho Chehab 			bpl = AUDIO_LINE_SIZE;
475b285192aSMauro Carvalho Chehab 
476b285192aSMauro Carvalho Chehab 		runtime->hw.period_bytes_min = bpl;
477b285192aSMauro Carvalho Chehab 		runtime->hw.period_bytes_max = bpl;
478b285192aSMauro Carvalho Chehab 	}
479b285192aSMauro Carvalho Chehab 
480b285192aSMauro Carvalho Chehab 	return 0;
481b285192aSMauro Carvalho Chehab _error:
482b285192aSMauro Carvalho Chehab 	dprintk(1, "Error opening PCM!\n");
483b285192aSMauro Carvalho Chehab 	return err;
484b285192aSMauro Carvalho Chehab }
485b285192aSMauro Carvalho Chehab 
486b285192aSMauro Carvalho Chehab /*
487b285192aSMauro Carvalho Chehab  * audio close callback
488b285192aSMauro Carvalho Chehab  */
489b285192aSMauro Carvalho Chehab static int snd_cx25821_close(struct snd_pcm_substream *substream)
490b285192aSMauro Carvalho Chehab {
491b285192aSMauro Carvalho Chehab 	return 0;
492b285192aSMauro Carvalho Chehab }
493b285192aSMauro Carvalho Chehab 
494b285192aSMauro Carvalho Chehab /*
495b285192aSMauro Carvalho Chehab  * hw_params callback
496b285192aSMauro Carvalho Chehab  */
497b285192aSMauro Carvalho Chehab static int snd_cx25821_hw_params(struct snd_pcm_substream *substream,
498b285192aSMauro Carvalho Chehab 				 struct snd_pcm_hw_params *hw_params)
499b285192aSMauro Carvalho Chehab {
500b285192aSMauro Carvalho Chehab 	struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
501b285192aSMauro Carvalho Chehab 	struct cx25821_audio_buffer *buf;
502b285192aSMauro Carvalho Chehab 	int ret;
503b285192aSMauro Carvalho Chehab 
504b285192aSMauro Carvalho Chehab 	if (substream->runtime->dma_area) {
505b285192aSMauro Carvalho Chehab 		dsp_buffer_free(chip);
506b285192aSMauro Carvalho Chehab 		substream->runtime->dma_area = NULL;
507b285192aSMauro Carvalho Chehab 	}
508b285192aSMauro Carvalho Chehab 
509b285192aSMauro Carvalho Chehab 	chip->period_size = params_period_bytes(hw_params);
510b285192aSMauro Carvalho Chehab 	chip->num_periods = params_periods(hw_params);
511b285192aSMauro Carvalho Chehab 	chip->dma_size = chip->period_size * params_periods(hw_params);
512b285192aSMauro Carvalho Chehab 
513b285192aSMauro Carvalho Chehab 	BUG_ON(!chip->dma_size);
514b285192aSMauro Carvalho Chehab 	BUG_ON(chip->num_periods & (chip->num_periods - 1));
515b285192aSMauro Carvalho Chehab 
516b285192aSMauro Carvalho Chehab 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
517b285192aSMauro Carvalho Chehab 	if (NULL == buf)
518b285192aSMauro Carvalho Chehab 		return -ENOMEM;
519b285192aSMauro Carvalho Chehab 
520b285192aSMauro Carvalho Chehab 	if (chip->period_size > AUDIO_LINE_SIZE)
521b285192aSMauro Carvalho Chehab 		chip->period_size = AUDIO_LINE_SIZE;
522b285192aSMauro Carvalho Chehab 
523b285192aSMauro Carvalho Chehab 	buf->bpl = chip->period_size;
5248d8e6d60SHans Verkuil 	chip->buf = buf;
525b285192aSMauro Carvalho Chehab 
5268d8e6d60SHans Verkuil 	ret = cx25821_alsa_dma_init(chip,
527b285192aSMauro Carvalho Chehab 			(PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
528b285192aSMauro Carvalho Chehab 	if (ret < 0)
529b285192aSMauro Carvalho Chehab 		goto error;
530b285192aSMauro Carvalho Chehab 
5318d8e6d60SHans Verkuil 	ret = cx25821_alsa_dma_map(chip);
532b285192aSMauro Carvalho Chehab 	if (ret < 0)
533b285192aSMauro Carvalho Chehab 		goto error;
534b285192aSMauro Carvalho Chehab 
5358d8e6d60SHans Verkuil 	ret = cx25821_risc_databuffer_audio(chip->pci, &buf->risc, buf->sglist,
536b285192aSMauro Carvalho Chehab 			chip->period_size, chip->num_periods, 1);
537b285192aSMauro Carvalho Chehab 	if (ret < 0) {
538b285192aSMauro Carvalho Chehab 		pr_info("DEBUG: ERROR after cx25821_risc_databuffer_audio()\n");
539b285192aSMauro Carvalho Chehab 		goto error;
540b285192aSMauro Carvalho Chehab 	}
541b285192aSMauro Carvalho Chehab 
542b285192aSMauro Carvalho Chehab 	/* Loop back to start of program */
543b285192aSMauro Carvalho Chehab 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
544b285192aSMauro Carvalho Chehab 	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
545b285192aSMauro Carvalho Chehab 	buf->risc.jmp[2] = cpu_to_le32(0);	/* bits 63-32 */
546b285192aSMauro Carvalho Chehab 
5478d8e6d60SHans Verkuil 	substream->runtime->dma_area = chip->buf->vaddr;
548b285192aSMauro Carvalho Chehab 	substream->runtime->dma_bytes = chip->dma_size;
549b285192aSMauro Carvalho Chehab 	substream->runtime->dma_addr = 0;
550b285192aSMauro Carvalho Chehab 
551b285192aSMauro Carvalho Chehab 	return 0;
552b285192aSMauro Carvalho Chehab 
553b285192aSMauro Carvalho Chehab error:
5548d8e6d60SHans Verkuil 	chip->buf = NULL;
555b285192aSMauro Carvalho Chehab 	kfree(buf);
556b285192aSMauro Carvalho Chehab 	return ret;
557b285192aSMauro Carvalho Chehab }
558b285192aSMauro Carvalho Chehab 
559b285192aSMauro Carvalho Chehab /*
560b285192aSMauro Carvalho Chehab  * hw free callback
561b285192aSMauro Carvalho Chehab  */
562b285192aSMauro Carvalho Chehab static int snd_cx25821_hw_free(struct snd_pcm_substream *substream)
563b285192aSMauro Carvalho Chehab {
564b285192aSMauro Carvalho Chehab 	struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
565b285192aSMauro Carvalho Chehab 
566b285192aSMauro Carvalho Chehab 	if (substream->runtime->dma_area) {
567b285192aSMauro Carvalho Chehab 		dsp_buffer_free(chip);
568b285192aSMauro Carvalho Chehab 		substream->runtime->dma_area = NULL;
569b285192aSMauro Carvalho Chehab 	}
570b285192aSMauro Carvalho Chehab 
571b285192aSMauro Carvalho Chehab 	return 0;
572b285192aSMauro Carvalho Chehab }
573b285192aSMauro Carvalho Chehab 
574b285192aSMauro Carvalho Chehab /*
575b285192aSMauro Carvalho Chehab  * prepare callback
576b285192aSMauro Carvalho Chehab  */
577b285192aSMauro Carvalho Chehab static int snd_cx25821_prepare(struct snd_pcm_substream *substream)
578b285192aSMauro Carvalho Chehab {
579b285192aSMauro Carvalho Chehab 	return 0;
580b285192aSMauro Carvalho Chehab }
581b285192aSMauro Carvalho Chehab 
582b285192aSMauro Carvalho Chehab /*
583b285192aSMauro Carvalho Chehab  * trigger callback
584b285192aSMauro Carvalho Chehab  */
585b285192aSMauro Carvalho Chehab static int snd_cx25821_card_trigger(struct snd_pcm_substream *substream,
586b285192aSMauro Carvalho Chehab 				    int cmd)
587b285192aSMauro Carvalho Chehab {
588b285192aSMauro Carvalho Chehab 	struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
589b285192aSMauro Carvalho Chehab 	int err = 0;
590b285192aSMauro Carvalho Chehab 
591b285192aSMauro Carvalho Chehab 	/* Local interrupts are already disabled by ALSA */
592b285192aSMauro Carvalho Chehab 	spin_lock(&chip->reg_lock);
593b285192aSMauro Carvalho Chehab 
594b285192aSMauro Carvalho Chehab 	switch (cmd) {
595b285192aSMauro Carvalho Chehab 	case SNDRV_PCM_TRIGGER_START:
596b285192aSMauro Carvalho Chehab 		err = _cx25821_start_audio_dma(chip);
597b285192aSMauro Carvalho Chehab 		break;
598b285192aSMauro Carvalho Chehab 	case SNDRV_PCM_TRIGGER_STOP:
599b285192aSMauro Carvalho Chehab 		err = _cx25821_stop_audio_dma(chip);
600b285192aSMauro Carvalho Chehab 		break;
601b285192aSMauro Carvalho Chehab 	default:
602b285192aSMauro Carvalho Chehab 		err = -EINVAL;
603b285192aSMauro Carvalho Chehab 		break;
604b285192aSMauro Carvalho Chehab 	}
605b285192aSMauro Carvalho Chehab 
606b285192aSMauro Carvalho Chehab 	spin_unlock(&chip->reg_lock);
607b285192aSMauro Carvalho Chehab 
608b285192aSMauro Carvalho Chehab 	return err;
609b285192aSMauro Carvalho Chehab }
610b285192aSMauro Carvalho Chehab 
611b285192aSMauro Carvalho Chehab /*
612b285192aSMauro Carvalho Chehab  * pointer callback
613b285192aSMauro Carvalho Chehab  */
614b285192aSMauro Carvalho Chehab static snd_pcm_uframes_t snd_cx25821_pointer(struct snd_pcm_substream
615b285192aSMauro Carvalho Chehab 					     *substream)
616b285192aSMauro Carvalho Chehab {
617b285192aSMauro Carvalho Chehab 	struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
618b285192aSMauro Carvalho Chehab 	struct snd_pcm_runtime *runtime = substream->runtime;
619b285192aSMauro Carvalho Chehab 	u16 count;
620b285192aSMauro Carvalho Chehab 
621b285192aSMauro Carvalho Chehab 	count = atomic_read(&chip->count);
622b285192aSMauro Carvalho Chehab 
623b285192aSMauro Carvalho Chehab 	return runtime->period_size * (count & (runtime->periods - 1));
624b285192aSMauro Carvalho Chehab }
625b285192aSMauro Carvalho Chehab 
626b285192aSMauro Carvalho Chehab /*
627b285192aSMauro Carvalho Chehab  * page callback (needed for mmap)
628b285192aSMauro Carvalho Chehab  */
629b285192aSMauro Carvalho Chehab static struct page *snd_cx25821_page(struct snd_pcm_substream *substream,
630b285192aSMauro Carvalho Chehab 				     unsigned long offset)
631b285192aSMauro Carvalho Chehab {
632b285192aSMauro Carvalho Chehab 	void *pageptr = substream->runtime->dma_area + offset;
633b285192aSMauro Carvalho Chehab 
634b285192aSMauro Carvalho Chehab 	return vmalloc_to_page(pageptr);
635b285192aSMauro Carvalho Chehab }
636b285192aSMauro Carvalho Chehab 
637b285192aSMauro Carvalho Chehab /*
638b285192aSMauro Carvalho Chehab  * operators
639b285192aSMauro Carvalho Chehab  */
6405c8d8c01SJulia Lawall static const struct snd_pcm_ops snd_cx25821_pcm_ops = {
641b285192aSMauro Carvalho Chehab 	.open = snd_cx25821_pcm_open,
642b285192aSMauro Carvalho Chehab 	.close = snd_cx25821_close,
643b285192aSMauro Carvalho Chehab 	.hw_params = snd_cx25821_hw_params,
644b285192aSMauro Carvalho Chehab 	.hw_free = snd_cx25821_hw_free,
645b285192aSMauro Carvalho Chehab 	.prepare = snd_cx25821_prepare,
646b285192aSMauro Carvalho Chehab 	.trigger = snd_cx25821_card_trigger,
647b285192aSMauro Carvalho Chehab 	.pointer = snd_cx25821_pointer,
648b285192aSMauro Carvalho Chehab 	.page = snd_cx25821_page,
649b285192aSMauro Carvalho Chehab };
650b285192aSMauro Carvalho Chehab 
651b285192aSMauro Carvalho Chehab /*
652b285192aSMauro Carvalho Chehab  * ALSA create a PCM device:  Called when initializing the board.
653b285192aSMauro Carvalho Chehab  * Sets up the name and hooks up the callbacks
654b285192aSMauro Carvalho Chehab  */
655b285192aSMauro Carvalho Chehab static int snd_cx25821_pcm(struct cx25821_audio_dev *chip, int device,
656b285192aSMauro Carvalho Chehab 			   char *name)
657b285192aSMauro Carvalho Chehab {
658b285192aSMauro Carvalho Chehab 	struct snd_pcm *pcm;
659b285192aSMauro Carvalho Chehab 	int err;
660b285192aSMauro Carvalho Chehab 
661b285192aSMauro Carvalho Chehab 	err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
662b285192aSMauro Carvalho Chehab 	if (err < 0) {
663b285192aSMauro Carvalho Chehab 		pr_info("ERROR: FAILED snd_pcm_new() in %s\n", __func__);
664b285192aSMauro Carvalho Chehab 		return err;
665b285192aSMauro Carvalho Chehab 	}
666b285192aSMauro Carvalho Chehab 	pcm->private_data = chip;
667b285192aSMauro Carvalho Chehab 	pcm->info_flags = 0;
668cc1e6315SMauro Carvalho Chehab 	strscpy(pcm->name, name, sizeof(pcm->name));
669b285192aSMauro Carvalho Chehab 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx25821_pcm_ops);
670b285192aSMauro Carvalho Chehab 
671b285192aSMauro Carvalho Chehab 	return 0;
672b285192aSMauro Carvalho Chehab }
673b285192aSMauro Carvalho Chehab 
674b285192aSMauro Carvalho Chehab /****************************************************************************
675b285192aSMauro Carvalho Chehab 			Basic Flow for Sound Devices
676b285192aSMauro Carvalho Chehab  ****************************************************************************/
677b285192aSMauro Carvalho Chehab 
678b285192aSMauro Carvalho Chehab /*
679b285192aSMauro Carvalho Chehab  * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
680b285192aSMauro Carvalho Chehab  * Only boards with eeprom and byte 1 at eeprom=1 have it
681b285192aSMauro Carvalho Chehab  */
682b285192aSMauro Carvalho Chehab 
683114e877aSMauro Carvalho Chehab static const struct pci_device_id __maybe_unused cx25821_audio_pci_tbl[] = {
684b285192aSMauro Carvalho Chehab 	{0x14f1, 0x0920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
685b285192aSMauro Carvalho Chehab 	{0,}
686b285192aSMauro Carvalho Chehab };
687b285192aSMauro Carvalho Chehab 
688b285192aSMauro Carvalho Chehab MODULE_DEVICE_TABLE(pci, cx25821_audio_pci_tbl);
689b285192aSMauro Carvalho Chehab 
690b285192aSMauro Carvalho Chehab /*
691b285192aSMauro Carvalho Chehab  * Alsa Constructor - Component probe
692b285192aSMauro Carvalho Chehab  */
693b285192aSMauro Carvalho Chehab static int cx25821_audio_initdev(struct cx25821_dev *dev)
694b285192aSMauro Carvalho Chehab {
695b285192aSMauro Carvalho Chehab 	struct snd_card *card;
696b285192aSMauro Carvalho Chehab 	struct cx25821_audio_dev *chip;
697b285192aSMauro Carvalho Chehab 	int err;
698b285192aSMauro Carvalho Chehab 
699b285192aSMauro Carvalho Chehab 	if (devno >= SNDRV_CARDS) {
700b285192aSMauro Carvalho Chehab 		pr_info("DEBUG ERROR: devno >= SNDRV_CARDS %s\n", __func__);
701b285192aSMauro Carvalho Chehab 		return -ENODEV;
702b285192aSMauro Carvalho Chehab 	}
703b285192aSMauro Carvalho Chehab 
704b285192aSMauro Carvalho Chehab 	if (!enable[devno]) {
705b285192aSMauro Carvalho Chehab 		++devno;
706b285192aSMauro Carvalho Chehab 		pr_info("DEBUG ERROR: !enable[devno] %s\n", __func__);
707b285192aSMauro Carvalho Chehab 		return -ENOENT;
708b285192aSMauro Carvalho Chehab 	}
709b285192aSMauro Carvalho Chehab 
710e7356888STakashi Iwai 	err = snd_card_new(&dev->pci->dev, index[devno], id[devno],
711e7356888STakashi Iwai 			   THIS_MODULE,
712b285192aSMauro Carvalho Chehab 			   sizeof(struct cx25821_audio_dev), &card);
713b285192aSMauro Carvalho Chehab 	if (err < 0) {
714b285192aSMauro Carvalho Chehab 		pr_info("DEBUG ERROR: cannot create snd_card_new in %s\n",
715b285192aSMauro Carvalho Chehab 			__func__);
716b285192aSMauro Carvalho Chehab 		return err;
717b285192aSMauro Carvalho Chehab 	}
718b285192aSMauro Carvalho Chehab 
719cc1e6315SMauro Carvalho Chehab 	strscpy(card->driver, "cx25821", sizeof(card->driver));
720b285192aSMauro Carvalho Chehab 
721b285192aSMauro Carvalho Chehab 	/* Card "creation" */
722b285192aSMauro Carvalho Chehab 	chip = card->private_data;
723b285192aSMauro Carvalho Chehab 	spin_lock_init(&chip->reg_lock);
724b285192aSMauro Carvalho Chehab 
725b285192aSMauro Carvalho Chehab 	chip->dev = dev;
726b285192aSMauro Carvalho Chehab 	chip->card = card;
727b285192aSMauro Carvalho Chehab 	chip->pci = dev->pci;
728b285192aSMauro Carvalho Chehab 	chip->iobase = pci_resource_start(dev->pci, 0);
729b285192aSMauro Carvalho Chehab 
730b285192aSMauro Carvalho Chehab 	chip->irq = dev->pci->irq;
731b285192aSMauro Carvalho Chehab 
732b285192aSMauro Carvalho Chehab 	err = request_irq(dev->pci->irq, cx25821_irq,
733b285192aSMauro Carvalho Chehab 			  IRQF_SHARED, chip->dev->name, chip);
734b285192aSMauro Carvalho Chehab 
735b285192aSMauro Carvalho Chehab 	if (err < 0) {
736b285192aSMauro Carvalho Chehab 		pr_err("ERROR %s: can't get IRQ %d for ALSA\n", chip->dev->name,
737b285192aSMauro Carvalho Chehab 			dev->pci->irq);
738b285192aSMauro Carvalho Chehab 		goto error;
739b285192aSMauro Carvalho Chehab 	}
740b285192aSMauro Carvalho Chehab 
741b285192aSMauro Carvalho Chehab 	err = snd_cx25821_pcm(chip, 0, "cx25821 Digital");
742b285192aSMauro Carvalho Chehab 	if (err < 0) {
743b285192aSMauro Carvalho Chehab 		pr_info("DEBUG ERROR: cannot create snd_cx25821_pcm %s\n",
744b285192aSMauro Carvalho Chehab 			__func__);
745b285192aSMauro Carvalho Chehab 		goto error;
746b285192aSMauro Carvalho Chehab 	}
747b285192aSMauro Carvalho Chehab 
748cc1e6315SMauro Carvalho Chehab 	strscpy(card->shortname, "cx25821", sizeof(card->shortname));
749b285192aSMauro Carvalho Chehab 	sprintf(card->longname, "%s at 0x%lx irq %d", chip->dev->name,
750b285192aSMauro Carvalho Chehab 		chip->iobase, chip->irq);
751cc1e6315SMauro Carvalho Chehab 	strscpy(card->mixername, "CX25821", sizeof(card->mixername));
752b285192aSMauro Carvalho Chehab 
753b285192aSMauro Carvalho Chehab 	pr_info("%s/%i: ALSA support for cx25821 boards\n", card->driver,
754b285192aSMauro Carvalho Chehab 		devno);
755b285192aSMauro Carvalho Chehab 
756b285192aSMauro Carvalho Chehab 	err = snd_card_register(card);
757b285192aSMauro Carvalho Chehab 	if (err < 0) {
758b285192aSMauro Carvalho Chehab 		pr_info("DEBUG ERROR: cannot register sound card %s\n",
759b285192aSMauro Carvalho Chehab 			__func__);
760b285192aSMauro Carvalho Chehab 		goto error;
761b285192aSMauro Carvalho Chehab 	}
762b285192aSMauro Carvalho Chehab 
763a8f35ce3SHans Verkuil 	dev->card = card;
764b285192aSMauro Carvalho Chehab 	devno++;
765b285192aSMauro Carvalho Chehab 	return 0;
766b285192aSMauro Carvalho Chehab 
767b285192aSMauro Carvalho Chehab error:
768b285192aSMauro Carvalho Chehab 	snd_card_free(card);
769b285192aSMauro Carvalho Chehab 	return err;
770b285192aSMauro Carvalho Chehab }
771b285192aSMauro Carvalho Chehab 
772b285192aSMauro Carvalho Chehab /****************************************************************************
773b285192aSMauro Carvalho Chehab 				LINUX MODULE INIT
774b285192aSMauro Carvalho Chehab  ****************************************************************************/
775a8f35ce3SHans Verkuil 
776a8f35ce3SHans Verkuil static int cx25821_alsa_exit_callback(struct device *dev, void *data)
777a8f35ce3SHans Verkuil {
778a8f35ce3SHans Verkuil 	struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
779a8f35ce3SHans Verkuil 	struct cx25821_dev *cxdev = get_cx25821(v4l2_dev);
780a8f35ce3SHans Verkuil 
781a8f35ce3SHans Verkuil 	snd_card_free(cxdev->card);
782a8f35ce3SHans Verkuil 	return 0;
783a8f35ce3SHans Verkuil }
784a8f35ce3SHans Verkuil 
785b285192aSMauro Carvalho Chehab static void cx25821_audio_fini(void)
786b285192aSMauro Carvalho Chehab {
787a8f35ce3SHans Verkuil 	struct device_driver *drv = driver_find("cx25821", &pci_bus_type);
788a8f35ce3SHans Verkuil 	int ret;
789a8f35ce3SHans Verkuil 
790a8f35ce3SHans Verkuil 	ret = driver_for_each_device(drv, NULL, NULL, cx25821_alsa_exit_callback);
791f327cabfSMauro Carvalho Chehab 	if (ret)
792f327cabfSMauro Carvalho Chehab 		pr_err("%s failed to find a cx25821 driver.\n", __func__);
793a8f35ce3SHans Verkuil }
794a8f35ce3SHans Verkuil 
795a8f35ce3SHans Verkuil static int cx25821_alsa_init_callback(struct device *dev, void *data)
796a8f35ce3SHans Verkuil {
797a8f35ce3SHans Verkuil 	struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
798a8f35ce3SHans Verkuil 	struct cx25821_dev *cxdev = get_cx25821(v4l2_dev);
799a8f35ce3SHans Verkuil 
800a8f35ce3SHans Verkuil 	cx25821_audio_initdev(cxdev);
801a8f35ce3SHans Verkuil 	return 0;
802b285192aSMauro Carvalho Chehab }
803b285192aSMauro Carvalho Chehab 
804b285192aSMauro Carvalho Chehab /*
805b285192aSMauro Carvalho Chehab  * Module initializer
806b285192aSMauro Carvalho Chehab  *
807b285192aSMauro Carvalho Chehab  * Loops through present saa7134 cards, and assigns an ALSA device
808b285192aSMauro Carvalho Chehab  * to each one
809b285192aSMauro Carvalho Chehab  *
810b285192aSMauro Carvalho Chehab  */
811b285192aSMauro Carvalho Chehab static int cx25821_alsa_init(void)
812b285192aSMauro Carvalho Chehab {
813a8f35ce3SHans Verkuil 	struct device_driver *drv = driver_find("cx25821", &pci_bus_type);
814b285192aSMauro Carvalho Chehab 
815a8f35ce3SHans Verkuil 	return driver_for_each_device(drv, NULL, NULL, cx25821_alsa_init_callback);
816b285192aSMauro Carvalho Chehab 
817b285192aSMauro Carvalho Chehab }
818b285192aSMauro Carvalho Chehab 
819b285192aSMauro Carvalho Chehab late_initcall(cx25821_alsa_init);
820b285192aSMauro Carvalho Chehab module_exit(cx25821_audio_fini);
821