blob: 680a9935d64ea01029e14e320d98ab29fcf7052e [file] [log] [blame]
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -03001/*
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
Joe Perches36d89f72010-11-07 17:48:21 -030023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030025#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030033
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -030034#include <linux/delay.h>
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030035#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
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030042#include "cx25821.h"
43#include "cx25821-reg.h"
44
45#define AUDIO_SRAM_CHANNEL SRAM_CH08
46
Joe Perches36d89f72010-11-07 17:48:21 -030047#define dprintk(level, fmt, arg...) \
48do { \
49 if (debug >= level) \
50 pr_info("%s/1: " fmt, chip->dev->name, ##arg); \
51} while (0)
52#define dprintk_core(level, fmt, arg...) \
53do { \
54 if (debug >= level) \
55 printk(KERN_DEBUG "%s/1: " fmt, chip->dev->name, ##arg); \
56} while (0)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030057
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030058/****************************************************************************
59 Data type declarations - Can be moded to a header file later
60 ****************************************************************************/
61
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030062static struct snd_card *snd_cx25821_cards[SNDRV_CARDS];
63static int devno;
64
Laurent Pinchartfecfede2010-05-11 10:36:31 -030065struct cx25821_audio_buffer {
66 unsigned int bpl;
67 struct btcx_riscmem risc;
68 struct videobuf_dmabuf dma;
69};
70
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030071struct cx25821_audio_dev {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030072 struct cx25821_dev *dev;
73 struct cx25821_dmaqueue q;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030074
75 /* pci i/o */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030076 struct pci_dev *pci;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030077
78 /* audio controls */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030079 int irq;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030080
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030081 struct snd_card *card;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030082
83 unsigned long iobase;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030084 spinlock_t reg_lock;
85 atomic_t count;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030086
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030087 unsigned int dma_size;
88 unsigned int period_size;
89 unsigned int num_periods;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030090
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030091 struct videobuf_dmabuf *dma_risc;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030092
Laurent Pinchartfecfede2010-05-11 10:36:31 -030093 struct cx25821_audio_buffer *buf;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030094
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030095 struct snd_pcm_substream *substream;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030096};
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030097
98
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030099/****************************************************************************
100 Module global static vars
101 ****************************************************************************/
102
103static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
104static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300105static int enable[SNDRV_CARDS] = { 1, [1 ... (SNDRV_CARDS - 1)] = 1 };
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300106
107module_param_array(enable, bool, NULL, 0444);
108MODULE_PARM_DESC(enable, "Enable cx25821 soundcard. default enabled.");
109
110module_param_array(index, int, NULL, 0444);
111MODULE_PARM_DESC(index, "Index value for cx25821 capture interface(s).");
112
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300113/****************************************************************************
114 Module macros
115 ****************************************************************************/
116
117MODULE_DESCRIPTION("ALSA driver module for cx25821 based capture cards");
118MODULE_AUTHOR("Hiep Huynh");
119MODULE_LICENSE("GPL");
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300120MODULE_SUPPORTED_DEVICE("{{Conexant,25821}"); /* "{{Conexant,23881}," */
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300121
122static unsigned int debug;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300123module_param(debug, int, 0644);
124MODULE_PARM_DESC(debug, "enable debug messages");
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300125
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)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300133#define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300134#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
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300150static int _cx25821_start_audio_dma(struct cx25821_audio_dev *chip)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300151{
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300152 struct cx25821_audio_buffer *buf = chip->buf;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300153 struct cx25821_dev *dev = chip->dev;
154 struct sram_channel *audio_ch =
155 &cx25821_sram_channels[AUDIO_SRAM_CHANNEL];
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300156 u32 tmp = 0;
157
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300158 /* enable output on the GPIO 0 for the MCLK ADC (Audio) */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300159 cx25821_set_gpiopin_direction(chip->dev, 0, 0);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300160
161 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300162 cx_clear(AUD_INT_DMA_CTL,
163 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300164
165 /* setup fifo + format - out channel */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300166 cx25821_sram_channel_setup_audio(chip->dev, audio_ch, buf->bpl,
167 buf->risc.dma);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300168
169 /* sets bpl size */
170 cx_write(AUD_A_LNGTH, buf->bpl);
171
172 /* reset counter */
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300173 /* GP_COUNT_CONTROL_RESET = 0x3 */
174 cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300175 atomic_set(&chip->count, 0);
176
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300177 /* Set the input mode to 16-bit */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300178 tmp = cx_read(AUD_A_CFG);
179 cx_write(AUD_A_CFG,
180 tmp | FLD_AUD_DST_PK_MODE | FLD_AUD_DST_ENABLE |
181 FLD_AUD_CLK_ENABLE);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300182
Joe Perches36d89f72010-11-07 17:48:21 -0300183 /*
184 pr_info("DEBUG: Start audio DMA, %d B/line, cmds_start(0x%x)= %d lines/FIFO, %d periods, %d byte buffer\n",
185 buf->bpl, audio_ch->cmds_start,
186 cx_read(audio_ch->cmds_start + 12)>>1,
187 chip->num_periods, buf->bpl * chip->num_periods);
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300188 */
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300189
190 /* Enables corresponding bits at AUD_INT_STAT */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300191 cx_write(AUD_A_INT_MSK,
192 FLD_AUD_DST_RISCI1 | FLD_AUD_DST_OF | FLD_AUD_DST_SYNC |
193 FLD_AUD_DST_OPC_ERR);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300194
195 /* Clean any pending interrupt bits already set */
196 cx_write(AUD_A_INT_STAT, ~0);
197
198 /* enable audio irqs */
199 cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
200
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300201 /* Turn on audio downstream fifo and risc enable 0x101 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300202 tmp = cx_read(AUD_INT_DMA_CTL);
203 cx_set(AUD_INT_DMA_CTL,
204 tmp | (FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN));
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300205
206 mdelay(100);
207 return 0;
208}
209
210/*
211 * BOARD Specific: Resets audio DMA
212 */
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300213static int _cx25821_stop_audio_dma(struct cx25821_audio_dev *chip)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300214{
215 struct cx25821_dev *dev = chip->dev;
216
217 /* stop dma */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300218 cx_clear(AUD_INT_DMA_CTL,
219 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300220
221 /* disable irqs */
222 cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300223 cx_clear(AUD_A_INT_MSK,
224 AUD_INT_OPC_ERR | AUD_INT_DN_SYNC | AUD_INT_DN_RISCI2 |
225 AUD_INT_DN_RISCI1);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300226
227 return 0;
228}
229
230#define MAX_IRQ_LOOP 50
231
232/*
233 * BOARD Specific: IRQ dma bits
234 */
235static char *cx25821_aud_irqs[32] = {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300236 "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */
Leonid V. Fedorenchik767f1d42011-10-22 01:43:21 -0300237 NULL, /* reserved */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300238 "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */
Leonid V. Fedorenchik767f1d42011-10-22 01:43:21 -0300239 NULL, /* reserved */
240 "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */
241 NULL, /* reserved */
242 "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */
243 NULL, /* reserved */
244 "opc_err", "par_err", "rip_err", /* 16-18 */
245 "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300246};
247
248/*
249 * BOARD Specific: Threats IRQ audio specific calls
250 */
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300251static void cx25821_aud_irq(struct cx25821_audio_dev *chip, u32 status,
252 u32 mask)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300253{
254 struct cx25821_dev *dev = chip->dev;
255
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300256 if (0 == (status & mask))
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300257 return;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300258
259 cx_write(AUD_A_INT_STAT, status);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300260 if (debug > 1 || (status & mask & ~0xff))
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300261 cx25821_print_irqbits(dev->name, "irq aud",
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300262 cx25821_aud_irqs,
263 ARRAY_SIZE(cx25821_aud_irqs), status,
264 mask);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300265
266 /* risc op code error */
267 if (status & AUD_INT_OPC_ERR) {
Joe Perches36d89f72010-11-07 17:48:21 -0300268 pr_warn("WARNING %s/1: Audio risc op code error\n", dev->name);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300269
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300270 cx_clear(AUD_INT_DMA_CTL,
271 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
272 cx25821_sram_channel_dump_audio(dev,
273 &cx25821_sram_channels
274 [AUDIO_SRAM_CHANNEL]);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300275 }
276 if (status & AUD_INT_DN_SYNC) {
Joe Perches36d89f72010-11-07 17:48:21 -0300277 pr_warn("WARNING %s: Downstream sync error!\n", dev->name);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300278 cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
279 return;
280 }
281
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300282 /* risc1 downstream */
283 if (status & AUD_INT_DN_RISCI1) {
284 atomic_set(&chip->count, cx_read(AUD_A_GPCNT));
285 snd_pcm_period_elapsed(chip->substream);
286 }
287}
288
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300289/*
290 * BOARD Specific: Handles IRQ calls
291 */
292static irqreturn_t cx25821_irq(int irq, void *dev_id)
293{
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300294 struct cx25821_audio_dev *chip = dev_id;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300295 struct cx25821_dev *dev = chip->dev;
296 u32 status, pci_status;
297 u32 audint_status, audint_mask;
298 int loop, handled = 0;
299 int audint_count = 0;
300
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300301 audint_status = cx_read(AUD_A_INT_STAT);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300302 audint_mask = cx_read(AUD_A_INT_MSK);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300303 audint_count = cx_read(AUD_A_GPCNT);
304 status = cx_read(PCI_INT_STAT);
305
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300306 for (loop = 0; loop < 1; loop++) {
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300307 status = cx_read(PCI_INT_STAT);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300308 if (0 == status) {
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300309 status = cx_read(PCI_INT_STAT);
310 audint_status = cx_read(AUD_A_INT_STAT);
311 audint_mask = cx_read(AUD_A_INT_MSK);
312
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300313 if (status) {
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300314 handled = 1;
315 cx_write(PCI_INT_STAT, status);
316
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300317 cx25821_aud_irq(chip, audint_status,
318 audint_mask);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300319 break;
Leonid V. Fedorenchik0ef80072011-10-22 01:43:22 -0300320 } else {
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300321 goto out;
Leonid V. Fedorenchik0ef80072011-10-22 01:43:22 -0300322 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300323 }
324
325 handled = 1;
326 cx_write(PCI_INT_STAT, status);
327
328 cx25821_aud_irq(chip, audint_status, audint_mask);
329 }
330
331 pci_status = cx_read(PCI_INT_STAT);
332
333 if (handled)
334 cx_write(PCI_INT_STAT, pci_status);
335
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300336out:
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300337 return IRQ_RETVAL(handled);
338}
339
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300340static int dsp_buffer_free(struct cx25821_audio_dev *chip)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300341{
342 BUG_ON(!chip->dma_size);
343
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300344 dprintk(2, "Freeing buffer\n");
Laurent Pinchart95268402010-05-11 10:36:30 -0300345 videobuf_dma_unmap(&chip->pci->dev, chip->dma_risc);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300346 videobuf_dma_free(chip->dma_risc);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300347 btcx_riscmem_free(chip->pci, &chip->buf->risc);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300348 kfree(chip->buf);
349
350 chip->dma_risc = NULL;
351 chip->dma_size = 0;
352
353 return 0;
354}
355
356/****************************************************************************
357 ALSA PCM Interface
358 ****************************************************************************/
359
360/*
361 * Digital hardware definition
362 */
363#define DEFAULT_FIFO_SIZE 384
364static struct snd_pcm_hardware snd_cx25821_digital_hw = {
365 .info = SNDRV_PCM_INFO_MMAP |
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300366 SNDRV_PCM_INFO_INTERLEAVED |
367 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID,
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300368 .formats = SNDRV_PCM_FMTBIT_S16_LE,
369
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300370 .rates = SNDRV_PCM_RATE_48000,
371 .rate_min = 48000,
372 .rate_max = 48000,
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300373 .channels_min = 2,
374 .channels_max = 2,
375 /* Analog audio output will be full of clicks and pops if there
376 are not exactly four lines in the SRAM FIFO buffer. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300377 .period_bytes_min = DEFAULT_FIFO_SIZE / 3,
378 .period_bytes_max = DEFAULT_FIFO_SIZE / 3,
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300379 .periods_min = 1,
380 .periods_max = AUDIO_LINE_SIZE,
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300381 /* 128 * 128 = 16384 = 1024 * 16 */
382 .buffer_bytes_max = (AUDIO_LINE_SIZE * AUDIO_LINE_SIZE),
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300383};
384
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300385/*
386 * audio pcm capture open callback
387 */
388static int snd_cx25821_pcm_open(struct snd_pcm_substream *substream)
389{
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300390 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300391 struct snd_pcm_runtime *runtime = substream->runtime;
392 int err;
393 unsigned int bpl = 0;
394
395 if (!chip) {
Joe Perches36d89f72010-11-07 17:48:21 -0300396 pr_err("DEBUG: cx25821 can't find device struct. Can't proceed with open\n");
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300397 return -ENODEV;
398 }
399
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300400 err =
401 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300402 if (err < 0)
403 goto _error;
404
405 chip->substream = substream;
406
407 runtime->hw = snd_cx25821_digital_hw;
408
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300409 if (cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
410 DEFAULT_FIFO_SIZE) {
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300411 /* since there are 3 audio Clusters */
412 bpl = cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 3;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300413 bpl &= ~7; /* must be multiple of 8 */
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300414
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300415 if (bpl > AUDIO_LINE_SIZE)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300416 bpl = AUDIO_LINE_SIZE;
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300417
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300418 runtime->hw.period_bytes_min = bpl;
419 runtime->hw.period_bytes_max = bpl;
420 }
421
422 return 0;
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300423_error:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300424 dprintk(1, "Error opening PCM!\n");
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300425 return err;
426}
427
428/*
429 * audio close callback
430 */
431static int snd_cx25821_close(struct snd_pcm_substream *substream)
432{
433 return 0;
434}
435
436/*
437 * hw_params callback
438 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300439static int snd_cx25821_hw_params(struct snd_pcm_substream *substream,
440 struct snd_pcm_hw_params *hw_params)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300441{
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300442 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300443 struct videobuf_dmabuf *dma;
444
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300445 struct cx25821_audio_buffer *buf;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300446 int ret;
447
448 if (substream->runtime->dma_area) {
449 dsp_buffer_free(chip);
450 substream->runtime->dma_area = NULL;
451 }
452
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300453 chip->period_size = params_period_bytes(hw_params);
454 chip->num_periods = params_periods(hw_params);
455 chip->dma_size = chip->period_size * params_periods(hw_params);
456
457 BUG_ON(!chip->dma_size);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300458 BUG_ON(chip->num_periods & (chip->num_periods - 1));
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300459
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300460 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300461 if (NULL == buf)
462 return -ENOMEM;
463
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300464 if (chip->period_size > AUDIO_LINE_SIZE)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300465 chip->period_size = AUDIO_LINE_SIZE;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300466
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300467 buf->bpl = chip->period_size;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300468
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300469 dma = &buf->dma;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300470 videobuf_dma_init(dma);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300471 ret = videobuf_dma_init_kernel(dma, PCI_DMA_FROMDEVICE,
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300472 (PAGE_ALIGN(chip->dma_size) >>
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300473 PAGE_SHIFT));
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300474 if (ret < 0)
475 goto error;
476
Laurent Pinchart95268402010-05-11 10:36:30 -0300477 ret = videobuf_dma_map(&chip->pci->dev, dma);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300478 if (ret < 0)
479 goto error;
480
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300481 ret =
482 cx25821_risc_databuffer_audio(chip->pci, &buf->risc, dma->sglist,
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300483 chip->period_size, chip->num_periods,
484 1);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300485 if (ret < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300486 pr_info("DEBUG: ERROR after cx25821_risc_databuffer_audio()\n");
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300487 goto error;
488 }
489
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300490 /* Loop back to start of program */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300491 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300492 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300493 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300494
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300495 chip->buf = buf;
496 chip->dma_risc = dma;
497
Laurent Pinchartbb6dbe72010-05-11 10:36:34 -0300498 substream->runtime->dma_area = chip->dma_risc->vaddr;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300499 substream->runtime->dma_bytes = chip->dma_size;
500 substream->runtime->dma_addr = 0;
501
502 return 0;
503
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300504error:
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300505 kfree(buf);
506 return ret;
507}
508
509/*
510 * hw free callback
511 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300512static int snd_cx25821_hw_free(struct snd_pcm_substream *substream)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300513{
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300514 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300515
516 if (substream->runtime->dma_area) {
517 dsp_buffer_free(chip);
518 substream->runtime->dma_area = NULL;
519 }
520
521 return 0;
522}
523
524/*
525 * prepare callback
526 */
527static int snd_cx25821_prepare(struct snd_pcm_substream *substream)
528{
529 return 0;
530}
531
532/*
533 * trigger callback
534 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300535static int snd_cx25821_card_trigger(struct snd_pcm_substream *substream,
536 int cmd)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300537{
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300538 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300539 int err = 0;
540
541 /* Local interrupts are already disabled by ALSA */
542 spin_lock(&chip->reg_lock);
543
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300544 switch (cmd) {
545 case SNDRV_PCM_TRIGGER_START:
546 err = _cx25821_start_audio_dma(chip);
547 break;
548 case SNDRV_PCM_TRIGGER_STOP:
549 err = _cx25821_stop_audio_dma(chip);
550 break;
551 default:
552 err = -EINVAL;
553 break;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300554 }
555
556 spin_unlock(&chip->reg_lock);
557
558 return err;
559}
560
561/*
562 * pointer callback
563 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300564static snd_pcm_uframes_t snd_cx25821_pointer(struct snd_pcm_substream
565 *substream)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300566{
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300567 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300568 struct snd_pcm_runtime *runtime = substream->runtime;
569 u16 count;
570
571 count = atomic_read(&chip->count);
572
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300573 return runtime->period_size * (count & (runtime->periods - 1));
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300574}
575
576/*
577 * page callback (needed for mmap)
578 */
579static struct page *snd_cx25821_page(struct snd_pcm_substream *substream,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300580 unsigned long offset)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300581{
582 void *pageptr = substream->runtime->dma_area + offset;
583
584 return vmalloc_to_page(pageptr);
585}
586
587/*
588 * operators
589 */
590static struct snd_pcm_ops snd_cx25821_pcm_ops = {
591 .open = snd_cx25821_pcm_open,
592 .close = snd_cx25821_close,
593 .ioctl = snd_pcm_lib_ioctl,
594 .hw_params = snd_cx25821_hw_params,
595 .hw_free = snd_cx25821_hw_free,
596 .prepare = snd_cx25821_prepare,
597 .trigger = snd_cx25821_card_trigger,
598 .pointer = snd_cx25821_pointer,
599 .page = snd_cx25821_page,
600};
601
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300602/*
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300603 * ALSA create a PCM device: Called when initializing the board.
604 * Sets up the name and hooks up the callbacks
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300605 */
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300606static int snd_cx25821_pcm(struct cx25821_audio_dev *chip, int device,
607 char *name)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300608{
609 struct snd_pcm *pcm;
610 int err;
611
612 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300613 if (err < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300614 pr_info("ERROR: FAILED snd_pcm_new() in %s\n", __func__);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300615 return err;
616 }
617 pcm->private_data = chip;
618 pcm->info_flags = 0;
619 strcpy(pcm->name, name);
620 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx25821_pcm_ops);
621
622 return 0;
623}
624
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300625/****************************************************************************
626 Basic Flow for Sound Devices
627 ****************************************************************************/
628
629/*
630 * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
631 * Only boards with eeprom and byte 1 at eeprom=1 have it
632 */
633
Namhyung Kim94ab8962010-12-10 01:40:26 +0900634static DEFINE_PCI_DEVICE_TABLE(cx25821_audio_pci_tbl) = {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300635 {0x14f1, 0x0920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
636 {0,}
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300637};
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300638
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300639MODULE_DEVICE_TABLE(pci, cx25821_audio_pci_tbl);
640
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300641/*
642 * Not used in the function snd_cx25821_dev_free so removing
643 * from the file.
644 */
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300645/*
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300646static int snd_cx25821_free(struct cx25821_audio_dev *chip)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300647{
648 if (chip->irq >= 0)
649 free_irq(chip->irq, chip);
650
651 cx25821_dev_unregister(chip->dev);
652 pci_disable_device(chip->pci);
653
654 return 0;
655}
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300656*/
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300657
658/*
659 * Component Destructor
660 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300661static void snd_cx25821_dev_free(struct snd_card *card)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300662{
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300663 struct cx25821_audio_dev *chip = card->private_data;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300664
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300665 /* snd_cx25821_free(chip); */
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300666 snd_card_free(chip->card);
667}
668
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300669/*
670 * Alsa Constructor - Component probe
671 */
672static int cx25821_audio_initdev(struct cx25821_dev *dev)
673{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300674 struct snd_card *card;
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300675 struct cx25821_audio_dev *chip;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300676 int err;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300677
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300678 if (devno >= SNDRV_CARDS) {
Joe Perches36d89f72010-11-07 17:48:21 -0300679 pr_info("DEBUG ERROR: devno >= SNDRV_CARDS %s\n", __func__);
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300680 return -ENODEV;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300681 }
682
683 if (!enable[devno]) {
684 ++devno;
Joe Perches36d89f72010-11-07 17:48:21 -0300685 pr_info("DEBUG ERROR: !enable[devno] %s\n", __func__);
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300686 return -ENOENT;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300687 }
688
Mauro Carvalho Chehab53e712d2009-09-13 11:39:12 -0300689 err = snd_card_create(index[devno], id[devno], THIS_MODULE,
Leonid V. Fedorenchik4d9304c2011-10-22 01:43:23 -0300690 sizeof(struct cx25821_audio_dev), &card);
Mauro Carvalho Chehab53e712d2009-09-13 11:39:12 -0300691 if (err < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300692 pr_info("DEBUG ERROR: cannot create snd_card_new in %s\n",
693 __func__);
Mauro Carvalho Chehab53e712d2009-09-13 11:39:12 -0300694 return err;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300695 }
696
697 strcpy(card->driver, "cx25821");
698
699 /* Card "creation" */
700 card->private_free = snd_cx25821_dev_free;
Joe Perchesba952d82010-07-12 13:50:10 -0700701 chip = card->private_data;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300702 spin_lock_init(&chip->reg_lock);
703
704 chip->dev = dev;
705 chip->card = card;
706 chip->pci = dev->pci;
707 chip->iobase = pci_resource_start(dev->pci, 0);
708
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300709 chip->irq = dev->pci->irq;
710
711 err = request_irq(dev->pci->irq, cx25821_irq,
Yong Zhang18e93512011-09-07 16:10:22 +0800712 IRQF_SHARED, chip->dev->name, chip);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300713
714 if (err < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300715 pr_err("ERROR %s: can't get IRQ %d for ALSA\n",
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300716 chip->dev->name, dev->pci->irq);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300717 goto error;
718 }
719
sai gopal629dcf22010-04-28 14:17:00 -0300720 err = snd_cx25821_pcm(chip, 0, "cx25821 Digital");
721 if (err < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300722 pr_info("DEBUG ERROR: cannot create snd_cx25821_pcm %s\n",
723 __func__);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300724 goto error;
725 }
726
727 snd_card_set_dev(card, &chip->pci->dev);
728
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300729 strcpy(card->shortname, "cx25821");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300730 sprintf(card->longname, "%s at 0x%lx irq %d", chip->dev->name,
731 chip->iobase, chip->irq);
732 strcpy(card->mixername, "CX25821");
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300733
Joe Perches36d89f72010-11-07 17:48:21 -0300734 pr_info("%s/%i: ALSA support for cx25821 boards\n",
735 card->driver, devno);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300736
737 err = snd_card_register(card);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300738 if (err < 0) {
Joe Perches36d89f72010-11-07 17:48:21 -0300739 pr_info("DEBUG ERROR: cannot register sound card %s\n",
740 __func__);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300741 goto error;
742 }
743
744 snd_cx25821_cards[devno] = card;
745
746 devno++;
747 return 0;
748
Olimpiu Pascariu8889a8a2010-03-14 12:44:37 -0300749error:
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300750 snd_card_free(card);
751 return err;
752}
753
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300754/****************************************************************************
755 LINUX MODULE INIT
756 ****************************************************************************/
757static void cx25821_audio_fini(void)
758{
759 snd_card_free(snd_cx25821_cards[0]);
760}
761
762/*
763 * Module initializer
764 *
765 * Loops through present saa7134 cards, and assigns an ALSA device
766 * to each one
767 *
768 */
769static int cx25821_alsa_init(void)
770{
771 struct cx25821_dev *dev = NULL;
772 struct list_head *list;
773
Arnd Bergmann0cd301f2011-03-02 17:15:15 -0300774 mutex_lock(&cx25821_devlist_mutex);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300775 list_for_each(list, &cx25821_devlist) {
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300776 dev = list_entry(list, struct cx25821_dev, devlist);
777 cx25821_audio_initdev(dev);
778 }
Arnd Bergmann0cd301f2011-03-02 17:15:15 -0300779 mutex_unlock(&cx25821_devlist_mutex);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300780
781 if (dev == NULL)
Joe Perches36d89f72010-11-07 17:48:21 -0300782 pr_info("ERROR ALSA: no cx25821 cards found\n");
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300783
784 return 0;
785
786}
787
788late_initcall(cx25821_alsa_init);
789module_exit(cx25821_audio_fini);
790
791/* ----------------------------------------------------------- */
792/*
793 * Local variables:
794 * c-basic-offset: 8
795 * End:
796 */