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>
30 #include <linux/slab.h>
32 #include <linux/delay.h>
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>
41 #include "cx25821-reg.h"
43 #define AUDIO_SRAM_CHANNEL SRAM_CH08
45 #define dprintk(level, fmt, arg...) if (debug >= level) \
46 printk(KERN_INFO "%s/1: " fmt, chip->dev->name , ## arg)
48 #define dprintk_core(level, fmt, arg...) if (debug >= level) \
49 printk(KERN_DEBUG "%s/1: " fmt, chip->dev->name , ## arg)
51 /****************************************************************************
52 Data type declarations - Can be moded to a header file later
53 ****************************************************************************/
55 static struct snd_card
*snd_cx25821_cards
[SNDRV_CARDS
];
58 struct cx25821_audio_buffer
{
60 struct btcx_riscmem risc
;
61 struct videobuf_dmabuf dma
;
64 struct cx25821_audio_dev
{
65 struct cx25821_dev
*dev
;
66 struct cx25821_dmaqueue q
;
74 struct snd_card
*card
;
80 unsigned int dma_size
;
81 unsigned int period_size
;
82 unsigned int num_periods
;
84 struct videobuf_dmabuf
*dma_risc
;
86 struct cx25821_audio_buffer
*buf
;
88 struct snd_pcm_substream
*substream
;
92 /****************************************************************************
93 Module global static vars
94 ****************************************************************************/
96 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
; /* Index 0-MAX */
97 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
; /* ID for this card */
98 static int enable
[SNDRV_CARDS
] = { 1, [1 ... (SNDRV_CARDS
- 1)] = 1 };
100 module_param_array(enable
, bool, NULL
, 0444);
101 MODULE_PARM_DESC(enable
, "Enable cx25821 soundcard. default enabled.");
103 module_param_array(index
, int, NULL
, 0444);
104 MODULE_PARM_DESC(index
, "Index value for cx25821 capture interface(s).");
106 /****************************************************************************
108 ****************************************************************************/
110 MODULE_DESCRIPTION("ALSA driver module for cx25821 based capture cards");
111 MODULE_AUTHOR("Hiep Huynh");
112 MODULE_LICENSE("GPL");
113 MODULE_SUPPORTED_DEVICE("{{Conexant,25821}"); /* "{{Conexant,23881}," */
115 static unsigned int debug
;
116 module_param(debug
, int, 0644);
117 MODULE_PARM_DESC(debug
, "enable debug messages");
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)
126 #define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */
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
137 #define PCI_MSK_AUD_EXT (1 << 4)
138 #define PCI_MSK_AUD_INT (1 << 3)
140 * BOARD Specific: Sets audio DMA
143 static int _cx25821_start_audio_dma(struct cx25821_audio_dev
*chip
)
145 struct cx25821_audio_buffer
*buf
= chip
->buf
;
146 struct cx25821_dev
*dev
= chip
->dev
;
147 struct sram_channel
*audio_ch
=
148 &cx25821_sram_channels
[AUDIO_SRAM_CHANNEL
];
151 /* enable output on the GPIO 0 for the MCLK ADC (Audio) */
152 cx25821_set_gpiopin_direction(chip
->dev
, 0, 0);
154 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
155 cx_clear(AUD_INT_DMA_CTL
,
156 FLD_AUD_DST_A_RISC_EN
| FLD_AUD_DST_A_FIFO_EN
);
158 /* setup fifo + format - out channel */
159 cx25821_sram_channel_setup_audio(chip
->dev
, audio_ch
, buf
->bpl
,
163 cx_write(AUD_A_LNGTH
, buf
->bpl
);
166 /* GP_COUNT_CONTROL_RESET = 0x3 */
167 cx_write(AUD_A_GPCNT_CTL
, GP_COUNT_CONTROL_RESET
);
168 atomic_set(&chip
->count
, 0);
170 /* Set the input mode to 16-bit */
171 tmp
= cx_read(AUD_A_CFG
);
173 tmp
| FLD_AUD_DST_PK_MODE
| FLD_AUD_DST_ENABLE
|
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);
184 /* Enables corresponding bits at AUD_INT_STAT */
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
);
189 /* Clean any pending interrupt bits already set */
190 cx_write(AUD_A_INT_STAT
, ~0);
192 /* enable audio irqs */
193 cx_set(PCI_INT_MSK
, chip
->dev
->pci_irqmask
| PCI_MSK_AUD_INT
);
195 /* Turn on audio downstream fifo and risc enable 0x101 */
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
));
205 * BOARD Specific: Resets audio DMA
207 static int _cx25821_stop_audio_dma(struct cx25821_audio_dev
*chip
)
209 struct cx25821_dev
*dev
= chip
->dev
;
212 cx_clear(AUD_INT_DMA_CTL
,
213 FLD_AUD_DST_A_RISC_EN
| FLD_AUD_DST_A_FIFO_EN
);
216 cx_clear(PCI_INT_MSK
, PCI_MSK_AUD_INT
);
217 cx_clear(AUD_A_INT_MSK
,
218 AUD_INT_OPC_ERR
| AUD_INT_DN_SYNC
| AUD_INT_DN_RISCI2
|
224 #define MAX_IRQ_LOOP 50
227 * BOARD Specific: IRQ dma bits
229 static char *cx25821_aud_irqs
[32] = {
230 "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */
232 "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */
234 "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */
236 "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */
238 "opc_err", "par_err", "rip_err", /* 16-18 */
239 "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */
243 * BOARD Specific: Threats IRQ audio specific calls
245 static void cx25821_aud_irq(struct cx25821_audio_dev
*chip
, u32 status
,
248 struct cx25821_dev
*dev
= chip
->dev
;
250 if (0 == (status
& mask
))
253 cx_write(AUD_A_INT_STAT
, status
);
254 if (debug
> 1 || (status
& mask
& ~0xff))
255 cx25821_print_irqbits(dev
->name
, "irq aud",
257 ARRAY_SIZE(cx25821_aud_irqs
), status
,
260 /* risc op code error */
261 if (status
& AUD_INT_OPC_ERR
) {
262 printk(KERN_WARNING
"WARNING %s/1: Audio risc op code error\n",
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
]);
271 if (status
& AUD_INT_DN_SYNC
) {
272 printk(KERN_WARNING
"WARNING %s: Downstream sync error!\n",
274 cx_write(AUD_A_GPCNT_CTL
, GP_COUNT_CONTROL_RESET
);
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
);
286 * BOARD Specific: Handles IRQ calls
288 static irqreturn_t
cx25821_irq(int irq
, void *dev_id
)
290 struct cx25821_audio_dev
*chip
= dev_id
;
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;
297 audint_status
= cx_read(AUD_A_INT_STAT
);
298 audint_mask
= cx_read(AUD_A_INT_MSK
);
299 audint_count
= cx_read(AUD_A_GPCNT
);
300 status
= cx_read(PCI_INT_STAT
);
302 for (loop
= 0; loop
< 1; loop
++) {
303 status
= cx_read(PCI_INT_STAT
);
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
);
311 cx_write(PCI_INT_STAT
, status
);
313 cx25821_aud_irq(chip
, audint_status
,
321 cx_write(PCI_INT_STAT
, status
);
323 cx25821_aud_irq(chip
, audint_status
, audint_mask
);
326 pci_status
= cx_read(PCI_INT_STAT
);
329 cx_write(PCI_INT_STAT
, pci_status
);
332 return IRQ_RETVAL(handled
);
335 static int dsp_buffer_free(struct cx25821_audio_dev
*chip
)
337 BUG_ON(!chip
->dma_size
);
339 dprintk(2, "Freeing buffer\n");
340 videobuf_dma_unmap(&chip
->pci
->dev
, chip
->dma_risc
);
341 videobuf_dma_free(chip
->dma_risc
);
342 btcx_riscmem_free(chip
->pci
, &chip
->buf
->risc
);
345 chip
->dma_risc
= NULL
;
351 /****************************************************************************
353 ****************************************************************************/
356 * Digital hardware definition
358 #define DEFAULT_FIFO_SIZE 384
359 static struct snd_pcm_hardware snd_cx25821_digital_hw
= {
360 .info
= SNDRV_PCM_INFO_MMAP
|
361 SNDRV_PCM_INFO_INTERLEAVED
|
362 SNDRV_PCM_INFO_BLOCK_TRANSFER
| SNDRV_PCM_INFO_MMAP_VALID
,
363 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
365 .rates
= SNDRV_PCM_RATE_48000
,
370 /* Analog audio output will be full of clicks and pops if there
371 are not exactly four lines in the SRAM FIFO buffer. */
372 .period_bytes_min
= DEFAULT_FIFO_SIZE
/ 3,
373 .period_bytes_max
= DEFAULT_FIFO_SIZE
/ 3,
375 .periods_max
= AUDIO_LINE_SIZE
,
376 /* 128 * 128 = 16384 = 1024 * 16 */
377 .buffer_bytes_max
= (AUDIO_LINE_SIZE
* AUDIO_LINE_SIZE
),
381 * audio pcm capture open callback
383 static int snd_cx25821_pcm_open(struct snd_pcm_substream
*substream
)
385 struct cx25821_audio_dev
*chip
= snd_pcm_substream_chip(substream
);
386 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
388 unsigned int bpl
= 0;
391 printk(KERN_ERR
"DEBUG: cx25821 can't find device struct."
392 " Can't proceed with open\n");
397 snd_pcm_hw_constraint_pow2(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
);
401 chip
->substream
= substream
;
403 runtime
->hw
= snd_cx25821_digital_hw
;
405 if (cx25821_sram_channels
[AUDIO_SRAM_CHANNEL
].fifo_size
!=
407 /* since there are 3 audio Clusters */
408 bpl
= cx25821_sram_channels
[AUDIO_SRAM_CHANNEL
].fifo_size
/ 3;
409 bpl
&= ~7; /* must be multiple of 8 */
411 if (bpl
> AUDIO_LINE_SIZE
)
412 bpl
= AUDIO_LINE_SIZE
;
414 runtime
->hw
.period_bytes_min
= bpl
;
415 runtime
->hw
.period_bytes_max
= bpl
;
420 dprintk(1, "Error opening PCM!\n");
425 * audio close callback
427 static int snd_cx25821_close(struct snd_pcm_substream
*substream
)
435 static int snd_cx25821_hw_params(struct snd_pcm_substream
*substream
,
436 struct snd_pcm_hw_params
*hw_params
)
438 struct cx25821_audio_dev
*chip
= snd_pcm_substream_chip(substream
);
439 struct videobuf_dmabuf
*dma
;
441 struct cx25821_audio_buffer
*buf
;
444 if (substream
->runtime
->dma_area
) {
445 dsp_buffer_free(chip
);
446 substream
->runtime
->dma_area
= NULL
;
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
);
453 BUG_ON(!chip
->dma_size
);
454 BUG_ON(chip
->num_periods
& (chip
->num_periods
- 1));
456 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
460 if (chip
->period_size
> AUDIO_LINE_SIZE
)
461 chip
->period_size
= AUDIO_LINE_SIZE
;
463 buf
->bpl
= chip
->period_size
;
466 videobuf_dma_init(dma
);
467 ret
= videobuf_dma_init_kernel(dma
, PCI_DMA_FROMDEVICE
,
468 (PAGE_ALIGN(chip
->dma_size
) >>
473 ret
= videobuf_dma_map(&chip
->pci
->dev
, dma
);
478 cx25821_risc_databuffer_audio(chip
->pci
, &buf
->risc
, dma
->sglist
,
479 chip
->period_size
, chip
->num_periods
,
483 "DEBUG: ERROR after cx25821_risc_databuffer_audio()\n");
487 /* Loop back to start of program */
488 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
| RISC_CNT_INC
);
489 buf
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
490 buf
->risc
.jmp
[2] = cpu_to_le32(0); /* bits 63-32 */
493 chip
->dma_risc
= dma
;
495 substream
->runtime
->dma_area
= chip
->dma_risc
->vaddr
;
496 substream
->runtime
->dma_bytes
= chip
->dma_size
;
497 substream
->runtime
->dma_addr
= 0;
509 static int snd_cx25821_hw_free(struct snd_pcm_substream
*substream
)
511 struct cx25821_audio_dev
*chip
= snd_pcm_substream_chip(substream
);
513 if (substream
->runtime
->dma_area
) {
514 dsp_buffer_free(chip
);
515 substream
->runtime
->dma_area
= NULL
;
524 static int snd_cx25821_prepare(struct snd_pcm_substream
*substream
)
532 static int snd_cx25821_card_trigger(struct snd_pcm_substream
*substream
,
535 struct cx25821_audio_dev
*chip
= snd_pcm_substream_chip(substream
);
538 /* Local interrupts are already disabled by ALSA */
539 spin_lock(&chip
->reg_lock
);
542 case SNDRV_PCM_TRIGGER_START
:
543 err
= _cx25821_start_audio_dma(chip
);
545 case SNDRV_PCM_TRIGGER_STOP
:
546 err
= _cx25821_stop_audio_dma(chip
);
553 spin_unlock(&chip
->reg_lock
);
561 static snd_pcm_uframes_t
snd_cx25821_pointer(struct snd_pcm_substream
564 struct cx25821_audio_dev
*chip
= snd_pcm_substream_chip(substream
);
565 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
568 count
= atomic_read(&chip
->count
);
570 return runtime
->period_size
* (count
& (runtime
->periods
- 1));
574 * page callback (needed for mmap)
576 static struct page
*snd_cx25821_page(struct snd_pcm_substream
*substream
,
577 unsigned long offset
)
579 void *pageptr
= substream
->runtime
->dma_area
+ offset
;
581 return vmalloc_to_page(pageptr
);
587 static 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
,
600 * ALSA create a PCM device: Called when initializing the board.
601 * Sets up the name and hooks up the callbacks
603 static int snd_cx25821_pcm(struct cx25821_audio_dev
*chip
, int device
,
609 err
= snd_pcm_new(chip
->card
, name
, device
, 0, 1, &pcm
);
611 printk(KERN_INFO
"ERROR: FAILED snd_pcm_new() in %s\n",
615 pcm
->private_data
= chip
;
617 strcpy(pcm
->name
, name
);
618 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_cx25821_pcm_ops
);
623 /****************************************************************************
624 Basic Flow for Sound Devices
625 ****************************************************************************/
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
632 static struct pci_device_id cx25821_audio_pci_tbl
[] __devinitdata
= {
633 {0x14f1, 0x0920, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
637 MODULE_DEVICE_TABLE(pci
, cx25821_audio_pci_tbl
);
640 * Not used in the function snd_cx25821_dev_free so removing
644 static int snd_cx25821_free(struct cx25821_audio_dev *chip)
647 free_irq(chip->irq, chip);
649 cx25821_dev_unregister(chip->dev);
650 pci_disable_device(chip->pci);
657 * Component Destructor
659 static void snd_cx25821_dev_free(struct snd_card
*card
)
661 struct cx25821_audio_dev
*chip
= card
->private_data
;
663 /* snd_cx25821_free(chip); */
664 snd_card_free(chip
->card
);
668 * Alsa Constructor - Component probe
670 static int cx25821_audio_initdev(struct cx25821_dev
*dev
)
672 struct snd_card
*card
;
673 struct cx25821_audio_dev
*chip
;
676 if (devno
>= SNDRV_CARDS
) {
677 printk(KERN_INFO
"DEBUG ERROR: devno >= SNDRV_CARDS %s\n",
682 if (!enable
[devno
]) {
684 printk(KERN_INFO
"DEBUG ERROR: !enable[devno] %s\n", __func__
);
688 err
= snd_card_create(index
[devno
], id
[devno
], THIS_MODULE
,
689 sizeof(struct cx25821_audio_dev
), &card
);
692 "DEBUG ERROR: cannot create snd_card_new in %s\n",
697 strcpy(card
->driver
, "cx25821");
699 /* Card "creation" */
700 card
->private_free
= snd_cx25821_dev_free
;
701 chip
= card
->private_data
;
702 spin_lock_init(&chip
->reg_lock
);
706 chip
->pci
= dev
->pci
;
707 chip
->iobase
= pci_resource_start(dev
->pci
, 0);
709 chip
->irq
= dev
->pci
->irq
;
711 err
= request_irq(dev
->pci
->irq
, cx25821_irq
,
712 IRQF_SHARED
| IRQF_DISABLED
, chip
->dev
->name
, chip
);
715 printk(KERN_ERR
"ERROR %s: can't get IRQ %d for ALSA\n",
716 chip
->dev
->name
, dev
->pci
->irq
);
720 err
= snd_cx25821_pcm(chip
, 0, "cx25821 Digital");
723 "DEBUG ERROR: cannot create snd_cx25821_pcm %s\n",
728 snd_card_set_dev(card
, &chip
->pci
->dev
);
730 strcpy(card
->shortname
, "cx25821");
731 sprintf(card
->longname
, "%s at 0x%lx irq %d", chip
->dev
->name
,
732 chip
->iobase
, chip
->irq
);
733 strcpy(card
->mixername
, "CX25821");
735 printk(KERN_INFO
"%s/%i: ALSA support for cx25821 boards\n",
736 card
->driver
, devno
);
738 err
= snd_card_register(card
);
740 printk(KERN_INFO
"DEBUG ERROR: cannot register sound card %s\n",
745 snd_cx25821_cards
[devno
] = card
;
755 /****************************************************************************
757 ****************************************************************************/
758 static void cx25821_audio_fini(void)
760 snd_card_free(snd_cx25821_cards
[0]);
766 * Loops through present saa7134 cards, and assigns an ALSA device
770 static int cx25821_alsa_init(void)
772 struct cx25821_dev
*dev
= NULL
;
773 struct list_head
*list
;
775 list_for_each(list
, &cx25821_devlist
) {
776 dev
= list_entry(list
, struct cx25821_dev
, devlist
);
777 cx25821_audio_initdev(dev
);
782 "cx25821 ERROR ALSA: no cx25821 cards found\n");
788 late_initcall(cx25821_alsa_init
);
789 module_exit(cx25821_audio_fini
);
791 /* ----------------------------------------------------------- */