Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / pci / cx23885 / cx23885-alsa.c
blobd8c3637e492e37245c8fb5494e1ed62ffa163039
1 /*
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 "cx23885.h"
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); \
47 } while(0)
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;
83 struct page *pg;
84 int i;
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);
89 return -ENOMEM;
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)
101 goto vzalloc_err;
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);
106 if (NULL == pg)
107 goto vmalloc_to_page_err;
108 sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0);
110 return 0;
112 vmalloc_to_page_err:
113 vfree(buf->sglist);
114 buf->sglist = NULL;
115 vzalloc_err:
116 vfree(buf->vaddr);
117 buf->vaddr = NULL;
118 return -ENOMEM;
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__);
130 return -ENOMEM;
132 return 0;
135 static int cx23885_alsa_dma_unmap(struct cx23885_audio_dev *dev)
137 struct cx23885_audio_buffer *buf = dev->buf;
139 if (!buf->sglen)
140 return 0;
142 dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->sglen, PCI_DMA_FROMDEVICE);
143 buf->sglen = 0;
144 return 0;
147 static int cx23885_alsa_dma_free(struct cx23885_audio_buffer *buf)
149 vfree(buf->sglist);
150 buf->sglist = NULL;
151 vfree(buf->vaddr);
152 buf->vaddr = NULL;
153 return 0;
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,
174 buf->risc.dma);
176 /* sets bpl size */
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);
182 /* reset counter */
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 |
192 AUD_INT_DN_RISCI1);
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);
200 /* start dma */
201 cx_set(DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */
202 cx_set(AUD_INT_DMA_CTL, 0x11); /* audio downstream FIFO and
203 RISC enable */
204 if (audio_debug)
205 cx23885_sram_channel_dump(chip->dev, audio_ch);
207 return 0;
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");
218 /* stop dma */
219 cx_clear(AUD_INT_DMA_CTL, 0x11);
221 /* disable irqs */
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 |
224 AUD_INT_DN_RISCI1);
226 if (audio_debug)
227 cx23885_sram_channel_dump(chip->dev,
228 &dev->sram_channels[AUDIO_SRAM_CHANNEL]);
230 return 0;
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))
241 return 0;
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",
248 dev->name);
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);
256 return 1;
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? */
265 return 1;
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);
279 kfree(chip->buf);
281 chip->buf = NULL;
282 chip->dma_size = 0;
284 return 0;
287 /****************************************************************************
288 ALSA PCM Interface
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,
304 .rate_min = 48000,
305 .rate_max = 48000,
306 .channels_min = 2,
307 .channels_max = 2,
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,
312 .periods_min = 1,
313 .periods_max = 1024,
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;
324 int err;
326 if (!chip) {
327 pr_err("BUG: cx23885 can't find device struct. Can't proceed with open\n");
328 return -ENODEV;
331 err = snd_pcm_hw_constraint_pow2(runtime, 0,
332 SNDRV_PCM_HW_PARAM_PERIODS);
333 if (err < 0)
334 goto _error;
336 chip->substream = substream;
338 runtime->hw = snd_cx23885_digital_hw;
340 if (chip->dev->sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
341 DEFAULT_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;
349 return 0;
350 _error:
351 dprintk(1, "Error opening PCM!\n");
352 return err;
356 * audio close callback
358 static int snd_cx23885_close(struct snd_pcm_substream *substream)
360 return 0;
365 * hw_params callback
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;
372 int ret;
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);
387 if (NULL == buf)
388 return -ENOMEM;
390 buf->bpl = chip->period_size;
391 chip->buf = buf;
393 ret = cx23885_alsa_dma_init(chip,
394 (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
395 if (ret < 0)
396 goto error;
398 ret = cx23885_alsa_dma_map(chip);
399 if (ret < 0)
400 goto error;
402 ret = cx23885_risc_databuffer(chip->pci, &buf->risc, buf->sglist,
403 chip->period_size, chip->num_periods, 1);
404 if (ret < 0)
405 goto error;
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;
416 return 0;
418 error:
419 kfree(buf);
420 chip->buf = NULL;
421 return ret;
425 * hw free callback
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;
437 return 0;
441 * prepare callback
443 static int snd_cx23885_prepare(struct snd_pcm_substream *substream)
445 return 0;
449 * trigger callback
451 static int snd_cx23885_card_trigger(struct snd_pcm_substream *substream,
452 int cmd)
454 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
455 int err;
457 /* Local interrupts are already disabled by ALSA */
458 spin_lock(&chip->lock);
460 switch (cmd) {
461 case SNDRV_PCM_TRIGGER_START:
462 err = cx23885_start_audio_dma(chip);
463 break;
464 case SNDRV_PCM_TRIGGER_STOP:
465 err = cx23885_stop_audio_dma(chip);
466 break;
467 default:
468 err = -EINVAL;
469 break;
472 spin_unlock(&chip->lock);
474 return err;
478 * pointer callback
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;
485 u16 count;
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);
503 * operators
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,
521 char *name)
523 int err;
524 struct snd_pcm *pcm;
526 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
527 if (err < 0)
528 return err;
529 pcm->private_data = chip;
530 strcpy(pcm->name, name);
531 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx23885_pcm_ops);
533 return 0;
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;
548 int err;
550 if (disable_analog_audio)
551 return NULL;
553 if (dev->sram_channels[AUDIO_SRAM_CHANNEL].cmds_start == 0) {
554 pr_warn("%s(): Missing SRAM channel configuration for analog TV Audio\n",
555 __func__);
556 return NULL;
559 err = snd_card_new(&dev->pci->dev,
560 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
561 THIS_MODULE, sizeof(struct cx23885_audio_dev), &card);
562 if (err < 0)
563 goto error;
565 chip = (struct cx23885_audio_dev *) card->private_data;
566 chip->dev = dev;
567 chip->pci = dev->pci;
568 chip->card = card;
569 spin_lock_init(&chip->lock);
571 err = snd_cx23885_pcm(chip, 0, "CX23885 Digital");
572 if (err < 0)
573 goto error;
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);
580 if (err < 0)
581 goto error;
583 dprintk(0, "registered ALSA audio device\n");
585 return chip;
587 error:
588 snd_card_free(card);
589 pr_err("%s(): Failed to register analog audio adapter\n",
590 __func__);
592 return NULL;
596 * ALSA destructor
598 void cx23885_audio_unregister(struct cx23885_dev *dev)
600 struct cx23885_audio_dev *chip = dev->audio_dev;
602 snd_card_free(chip->card);