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