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 %p, size=%d\n",
93 buf->vaddr, nr_pages << PAGE_SHIFT);
95 memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT);
96 buf->nr_pages = nr_pages;
98 buf->sglist = vzalloc(array_size(sizeof(*buf->sglist), buf->nr_pages));
99 if (NULL == buf->sglist)
102 sg_init_table(buf->sglist, buf->nr_pages);
103 for (i = 0; i < buf->nr_pages; i++) {
104 pg = vmalloc_to_page(buf->vaddr + i * PAGE_SIZE);
106 goto vmalloc_to_page_err;
107 sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0);
120 static int cx23885_alsa_dma_map(struct cx23885_audio_dev *dev)
122 struct cx23885_audio_buffer *buf = dev->buf;
124 buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist,
125 buf->nr_pages, PCI_DMA_FROMDEVICE);
127 if (0 == buf->sglen) {
128 pr_warn("%s: cx23885_alsa_map_sg failed\n", __func__);
134 static int cx23885_alsa_dma_unmap(struct cx23885_audio_dev *dev)
136 struct cx23885_audio_buffer *buf = dev->buf;
141 dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->sglen, PCI_DMA_FROMDEVICE);
146 static int cx23885_alsa_dma_free(struct cx23885_audio_buffer *buf)
156 * BOARD Specific: Sets audio DMA
159 static int cx23885_start_audio_dma(struct cx23885_audio_dev *chip)
161 struct cx23885_audio_buffer *buf = chip->buf;
162 struct cx23885_dev *dev = chip->dev;
163 struct sram_channel *audio_ch =
164 &dev->sram_channels[AUDIO_SRAM_CHANNEL];
166 dprintk(1, "%s()\n", __func__);
168 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
169 cx_clear(AUD_INT_DMA_CTL, 0x11);
171 /* setup fifo + format - out channel */
172 cx23885_sram_channel_setup(chip->dev, audio_ch, buf->bpl,
176 cx_write(AUD_INT_A_LNGTH, buf->bpl);
178 /* This is required to get good audio (1 seems to be ok) */
179 cx_write(AUD_INT_A_MODE, 1);
182 cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
183 atomic_set(&chip->count, 0);
185 dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d byte buffer\n",
186 buf->bpl, cx_read(audio_ch->cmds_start+12)>>1,
187 chip->num_periods, buf->bpl * chip->num_periods);
189 /* Enables corresponding bits at AUD_INT_STAT */
190 cx_write(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
193 /* Clean any pending interrupt bits already set */
194 cx_write(AUDIO_INT_INT_STAT, ~0);
196 /* enable audio irqs */
197 cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
200 cx_set(DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */
201 cx_set(AUD_INT_DMA_CTL, 0x11); /* audio downstream FIFO and
204 cx23885_sram_channel_dump(chip->dev, audio_ch);
210 * BOARD Specific: Resets audio DMA
212 static int cx23885_stop_audio_dma(struct cx23885_audio_dev *chip)
214 struct cx23885_dev *dev = chip->dev;
215 dprintk(1, "Stopping audio DMA\n");
218 cx_clear(AUD_INT_DMA_CTL, 0x11);
221 cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
222 cx_clear(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
226 cx23885_sram_channel_dump(chip->dev,
227 &dev->sram_channels[AUDIO_SRAM_CHANNEL]);
233 * BOARD Specific: Handles audio IRQ
235 int cx23885_audio_irq(struct cx23885_dev *dev, u32 status, u32 mask)
237 struct cx23885_audio_dev *chip = dev->audio_dev;
239 if (0 == (status & mask))
242 cx_write(AUDIO_INT_INT_STAT, status);
244 /* risc op code error */
245 if (status & AUD_INT_OPC_ERR) {
246 pr_warn("%s/1: Audio risc op code error\n",
248 cx_clear(AUD_INT_DMA_CTL, 0x11);
249 cx23885_sram_channel_dump(dev,
250 &dev->sram_channels[AUDIO_SRAM_CHANNEL]);
252 if (status & AUD_INT_DN_SYNC) {
253 dprintk(1, "Downstream sync error\n");
254 cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
257 /* risc1 downstream */
258 if (status & AUD_INT_DN_RISCI1) {
259 atomic_set(&chip->count, cx_read(AUD_INT_A_GPCNT));
260 snd_pcm_period_elapsed(chip->substream);
262 /* FIXME: Any other status should deserve a special handling? */
267 static int dsp_buffer_free(struct cx23885_audio_dev *chip)
269 struct cx23885_riscmem *risc;
271 BUG_ON(!chip->dma_size);
273 dprintk(2, "Freeing buffer\n");
274 cx23885_alsa_dma_unmap(chip);
275 cx23885_alsa_dma_free(chip->buf);
276 risc = &chip->buf->risc;
277 pci_free_consistent(chip->pci, risc->size, risc->cpu, risc->dma);
286 /****************************************************************************
288 ****************************************************************************/
291 * Digital hardware definition
293 #define DEFAULT_FIFO_SIZE 4096
295 static const struct snd_pcm_hardware snd_cx23885_digital_hw = {
296 .info = SNDRV_PCM_INFO_MMAP |
297 SNDRV_PCM_INFO_INTERLEAVED |
298 SNDRV_PCM_INFO_BLOCK_TRANSFER |
299 SNDRV_PCM_INFO_MMAP_VALID,
300 .formats = SNDRV_PCM_FMTBIT_S16_LE,
302 .rates = SNDRV_PCM_RATE_48000,
307 /* Analog audio output will be full of clicks and pops if there
308 are not exactly four lines in the SRAM FIFO buffer. */
309 .period_bytes_min = DEFAULT_FIFO_SIZE/4,
310 .period_bytes_max = DEFAULT_FIFO_SIZE/4,
313 .buffer_bytes_max = (1024*1024),
317 * audio pcm capture open callback
319 static int snd_cx23885_pcm_open(struct snd_pcm_substream *substream)
321 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
322 struct snd_pcm_runtime *runtime = substream->runtime;
326 pr_err("BUG: cx23885 can't find device struct. Can't proceed with open\n");
330 err = snd_pcm_hw_constraint_pow2(runtime, 0,
331 SNDRV_PCM_HW_PARAM_PERIODS);
335 chip->substream = substream;
337 runtime->hw = snd_cx23885_digital_hw;
339 if (chip->dev->sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
341 unsigned int bpl = chip->dev->
342 sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 4;
343 bpl &= ~7; /* must be multiple of 8 */
344 runtime->hw.period_bytes_min = bpl;
345 runtime->hw.period_bytes_max = bpl;
350 dprintk(1, "Error opening PCM!\n");
355 * audio close callback
357 static int snd_cx23885_close(struct snd_pcm_substream *substream)
366 static int snd_cx23885_hw_params(struct snd_pcm_substream *substream,
367 struct snd_pcm_hw_params *hw_params)
369 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
370 struct cx23885_audio_buffer *buf;
373 if (substream->runtime->dma_area) {
374 dsp_buffer_free(chip);
375 substream->runtime->dma_area = NULL;
378 chip->period_size = params_period_bytes(hw_params);
379 chip->num_periods = params_periods(hw_params);
380 chip->dma_size = chip->period_size * params_periods(hw_params);
382 BUG_ON(!chip->dma_size);
383 BUG_ON(chip->num_periods & (chip->num_periods-1));
385 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
389 buf->bpl = chip->period_size;
392 ret = cx23885_alsa_dma_init(chip,
393 (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
397 ret = cx23885_alsa_dma_map(chip);
401 ret = cx23885_risc_databuffer(chip->pci, &buf->risc, buf->sglist,
402 chip->period_size, chip->num_periods, 1);
406 /* Loop back to start of program */
407 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
408 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
409 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
411 substream->runtime->dma_area = chip->buf->vaddr;
412 substream->runtime->dma_bytes = chip->dma_size;
413 substream->runtime->dma_addr = 0;
426 static int snd_cx23885_hw_free(struct snd_pcm_substream *substream)
429 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
431 if (substream->runtime->dma_area) {
432 dsp_buffer_free(chip);
433 substream->runtime->dma_area = NULL;
442 static int snd_cx23885_prepare(struct snd_pcm_substream *substream)
450 static int snd_cx23885_card_trigger(struct snd_pcm_substream *substream,
453 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
456 /* Local interrupts are already disabled by ALSA */
457 spin_lock(&chip->lock);
460 case SNDRV_PCM_TRIGGER_START:
461 err = cx23885_start_audio_dma(chip);
463 case SNDRV_PCM_TRIGGER_STOP:
464 err = cx23885_stop_audio_dma(chip);
471 spin_unlock(&chip->lock);
479 static snd_pcm_uframes_t snd_cx23885_pointer(
480 struct snd_pcm_substream *substream)
482 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
483 struct snd_pcm_runtime *runtime = substream->runtime;
486 count = atomic_read(&chip->count);
488 return runtime->period_size * (count & (runtime->periods-1));
492 * page callback (needed for mmap)
494 static struct page *snd_cx23885_page(struct snd_pcm_substream *substream,
495 unsigned long offset)
497 void *pageptr = substream->runtime->dma_area + offset;
498 return vmalloc_to_page(pageptr);
504 static const struct snd_pcm_ops snd_cx23885_pcm_ops = {
505 .open = snd_cx23885_pcm_open,
506 .close = snd_cx23885_close,
507 .ioctl = snd_pcm_lib_ioctl,
508 .hw_params = snd_cx23885_hw_params,
509 .hw_free = snd_cx23885_hw_free,
510 .prepare = snd_cx23885_prepare,
511 .trigger = snd_cx23885_card_trigger,
512 .pointer = snd_cx23885_pointer,
513 .page = snd_cx23885_page,
517 * create a PCM device
519 static int snd_cx23885_pcm(struct cx23885_audio_dev *chip, int device,
525 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
528 pcm->private_data = chip;
529 strcpy(pcm->name, name);
530 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx23885_pcm_ops);
535 /****************************************************************************
536 Basic Flow for Sound Devices
537 ****************************************************************************/
540 * Alsa Constructor - Component probe
543 struct cx23885_audio_dev *cx23885_audio_register(struct cx23885_dev *dev)
545 struct snd_card *card;
546 struct cx23885_audio_dev *chip;
549 if (disable_analog_audio)
552 if (dev->sram_channels[AUDIO_SRAM_CHANNEL].cmds_start == 0) {
553 pr_warn("%s(): Missing SRAM channel configuration for analog TV Audio\n",
558 err = snd_card_new(&dev->pci->dev,
559 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
560 THIS_MODULE, sizeof(struct cx23885_audio_dev), &card);
564 chip = (struct cx23885_audio_dev *) card->private_data;
566 chip->pci = dev->pci;
568 spin_lock_init(&chip->lock);
570 err = snd_cx23885_pcm(chip, 0, "CX23885 Digital");
574 strcpy(card->driver, "CX23885");
575 sprintf(card->shortname, "Conexant CX23885");
576 sprintf(card->longname, "%s at %s", card->shortname, dev->name);
578 err = snd_card_register(card);
582 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);