blob: 76a8c01208c1fb1b58926f9f9c856b75d84935fc [file] [log] [blame]
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02001/*
2 *
3 * Support for audio capture
4 * PCI function #1 of the cx2388x.
5 *
6 * (c) 2005,2006 Ricardo Cerqueira <v4l@cerqueira.org>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03007 * (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02008 * Based on a dummy cx88 module by Gerd Knorr <kraxel@bytesex.org>
9 * Based on dummy.c by Jaroslav Kysela <perex@suse.cz>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/device.h>
29#include <linux/interrupt.h>
Andrew Mortonc24228d2007-05-06 14:51:55 -070030#include <linux/dma-mapping.h>
31
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020032#include <asm/delay.h>
33#include <sound/driver.h>
34#include <sound/core.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
37#include <sound/control.h>
38#include <sound/initval.h>
39
40#include "cx88.h"
41#include "cx88-reg.h"
42
43#define dprintk(level,fmt, arg...) if (debug >= level) \
44 printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg)
45
46#define dprintk_core(level,fmt, arg...) if (debug >= level) \
47 printk(KERN_DEBUG "%s/1: " fmt, chip->core->name , ## arg)
48
49
50/****************************************************************************
51 Data type declarations - Can be moded to a header file later
52 ****************************************************************************/
53
54/* These can be replaced after done */
55#define MIXER_ADDR_LAST MAX_CX88_INPUT
56
57struct cx88_audio_dev {
58 struct cx88_core *core;
59 struct cx88_dmaqueue q;
60
61 /* pci i/o */
62 struct pci_dev *pci;
63 unsigned char pci_rev,pci_lat;
64
65 /* audio controls */
66 int irq;
67
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +010068 struct snd_card *card;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020069
70 spinlock_t reg_lock;
71
72 unsigned int dma_size;
73 unsigned int period_size;
74 unsigned int num_periods;
75
76 struct videobuf_dmabuf dma_risc;
77
78 int mixer_volume[MIXER_ADDR_LAST+1][2];
79 int capture_source[MIXER_ADDR_LAST+1][2];
80
81 long int read_count;
82 long int read_offset;
83
84 struct cx88_buffer *buf;
85
86 long opened;
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +010087 struct snd_pcm_substream *substream;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020088
89};
90typedef struct cx88_audio_dev snd_cx88_card_t;
91
92
93
94/****************************************************************************
95 Module global static vars
96 ****************************************************************************/
97
98static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
99static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
100static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100101static struct snd_card *snd_cx88_cards[SNDRV_CARDS];
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200102
103module_param_array(enable, bool, NULL, 0444);
104MODULE_PARM_DESC(enable, "Enable cx88x soundcard. default enabled.");
105
106module_param_array(index, int, NULL, 0444);
107MODULE_PARM_DESC(index, "Index value for cx88x capture interface(s).");
108
109
110/****************************************************************************
111 Module macros
112 ****************************************************************************/
113
114MODULE_DESCRIPTION("ALSA driver module for cx2388x based TV cards");
115MODULE_AUTHOR("Ricardo Cerqueira");
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -0300116MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200117MODULE_LICENSE("GPL");
118MODULE_SUPPORTED_DEVICE("{{Conexant,23881},"
119 "{{Conexant,23882},"
120 "{{Conexant,23883}");
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200121static unsigned int debug;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200122module_param(debug,int,0644);
123MODULE_PARM_DESC(debug,"enable debug messages");
124
125/****************************************************************************
126 Module specific funtions
127 ****************************************************************************/
128
129/*
130 * BOARD Specific: Sets audio DMA
131 */
132
Mauro Carvalho Chehabbe8a82d2006-02-07 06:49:14 -0200133static int _cx88_start_audio_dma(snd_cx88_card_t *chip)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200134{
135 struct cx88_buffer *buf = chip->buf;
136 struct cx88_core *core=chip->core;
137 struct sram_channel *audio_ch = &cx88_sram_channels[SRAM_CH25];
138
139
140 dprintk(1, "Starting audio DMA for %i bytes/line and %i (%i) lines at address %08x\n",buf->bpl, chip->num_periods, audio_ch->fifo_size / buf->bpl, audio_ch->fifo_start);
141
142 /* setup fifo + format - out channel */
143 cx88_sram_channel_setup(chip->core, &cx88_sram_channels[SRAM_CH25],
144 buf->bpl, buf->risc.dma);
145
146 /* sets bpl size */
147 cx_write(MO_AUDD_LNGTH, buf->bpl);
148
149 /* reset counter */
150 cx_write(MO_AUDD_GPCNTRL,GP_COUNT_CONTROL_RESET);
151
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300152 dprintk(1, "Enabling IRQ, setting mask from 0x%x to 0x%x\n",
153 chip->core->pci_irqmask,
154 chip->core->pci_irqmask | PCI_INT_AUDINT);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200155 /* enable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300156 cx_set(MO_PCI_INTMSK, chip->core->pci_irqmask | PCI_INT_AUDINT);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200157
158
159 /* Enables corresponding bits at AUD_INT_STAT */
160 cx_write(MO_AUD_INTMSK,
161 (1<<16)|
162 (1<<12)|
163 (1<<4)|
164 (1<<0)
165 );
166
167 /* start dma */
168 cx_set(MO_DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */
169 cx_set(MO_AUD_DMACNTRL, 0x11); /* audio downstream FIFO and RISC enable */
170
171 if (debug)
172 cx88_sram_channel_dump(chip->core, &cx88_sram_channels[SRAM_CH25]);
173
174 return 0;
175}
176
177/*
178 * BOARD Specific: Resets audio DMA
179 */
Mauro Carvalho Chehabbe8a82d2006-02-07 06:49:14 -0200180static int _cx88_stop_audio_dma(snd_cx88_card_t *chip)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200181{
182 struct cx88_core *core=chip->core;
183 dprintk(1, "Stopping audio DMA\n");
184
185 /* stop dma */
186 cx_clear(MO_AUD_DMACNTRL, 0x11);
187
188 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300189 cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200190 cx_clear(MO_AUD_INTMSK,
191 (1<<16)|
192 (1<<12)|
193 (1<<4)|
194 (1<<0)
195 );
196
197 if (debug)
198 cx88_sram_channel_dump(chip->core, &cx88_sram_channels[SRAM_CH25]);
199
200 return 0;
201}
202
203#define MAX_IRQ_LOOP 10
204
205/*
206 * BOARD Specific: IRQ dma bits
207 */
208static char *cx88_aud_irqs[32] = {
209 "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */
210 NULL, /* reserved */
211 "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */
212 NULL, /* reserved */
213 "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */
214 NULL, /* reserved */
215 "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */
216 NULL, /* reserved */
217 "opc_err", "par_err", "rip_err", /* 16-18 */
218 "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */
219};
220
221/*
222 * BOARD Specific: Threats IRQ audio specific calls
223 */
224static void cx8801_aud_irq(snd_cx88_card_t *chip)
225{
226 struct cx88_core *core = chip->core;
227 u32 status, mask;
228 u32 count;
229
230 status = cx_read(MO_AUD_INTSTAT);
231 mask = cx_read(MO_AUD_INTMSK);
232 if (0 == (status & mask)) {
233 spin_unlock(&chip->reg_lock);
234 return;
235 }
236 cx_write(MO_AUD_INTSTAT, status);
237 if (debug > 1 || (status & mask & ~0xff))
238 cx88_print_irqbits(core->name, "irq aud",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -0300239 cx88_aud_irqs, ARRAY_SIZE(cx88_aud_irqs),
240 status, mask);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200241 /* risc op code error */
242 if (status & (1 << 16)) {
243 printk(KERN_WARNING "%s/0: audio risc op code error\n",core->name);
244 cx_clear(MO_AUD_DMACNTRL, 0x11);
245 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH25]);
246 }
247
248 /* risc1 downstream */
249 if (status & 0x01) {
250 spin_lock(&chip->reg_lock);
251 count = cx_read(MO_AUDD_GPCNT);
252 spin_unlock(&chip->reg_lock);
253 if (chip->read_count == 0)
254 chip->read_count += chip->dma_size;
255 }
256
257 if (chip->read_count >= chip->period_size) {
258 dprintk(2, "Elapsing period\n");
259 snd_pcm_period_elapsed(chip->substream);
260 }
261
262 dprintk(3,"Leaving audio IRQ handler...\n");
263
264 /* FIXME: Any other status should deserve a special handling? */
265}
266
267/*
268 * BOARD Specific: Handles IRQ calls
269 */
David Howells7d12e782006-10-05 14:55:46 +0100270static irqreturn_t cx8801_irq(int irq, void *dev_id)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200271{
272 snd_cx88_card_t *chip = dev_id;
273 struct cx88_core *core = chip->core;
274 u32 status;
275 int loop, handled = 0;
276
277 for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300278 status = cx_read(MO_PCI_INTSTAT) &
279 (core->pci_irqmask | PCI_INT_AUDINT);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200280 if (0 == status)
281 goto out;
282 dprintk( 3, "cx8801_irq\n" );
283 dprintk( 3, " loop: %d/%d\n", loop, MAX_IRQ_LOOP );
284 dprintk( 3, " status: %d\n", status );
285 handled = 1;
286 cx_write(MO_PCI_INTSTAT, status);
287
Trent Piepho5ba862b2007-08-18 07:02:26 -0300288 if (status & core->pci_irqmask)
289 cx88_core_irq(core, status);
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300290 if (status & PCI_INT_AUDINT) {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200291 dprintk( 2, " ALSA IRQ handling\n" );
292 cx8801_aud_irq(chip);
293 }
294 };
295
296 if (MAX_IRQ_LOOP == loop) {
297 dprintk( 0, "clearing mask\n" );
298 dprintk(1,"%s/0: irq loop -- clearing mask\n",
299 core->name);
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300300 cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200301 }
302
303 out:
304 return IRQ_RETVAL(handled);
305}
306
307
308static int dsp_buffer_free(snd_cx88_card_t *chip)
309{
310 BUG_ON(!chip->dma_size);
311
312 dprintk(2,"Freeing buffer\n");
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300313 videobuf_pci_dma_unmap(chip->pci, &chip->dma_risc);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200314 videobuf_dma_free(&chip->dma_risc);
315 btcx_riscmem_free(chip->pci,&chip->buf->risc);
316 kfree(chip->buf);
317
318 chip->dma_size = 0;
319
320 return 0;
321}
322
323/****************************************************************************
324 ALSA PCM Interface
325 ****************************************************************************/
326
327/*
328 * Digital hardware definition
329 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100330static struct snd_pcm_hardware snd_cx88_digital_hw = {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200331 .info = SNDRV_PCM_INFO_MMAP |
332 SNDRV_PCM_INFO_INTERLEAVED |
333 SNDRV_PCM_INFO_BLOCK_TRANSFER |
334 SNDRV_PCM_INFO_MMAP_VALID,
335 .formats = SNDRV_PCM_FMTBIT_S16_LE,
336
337 .rates = SNDRV_PCM_RATE_48000,
338 .rate_min = 48000,
339 .rate_max = 48000,
340 .channels_min = 1,
341 .channels_max = 2,
342 .buffer_bytes_max = (2*2048),
Ricardo Cerqueira18adfe72006-01-15 15:33:02 -0200343 .period_bytes_min = 2048,
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200344 .period_bytes_max = 2048,
345 .periods_min = 2,
Ricardo Cerqueira18adfe72006-01-15 15:33:02 -0200346 .periods_max = 2,
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200347};
348
349/*
350 * audio pcm capture runtime free
351 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100352static void snd_card_cx88_runtime_free(struct snd_pcm_runtime *runtime)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200353{
354}
355/*
356 * audio pcm capture open callback
357 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100358static int snd_cx88_pcm_open(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200359{
360 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100361 struct snd_pcm_runtime *runtime = substream->runtime;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200362 int err;
363
364 if (test_and_set_bit(0, &chip->opened))
365 return -EBUSY;
366
367 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
368 if (err < 0)
369 goto _error;
370
371 chip->substream = substream;
372
373 chip->read_count = 0;
374 chip->read_offset = 0;
375
376 runtime->private_free = snd_card_cx88_runtime_free;
377 runtime->hw = snd_cx88_digital_hw;
378
379 return 0;
380_error:
381 dprintk(1,"Error opening PCM!\n");
382 clear_bit(0, &chip->opened);
383 smp_mb__after_clear_bit();
384 return err;
385}
386
387/*
388 * audio close callback
389 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100390static int snd_cx88_close(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200391{
392 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
393
394 clear_bit(0, &chip->opened);
395 smp_mb__after_clear_bit();
396
397 return 0;
398}
399
400/*
401 * hw_params callback
402 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100403static int snd_cx88_hw_params(struct snd_pcm_substream * substream,
404 struct snd_pcm_hw_params * hw_params)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200405{
406 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
407 struct cx88_buffer *buf;
408
409 if (substream->runtime->dma_area) {
410 dsp_buffer_free(chip);
411 substream->runtime->dma_area = NULL;
412 }
413
414
415 chip->period_size = params_period_bytes(hw_params);
416 chip->num_periods = params_periods(hw_params);
417 chip->dma_size = chip->period_size * params_periods(hw_params);
418
419 BUG_ON(!chip->dma_size);
420
421 dprintk(1,"Setting buffer\n");
422
vignesh.babu@wipro.comc42cabe2007-04-16 10:34:33 -0300423 buf = kzalloc(sizeof(*buf),GFP_KERNEL);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200424 if (NULL == buf)
425 return -ENOMEM;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200426
427 buf->vb.memory = V4L2_MEMORY_MMAP;
428 buf->vb.width = chip->period_size;
429 buf->vb.height = chip->num_periods;
430 buf->vb.size = chip->dma_size;
431 buf->vb.field = V4L2_FIELD_NONE;
432
433 videobuf_dma_init(&buf->vb.dma);
434 videobuf_dma_init_kernel(&buf->vb.dma,PCI_DMA_FROMDEVICE,
435 (PAGE_ALIGN(buf->vb.size) >> PAGE_SHIFT));
436
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300437 videobuf_pci_dma_map(chip->pci,&buf->vb.dma);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200438
439
440 cx88_risc_databuffer(chip->pci, &buf->risc,
441 buf->vb.dma.sglist,
442 buf->vb.width, buf->vb.height);
443
444 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
445 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
446
447 buf->vb.state = STATE_PREPARED;
448
449 buf->bpl = chip->period_size;
450 chip->buf = buf;
451 chip->dma_risc = buf->vb.dma;
452
453 dprintk(1,"Buffer ready at %u\n",chip->dma_risc.nr_pages);
454 substream->runtime->dma_area = chip->dma_risc.vmalloc;
455 return 0;
456}
457
458/*
459 * hw free callback
460 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100461static int snd_cx88_hw_free(struct snd_pcm_substream * substream)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200462{
463
464 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
465
466 if (substream->runtime->dma_area) {
467 dsp_buffer_free(chip);
468 substream->runtime->dma_area = NULL;
469 }
470
471 return 0;
472}
473
474/*
475 * prepare callback
476 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100477static int snd_cx88_prepare(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200478{
479 return 0;
480}
481
482
483/*
484 * trigger callback
485 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100486static int snd_cx88_card_trigger(struct snd_pcm_substream *substream, int cmd)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200487{
488 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
489 int err;
490
491 spin_lock(&chip->reg_lock);
492
493 switch (cmd) {
494 case SNDRV_PCM_TRIGGER_START:
495 err=_cx88_start_audio_dma(chip);
496 break;
497 case SNDRV_PCM_TRIGGER_STOP:
498 err=_cx88_stop_audio_dma(chip);
499 break;
500 default:
501 err=-EINVAL;
502 break;
503 }
504
505 spin_unlock(&chip->reg_lock);
506
507 return err;
508}
509
510/*
511 * pointer callback
512 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100513static snd_pcm_uframes_t snd_cx88_pointer(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200514{
515 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100516 struct snd_pcm_runtime *runtime = substream->runtime;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200517
518 if (chip->read_count) {
519 chip->read_count -= snd_pcm_lib_period_bytes(substream);
520 chip->read_offset += snd_pcm_lib_period_bytes(substream);
521 if (chip->read_offset == chip->dma_size)
522 chip->read_offset = 0;
523 }
524
525 dprintk(2, "Pointer time, will return %li, read %li\n",chip->read_offset,chip->read_count);
526 return bytes_to_frames(runtime, chip->read_offset);
527
528}
529
530/*
531 * operators
532 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100533static struct snd_pcm_ops snd_cx88_pcm_ops = {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200534 .open = snd_cx88_pcm_open,
535 .close = snd_cx88_close,
536 .ioctl = snd_pcm_lib_ioctl,
537 .hw_params = snd_cx88_hw_params,
538 .hw_free = snd_cx88_hw_free,
539 .prepare = snd_cx88_prepare,
540 .trigger = snd_cx88_card_trigger,
541 .pointer = snd_cx88_pointer,
542};
543
544/*
545 * create a PCM device
546 */
547static int __devinit snd_cx88_pcm(snd_cx88_card_t *chip, int device, char *name)
548{
549 int err;
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100550 struct snd_pcm *pcm;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200551
552 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
553 if (err < 0)
554 return err;
555 pcm->private_data = chip;
556 strcpy(pcm->name, name);
557 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx88_pcm_ops);
558
559 return 0;
560}
561
562/****************************************************************************
563 CONTROL INTERFACE
564 ****************************************************************************/
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100565static int snd_cx88_capture_volume_info(struct snd_kcontrol *kcontrol,
566 struct snd_ctl_elem_info *info)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200567{
568 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
569 info->count = 1;
570 info->value.integer.min = 0;
571 info->value.integer.max = 0x3f;
572
573 return 0;
574}
575
576/* OK - TODO: test it */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100577static int snd_cx88_capture_volume_get(struct snd_kcontrol *kcontrol,
578 struct snd_ctl_elem_value *value)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200579{
580 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
581 struct cx88_core *core=chip->core;
582
583 value->value.integer.value[0] = 0x3f - (cx_read(AUD_VOL_CTL) & 0x3f);
584
585 return 0;
586}
587
588/* OK - TODO: test it */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100589static int snd_cx88_capture_volume_put(struct snd_kcontrol *kcontrol,
590 struct snd_ctl_elem_value *value)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200591{
592 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
593 struct cx88_core *core=chip->core;
594 int v;
595 u32 old_control;
596
597 spin_lock_irq(&chip->reg_lock);
598 old_control = 0x3f - (cx_read(AUD_VOL_CTL) & 0x3f);
599 v = 0x3f - (value->value.integer.value[0] & 0x3f);
600 cx_andor(AUD_VOL_CTL, 0x3f, v);
601 spin_unlock_irq(&chip->reg_lock);
602
603 return v != old_control;
604}
605
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100606static struct snd_kcontrol_new snd_cx88_capture_volume = {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200607 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
608 .name = "Capture Volume",
609 .info = snd_cx88_capture_volume_info,
610 .get = snd_cx88_capture_volume_get,
611 .put = snd_cx88_capture_volume_put,
612};
613
614
615/****************************************************************************
616 Basic Flow for Sound Devices
617 ****************************************************************************/
618
619/*
620 * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
621 * Only boards with eeprom and byte 1 at eeprom=1 have it
622 */
623
Henrik Kretzschmar396c9b92006-04-24 15:59:04 +0200624static struct pci_device_id cx88_audio_pci_tbl[] __devinitdata = {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200625 {0x14f1,0x8801,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
626 {0x14f1,0x8811,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
627 {0, }
628};
629MODULE_DEVICE_TABLE(pci, cx88_audio_pci_tbl);
630
631/*
632 * Chip-specific destructor
633 */
634
635static int snd_cx88_free(snd_cx88_card_t *chip)
636{
637
638 if (chip->irq >= 0){
639 synchronize_irq(chip->irq);
640 free_irq(chip->irq, chip);
641 }
642
643 cx88_core_put(chip->core,chip->pci);
644
645 pci_disable_device(chip->pci);
646 return 0;
647}
648
649/*
650 * Component Destructor
651 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100652static void snd_cx88_dev_free(struct snd_card * card)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200653{
654 snd_cx88_card_t *chip = card->private_data;
655
656 snd_cx88_free(chip);
657}
658
659
660/*
661 * Alsa Constructor - Component probe
662 */
663
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200664static int devno;
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100665static int __devinit snd_cx88_create(struct snd_card *card,
666 struct pci_dev *pci,
667 snd_cx88_card_t **rchip)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200668{
669 snd_cx88_card_t *chip;
670 struct cx88_core *core;
671 int err;
672
673 *rchip = NULL;
674
675 err = pci_enable_device(pci);
676 if (err < 0)
677 return err;
678
679 pci_set_master(pci);
680
681 chip = (snd_cx88_card_t *) card->private_data;
682
683 core = cx88_core_get(pci);
Duncan Sands31dcbf92006-03-14 12:12:39 -0300684 if (NULL == core) {
685 err = -EINVAL;
686 kfree (chip);
687 return err;
688 }
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200689
Mauro Carvalho Chehabaaa40cb2007-03-30 10:58:01 -0300690 if (!pci_dma_supported(pci,DMA_32BIT_MASK)) {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200691 dprintk(0, "%s/1: Oops: no 32bit PCI DMA ???\n",core->name);
692 err = -EIO;
693 cx88_core_put(core,pci);
694 return err;
695 }
696
697
698 /* pci init */
699 chip->card = card;
700 chip->pci = pci;
701 chip->irq = -1;
702 spin_lock_init(&chip->reg_lock);
703
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200704 chip->core = core;
705
706 /* get irq */
707 err = request_irq(chip->pci->irq, cx8801_irq,
Thomas Gleixner8076fe32006-07-01 19:29:37 -0700708 IRQF_SHARED | IRQF_DISABLED, chip->core->name, chip);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200709 if (err < 0) {
710 dprintk(0, "%s: can't get IRQ %d\n",
711 chip->core->name, chip->pci->irq);
712 return err;
713 }
714
715 /* print pci info */
716 pci_read_config_byte(pci, PCI_CLASS_REVISION, &chip->pci_rev);
717 pci_read_config_byte(pci, PCI_LATENCY_TIMER, &chip->pci_lat);
718
719 dprintk(1,"ALSA %s/%i: found at %s, rev: %d, irq: %d, "
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -0700720 "latency: %d, mmio: 0x%llx\n", core->name, devno,
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200721 pci_name(pci), chip->pci_rev, pci->irq,
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -0700722 chip->pci_lat,(unsigned long long)pci_resource_start(pci,0));
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200723
724 chip->irq = pci->irq;
725 synchronize_irq(chip->irq);
726
727 snd_card_set_dev(card, &pci->dev);
728
729 *rchip = chip;
730
731 return 0;
732}
733
734static int __devinit cx88_audio_initdev(struct pci_dev *pci,
735 const struct pci_device_id *pci_id)
736{
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100737 struct snd_card *card;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200738 snd_cx88_card_t *chip;
739 int err;
740
741 if (devno >= SNDRV_CARDS)
742 return (-ENODEV);
743
744 if (!enable[devno]) {
745 ++devno;
746 return (-ENOENT);
747 }
748
749 card = snd_card_new(index[devno], id[devno], THIS_MODULE, sizeof(snd_cx88_card_t));
750 if (!card)
751 return (-ENOMEM);
752
753 card->private_free = snd_cx88_dev_free;
754
755 err = snd_cx88_create(card, pci, &chip);
756 if (err < 0)
757 return (err);
758
759 err = snd_cx88_pcm(chip, 0, "CX88 Digital");
760
761 if (err < 0) {
762 snd_card_free(card);
763 return (err);
764 }
765
766 err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_capture_volume, chip));
767 if (err < 0) {
768 snd_card_free(card);
769 return (err);
770 }
771
772 strcpy (card->driver, "CX88x");
773 sprintf(card->shortname, "Conexant CX%x", pci->device);
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -0700774 sprintf(card->longname, "%s at %#llx",
775 card->shortname,(unsigned long long)pci_resource_start(pci, 0));
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200776 strcpy (card->mixername, "CX88");
777
778 dprintk (0, "%s/%i: ALSA support for cx2388x boards\n",
779 card->driver,devno);
780
781 err = snd_card_register(card);
782 if (err < 0) {
783 snd_card_free(card);
784 return (err);
785 }
786 snd_cx88_cards[devno] = card;
787
788 pci_set_drvdata(pci,card);
789
790 devno++;
791 return 0;
792}
793/*
794 * ALSA destructor
795 */
796static void __devexit cx88_audio_finidev(struct pci_dev *pci)
797{
798 struct cx88_audio_dev *card = pci_get_drvdata(pci);
799
800 snd_card_free((void *)card);
801
802 pci_set_drvdata(pci, NULL);
803
804 devno--;
805}
806
807/*
808 * PCI driver definition
809 */
810
811static struct pci_driver cx88_audio_pci_driver = {
812 .name = "cx88_audio",
813 .id_table = cx88_audio_pci_tbl,
814 .probe = cx88_audio_initdev,
815 .remove = cx88_audio_finidev,
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200816};
817
818/****************************************************************************
819 LINUX MODULE INIT
820 ****************************************************************************/
821
822/*
823 * module init
824 */
825static int cx88_audio_init(void)
826{
827 printk(KERN_INFO "cx2388x alsa driver version %d.%d.%d loaded\n",
828 (CX88_VERSION_CODE >> 16) & 0xff,
829 (CX88_VERSION_CODE >> 8) & 0xff,
830 CX88_VERSION_CODE & 0xff);
831#ifdef SNAPSHOT
832 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
833 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
834#endif
835 return pci_register_driver(&cx88_audio_pci_driver);
836}
837
838/*
839 * module remove
840 */
841static void cx88_audio_fini(void)
842{
843
844 pci_unregister_driver(&cx88_audio_pci_driver);
845}
846
847module_init(cx88_audio_init);
848module_exit(cx88_audio_fini);
849
850/* ----------------------------------------------------------- */
851/*
852 * Local variables:
853 * c-basic-offset: 8
854 * End:
855 */