3 * Support for CX23885 analog audio capture
5 * (c) 2008 Mijhail Moreyra <mijhail.moreyra@gmail.com>
6 * Adapted from cx88-alsa.c
7 * (c) 2009 Steven Toth <stoth@kernellabs.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/device.h>
23 #include <linux/interrupt.h>
24 #include <linux/vmalloc.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/pci.h>
28 #include <asm/delay.h>
30 #include <sound/core.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/control.h>
34 #include <sound/initval.h>
36 #include <sound/tlv.h>
40 #include "cx23885-reg.h"
42 #define AUDIO_SRAM_CHANNEL SRAM_CH07
44 #define dprintk(level, fmt, arg...) do { \
45 if (audio_debug + 1 > level) \
46 printk(KERN_INFO "%s: " fmt, chip->dev->name , ## arg); \
49 #define dprintk_core(level, fmt, arg...) if (audio_debug >= level) \
50 printk(KERN_DEBUG "%s: " fmt, chip->dev->name , ## arg)
52 /****************************************************************************
53 Module global static vars
54 ****************************************************************************/
56 static unsigned int disable_analog_audio
;
57 module_param(disable_analog_audio
, int, 0644);
58 MODULE_PARM_DESC(disable_analog_audio
, "disable analog audio ALSA driver");
60 static unsigned int audio_debug
;
61 module_param(audio_debug
, int, 0644);
62 MODULE_PARM_DESC(audio_debug
, "enable debug messages [analog audio]");
64 /****************************************************************************
65 Board specific funtions
66 ****************************************************************************/
68 /* Constants taken from cx88-reg.h */
69 #define AUD_INT_DN_RISCI1 (1 << 0)
70 #define AUD_INT_UP_RISCI1 (1 << 1)
71 #define AUD_INT_RDS_DN_RISCI1 (1 << 2)
72 #define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */
73 #define AUD_INT_UP_RISCI2 (1 << 5)
74 #define AUD_INT_RDS_DN_RISCI2 (1 << 6)
75 #define AUD_INT_DN_SYNC (1 << 12)
76 #define AUD_INT_UP_SYNC (1 << 13)
77 #define AUD_INT_RDS_DN_SYNC (1 << 14)
78 #define AUD_INT_OPC_ERR (1 << 16)
79 #define AUD_INT_BER_IRQ (1 << 20)
80 #define AUD_INT_MCHG_IRQ (1 << 21)
81 #define GP_COUNT_CONTROL_RESET 0x3
83 static int cx23885_alsa_dma_init(struct cx23885_audio_dev
*chip
, int nr_pages
)
85 struct cx23885_audio_buffer
*buf
= chip
->buf
;
89 buf
->vaddr
= vmalloc_32(nr_pages
<< PAGE_SHIFT
);
90 if (NULL
== buf
->vaddr
) {
91 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages
);
95 dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
96 (unsigned long)buf
->vaddr
,
97 nr_pages
<< PAGE_SHIFT
);
99 memset(buf
->vaddr
, 0, nr_pages
<< PAGE_SHIFT
);
100 buf
->nr_pages
= nr_pages
;
102 buf
->sglist
= vzalloc(buf
->nr_pages
* sizeof(*buf
->sglist
));
103 if (NULL
== buf
->sglist
)
106 sg_init_table(buf
->sglist
, buf
->nr_pages
);
107 for (i
= 0; i
< buf
->nr_pages
; i
++) {
108 pg
= vmalloc_to_page(buf
->vaddr
+ i
* PAGE_SIZE
);
110 goto vmalloc_to_page_err
;
111 sg_set_page(&buf
->sglist
[i
], pg
, PAGE_SIZE
, 0);
124 static int cx23885_alsa_dma_map(struct cx23885_audio_dev
*dev
)
126 struct cx23885_audio_buffer
*buf
= dev
->buf
;
128 buf
->sglen
= dma_map_sg(&dev
->pci
->dev
, buf
->sglist
,
129 buf
->nr_pages
, PCI_DMA_FROMDEVICE
);
131 if (0 == buf
->sglen
) {
132 pr_warn("%s: cx23885_alsa_map_sg failed\n", __func__
);
138 static int cx23885_alsa_dma_unmap(struct cx23885_audio_dev
*dev
)
140 struct cx23885_audio_buffer
*buf
= dev
->buf
;
145 dma_unmap_sg(&dev
->pci
->dev
, buf
->sglist
, buf
->sglen
, PCI_DMA_FROMDEVICE
);
150 static int cx23885_alsa_dma_free(struct cx23885_audio_buffer
*buf
)
160 * BOARD Specific: Sets audio DMA
163 static int cx23885_start_audio_dma(struct cx23885_audio_dev
*chip
)
165 struct cx23885_audio_buffer
*buf
= chip
->buf
;
166 struct cx23885_dev
*dev
= chip
->dev
;
167 struct sram_channel
*audio_ch
=
168 &dev
->sram_channels
[AUDIO_SRAM_CHANNEL
];
170 dprintk(1, "%s()\n", __func__
);
172 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
173 cx_clear(AUD_INT_DMA_CTL
, 0x11);
175 /* setup fifo + format - out channel */
176 cx23885_sram_channel_setup(chip
->dev
, audio_ch
, buf
->bpl
,
180 cx_write(AUD_INT_A_LNGTH
, buf
->bpl
);
182 /* This is required to get good audio (1 seems to be ok) */
183 cx_write(AUD_INT_A_MODE
, 1);
186 cx_write(AUD_INT_A_GPCNT_CTL
, GP_COUNT_CONTROL_RESET
);
187 atomic_set(&chip
->count
, 0);
189 dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d "
190 "byte buffer\n", buf
->bpl
, cx_read(audio_ch
->cmds_start
+12)>>1,
191 chip
->num_periods
, buf
->bpl
* chip
->num_periods
);
193 /* Enables corresponding bits at AUD_INT_STAT */
194 cx_write(AUDIO_INT_INT_MSK
, AUD_INT_OPC_ERR
| AUD_INT_DN_SYNC
|
197 /* Clean any pending interrupt bits already set */
198 cx_write(AUDIO_INT_INT_STAT
, ~0);
200 /* enable audio irqs */
201 cx_set(PCI_INT_MSK
, chip
->dev
->pci_irqmask
| PCI_MSK_AUD_INT
);
204 cx_set(DEV_CNTRL2
, (1<<5)); /* Enables Risc Processor */
205 cx_set(AUD_INT_DMA_CTL
, 0x11); /* audio downstream FIFO and
208 cx23885_sram_channel_dump(chip
->dev
, audio_ch
);
214 * BOARD Specific: Resets audio DMA
216 static int cx23885_stop_audio_dma(struct cx23885_audio_dev
*chip
)
218 struct cx23885_dev
*dev
= chip
->dev
;
219 dprintk(1, "Stopping audio DMA\n");
222 cx_clear(AUD_INT_DMA_CTL
, 0x11);
225 cx_clear(PCI_INT_MSK
, PCI_MSK_AUD_INT
);
226 cx_clear(AUDIO_INT_INT_MSK
, AUD_INT_OPC_ERR
| AUD_INT_DN_SYNC
|
230 cx23885_sram_channel_dump(chip
->dev
,
231 &dev
->sram_channels
[AUDIO_SRAM_CHANNEL
]);
237 * BOARD Specific: Handles audio IRQ
239 int cx23885_audio_irq(struct cx23885_dev
*dev
, u32 status
, u32 mask
)
241 struct cx23885_audio_dev
*chip
= dev
->audio_dev
;
243 if (0 == (status
& mask
))
246 cx_write(AUDIO_INT_INT_STAT
, status
);
248 /* risc op code error */
249 if (status
& AUD_INT_OPC_ERR
) {
250 printk(KERN_WARNING
"%s/1: Audio risc op code error\n",
252 cx_clear(AUD_INT_DMA_CTL
, 0x11);
253 cx23885_sram_channel_dump(dev
,
254 &dev
->sram_channels
[AUDIO_SRAM_CHANNEL
]);
256 if (status
& AUD_INT_DN_SYNC
) {
257 dprintk(1, "Downstream sync error\n");
258 cx_write(AUD_INT_A_GPCNT_CTL
, GP_COUNT_CONTROL_RESET
);
261 /* risc1 downstream */
262 if (status
& AUD_INT_DN_RISCI1
) {
263 atomic_set(&chip
->count
, cx_read(AUD_INT_A_GPCNT
));
264 snd_pcm_period_elapsed(chip
->substream
);
266 /* FIXME: Any other status should deserve a special handling? */
271 static int dsp_buffer_free(struct cx23885_audio_dev
*chip
)
273 BUG_ON(!chip
->dma_size
);
275 dprintk(2, "Freeing buffer\n");
276 cx23885_alsa_dma_unmap(chip
);
277 cx23885_alsa_dma_free(chip
->buf
);
278 btcx_riscmem_free(chip
->pci
, &chip
->buf
->risc
);
287 /****************************************************************************
289 ****************************************************************************/
292 * Digital hardware definition
294 #define DEFAULT_FIFO_SIZE 4096
296 static struct snd_pcm_hardware snd_cx23885_digital_hw
= {
297 .info
= SNDRV_PCM_INFO_MMAP
|
298 SNDRV_PCM_INFO_INTERLEAVED
|
299 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
300 SNDRV_PCM_INFO_MMAP_VALID
,
301 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
303 .rates
= SNDRV_PCM_RATE_48000
,
308 /* Analog audio output will be full of clicks and pops if there
309 are not exactly four lines in the SRAM FIFO buffer. */
310 .period_bytes_min
= DEFAULT_FIFO_SIZE
/4,
311 .period_bytes_max
= DEFAULT_FIFO_SIZE
/4,
314 .buffer_bytes_max
= (1024*1024),
318 * audio pcm capture open callback
320 static int snd_cx23885_pcm_open(struct snd_pcm_substream
*substream
)
322 struct cx23885_audio_dev
*chip
= snd_pcm_substream_chip(substream
);
323 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
327 printk(KERN_ERR
"BUG: cx23885 can't find device struct."
328 " Can't proceed with open\n");
332 err
= snd_pcm_hw_constraint_pow2(runtime
, 0,
333 SNDRV_PCM_HW_PARAM_PERIODS
);
337 chip
->substream
= substream
;
339 runtime
->hw
= snd_cx23885_digital_hw
;
341 if (chip
->dev
->sram_channels
[AUDIO_SRAM_CHANNEL
].fifo_size
!=
343 unsigned int bpl
= chip
->dev
->
344 sram_channels
[AUDIO_SRAM_CHANNEL
].fifo_size
/ 4;
345 bpl
&= ~7; /* must be multiple of 8 */
346 runtime
->hw
.period_bytes_min
= bpl
;
347 runtime
->hw
.period_bytes_max
= bpl
;
352 dprintk(1, "Error opening PCM!\n");
357 * audio close callback
359 static int snd_cx23885_close(struct snd_pcm_substream
*substream
)
368 static int snd_cx23885_hw_params(struct snd_pcm_substream
*substream
,
369 struct snd_pcm_hw_params
*hw_params
)
371 struct cx23885_audio_dev
*chip
= snd_pcm_substream_chip(substream
);
372 struct cx23885_audio_buffer
*buf
;
375 if (substream
->runtime
->dma_area
) {
376 dsp_buffer_free(chip
);
377 substream
->runtime
->dma_area
= NULL
;
380 chip
->period_size
= params_period_bytes(hw_params
);
381 chip
->num_periods
= params_periods(hw_params
);
382 chip
->dma_size
= chip
->period_size
* params_periods(hw_params
);
384 BUG_ON(!chip
->dma_size
);
385 BUG_ON(chip
->num_periods
& (chip
->num_periods
-1));
387 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
391 buf
->bpl
= chip
->period_size
;
394 ret
= cx23885_alsa_dma_init(chip
,
395 (PAGE_ALIGN(chip
->dma_size
) >> PAGE_SHIFT
));
399 ret
= cx23885_alsa_dma_map(chip
);
403 ret
= cx23885_risc_databuffer(chip
->pci
, &buf
->risc
, buf
->sglist
,
404 chip
->period_size
, chip
->num_periods
, 1);
408 /* Loop back to start of program */
409 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
|RISC_IRQ1
|RISC_CNT_INC
);
410 buf
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
411 buf
->risc
.jmp
[2] = cpu_to_le32(0); /* bits 63-32 */
413 substream
->runtime
->dma_area
= chip
->buf
->vaddr
;
414 substream
->runtime
->dma_bytes
= chip
->dma_size
;
415 substream
->runtime
->dma_addr
= 0;
428 static int snd_cx23885_hw_free(struct snd_pcm_substream
*substream
)
431 struct cx23885_audio_dev
*chip
= snd_pcm_substream_chip(substream
);
433 if (substream
->runtime
->dma_area
) {
434 dsp_buffer_free(chip
);
435 substream
->runtime
->dma_area
= NULL
;
444 static int snd_cx23885_prepare(struct snd_pcm_substream
*substream
)
452 static int snd_cx23885_card_trigger(struct snd_pcm_substream
*substream
,
455 struct cx23885_audio_dev
*chip
= snd_pcm_substream_chip(substream
);
458 /* Local interrupts are already disabled by ALSA */
459 spin_lock(&chip
->lock
);
462 case SNDRV_PCM_TRIGGER_START
:
463 err
= cx23885_start_audio_dma(chip
);
465 case SNDRV_PCM_TRIGGER_STOP
:
466 err
= cx23885_stop_audio_dma(chip
);
473 spin_unlock(&chip
->lock
);
481 static snd_pcm_uframes_t
snd_cx23885_pointer(
482 struct snd_pcm_substream
*substream
)
484 struct cx23885_audio_dev
*chip
= snd_pcm_substream_chip(substream
);
485 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
488 count
= atomic_read(&chip
->count
);
490 return runtime
->period_size
* (count
& (runtime
->periods
-1));
494 * page callback (needed for mmap)
496 static struct page
*snd_cx23885_page(struct snd_pcm_substream
*substream
,
497 unsigned long offset
)
499 void *pageptr
= substream
->runtime
->dma_area
+ offset
;
500 return vmalloc_to_page(pageptr
);
506 static struct snd_pcm_ops snd_cx23885_pcm_ops
= {
507 .open
= snd_cx23885_pcm_open
,
508 .close
= snd_cx23885_close
,
509 .ioctl
= snd_pcm_lib_ioctl
,
510 .hw_params
= snd_cx23885_hw_params
,
511 .hw_free
= snd_cx23885_hw_free
,
512 .prepare
= snd_cx23885_prepare
,
513 .trigger
= snd_cx23885_card_trigger
,
514 .pointer
= snd_cx23885_pointer
,
515 .page
= snd_cx23885_page
,
519 * create a PCM device
521 static int snd_cx23885_pcm(struct cx23885_audio_dev
*chip
, int device
,
527 err
= snd_pcm_new(chip
->card
, name
, device
, 0, 1, &pcm
);
530 pcm
->private_data
= chip
;
531 strcpy(pcm
->name
, name
);
532 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_cx23885_pcm_ops
);
537 /****************************************************************************
538 Basic Flow for Sound Devices
539 ****************************************************************************/
542 * Alsa Constructor - Component probe
545 struct cx23885_audio_dev
*cx23885_audio_register(struct cx23885_dev
*dev
)
547 struct snd_card
*card
;
548 struct cx23885_audio_dev
*chip
;
551 if (disable_analog_audio
)
554 if (dev
->sram_channels
[AUDIO_SRAM_CHANNEL
].cmds_start
== 0) {
555 printk(KERN_WARNING
"%s(): Missing SRAM channel configuration "
556 "for analog TV Audio\n", __func__
);
560 err
= snd_card_new(&dev
->pci
->dev
,
561 SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
562 THIS_MODULE
, sizeof(struct cx23885_audio_dev
), &card
);
566 chip
= (struct cx23885_audio_dev
*) card
->private_data
;
568 chip
->pci
= dev
->pci
;
570 spin_lock_init(&chip
->lock
);
572 err
= snd_cx23885_pcm(chip
, 0, "CX23885 Digital");
576 strcpy(card
->driver
, "CX23885");
577 sprintf(card
->shortname
, "Conexant CX23885");
578 sprintf(card
->longname
, "%s at %s", card
->shortname
, dev
->name
);
580 err
= snd_card_register(card
);
584 dprintk(0, "registered ALSA audio device\n");
590 printk(KERN_ERR
"%s(): Failed to register analog "
591 "audio adapter\n", __func__
);
599 void cx23885_audio_unregister(struct cx23885_dev
*dev
)
601 struct cx23885_audio_dev
*chip
= dev
->audio_dev
;
603 snd_card_free(chip
->card
);