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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/device.h>
27 #include <linux/interrupt.h>
28 #include <linux/vmalloc.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/pci.h>
32 #include <asm/delay.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>
40 #include <sound/tlv.h>
44 #include "cx23885-reg.h"
46 #define AUDIO_SRAM_CHANNEL SRAM_CH07
48 #define dprintk(level, fmt, arg...) if (audio_debug >= level) \
49 printk(KERN_INFO "%s: " fmt, chip->dev->name , ## arg)
51 #define dprintk_core(level, fmt, arg...) if (audio_debug >= level) \
52 printk(KERN_DEBUG "%s: " fmt, chip->dev->name , ## arg)
54 /****************************************************************************
55 Module global static vars
56 ****************************************************************************/
58 static unsigned int disable_analog_audio
;
59 module_param(disable_analog_audio
, int, 0644);
60 MODULE_PARM_DESC(disable_analog_audio
, "disable analog audio ALSA driver");
62 static unsigned int audio_debug
;
63 module_param(audio_debug
, int, 0644);
64 MODULE_PARM_DESC(audio_debug
, "enable debug messages [analog audio]");
66 /****************************************************************************
67 Board specific funtions
68 ****************************************************************************/
70 /* Constants taken from cx88-reg.h */
71 #define AUD_INT_DN_RISCI1 (1 << 0)
72 #define AUD_INT_UP_RISCI1 (1 << 1)
73 #define AUD_INT_RDS_DN_RISCI1 (1 << 2)
74 #define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */
75 #define AUD_INT_UP_RISCI2 (1 << 5)
76 #define AUD_INT_RDS_DN_RISCI2 (1 << 6)
77 #define AUD_INT_DN_SYNC (1 << 12)
78 #define AUD_INT_UP_SYNC (1 << 13)
79 #define AUD_INT_RDS_DN_SYNC (1 << 14)
80 #define AUD_INT_OPC_ERR (1 << 16)
81 #define AUD_INT_BER_IRQ (1 << 20)
82 #define AUD_INT_MCHG_IRQ (1 << 21)
83 #define GP_COUNT_CONTROL_RESET 0x3
86 * BOARD Specific: Sets audio DMA
89 static int cx23885_start_audio_dma(struct cx23885_audio_dev
*chip
)
91 struct cx23885_audio_buffer
*buf
= chip
->buf
;
92 struct cx23885_dev
*dev
= chip
->dev
;
93 struct sram_channel
*audio_ch
=
94 &dev
->sram_channels
[AUDIO_SRAM_CHANNEL
];
96 dprintk(1, "%s()\n", __func__
);
98 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
99 cx_clear(AUD_INT_DMA_CTL
, 0x11);
101 /* setup fifo + format - out channel */
102 cx23885_sram_channel_setup(chip
->dev
, audio_ch
, buf
->bpl
,
106 cx_write(AUD_INT_A_LNGTH
, buf
->bpl
);
108 /* This is required to get good audio (1 seems to be ok) */
109 cx_write(AUD_INT_A_MODE
, 1);
112 cx_write(AUD_INT_A_GPCNT_CTL
, GP_COUNT_CONTROL_RESET
);
113 atomic_set(&chip
->count
, 0);
115 dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d "
116 "byte buffer\n", buf
->bpl
, cx_read(audio_ch
->cmds_start
+12)>>1,
117 chip
->num_periods
, buf
->bpl
* chip
->num_periods
);
119 /* Enables corresponding bits at AUD_INT_STAT */
120 cx_write(AUDIO_INT_INT_MSK
, AUD_INT_OPC_ERR
| AUD_INT_DN_SYNC
|
123 /* Clean any pending interrupt bits already set */
124 cx_write(AUDIO_INT_INT_STAT
, ~0);
126 /* enable audio irqs */
127 cx_set(PCI_INT_MSK
, chip
->dev
->pci_irqmask
| PCI_MSK_AUD_INT
);
130 cx_set(DEV_CNTRL2
, (1<<5)); /* Enables Risc Processor */
131 cx_set(AUD_INT_DMA_CTL
, 0x11); /* audio downstream FIFO and
134 cx23885_sram_channel_dump(chip
->dev
, audio_ch
);
140 * BOARD Specific: Resets audio DMA
142 static int cx23885_stop_audio_dma(struct cx23885_audio_dev
*chip
)
144 struct cx23885_dev
*dev
= chip
->dev
;
145 dprintk(1, "Stopping audio DMA\n");
148 cx_clear(AUD_INT_DMA_CTL
, 0x11);
151 cx_clear(PCI_INT_MSK
, PCI_MSK_AUD_INT
);
152 cx_clear(AUDIO_INT_INT_MSK
, AUD_INT_OPC_ERR
| AUD_INT_DN_SYNC
|
156 cx23885_sram_channel_dump(chip
->dev
,
157 &dev
->sram_channels
[AUDIO_SRAM_CHANNEL
]);
163 * BOARD Specific: Handles audio IRQ
165 int cx23885_audio_irq(struct cx23885_dev
*dev
, u32 status
, u32 mask
)
167 struct cx23885_audio_dev
*chip
= dev
->audio_dev
;
169 if (0 == (status
& mask
))
172 cx_write(AUDIO_INT_INT_STAT
, status
);
174 /* risc op code error */
175 if (status
& AUD_INT_OPC_ERR
) {
176 printk(KERN_WARNING
"%s/1: Audio risc op code error\n",
178 cx_clear(AUD_INT_DMA_CTL
, 0x11);
179 cx23885_sram_channel_dump(dev
,
180 &dev
->sram_channels
[AUDIO_SRAM_CHANNEL
]);
182 if (status
& AUD_INT_DN_SYNC
) {
183 dprintk(1, "Downstream sync error\n");
184 cx_write(AUD_INT_A_GPCNT_CTL
, GP_COUNT_CONTROL_RESET
);
187 /* risc1 downstream */
188 if (status
& AUD_INT_DN_RISCI1
) {
189 atomic_set(&chip
->count
, cx_read(AUD_INT_A_GPCNT
));
190 snd_pcm_period_elapsed(chip
->substream
);
192 /* FIXME: Any other status should deserve a special handling? */
197 static int dsp_buffer_free(struct cx23885_audio_dev
*chip
)
199 BUG_ON(!chip
->dma_size
);
201 dprintk(2, "Freeing buffer\n");
202 videobuf_dma_unmap(&chip
->pci
->dev
, chip
->dma_risc
);
203 videobuf_dma_free(chip
->dma_risc
);
204 btcx_riscmem_free(chip
->pci
, &chip
->buf
->risc
);
207 chip
->dma_risc
= NULL
;
213 /****************************************************************************
215 ****************************************************************************/
218 * Digital hardware definition
220 #define DEFAULT_FIFO_SIZE 4096
222 static struct snd_pcm_hardware snd_cx23885_digital_hw
= {
223 .info
= SNDRV_PCM_INFO_MMAP
|
224 SNDRV_PCM_INFO_INTERLEAVED
|
225 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
226 SNDRV_PCM_INFO_MMAP_VALID
,
227 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
229 .rates
= SNDRV_PCM_RATE_48000
,
234 /* Analog audio output will be full of clicks and pops if there
235 are not exactly four lines in the SRAM FIFO buffer. */
236 .period_bytes_min
= DEFAULT_FIFO_SIZE
/4,
237 .period_bytes_max
= DEFAULT_FIFO_SIZE
/4,
240 .buffer_bytes_max
= (1024*1024),
244 * audio pcm capture open callback
246 static int snd_cx23885_pcm_open(struct snd_pcm_substream
*substream
)
248 struct cx23885_audio_dev
*chip
= snd_pcm_substream_chip(substream
);
249 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
253 printk(KERN_ERR
"BUG: cx23885 can't find device struct."
254 " Can't proceed with open\n");
258 err
= snd_pcm_hw_constraint_pow2(runtime
, 0,
259 SNDRV_PCM_HW_PARAM_PERIODS
);
263 chip
->substream
= substream
;
265 runtime
->hw
= snd_cx23885_digital_hw
;
267 if (chip
->dev
->sram_channels
[AUDIO_SRAM_CHANNEL
].fifo_size
!=
269 unsigned int bpl
= chip
->dev
->
270 sram_channels
[AUDIO_SRAM_CHANNEL
].fifo_size
/ 4;
271 bpl
&= ~7; /* must be multiple of 8 */
272 runtime
->hw
.period_bytes_min
= bpl
;
273 runtime
->hw
.period_bytes_max
= bpl
;
278 dprintk(1, "Error opening PCM!\n");
283 * audio close callback
285 static int snd_cx23885_close(struct snd_pcm_substream
*substream
)
293 static int snd_cx23885_hw_params(struct snd_pcm_substream
*substream
,
294 struct snd_pcm_hw_params
*hw_params
)
296 struct cx23885_audio_dev
*chip
= snd_pcm_substream_chip(substream
);
297 struct videobuf_dmabuf
*dma
;
299 struct cx23885_audio_buffer
*buf
;
302 if (substream
->runtime
->dma_area
) {
303 dsp_buffer_free(chip
);
304 substream
->runtime
->dma_area
= NULL
;
307 chip
->period_size
= params_period_bytes(hw_params
);
308 chip
->num_periods
= params_periods(hw_params
);
309 chip
->dma_size
= chip
->period_size
* params_periods(hw_params
);
311 BUG_ON(!chip
->dma_size
);
312 BUG_ON(chip
->num_periods
& (chip
->num_periods
-1));
314 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
318 buf
->bpl
= chip
->period_size
;
321 videobuf_dma_init(dma
);
322 ret
= videobuf_dma_init_kernel(dma
, PCI_DMA_FROMDEVICE
,
323 (PAGE_ALIGN(chip
->dma_size
) >> PAGE_SHIFT
));
327 ret
= videobuf_dma_map(&chip
->pci
->dev
, dma
);
331 ret
= cx23885_risc_databuffer(chip
->pci
, &buf
->risc
, dma
->sglist
,
332 chip
->period_size
, chip
->num_periods
, 1);
336 /* Loop back to start of program */
337 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
|RISC_IRQ1
|RISC_CNT_INC
);
338 buf
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
339 buf
->risc
.jmp
[2] = cpu_to_le32(0); /* bits 63-32 */
342 chip
->dma_risc
= dma
;
344 substream
->runtime
->dma_area
= chip
->dma_risc
->vaddr
;
345 substream
->runtime
->dma_bytes
= chip
->dma_size
;
346 substream
->runtime
->dma_addr
= 0;
358 static int snd_cx23885_hw_free(struct snd_pcm_substream
*substream
)
361 struct cx23885_audio_dev
*chip
= snd_pcm_substream_chip(substream
);
363 if (substream
->runtime
->dma_area
) {
364 dsp_buffer_free(chip
);
365 substream
->runtime
->dma_area
= NULL
;
374 static int snd_cx23885_prepare(struct snd_pcm_substream
*substream
)
382 static int snd_cx23885_card_trigger(struct snd_pcm_substream
*substream
,
385 struct cx23885_audio_dev
*chip
= snd_pcm_substream_chip(substream
);
388 /* Local interrupts are already disabled by ALSA */
389 spin_lock(&chip
->lock
);
392 case SNDRV_PCM_TRIGGER_START
:
393 err
= cx23885_start_audio_dma(chip
);
395 case SNDRV_PCM_TRIGGER_STOP
:
396 err
= cx23885_stop_audio_dma(chip
);
403 spin_unlock(&chip
->lock
);
411 static snd_pcm_uframes_t
snd_cx23885_pointer(
412 struct snd_pcm_substream
*substream
)
414 struct cx23885_audio_dev
*chip
= snd_pcm_substream_chip(substream
);
415 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
418 count
= atomic_read(&chip
->count
);
420 return runtime
->period_size
* (count
& (runtime
->periods
-1));
424 * page callback (needed for mmap)
426 static struct page
*snd_cx23885_page(struct snd_pcm_substream
*substream
,
427 unsigned long offset
)
429 void *pageptr
= substream
->runtime
->dma_area
+ offset
;
430 return vmalloc_to_page(pageptr
);
436 static struct snd_pcm_ops snd_cx23885_pcm_ops
= {
437 .open
= snd_cx23885_pcm_open
,
438 .close
= snd_cx23885_close
,
439 .ioctl
= snd_pcm_lib_ioctl
,
440 .hw_params
= snd_cx23885_hw_params
,
441 .hw_free
= snd_cx23885_hw_free
,
442 .prepare
= snd_cx23885_prepare
,
443 .trigger
= snd_cx23885_card_trigger
,
444 .pointer
= snd_cx23885_pointer
,
445 .page
= snd_cx23885_page
,
449 * create a PCM device
451 static int snd_cx23885_pcm(struct cx23885_audio_dev
*chip
, int device
,
457 err
= snd_pcm_new(chip
->card
, name
, device
, 0, 1, &pcm
);
460 pcm
->private_data
= chip
;
461 strcpy(pcm
->name
, name
);
462 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_cx23885_pcm_ops
);
467 /****************************************************************************
468 Basic Flow for Sound Devices
469 ****************************************************************************/
472 * Alsa Constructor - Component probe
475 struct cx23885_audio_dev
*cx23885_audio_register(struct cx23885_dev
*dev
)
477 struct snd_card
*card
;
478 struct cx23885_audio_dev
*chip
;
481 if (disable_analog_audio
)
484 if (dev
->sram_channels
[AUDIO_SRAM_CHANNEL
].cmds_start
== 0) {
485 printk(KERN_WARNING
"%s(): Missing SRAM channel configuration "
486 "for analog TV Audio\n", __func__
);
490 err
= snd_card_create(SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
491 THIS_MODULE
, sizeof(struct cx23885_audio_dev
), &card
);
495 chip
= (struct cx23885_audio_dev
*) card
->private_data
;
497 chip
->pci
= dev
->pci
;
499 spin_lock_init(&chip
->lock
);
501 snd_card_set_dev(card
, &dev
->pci
->dev
);
503 err
= snd_cx23885_pcm(chip
, 0, "CX23885 Digital");
507 strcpy(card
->driver
, "CX23885");
508 sprintf(card
->shortname
, "Conexant CX23885");
509 sprintf(card
->longname
, "%s at %s", card
->shortname
, dev
->name
);
511 err
= snd_card_register(card
);
515 dprintk(0, "registered ALSA audio device\n");
521 printk(KERN_ERR
"%s(): Failed to register analog "
522 "audio adapter\n", __func__
);
530 void cx23885_audio_unregister(struct cx23885_dev
*dev
)
532 struct cx23885_audio_dev
*chip
= dev
->audio_dev
;
534 snd_card_free(chip
->card
);