1 /*
2  *
3  *  Support for CX23885 analog audio capture
4  *
5  *    (c) 2008 Mijhail Moreyra <mijhail.moreyra@gmail.com>
6  *    Adapted from cx88-alsa.c
7  *    (c) 2009 Steven Toth <stoth@kernellabs.com>
8  *
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.
13  *
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.
18  */
19 
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/device.h>
23 #include <linux/interrupt.h>
24 #include <linux/vmalloc.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/pci.h>
27 
28 #include <asm/delay.h>
29 
30 #include <sound/core.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/control.h>
34 #include <sound/initval.h>
35 
36 #include <sound/tlv.h>
37 
38 
39 #include "cx23885.h"
40 #include "cx23885-reg.h"
41 
42 #define AUDIO_SRAM_CHANNEL	SRAM_CH07
43 
44 #define dprintk(level, fmt, arg...) do {				\
45 	if (audio_debug + 1 > level)					\
46 		printk(KERN_INFO "%s: " fmt, chip->dev->name , ## arg);	\
47 } while(0)
48 
49 #define dprintk_core(level, fmt, arg...)	if (audio_debug >= level) \
50 	printk(KERN_DEBUG "%s: " fmt, chip->dev->name , ## arg)
51 
52 /****************************************************************************
53 			Module global static vars
54  ****************************************************************************/
55 
56 static unsigned int disable_analog_audio;
57 module_param(disable_analog_audio, int, 0644);
58 MODULE_PARM_DESC(disable_analog_audio, "disable analog audio ALSA driver");
59 
60 static unsigned int audio_debug;
61 module_param(audio_debug, int, 0644);
62 MODULE_PARM_DESC(audio_debug, "enable debug messages [analog audio]");
63 
64 /****************************************************************************
65 			Board specific funtions
66  ****************************************************************************/
67 
68 /* Constants taken from cx88-reg.h */
69 #define AUD_INT_DN_RISCI1       (1 <<  0)
70 #define AUD_INT_UP_RISCI1       (1 <<  1)
71 #define AUD_INT_RDS_DN_RISCI1   (1 <<  2)
72 #define AUD_INT_DN_RISCI2       (1 <<  4) /* yes, 3 is skipped */
73 #define AUD_INT_UP_RISCI2       (1 <<  5)
74 #define AUD_INT_RDS_DN_RISCI2   (1 <<  6)
75 #define AUD_INT_DN_SYNC         (1 << 12)
76 #define AUD_INT_UP_SYNC         (1 << 13)
77 #define AUD_INT_RDS_DN_SYNC     (1 << 14)
78 #define AUD_INT_OPC_ERR         (1 << 16)
79 #define AUD_INT_BER_IRQ         (1 << 20)
80 #define AUD_INT_MCHG_IRQ        (1 << 21)
81 #define GP_COUNT_CONTROL_RESET	0x3
82 
83 static int cx23885_alsa_dma_init(struct cx23885_audio_dev *chip, int nr_pages)
84 {
85 	struct cx23885_audio_buffer *buf = chip->buf;
86 	struct page *pg;
87 	int i;
88 
89 	buf->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
90 	if (NULL == buf->vaddr) {
91 		dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
92 		return -ENOMEM;
93 	}
94 
95 	dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
96 				(unsigned long)buf->vaddr,
97 				nr_pages << PAGE_SHIFT);
98 
99 	memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT);
100 	buf->nr_pages = nr_pages;
101 
102 	buf->sglist = vzalloc(buf->nr_pages * sizeof(*buf->sglist));
103 	if (NULL == buf->sglist)
104 		goto vzalloc_err;
105 
106 	sg_init_table(buf->sglist, buf->nr_pages);
107 	for (i = 0; i < buf->nr_pages; i++) {
108 		pg = vmalloc_to_page(buf->vaddr + i * PAGE_SIZE);
109 		if (NULL == pg)
110 			goto vmalloc_to_page_err;
111 		sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0);
112 	}
113 	return 0;
114 
115 vmalloc_to_page_err:
116 	vfree(buf->sglist);
117 	buf->sglist = NULL;
118 vzalloc_err:
119 	vfree(buf->vaddr);
120 	buf->vaddr = NULL;
121 	return -ENOMEM;
122 }
123 
124 static int cx23885_alsa_dma_map(struct cx23885_audio_dev *dev)
125 {
126 	struct cx23885_audio_buffer *buf = dev->buf;
127 
128 	buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist,
129 			buf->nr_pages, PCI_DMA_FROMDEVICE);
130 
131 	if (0 == buf->sglen) {
132 		pr_warn("%s: cx23885_alsa_map_sg failed\n", __func__);
133 		return -ENOMEM;
134 	}
135 	return 0;
136 }
137 
138 static int cx23885_alsa_dma_unmap(struct cx23885_audio_dev *dev)
139 {
140 	struct cx23885_audio_buffer *buf = dev->buf;
141 
142 	if (!buf->sglen)
143 		return 0;
144 
145 	dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->sglen, PCI_DMA_FROMDEVICE);
146 	buf->sglen = 0;
147 	return 0;
148 }
149 
150 static int cx23885_alsa_dma_free(struct cx23885_audio_buffer *buf)
151 {
152 	vfree(buf->sglist);
153 	buf->sglist = NULL;
154 	vfree(buf->vaddr);
155 	buf->vaddr = NULL;
156 	return 0;
157 }
158 
159 /*
160  * BOARD Specific: Sets audio DMA
161  */
162 
163 static int cx23885_start_audio_dma(struct cx23885_audio_dev *chip)
164 {
165 	struct cx23885_audio_buffer *buf = chip->buf;
166 	struct cx23885_dev *dev  = chip->dev;
167 	struct sram_channel *audio_ch =
168 		&dev->sram_channels[AUDIO_SRAM_CHANNEL];
169 
170 	dprintk(1, "%s()\n", __func__);
171 
172 	/* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
173 	cx_clear(AUD_INT_DMA_CTL, 0x11);
174 
175 	/* setup fifo + format - out channel */
176 	cx23885_sram_channel_setup(chip->dev, audio_ch, buf->bpl,
177 		buf->risc.dma);
178 
179 	/* sets bpl size */
180 	cx_write(AUD_INT_A_LNGTH, buf->bpl);
181 
182 	/* This is required to get good audio (1 seems to be ok) */
183 	cx_write(AUD_INT_A_MODE, 1);
184 
185 	/* reset counter */
186 	cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
187 	atomic_set(&chip->count, 0);
188 
189 	dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d byte buffer\n",
190 		buf->bpl, cx_read(audio_ch->cmds_start+12)>>1,
191 		chip->num_periods, buf->bpl * chip->num_periods);
192 
193 	/* Enables corresponding bits at AUD_INT_STAT */
194 	cx_write(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
195 				    AUD_INT_DN_RISCI1);
196 
197 	/* Clean any pending interrupt bits already set */
198 	cx_write(AUDIO_INT_INT_STAT, ~0);
199 
200 	/* enable audio irqs */
201 	cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
202 
203 	/* start dma */
204 	cx_set(DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */
205 	cx_set(AUD_INT_DMA_CTL, 0x11); /* audio downstream FIFO and
206 					  RISC enable */
207 	if (audio_debug)
208 		cx23885_sram_channel_dump(chip->dev, audio_ch);
209 
210 	return 0;
211 }
212 
213 /*
214  * BOARD Specific: Resets audio DMA
215  */
216 static int cx23885_stop_audio_dma(struct cx23885_audio_dev *chip)
217 {
218 	struct cx23885_dev *dev = chip->dev;
219 	dprintk(1, "Stopping audio DMA\n");
220 
221 	/* stop dma */
222 	cx_clear(AUD_INT_DMA_CTL, 0x11);
223 
224 	/* disable irqs */
225 	cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
226 	cx_clear(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
227 				    AUD_INT_DN_RISCI1);
228 
229 	if (audio_debug)
230 		cx23885_sram_channel_dump(chip->dev,
231 			&dev->sram_channels[AUDIO_SRAM_CHANNEL]);
232 
233 	return 0;
234 }
235 
236 /*
237  * BOARD Specific: Handles audio IRQ
238  */
239 int cx23885_audio_irq(struct cx23885_dev *dev, u32 status, u32 mask)
240 {
241 	struct cx23885_audio_dev *chip = dev->audio_dev;
242 
243 	if (0 == (status & mask))
244 		return 0;
245 
246 	cx_write(AUDIO_INT_INT_STAT, status);
247 
248 	/* risc op code error */
249 	if (status & AUD_INT_OPC_ERR) {
250 		printk(KERN_WARNING "%s/1: Audio risc op code error\n",
251 			dev->name);
252 		cx_clear(AUD_INT_DMA_CTL, 0x11);
253 		cx23885_sram_channel_dump(dev,
254 			&dev->sram_channels[AUDIO_SRAM_CHANNEL]);
255 	}
256 	if (status & AUD_INT_DN_SYNC) {
257 		dprintk(1, "Downstream sync error\n");
258 		cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
259 		return 1;
260 	}
261 	/* risc1 downstream */
262 	if (status & AUD_INT_DN_RISCI1) {
263 		atomic_set(&chip->count, cx_read(AUD_INT_A_GPCNT));
264 		snd_pcm_period_elapsed(chip->substream);
265 	}
266 	/* FIXME: Any other status should deserve a special handling? */
267 
268 	return 1;
269 }
270 
271 static int dsp_buffer_free(struct cx23885_audio_dev *chip)
272 {
273 	struct cx23885_riscmem *risc;
274 
275 	BUG_ON(!chip->dma_size);
276 
277 	dprintk(2, "Freeing buffer\n");
278 	cx23885_alsa_dma_unmap(chip);
279 	cx23885_alsa_dma_free(chip->buf);
280 	risc = &chip->buf->risc;
281 	pci_free_consistent(chip->pci, risc->size, risc->cpu, risc->dma);
282 	kfree(chip->buf);
283 
284 	chip->buf = NULL;
285 	chip->dma_size = 0;
286 
287 	return 0;
288 }
289 
290 /****************************************************************************
291 				ALSA PCM Interface
292  ****************************************************************************/
293 
294 /*
295  * Digital hardware definition
296  */
297 #define DEFAULT_FIFO_SIZE	4096
298 
299 static struct snd_pcm_hardware snd_cx23885_digital_hw = {
300 	.info = SNDRV_PCM_INFO_MMAP |
301 		SNDRV_PCM_INFO_INTERLEAVED |
302 		SNDRV_PCM_INFO_BLOCK_TRANSFER |
303 		SNDRV_PCM_INFO_MMAP_VALID,
304 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
305 
306 	.rates =		SNDRV_PCM_RATE_48000,
307 	.rate_min =		48000,
308 	.rate_max =		48000,
309 	.channels_min = 2,
310 	.channels_max = 2,
311 	/* Analog audio output will be full of clicks and pops if there
312 	   are not exactly four lines in the SRAM FIFO buffer.  */
313 	.period_bytes_min = DEFAULT_FIFO_SIZE/4,
314 	.period_bytes_max = DEFAULT_FIFO_SIZE/4,
315 	.periods_min = 1,
316 	.periods_max = 1024,
317 	.buffer_bytes_max = (1024*1024),
318 };
319 
320 /*
321  * audio pcm capture open callback
322  */
323 static int snd_cx23885_pcm_open(struct snd_pcm_substream *substream)
324 {
325 	struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
326 	struct snd_pcm_runtime *runtime = substream->runtime;
327 	int err;
328 
329 	if (!chip) {
330 		printk(KERN_ERR "BUG: cx23885 can't find device struct. Can't proceed with open\n");
331 		return -ENODEV;
332 	}
333 
334 	err = snd_pcm_hw_constraint_pow2(runtime, 0,
335 		SNDRV_PCM_HW_PARAM_PERIODS);
336 	if (err < 0)
337 		goto _error;
338 
339 	chip->substream = substream;
340 
341 	runtime->hw = snd_cx23885_digital_hw;
342 
343 	if (chip->dev->sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
344 		DEFAULT_FIFO_SIZE) {
345 		unsigned int bpl = chip->dev->
346 			sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 4;
347 		bpl &= ~7; /* must be multiple of 8 */
348 		runtime->hw.period_bytes_min = bpl;
349 		runtime->hw.period_bytes_max = bpl;
350 	}
351 
352 	return 0;
353 _error:
354 	dprintk(1, "Error opening PCM!\n");
355 	return err;
356 }
357 
358 /*
359  * audio close callback
360  */
361 static int snd_cx23885_close(struct snd_pcm_substream *substream)
362 {
363 	return 0;
364 }
365 
366 
367 /*
368  * hw_params callback
369  */
370 static int snd_cx23885_hw_params(struct snd_pcm_substream *substream,
371 			      struct snd_pcm_hw_params *hw_params)
372 {
373 	struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
374 	struct cx23885_audio_buffer *buf;
375 	int ret;
376 
377 	if (substream->runtime->dma_area) {
378 		dsp_buffer_free(chip);
379 		substream->runtime->dma_area = NULL;
380 	}
381 
382 	chip->period_size = params_period_bytes(hw_params);
383 	chip->num_periods = params_periods(hw_params);
384 	chip->dma_size = chip->period_size * params_periods(hw_params);
385 
386 	BUG_ON(!chip->dma_size);
387 	BUG_ON(chip->num_periods & (chip->num_periods-1));
388 
389 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
390 	if (NULL == buf)
391 		return -ENOMEM;
392 
393 	buf->bpl = chip->period_size;
394 	chip->buf = buf;
395 
396 	ret = cx23885_alsa_dma_init(chip,
397 			(PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
398 	if (ret < 0)
399 		goto error;
400 
401 	ret = cx23885_alsa_dma_map(chip);
402 	if (ret < 0)
403 		goto error;
404 
405 	ret = cx23885_risc_databuffer(chip->pci, &buf->risc, buf->sglist,
406 				   chip->period_size, chip->num_periods, 1);
407 	if (ret < 0)
408 		goto error;
409 
410 	/* Loop back to start of program */
411 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
412 	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
413 	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
414 
415 	substream->runtime->dma_area = chip->buf->vaddr;
416 	substream->runtime->dma_bytes = chip->dma_size;
417 	substream->runtime->dma_addr = 0;
418 
419 	return 0;
420 
421 error:
422 	kfree(buf);
423 	chip->buf = NULL;
424 	return ret;
425 }
426 
427 /*
428  * hw free callback
429  */
430 static int snd_cx23885_hw_free(struct snd_pcm_substream *substream)
431 {
432 
433 	struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
434 
435 	if (substream->runtime->dma_area) {
436 		dsp_buffer_free(chip);
437 		substream->runtime->dma_area = NULL;
438 	}
439 
440 	return 0;
441 }
442 
443 /*
444  * prepare callback
445  */
446 static int snd_cx23885_prepare(struct snd_pcm_substream *substream)
447 {
448 	return 0;
449 }
450 
451 /*
452  * trigger callback
453  */
454 static int snd_cx23885_card_trigger(struct snd_pcm_substream *substream,
455 	int cmd)
456 {
457 	struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
458 	int err;
459 
460 	/* Local interrupts are already disabled by ALSA */
461 	spin_lock(&chip->lock);
462 
463 	switch (cmd) {
464 	case SNDRV_PCM_TRIGGER_START:
465 		err = cx23885_start_audio_dma(chip);
466 		break;
467 	case SNDRV_PCM_TRIGGER_STOP:
468 		err = cx23885_stop_audio_dma(chip);
469 		break;
470 	default:
471 		err = -EINVAL;
472 		break;
473 	}
474 
475 	spin_unlock(&chip->lock);
476 
477 	return err;
478 }
479 
480 /*
481  * pointer callback
482  */
483 static snd_pcm_uframes_t snd_cx23885_pointer(
484 	struct snd_pcm_substream *substream)
485 {
486 	struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
487 	struct snd_pcm_runtime *runtime = substream->runtime;
488 	u16 count;
489 
490 	count = atomic_read(&chip->count);
491 
492 	return runtime->period_size * (count & (runtime->periods-1));
493 }
494 
495 /*
496  * page callback (needed for mmap)
497  */
498 static struct page *snd_cx23885_page(struct snd_pcm_substream *substream,
499 				unsigned long offset)
500 {
501 	void *pageptr = substream->runtime->dma_area + offset;
502 	return vmalloc_to_page(pageptr);
503 }
504 
505 /*
506  * operators
507  */
508 static const struct snd_pcm_ops snd_cx23885_pcm_ops = {
509 	.open = snd_cx23885_pcm_open,
510 	.close = snd_cx23885_close,
511 	.ioctl = snd_pcm_lib_ioctl,
512 	.hw_params = snd_cx23885_hw_params,
513 	.hw_free = snd_cx23885_hw_free,
514 	.prepare = snd_cx23885_prepare,
515 	.trigger = snd_cx23885_card_trigger,
516 	.pointer = snd_cx23885_pointer,
517 	.page = snd_cx23885_page,
518 };
519 
520 /*
521  * create a PCM device
522  */
523 static int snd_cx23885_pcm(struct cx23885_audio_dev *chip, int device,
524 	char *name)
525 {
526 	int err;
527 	struct snd_pcm *pcm;
528 
529 	err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
530 	if (err < 0)
531 		return err;
532 	pcm->private_data = chip;
533 	strcpy(pcm->name, name);
534 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx23885_pcm_ops);
535 
536 	return 0;
537 }
538 
539 /****************************************************************************
540 			Basic Flow for Sound Devices
541  ****************************************************************************/
542 
543 /*
544  * Alsa Constructor - Component probe
545  */
546 
547 struct cx23885_audio_dev *cx23885_audio_register(struct cx23885_dev *dev)
548 {
549 	struct snd_card *card;
550 	struct cx23885_audio_dev *chip;
551 	int err;
552 
553 	if (disable_analog_audio)
554 		return NULL;
555 
556 	if (dev->sram_channels[AUDIO_SRAM_CHANNEL].cmds_start == 0) {
557 		printk(KERN_WARNING "%s(): Missing SRAM channel configuration for analog TV Audio\n",
558 		       __func__);
559 		return NULL;
560 	}
561 
562 	err = snd_card_new(&dev->pci->dev,
563 			   SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
564 			THIS_MODULE, sizeof(struct cx23885_audio_dev), &card);
565 	if (err < 0)
566 		goto error;
567 
568 	chip = (struct cx23885_audio_dev *) card->private_data;
569 	chip->dev = dev;
570 	chip->pci = dev->pci;
571 	chip->card = card;
572 	spin_lock_init(&chip->lock);
573 
574 	err = snd_cx23885_pcm(chip, 0, "CX23885 Digital");
575 	if (err < 0)
576 		goto error;
577 
578 	strcpy(card->driver, "CX23885");
579 	sprintf(card->shortname, "Conexant CX23885");
580 	sprintf(card->longname, "%s at %s", card->shortname, dev->name);
581 
582 	err = snd_card_register(card);
583 	if (err < 0)
584 		goto error;
585 
586 	dprintk(0, "registered ALSA audio device\n");
587 
588 	return chip;
589 
590 error:
591 	snd_card_free(card);
592 	printk(KERN_ERR "%s(): Failed to register analog audio adapter\n",
593 	       __func__);
594 
595 	return NULL;
596 }
597 
598 /*
599  * ALSA destructor
600  */
601 void cx23885_audio_unregister(struct cx23885_dev *dev)
602 {
603 	struct cx23885_audio_dev *chip = dev->audio_dev;
604 
605 	snd_card_free(chip->card);
606 }
607