treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / pci / cx23885 / cx23885-alsa.c
blobdf44ed7393a0250d687834196918ce06133e99d8
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
4 * Support for CX23885 analog audio capture
6 * (c) 2008 Mijhail Moreyra <mijhail.moreyra@gmail.com>
7 * Adapted from cx88-alsa.c
8 * (c) 2009 Steven Toth <stoth@kernellabs.com>
9 */
11 #include "cx23885.h"
12 #include "cx23885-reg.h"
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17 #include <linux/interrupt.h>
18 #include <linux/vmalloc.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/pci.h>
22 #include <asm/delay.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/control.h>
28 #include <sound/initval.h>
30 #include <sound/tlv.h>
32 #define AUDIO_SRAM_CHANNEL SRAM_CH07
34 #define dprintk(level, fmt, arg...) do { \
35 if (audio_debug + 1 > level) \
36 printk(KERN_DEBUG pr_fmt("%s: alsa: " fmt), \
37 chip->dev->name, ##arg); \
38 } while(0)
40 /****************************************************************************
41 Module global static vars
42 ****************************************************************************/
44 static unsigned int disable_analog_audio;
45 module_param(disable_analog_audio, int, 0644);
46 MODULE_PARM_DESC(disable_analog_audio, "disable analog audio ALSA driver");
48 static unsigned int audio_debug;
49 module_param(audio_debug, int, 0644);
50 MODULE_PARM_DESC(audio_debug, "enable debug messages [analog audio]");
52 /****************************************************************************
53 Board specific functions
54 ****************************************************************************/
56 /* Constants taken from cx88-reg.h */
57 #define AUD_INT_DN_RISCI1 (1 << 0)
58 #define AUD_INT_UP_RISCI1 (1 << 1)
59 #define AUD_INT_RDS_DN_RISCI1 (1 << 2)
60 #define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */
61 #define AUD_INT_UP_RISCI2 (1 << 5)
62 #define AUD_INT_RDS_DN_RISCI2 (1 << 6)
63 #define AUD_INT_DN_SYNC (1 << 12)
64 #define AUD_INT_UP_SYNC (1 << 13)
65 #define AUD_INT_RDS_DN_SYNC (1 << 14)
66 #define AUD_INT_OPC_ERR (1 << 16)
67 #define AUD_INT_BER_IRQ (1 << 20)
68 #define AUD_INT_MCHG_IRQ (1 << 21)
69 #define GP_COUNT_CONTROL_RESET 0x3
71 static int cx23885_alsa_dma_init(struct cx23885_audio_dev *chip, int nr_pages)
73 struct cx23885_audio_buffer *buf = chip->buf;
74 struct page *pg;
75 int i;
77 buf->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
78 if (NULL == buf->vaddr) {
79 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
80 return -ENOMEM;
83 dprintk(1, "vmalloc is at addr %p, size=%d\n",
84 buf->vaddr, nr_pages << PAGE_SHIFT);
86 memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT);
87 buf->nr_pages = nr_pages;
89 buf->sglist = vzalloc(array_size(sizeof(*buf->sglist), buf->nr_pages));
90 if (NULL == buf->sglist)
91 goto vzalloc_err;
93 sg_init_table(buf->sglist, buf->nr_pages);
94 for (i = 0; i < buf->nr_pages; i++) {
95 pg = vmalloc_to_page(buf->vaddr + i * PAGE_SIZE);
96 if (NULL == pg)
97 goto vmalloc_to_page_err;
98 sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0);
100 return 0;
102 vmalloc_to_page_err:
103 vfree(buf->sglist);
104 buf->sglist = NULL;
105 vzalloc_err:
106 vfree(buf->vaddr);
107 buf->vaddr = NULL;
108 return -ENOMEM;
111 static int cx23885_alsa_dma_map(struct cx23885_audio_dev *dev)
113 struct cx23885_audio_buffer *buf = dev->buf;
115 buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist,
116 buf->nr_pages, PCI_DMA_FROMDEVICE);
118 if (0 == buf->sglen) {
119 pr_warn("%s: cx23885_alsa_map_sg failed\n", __func__);
120 return -ENOMEM;
122 return 0;
125 static int cx23885_alsa_dma_unmap(struct cx23885_audio_dev *dev)
127 struct cx23885_audio_buffer *buf = dev->buf;
129 if (!buf->sglen)
130 return 0;
132 dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->sglen, PCI_DMA_FROMDEVICE);
133 buf->sglen = 0;
134 return 0;
137 static int cx23885_alsa_dma_free(struct cx23885_audio_buffer *buf)
139 vfree(buf->sglist);
140 buf->sglist = NULL;
141 vfree(buf->vaddr);
142 buf->vaddr = NULL;
143 return 0;
147 * BOARD Specific: Sets audio DMA
150 static int cx23885_start_audio_dma(struct cx23885_audio_dev *chip)
152 struct cx23885_audio_buffer *buf = chip->buf;
153 struct cx23885_dev *dev = chip->dev;
154 struct sram_channel *audio_ch =
155 &dev->sram_channels[AUDIO_SRAM_CHANNEL];
157 dprintk(1, "%s()\n", __func__);
159 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
160 cx_clear(AUD_INT_DMA_CTL, 0x11);
162 /* setup fifo + format - out channel */
163 cx23885_sram_channel_setup(chip->dev, audio_ch, buf->bpl,
164 buf->risc.dma);
166 /* sets bpl size */
167 cx_write(AUD_INT_A_LNGTH, buf->bpl);
169 /* This is required to get good audio (1 seems to be ok) */
170 cx_write(AUD_INT_A_MODE, 1);
172 /* reset counter */
173 cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
174 atomic_set(&chip->count, 0);
176 dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d byte buffer\n",
177 buf->bpl, cx_read(audio_ch->cmds_start+12)>>1,
178 chip->num_periods, buf->bpl * chip->num_periods);
180 /* Enables corresponding bits at AUD_INT_STAT */
181 cx_write(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
182 AUD_INT_DN_RISCI1);
184 /* Clean any pending interrupt bits already set */
185 cx_write(AUDIO_INT_INT_STAT, ~0);
187 /* enable audio irqs */
188 cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
190 /* start dma */
191 cx_set(DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */
192 cx_set(AUD_INT_DMA_CTL, 0x11); /* audio downstream FIFO and
193 RISC enable */
194 if (audio_debug)
195 cx23885_sram_channel_dump(chip->dev, audio_ch);
197 return 0;
201 * BOARD Specific: Resets audio DMA
203 static int cx23885_stop_audio_dma(struct cx23885_audio_dev *chip)
205 struct cx23885_dev *dev = chip->dev;
206 dprintk(1, "Stopping audio DMA\n");
208 /* stop dma */
209 cx_clear(AUD_INT_DMA_CTL, 0x11);
211 /* disable irqs */
212 cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
213 cx_clear(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
214 AUD_INT_DN_RISCI1);
216 if (audio_debug)
217 cx23885_sram_channel_dump(chip->dev,
218 &dev->sram_channels[AUDIO_SRAM_CHANNEL]);
220 return 0;
224 * BOARD Specific: Handles audio IRQ
226 int cx23885_audio_irq(struct cx23885_dev *dev, u32 status, u32 mask)
228 struct cx23885_audio_dev *chip = dev->audio_dev;
230 if (0 == (status & mask))
231 return 0;
233 cx_write(AUDIO_INT_INT_STAT, status);
235 /* risc op code error */
236 if (status & AUD_INT_OPC_ERR) {
237 pr_warn("%s/1: Audio risc op code error\n",
238 dev->name);
239 cx_clear(AUD_INT_DMA_CTL, 0x11);
240 cx23885_sram_channel_dump(dev,
241 &dev->sram_channels[AUDIO_SRAM_CHANNEL]);
243 if (status & AUD_INT_DN_SYNC) {
244 dprintk(1, "Downstream sync error\n");
245 cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
246 return 1;
248 /* risc1 downstream */
249 if (status & AUD_INT_DN_RISCI1) {
250 atomic_set(&chip->count, cx_read(AUD_INT_A_GPCNT));
251 snd_pcm_period_elapsed(chip->substream);
253 /* FIXME: Any other status should deserve a special handling? */
255 return 1;
258 static int dsp_buffer_free(struct cx23885_audio_dev *chip)
260 struct cx23885_riscmem *risc;
262 BUG_ON(!chip->dma_size);
264 dprintk(2, "Freeing buffer\n");
265 cx23885_alsa_dma_unmap(chip);
266 cx23885_alsa_dma_free(chip->buf);
267 risc = &chip->buf->risc;
268 pci_free_consistent(chip->pci, risc->size, risc->cpu, risc->dma);
269 kfree(chip->buf);
271 chip->buf = NULL;
272 chip->dma_size = 0;
274 return 0;
277 /****************************************************************************
278 ALSA PCM Interface
279 ****************************************************************************/
282 * Digital hardware definition
284 #define DEFAULT_FIFO_SIZE 4096
286 static const struct snd_pcm_hardware snd_cx23885_digital_hw = {
287 .info = SNDRV_PCM_INFO_MMAP |
288 SNDRV_PCM_INFO_INTERLEAVED |
289 SNDRV_PCM_INFO_BLOCK_TRANSFER |
290 SNDRV_PCM_INFO_MMAP_VALID,
291 .formats = SNDRV_PCM_FMTBIT_S16_LE,
293 .rates = SNDRV_PCM_RATE_48000,
294 .rate_min = 48000,
295 .rate_max = 48000,
296 .channels_min = 2,
297 .channels_max = 2,
298 /* Analog audio output will be full of clicks and pops if there
299 are not exactly four lines in the SRAM FIFO buffer. */
300 .period_bytes_min = DEFAULT_FIFO_SIZE/4,
301 .period_bytes_max = DEFAULT_FIFO_SIZE/4,
302 .periods_min = 1,
303 .periods_max = 1024,
304 .buffer_bytes_max = (1024*1024),
308 * audio pcm capture open callback
310 static int snd_cx23885_pcm_open(struct snd_pcm_substream *substream)
312 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
313 struct snd_pcm_runtime *runtime = substream->runtime;
314 int err;
316 if (!chip) {
317 pr_err("BUG: cx23885 can't find device struct. Can't proceed with open\n");
318 return -ENODEV;
321 err = snd_pcm_hw_constraint_pow2(runtime, 0,
322 SNDRV_PCM_HW_PARAM_PERIODS);
323 if (err < 0)
324 goto _error;
326 chip->substream = substream;
328 runtime->hw = snd_cx23885_digital_hw;
330 if (chip->dev->sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
331 DEFAULT_FIFO_SIZE) {
332 unsigned int bpl = chip->dev->
333 sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 4;
334 bpl &= ~7; /* must be multiple of 8 */
335 runtime->hw.period_bytes_min = bpl;
336 runtime->hw.period_bytes_max = bpl;
339 return 0;
340 _error:
341 dprintk(1, "Error opening PCM!\n");
342 return err;
346 * audio close callback
348 static int snd_cx23885_close(struct snd_pcm_substream *substream)
350 return 0;
355 * hw_params callback
357 static int snd_cx23885_hw_params(struct snd_pcm_substream *substream,
358 struct snd_pcm_hw_params *hw_params)
360 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
361 struct cx23885_audio_buffer *buf;
362 int ret;
364 if (substream->runtime->dma_area) {
365 dsp_buffer_free(chip);
366 substream->runtime->dma_area = NULL;
369 chip->period_size = params_period_bytes(hw_params);
370 chip->num_periods = params_periods(hw_params);
371 chip->dma_size = chip->period_size * params_periods(hw_params);
373 BUG_ON(!chip->dma_size);
374 BUG_ON(chip->num_periods & (chip->num_periods-1));
376 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
377 if (NULL == buf)
378 return -ENOMEM;
380 buf->bpl = chip->period_size;
381 chip->buf = buf;
383 ret = cx23885_alsa_dma_init(chip,
384 (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
385 if (ret < 0)
386 goto error;
388 ret = cx23885_alsa_dma_map(chip);
389 if (ret < 0)
390 goto error;
392 ret = cx23885_risc_databuffer(chip->pci, &buf->risc, buf->sglist,
393 chip->period_size, chip->num_periods, 1);
394 if (ret < 0)
395 goto error;
397 /* Loop back to start of program */
398 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
399 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
400 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
402 substream->runtime->dma_area = chip->buf->vaddr;
403 substream->runtime->dma_bytes = chip->dma_size;
404 substream->runtime->dma_addr = 0;
406 return 0;
408 error:
409 kfree(buf);
410 chip->buf = NULL;
411 return ret;
415 * hw free callback
417 static int snd_cx23885_hw_free(struct snd_pcm_substream *substream)
420 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
422 if (substream->runtime->dma_area) {
423 dsp_buffer_free(chip);
424 substream->runtime->dma_area = NULL;
427 return 0;
431 * prepare callback
433 static int snd_cx23885_prepare(struct snd_pcm_substream *substream)
435 return 0;
439 * trigger callback
441 static int snd_cx23885_card_trigger(struct snd_pcm_substream *substream,
442 int cmd)
444 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
445 int err;
447 /* Local interrupts are already disabled by ALSA */
448 spin_lock(&chip->lock);
450 switch (cmd) {
451 case SNDRV_PCM_TRIGGER_START:
452 err = cx23885_start_audio_dma(chip);
453 break;
454 case SNDRV_PCM_TRIGGER_STOP:
455 err = cx23885_stop_audio_dma(chip);
456 break;
457 default:
458 err = -EINVAL;
459 break;
462 spin_unlock(&chip->lock);
464 return err;
468 * pointer callback
470 static snd_pcm_uframes_t snd_cx23885_pointer(
471 struct snd_pcm_substream *substream)
473 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
474 struct snd_pcm_runtime *runtime = substream->runtime;
475 u16 count;
477 count = atomic_read(&chip->count);
479 return runtime->period_size * (count & (runtime->periods-1));
483 * page callback (needed for mmap)
485 static struct page *snd_cx23885_page(struct snd_pcm_substream *substream,
486 unsigned long offset)
488 void *pageptr = substream->runtime->dma_area + offset;
489 return vmalloc_to_page(pageptr);
493 * operators
495 static const struct snd_pcm_ops snd_cx23885_pcm_ops = {
496 .open = snd_cx23885_pcm_open,
497 .close = snd_cx23885_close,
498 .hw_params = snd_cx23885_hw_params,
499 .hw_free = snd_cx23885_hw_free,
500 .prepare = snd_cx23885_prepare,
501 .trigger = snd_cx23885_card_trigger,
502 .pointer = snd_cx23885_pointer,
503 .page = snd_cx23885_page,
507 * create a PCM device
509 static int snd_cx23885_pcm(struct cx23885_audio_dev *chip, int device,
510 char *name)
512 int err;
513 struct snd_pcm *pcm;
515 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
516 if (err < 0)
517 return err;
518 pcm->private_data = chip;
519 strscpy(pcm->name, name, sizeof(pcm->name));
520 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx23885_pcm_ops);
522 return 0;
525 /****************************************************************************
526 Basic Flow for Sound Devices
527 ****************************************************************************/
530 * Alsa Constructor - Component probe
533 struct cx23885_audio_dev *cx23885_audio_register(struct cx23885_dev *dev)
535 struct snd_card *card;
536 struct cx23885_audio_dev *chip;
537 int err;
539 if (disable_analog_audio)
540 return NULL;
542 if (dev->sram_channels[AUDIO_SRAM_CHANNEL].cmds_start == 0) {
543 pr_warn("%s(): Missing SRAM channel configuration for analog TV Audio\n",
544 __func__);
545 return NULL;
548 err = snd_card_new(&dev->pci->dev,
549 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
550 THIS_MODULE, sizeof(struct cx23885_audio_dev), &card);
551 if (err < 0)
552 goto error;
554 chip = (struct cx23885_audio_dev *) card->private_data;
555 chip->dev = dev;
556 chip->pci = dev->pci;
557 chip->card = card;
558 spin_lock_init(&chip->lock);
560 err = snd_cx23885_pcm(chip, 0, "CX23885 Digital");
561 if (err < 0)
562 goto error;
564 strscpy(card->driver, "CX23885", sizeof(card->driver));
565 sprintf(card->shortname, "Conexant CX23885");
566 sprintf(card->longname, "%s at %s", card->shortname, dev->name);
568 err = snd_card_register(card);
569 if (err < 0)
570 goto error;
572 dprintk(0, "registered ALSA audio device\n");
574 return chip;
576 error:
577 snd_card_free(card);
578 pr_err("%s(): Failed to register analog audio adapter\n",
579 __func__);
581 return NULL;
585 * ALSA destructor
587 void cx23885_audio_unregister(struct cx23885_dev *dev)
589 struct cx23885_audio_dev *chip = dev->audio_dev;
591 snd_card_free(chip->card);