2 * Driver for the Conexant CX25821 PCIe bridge
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
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
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.
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
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>
31 #include <asm/delay.h>
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/control.h>
36 #include <sound/initval.h>
37 #include <sound/tlv.h>
40 #include "cx25821-reg.h"
42 #define AUDIO_SRAM_CHANNEL SRAM_CH08
44 #define dprintk(level,fmt, arg...) if (debug >= level) \
45 printk(KERN_INFO "%s/1: " fmt, chip->dev->name , ## arg)
47 #define dprintk_core(level,fmt, arg...) if (debug >= level) \
48 printk(KERN_DEBUG "%s/1: " fmt, chip->dev->name , ## arg)
50 /****************************************************************************
51 Data type declarations - Can be moded to a header file later
52 ****************************************************************************/
54 static struct snd_card
*snd_cx25821_cards
[SNDRV_CARDS
];
57 struct cx25821_audio_dev
{
58 struct cx25821_dev
*dev
;
59 struct cx25821_dmaqueue q
;
67 struct snd_card
*card
;
73 unsigned int dma_size
;
74 unsigned int period_size
;
75 unsigned int num_periods
;
77 struct videobuf_dmabuf
*dma_risc
;
79 struct cx25821_buffer
*buf
;
81 struct snd_pcm_substream
*substream
;
83 typedef struct cx25821_audio_dev snd_cx25821_card_t
;
86 /****************************************************************************
87 Module global static vars
88 ****************************************************************************/
90 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
; /* Index 0-MAX */
91 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
; /* ID for this card */
92 static int enable
[SNDRV_CARDS
] = { 1,[1 ... (SNDRV_CARDS
- 1)] = 1 };
94 module_param_array(enable
, bool, NULL
, 0444);
95 MODULE_PARM_DESC(enable
, "Enable cx25821 soundcard. default enabled.");
97 module_param_array(index
, int, NULL
, 0444);
98 MODULE_PARM_DESC(index
, "Index value for cx25821 capture interface(s).");
100 /****************************************************************************
102 ****************************************************************************/
104 MODULE_DESCRIPTION("ALSA driver module for cx25821 based capture cards");
105 MODULE_AUTHOR("Hiep Huynh");
106 MODULE_LICENSE("GPL");
107 MODULE_SUPPORTED_DEVICE("{{Conexant,25821}"); //"{{Conexant,23881},"
109 static unsigned int debug
;
110 module_param(debug
, int, 0644);
111 MODULE_PARM_DESC(debug
, "enable debug messages");
113 /****************************************************************************
114 Module specific funtions
115 ****************************************************************************/
116 /* Constants taken from cx88-reg.h */
117 #define AUD_INT_DN_RISCI1 (1 << 0)
118 #define AUD_INT_UP_RISCI1 (1 << 1)
119 #define AUD_INT_RDS_DN_RISCI1 (1 << 2)
120 #define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */
121 #define AUD_INT_UP_RISCI2 (1 << 5)
122 #define AUD_INT_RDS_DN_RISCI2 (1 << 6)
123 #define AUD_INT_DN_SYNC (1 << 12)
124 #define AUD_INT_UP_SYNC (1 << 13)
125 #define AUD_INT_RDS_DN_SYNC (1 << 14)
126 #define AUD_INT_OPC_ERR (1 << 16)
127 #define AUD_INT_BER_IRQ (1 << 20)
128 #define AUD_INT_MCHG_IRQ (1 << 21)
129 #define GP_COUNT_CONTROL_RESET 0x3
131 #define PCI_MSK_AUD_EXT (1 << 4)
132 #define PCI_MSK_AUD_INT (1 << 3)
134 * BOARD Specific: Sets audio DMA
137 static int _cx25821_start_audio_dma(snd_cx25821_card_t
* chip
)
139 struct cx25821_buffer
*buf
= chip
->buf
;
140 struct cx25821_dev
*dev
= chip
->dev
;
141 struct sram_channel
*audio_ch
=
142 &cx25821_sram_channels
[AUDIO_SRAM_CHANNEL
];
145 // enable output on the GPIO 0 for the MCLK ADC (Audio)
146 cx25821_set_gpiopin_direction(chip
->dev
, 0, 0);
148 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
149 cx_clear(AUD_INT_DMA_CTL
,
150 FLD_AUD_DST_A_RISC_EN
| FLD_AUD_DST_A_FIFO_EN
);
152 /* setup fifo + format - out channel */
153 cx25821_sram_channel_setup_audio(chip
->dev
, audio_ch
, buf
->bpl
,
157 cx_write(AUD_A_LNGTH
, buf
->bpl
);
160 cx_write(AUD_A_GPCNT_CTL
, GP_COUNT_CONTROL_RESET
); //GP_COUNT_CONTROL_RESET = 0x3
161 atomic_set(&chip
->count
, 0);
163 //Set the input mode to 16-bit
164 tmp
= cx_read(AUD_A_CFG
);
166 tmp
| FLD_AUD_DST_PK_MODE
| FLD_AUD_DST_ENABLE
|
169 //printk(KERN_INFO "DEBUG: Start audio DMA, %d B/line, cmds_start(0x%x)= %d lines/FIFO, %d periods, %d "
170 // "byte buffer\n", buf->bpl, audio_ch->cmds_start, cx_read(audio_ch->cmds_start + 12)>>1,
171 // chip->num_periods, buf->bpl * chip->num_periods);
173 /* Enables corresponding bits at AUD_INT_STAT */
174 cx_write(AUD_A_INT_MSK
,
175 FLD_AUD_DST_RISCI1
| FLD_AUD_DST_OF
| FLD_AUD_DST_SYNC
|
176 FLD_AUD_DST_OPC_ERR
);
178 /* Clean any pending interrupt bits already set */
179 cx_write(AUD_A_INT_STAT
, ~0);
181 /* enable audio irqs */
182 cx_set(PCI_INT_MSK
, chip
->dev
->pci_irqmask
| PCI_MSK_AUD_INT
);
184 // Turn on audio downstream fifo and risc enable 0x101
185 tmp
= cx_read(AUD_INT_DMA_CTL
);
186 cx_set(AUD_INT_DMA_CTL
,
187 tmp
| (FLD_AUD_DST_A_RISC_EN
| FLD_AUD_DST_A_FIFO_EN
));
194 * BOARD Specific: Resets audio DMA
196 static int _cx25821_stop_audio_dma(snd_cx25821_card_t
* chip
)
198 struct cx25821_dev
*dev
= chip
->dev
;
201 cx_clear(AUD_INT_DMA_CTL
,
202 FLD_AUD_DST_A_RISC_EN
| FLD_AUD_DST_A_FIFO_EN
);
205 cx_clear(PCI_INT_MSK
, PCI_MSK_AUD_INT
);
206 cx_clear(AUD_A_INT_MSK
,
207 AUD_INT_OPC_ERR
| AUD_INT_DN_SYNC
| AUD_INT_DN_RISCI2
|
213 #define MAX_IRQ_LOOP 50
216 * BOARD Specific: IRQ dma bits
218 static char *cx25821_aud_irqs
[32] = {
219 "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */
221 "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */
223 "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */
225 "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */
227 "opc_err", "par_err", "rip_err", /* 16-18 */
228 "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */
232 * BOARD Specific: Threats IRQ audio specific calls
234 static void cx25821_aud_irq(snd_cx25821_card_t
* chip
, u32 status
, u32 mask
)
236 struct cx25821_dev
*dev
= chip
->dev
;
238 if (0 == (status
& mask
)) {
242 cx_write(AUD_A_INT_STAT
, status
);
243 if (debug
> 1 || (status
& mask
& ~0xff))
244 cx25821_print_irqbits(dev
->name
, "irq aud",
246 ARRAY_SIZE(cx25821_aud_irqs
), status
,
249 /* risc op code error */
250 if (status
& AUD_INT_OPC_ERR
) {
251 printk(KERN_WARNING
"WARNING %s/1: Audio risc op code error\n",
254 cx_clear(AUD_INT_DMA_CTL
,
255 FLD_AUD_DST_A_RISC_EN
| FLD_AUD_DST_A_FIFO_EN
);
256 cx25821_sram_channel_dump_audio(dev
,
257 &cx25821_sram_channels
258 [AUDIO_SRAM_CHANNEL
]);
260 if (status
& AUD_INT_DN_SYNC
) {
261 printk(KERN_WARNING
"WARNING %s: Downstream sync error!\n",
263 cx_write(AUD_A_GPCNT_CTL
, GP_COUNT_CONTROL_RESET
);
267 /* risc1 downstream */
268 if (status
& AUD_INT_DN_RISCI1
) {
269 atomic_set(&chip
->count
, cx_read(AUD_A_GPCNT
));
270 snd_pcm_period_elapsed(chip
->substream
);
275 * BOARD Specific: Handles IRQ calls
277 static irqreturn_t
cx25821_irq(int irq
, void *dev_id
)
279 snd_cx25821_card_t
*chip
= dev_id
;
280 struct cx25821_dev
*dev
= chip
->dev
;
281 u32 status
, pci_status
;
282 u32 audint_status
, audint_mask
;
283 int loop
, handled
= 0;
284 int audint_count
= 0;
286 audint_status
= cx_read(AUD_A_INT_STAT
);
287 audint_mask
= cx_read(AUD_A_INT_MSK
);
288 audint_count
= cx_read(AUD_A_GPCNT
);
289 status
= cx_read(PCI_INT_STAT
);
291 for (loop
= 0; loop
< 1; loop
++) {
292 status
= cx_read(PCI_INT_STAT
);
294 status
= cx_read(PCI_INT_STAT
);
295 audint_status
= cx_read(AUD_A_INT_STAT
);
296 audint_mask
= cx_read(AUD_A_INT_MSK
);
300 cx_write(PCI_INT_STAT
, status
);
302 cx25821_aud_irq(chip
, audint_status
,
310 cx_write(PCI_INT_STAT
, status
);
312 cx25821_aud_irq(chip
, audint_status
, audint_mask
);
315 pci_status
= cx_read(PCI_INT_STAT
);
318 cx_write(PCI_INT_STAT
, pci_status
);
321 return IRQ_RETVAL(handled
);
324 static int dsp_buffer_free(snd_cx25821_card_t
* chip
)
326 BUG_ON(!chip
->dma_size
);
328 dprintk(2, "Freeing buffer\n");
329 videobuf_sg_dma_unmap(&chip
->pci
->dev
, chip
->dma_risc
);
330 videobuf_dma_free(chip
->dma_risc
);
331 btcx_riscmem_free(chip
->pci
, &chip
->buf
->risc
);
334 chip
->dma_risc
= NULL
;
340 /****************************************************************************
342 ****************************************************************************/
345 * Digital hardware definition
347 #define DEFAULT_FIFO_SIZE 384
348 static struct snd_pcm_hardware snd_cx25821_digital_hw
= {
349 .info
= SNDRV_PCM_INFO_MMAP
|
350 SNDRV_PCM_INFO_INTERLEAVED
|
351 SNDRV_PCM_INFO_BLOCK_TRANSFER
| SNDRV_PCM_INFO_MMAP_VALID
,
352 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
354 .rates
= SNDRV_PCM_RATE_48000
,
359 /* Analog audio output will be full of clicks and pops if there
360 are not exactly four lines in the SRAM FIFO buffer. */
361 .period_bytes_min
= DEFAULT_FIFO_SIZE
/ 3,
362 .period_bytes_max
= DEFAULT_FIFO_SIZE
/ 3,
364 .periods_max
= AUDIO_LINE_SIZE
,
365 .buffer_bytes_max
= (AUDIO_LINE_SIZE
* AUDIO_LINE_SIZE
), //128*128 = 16384 = 1024 * 16
369 * audio pcm capture open callback
371 static int snd_cx25821_pcm_open(struct snd_pcm_substream
*substream
)
373 snd_cx25821_card_t
*chip
= snd_pcm_substream_chip(substream
);
374 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
376 unsigned int bpl
= 0;
379 printk(KERN_ERR
"DEBUG: cx25821 can't find device struct."
380 " Can't proceed with open\n");
385 snd_pcm_hw_constraint_pow2(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
);
389 chip
->substream
= substream
;
391 runtime
->hw
= snd_cx25821_digital_hw
;
393 if (cx25821_sram_channels
[AUDIO_SRAM_CHANNEL
].fifo_size
!=
395 bpl
= cx25821_sram_channels
[AUDIO_SRAM_CHANNEL
].fifo_size
/ 3; //since there are 3 audio Clusters
396 bpl
&= ~7; /* must be multiple of 8 */
398 if (bpl
> AUDIO_LINE_SIZE
) {
399 bpl
= AUDIO_LINE_SIZE
;
401 runtime
->hw
.period_bytes_min
= bpl
;
402 runtime
->hw
.period_bytes_max
= bpl
;
407 dprintk(1, "Error opening PCM!\n");
412 * audio close callback
414 static int snd_cx25821_close(struct snd_pcm_substream
*substream
)
422 static int snd_cx25821_hw_params(struct snd_pcm_substream
*substream
,
423 struct snd_pcm_hw_params
*hw_params
)
425 snd_cx25821_card_t
*chip
= snd_pcm_substream_chip(substream
);
426 struct videobuf_dmabuf
*dma
;
428 struct cx25821_buffer
*buf
;
431 if (substream
->runtime
->dma_area
) {
432 dsp_buffer_free(chip
);
433 substream
->runtime
->dma_area
= NULL
;
436 chip
->period_size
= params_period_bytes(hw_params
);
437 chip
->num_periods
= params_periods(hw_params
);
438 chip
->dma_size
= chip
->period_size
* params_periods(hw_params
);
440 BUG_ON(!chip
->dma_size
);
441 BUG_ON(chip
->num_periods
& (chip
->num_periods
- 1));
443 buf
= videobuf_sg_alloc(sizeof(*buf
));
447 if (chip
->period_size
> AUDIO_LINE_SIZE
) {
448 chip
->period_size
= AUDIO_LINE_SIZE
;
451 buf
->vb
.memory
= V4L2_MEMORY_MMAP
;
452 buf
->vb
.field
= V4L2_FIELD_NONE
;
453 buf
->vb
.width
= chip
->period_size
;
454 buf
->bpl
= chip
->period_size
;
455 buf
->vb
.height
= chip
->num_periods
;
456 buf
->vb
.size
= chip
->dma_size
;
458 dma
= videobuf_to_dma(&buf
->vb
);
459 videobuf_dma_init(dma
);
461 ret
= videobuf_dma_init_kernel(dma
, PCI_DMA_FROMDEVICE
,
462 (PAGE_ALIGN(buf
->vb
.size
) >>
467 ret
= videobuf_sg_dma_map(&chip
->pci
->dev
, dma
);
472 cx25821_risc_databuffer_audio(chip
->pci
, &buf
->risc
, dma
->sglist
,
473 buf
->vb
.width
, buf
->vb
.height
, 1);
476 "DEBUG: ERROR after cx25821_risc_databuffer_audio() \n");
480 /* Loop back to start of program */
481 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
| RISC_CNT_INC
);
482 buf
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
483 buf
->risc
.jmp
[2] = cpu_to_le32(0); /* bits 63-32 */
485 buf
->vb
.state
= VIDEOBUF_PREPARED
;
488 chip
->dma_risc
= dma
;
490 substream
->runtime
->dma_area
= chip
->dma_risc
->vmalloc
;
491 substream
->runtime
->dma_bytes
= chip
->dma_size
;
492 substream
->runtime
->dma_addr
= 0;
504 static int snd_cx25821_hw_free(struct snd_pcm_substream
*substream
)
506 snd_cx25821_card_t
*chip
= snd_pcm_substream_chip(substream
);
508 if (substream
->runtime
->dma_area
) {
509 dsp_buffer_free(chip
);
510 substream
->runtime
->dma_area
= NULL
;
519 static int snd_cx25821_prepare(struct snd_pcm_substream
*substream
)
527 static int snd_cx25821_card_trigger(struct snd_pcm_substream
*substream
,
530 snd_cx25821_card_t
*chip
= snd_pcm_substream_chip(substream
);
533 /* Local interrupts are already disabled by ALSA */
534 spin_lock(&chip
->reg_lock
);
537 case SNDRV_PCM_TRIGGER_START
:
538 err
= _cx25821_start_audio_dma(chip
);
540 case SNDRV_PCM_TRIGGER_STOP
:
541 err
= _cx25821_stop_audio_dma(chip
);
548 spin_unlock(&chip
->reg_lock
);
556 static snd_pcm_uframes_t
snd_cx25821_pointer(struct snd_pcm_substream
559 snd_cx25821_card_t
*chip
= snd_pcm_substream_chip(substream
);
560 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
563 count
= atomic_read(&chip
->count
);
565 return runtime
->period_size
* (count
& (runtime
->periods
- 1));
569 * page callback (needed for mmap)
571 static struct page
*snd_cx25821_page(struct snd_pcm_substream
*substream
,
572 unsigned long offset
)
574 void *pageptr
= substream
->runtime
->dma_area
+ offset
;
576 return vmalloc_to_page(pageptr
);
582 static struct snd_pcm_ops snd_cx25821_pcm_ops
= {
583 .open
= snd_cx25821_pcm_open
,
584 .close
= snd_cx25821_close
,
585 .ioctl
= snd_pcm_lib_ioctl
,
586 .hw_params
= snd_cx25821_hw_params
,
587 .hw_free
= snd_cx25821_hw_free
,
588 .prepare
= snd_cx25821_prepare
,
589 .trigger
= snd_cx25821_card_trigger
,
590 .pointer
= snd_cx25821_pointer
,
591 .page
= snd_cx25821_page
,
595 * ALSA create a PCM device: Called when initializing the board. Sets up the name and hooks up
598 static int snd_cx25821_pcm(snd_cx25821_card_t
* chip
, int device
, char *name
)
603 err
= snd_pcm_new(chip
->card
, name
, device
, 0, 1, &pcm
);
605 printk(KERN_INFO
"ERROR: FAILED snd_pcm_new() in %s\n",
609 pcm
->private_data
= chip
;
611 strcpy(pcm
->name
, name
);
612 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_cx25821_pcm_ops
);
617 /****************************************************************************
618 Basic Flow for Sound Devices
619 ****************************************************************************/
622 * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
623 * Only boards with eeprom and byte 1 at eeprom=1 have it
626 static struct pci_device_id cx25821_audio_pci_tbl
[] __devinitdata
= {
627 {0x14f1, 0x0920, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
631 MODULE_DEVICE_TABLE(pci
, cx25821_audio_pci_tbl
);
634 * Not used in the function snd_cx25821_dev_free so removing
638 static int snd_cx25821_free(snd_cx25821_card_t *chip)
641 free_irq(chip->irq, chip);
643 cx25821_dev_unregister(chip->dev);
644 pci_disable_device(chip->pci);
651 * Component Destructor
653 static void snd_cx25821_dev_free(struct snd_card
*card
)
655 snd_cx25821_card_t
*chip
= card
->private_data
;
657 //snd_cx25821_free(chip);
658 snd_card_free(chip
->card
);
662 * Alsa Constructor - Component probe
664 static int cx25821_audio_initdev(struct cx25821_dev
*dev
)
666 struct snd_card
*card
;
667 snd_cx25821_card_t
*chip
;
670 if (devno
>= SNDRV_CARDS
) {
671 printk(KERN_INFO
"DEBUG ERROR: devno >= SNDRV_CARDS %s\n",
676 if (!enable
[devno
]) {
678 printk(KERN_INFO
"DEBUG ERROR: !enable[devno] %s\n", __func__
);
682 err
= snd_card_create(index
[devno
], id
[devno
], THIS_MODULE
,
683 sizeof(snd_cx25821_card_t
), &card
);
686 "DEBUG ERROR: cannot create snd_card_new in %s\n",
691 strcpy(card
->driver
, "cx25821");
693 /* Card "creation" */
694 card
->private_free
= snd_cx25821_dev_free
;
695 chip
= (snd_cx25821_card_t
*) card
->private_data
;
696 spin_lock_init(&chip
->reg_lock
);
700 chip
->pci
= dev
->pci
;
701 chip
->iobase
= pci_resource_start(dev
->pci
, 0);
703 chip
->irq
= dev
->pci
->irq
;
705 err
= request_irq(dev
->pci
->irq
, cx25821_irq
,
706 IRQF_SHARED
| IRQF_DISABLED
, chip
->dev
->name
, chip
);
709 printk(KERN_ERR
"ERROR %s: can't get IRQ %d for ALSA\n",
710 chip
->dev
->name
, dev
->pci
->irq
);
714 if ((err
= snd_cx25821_pcm(chip
, 0, "cx25821 Digital")) < 0) {
716 "DEBUG ERROR: cannot create snd_cx25821_pcm %s\n",
721 snd_card_set_dev(card
, &chip
->pci
->dev
);
723 strcpy(card
->shortname
, "cx25821");
724 sprintf(card
->longname
, "%s at 0x%lx irq %d", chip
->dev
->name
,
725 chip
->iobase
, chip
->irq
);
726 strcpy(card
->mixername
, "CX25821");
728 printk(KERN_INFO
"%s/%i: ALSA support for cx25821 boards\n",
729 card
->driver
, devno
);
731 err
= snd_card_register(card
);
733 printk(KERN_INFO
"DEBUG ERROR: cannot register sound card %s\n",
738 snd_cx25821_cards
[devno
] = card
;
748 /****************************************************************************
750 ****************************************************************************/
751 static void cx25821_audio_fini(void)
753 snd_card_free(snd_cx25821_cards
[0]);
759 * Loops through present saa7134 cards, and assigns an ALSA device
763 static int cx25821_alsa_init(void)
765 struct cx25821_dev
*dev
= NULL
;
766 struct list_head
*list
;
768 list_for_each(list
, &cx25821_devlist
) {
769 dev
= list_entry(list
, struct cx25821_dev
, devlist
);
770 cx25821_audio_initdev(dev
);
775 "cx25821 ERROR ALSA: no cx25821 cards found\n");
781 late_initcall(cx25821_alsa_init
);
782 module_exit(cx25821_audio_fini
);
784 /* ----------------------------------------------------------- */