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.
21 #include "cx23885-reg.h"
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>
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>
39 #include <sound/tlv.h>
41 #define AUDIO_SRAM_CHANNEL SRAM_CH07
43 #define dprintk(level, fmt, arg...) do { \
44 if (audio_debug + 1 > level) \
45 printk(KERN_DEBUG pr_fmt("%s: alsa: " fmt), \
46 chip->dev->name, ##arg); \
49 /****************************************************************************
50 Module global static vars
51 ****************************************************************************/
53 static unsigned int disable_analog_audio;
54 module_param(disable_analog_audio, int, 0644);
55 MODULE_PARM_DESC(disable_analog_audio, "disable analog audio ALSA driver");
57 static unsigned int audio_debug;
58 module_param(audio_debug, int, 0644);
59 MODULE_PARM_DESC(audio_debug, "enable debug messages [analog audio]");
61 /****************************************************************************
62 Board specific funtions
63 ****************************************************************************/
65 /* Constants taken from cx88-reg.h */
66 #define AUD_INT_DN_RISCI1 (1 << 0)
67 #define AUD_INT_UP_RISCI1 (1 << 1)
68 #define AUD_INT_RDS_DN_RISCI1 (1 << 2)
69 #define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */
70 #define AUD_INT_UP_RISCI2 (1 << 5)
71 #define AUD_INT_RDS_DN_RISCI2 (1 << 6)
72 #define AUD_INT_DN_SYNC (1 << 12)
73 #define AUD_INT_UP_SYNC (1 << 13)
74 #define AUD_INT_RDS_DN_SYNC (1 << 14)
75 #define AUD_INT_OPC_ERR (1 << 16)
76 #define AUD_INT_BER_IRQ (1 << 20)
77 #define AUD_INT_MCHG_IRQ (1 << 21)
78 #define GP_COUNT_CONTROL_RESET 0x3
80 static int cx23885_alsa_dma_init(struct cx23885_audio_dev *chip, int nr_pages)
82 struct cx23885_audio_buffer *buf = chip->buf;
86 buf->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
87 if (NULL == buf->vaddr) {
88 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
92 dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
93 (unsigned long)buf->vaddr,
94 nr_pages << PAGE_SHIFT);
96 memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT);
97 buf->nr_pages = nr_pages;
99 buf->sglist = vzalloc(buf->nr_pages * sizeof(*buf->sglist));
100 if (NULL == buf->sglist)
103 sg_init_table(buf->sglist, buf->nr_pages);
104 for (i = 0; i < buf->nr_pages; i++) {
105 pg = vmalloc_to_page(buf->vaddr + i * PAGE_SIZE);
107 goto vmalloc_to_page_err;
108 sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0);
121 static int cx23885_alsa_dma_map(struct cx23885_audio_dev *dev)
123 struct cx23885_audio_buffer *buf = dev->buf;
125 buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist,
126 buf->nr_pages, PCI_DMA_FROMDEVICE);
128 if (0 == buf->sglen) {
129 pr_warn("%s: cx23885_alsa_map_sg failed\n", __func__);
135 static int cx23885_alsa_dma_unmap(struct cx23885_audio_dev *dev)
137 struct cx23885_audio_buffer *buf = dev->buf;
142 dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->sglen, PCI_DMA_FROMDEVICE);
147 static int cx23885_alsa_dma_free(struct cx23885_audio_buffer *buf)
157 * BOARD Specific: Sets audio DMA
160 static int cx23885_start_audio_dma(struct cx23885_audio_dev *chip)
162 struct cx23885_audio_buffer *buf = chip->buf;
163 struct cx23885_dev *dev = chip->dev;
164 struct sram_channel *audio_ch =
165 &dev->sram_channels[AUDIO_SRAM_CHANNEL];
167 dprintk(1, "%s()\n", __func__);
169 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
170 cx_clear(AUD_INT_DMA_CTL, 0x11);
172 /* setup fifo + format - out channel */
173 cx23885_sram_channel_setup(chip->dev, audio_ch, buf->bpl,
177 cx_write(AUD_INT_A_LNGTH, buf->bpl);
179 /* This is required to get good audio (1 seems to be ok) */
180 cx_write(AUD_INT_A_MODE, 1);
183 cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
184 atomic_set(&chip->count, 0);
186 dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d byte buffer\n",
187 buf->bpl, cx_read(audio_ch->cmds_start+12)>>1,
188 chip->num_periods, buf->bpl * chip->num_periods);
190 /* Enables corresponding bits at AUD_INT_STAT */
191 cx_write(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
194 /* Clean any pending interrupt bits already set */
195 cx_write(AUDIO_INT_INT_STAT, ~0);
197 /* enable audio irqs */
198 cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
201 cx_set(DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */
202 cx_set(AUD_INT_DMA_CTL, 0x11); /* audio downstream FIFO and
205 cx23885_sram_channel_dump(chip->dev, audio_ch);
211 * BOARD Specific: Resets audio DMA
213 static int cx23885_stop_audio_dma(struct cx23885_audio_dev *chip)
215 struct cx23885_dev *dev = chip->dev;
216 dprintk(1, "Stopping audio DMA\n");
219 cx_clear(AUD_INT_DMA_CTL, 0x11);
222 cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
223 cx_clear(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
227 cx23885_sram_channel_dump(chip->dev,
228 &dev->sram_channels[AUDIO_SRAM_CHANNEL]);
234 * BOARD Specific: Handles audio IRQ
236 int cx23885_audio_irq(struct cx23885_dev *dev, u32 status, u32 mask)
238 struct cx23885_audio_dev *chip = dev->audio_dev;
240 if (0 == (status & mask))
243 cx_write(AUDIO_INT_INT_STAT, status);
245 /* risc op code error */
246 if (status & AUD_INT_OPC_ERR) {
247 pr_warn("%s/1: Audio risc op code error\n",
249 cx_clear(AUD_INT_DMA_CTL, 0x11);
250 cx23885_sram_channel_dump(dev,
251 &dev->sram_channels[AUDIO_SRAM_CHANNEL]);
253 if (status & AUD_INT_DN_SYNC) {
254 dprintk(1, "Downstream sync error\n");
255 cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
258 /* risc1 downstream */
259 if (status & AUD_INT_DN_RISCI1) {
260 atomic_set(&chip->count, cx_read(AUD_INT_A_GPCNT));
261 snd_pcm_period_elapsed(chip->substream);
263 /* FIXME: Any other status should deserve a special handling? */
268 static int dsp_buffer_free(struct cx23885_audio_dev *chip)
270 struct cx23885_riscmem *risc;
272 BUG_ON(!chip->dma_size);
274 dprintk(2, "Freeing buffer\n");
275 cx23885_alsa_dma_unmap(chip);
276 cx23885_alsa_dma_free(chip->buf);
277 risc = &chip->buf->risc;
278 pci_free_consistent(chip->pci, risc->size, risc->cpu, risc->dma);
287 /****************************************************************************
289 ****************************************************************************/
292 * Digital hardware definition
294 #define DEFAULT_FIFO_SIZE 4096
296 static const 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 pr_err("BUG: cx23885 can't find device struct. Can't proceed with open\n");
331 err = snd_pcm_hw_constraint_pow2(runtime, 0,
332 SNDRV_PCM_HW_PARAM_PERIODS);
336 chip->substream = substream;
338 runtime->hw = snd_cx23885_digital_hw;
340 if (chip->dev->sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
342 unsigned int bpl = chip->dev->
343 sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 4;
344 bpl &= ~7; /* must be multiple of 8 */
345 runtime->hw.period_bytes_min = bpl;
346 runtime->hw.period_bytes_max = bpl;
351 dprintk(1, "Error opening PCM!\n");
356 * audio close callback
358 static int snd_cx23885_close(struct snd_pcm_substream *substream)
367 static int snd_cx23885_hw_params(struct snd_pcm_substream *substream,
368 struct snd_pcm_hw_params *hw_params)
370 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
371 struct cx23885_audio_buffer *buf;
374 if (substream->runtime->dma_area) {
375 dsp_buffer_free(chip);
376 substream->runtime->dma_area = NULL;
379 chip->period_size = params_period_bytes(hw_params);
380 chip->num_periods = params_periods(hw_params);
381 chip->dma_size = chip->period_size * params_periods(hw_params);
383 BUG_ON(!chip->dma_size);
384 BUG_ON(chip->num_periods & (chip->num_periods-1));
386 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
390 buf->bpl = chip->period_size;
393 ret = cx23885_alsa_dma_init(chip,
394 (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
398 ret = cx23885_alsa_dma_map(chip);
402 ret = cx23885_risc_databuffer(chip->pci, &buf->risc, buf->sglist,
403 chip->period_size, chip->num_periods, 1);
407 /* Loop back to start of program */
408 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
409 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
410 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
412 substream->runtime->dma_area = chip->buf->vaddr;
413 substream->runtime->dma_bytes = chip->dma_size;
414 substream->runtime->dma_addr = 0;
427 static int snd_cx23885_hw_free(struct snd_pcm_substream *substream)
430 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
432 if (substream->runtime->dma_area) {
433 dsp_buffer_free(chip);
434 substream->runtime->dma_area = NULL;
443 static int snd_cx23885_prepare(struct snd_pcm_substream *substream)
451 static int snd_cx23885_card_trigger(struct snd_pcm_substream *substream,
454 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
457 /* Local interrupts are already disabled by ALSA */
458 spin_lock(&chip->lock);
461 case SNDRV_PCM_TRIGGER_START:
462 err = cx23885_start_audio_dma(chip);
464 case SNDRV_PCM_TRIGGER_STOP:
465 err = cx23885_stop_audio_dma(chip);
472 spin_unlock(&chip->lock);
480 static snd_pcm_uframes_t snd_cx23885_pointer(
481 struct snd_pcm_substream *substream)
483 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
484 struct snd_pcm_runtime *runtime = substream->runtime;
487 count = atomic_read(&chip->count);
489 return runtime->period_size * (count & (runtime->periods-1));
493 * page callback (needed for mmap)
495 static struct page *snd_cx23885_page(struct snd_pcm_substream *substream,
496 unsigned long offset)
498 void *pageptr = substream->runtime->dma_area + offset;
499 return vmalloc_to_page(pageptr);
505 static const struct snd_pcm_ops snd_cx23885_pcm_ops = {
506 .open = snd_cx23885_pcm_open,
507 .close = snd_cx23885_close,
508 .ioctl = snd_pcm_lib_ioctl,
509 .hw_params = snd_cx23885_hw_params,
510 .hw_free = snd_cx23885_hw_free,
511 .prepare = snd_cx23885_prepare,
512 .trigger = snd_cx23885_card_trigger,
513 .pointer = snd_cx23885_pointer,
514 .page = snd_cx23885_page,
518 * create a PCM device
520 static int snd_cx23885_pcm(struct cx23885_audio_dev *chip, int device,
526 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
529 pcm->private_data = chip;
530 strcpy(pcm->name, name);
531 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx23885_pcm_ops);
536 /****************************************************************************
537 Basic Flow for Sound Devices
538 ****************************************************************************/
541 * Alsa Constructor - Component probe
544 struct cx23885_audio_dev *cx23885_audio_register(struct cx23885_dev *dev)
546 struct snd_card *card;
547 struct cx23885_audio_dev *chip;
550 if (disable_analog_audio)
553 if (dev->sram_channels[AUDIO_SRAM_CHANNEL].cmds_start == 0) {
554 pr_warn("%s(): Missing SRAM channel configuration for analog TV Audio\n",
559 err = snd_card_new(&dev->pci->dev,
560 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
561 THIS_MODULE, sizeof(struct cx23885_audio_dev), &card);
565 chip = (struct cx23885_audio_dev *) card->private_data;
567 chip->pci = dev->pci;
569 spin_lock_init(&chip->lock);
571 err = snd_cx23885_pcm(chip, 0, "CX23885 Digital");
575 strcpy(card->driver, "CX23885");
576 sprintf(card->shortname, "Conexant CX23885");
577 sprintf(card->longname, "%s at %s", card->shortname, dev->name);
579 err = snd_card_register(card);
583 dprintk(0, "registered ALSA audio device\n");
589 pr_err("%s(): Failed to register analog audio adapter\n",
598 void cx23885_audio_unregister(struct cx23885_dev *dev)
600 struct cx23885_audio_dev *chip = dev->audio_dev;
602 snd_card_free(chip->card);