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 "cx23885.h"
21 #include "cx23885-reg.h"
22 
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>
30 
31 #include <asm/delay.h>
32 
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>
38 
39 #include <sound/tlv.h>
40 
41 #define AUDIO_SRAM_CHANNEL	SRAM_CH07
42 
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)
48 
49 /****************************************************************************
50 			Module global static vars
51  ****************************************************************************/
52 
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");
56 
57 static unsigned int audio_debug;
58 module_param(audio_debug, int, 0644);
59 MODULE_PARM_DESC(audio_debug, "enable debug messages [analog audio]");
60 
61 /****************************************************************************
62 			Board specific funtions
63  ****************************************************************************/
64 
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
79 
80 static int cx23885_alsa_dma_init(struct cx23885_audio_dev *chip, int nr_pages)
81 {
82 	struct cx23885_audio_buffer *buf = chip->buf;
83 	struct page *pg;
84 	int i;
85 
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;
90 	}
91 
92 	dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
93 				(unsigned long)buf->vaddr,
94 				nr_pages << PAGE_SHIFT);
95 
96 	memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT);
97 	buf->nr_pages = nr_pages;
98 
99 	buf->sglist = vzalloc(buf->nr_pages * sizeof(*buf->sglist));
100 	if (NULL == buf->sglist)
101 		goto vzalloc_err;
102 
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);
109 	}
110 	return 0;
111 
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;
119 }
120 
121 static int cx23885_alsa_dma_map(struct cx23885_audio_dev *dev)
122 {
123 	struct cx23885_audio_buffer *buf = dev->buf;
124 
125 	buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist,
126 			buf->nr_pages, PCI_DMA_FROMDEVICE);
127 
128 	if (0 == buf->sglen) {
129 		pr_warn("%s: cx23885_alsa_map_sg failed\n", __func__);
130 		return -ENOMEM;
131 	}
132 	return 0;
133 }
134 
135 static int cx23885_alsa_dma_unmap(struct cx23885_audio_dev *dev)
136 {
137 	struct cx23885_audio_buffer *buf = dev->buf;
138 
139 	if (!buf->sglen)
140 		return 0;
141 
142 	dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->sglen, PCI_DMA_FROMDEVICE);
143 	buf->sglen = 0;
144 	return 0;
145 }
146 
147 static int cx23885_alsa_dma_free(struct cx23885_audio_buffer *buf)
148 {
149 	vfree(buf->sglist);
150 	buf->sglist = NULL;
151 	vfree(buf->vaddr);
152 	buf->vaddr = NULL;
153 	return 0;
154 }
155 
156 /*
157  * BOARD Specific: Sets audio DMA
158  */
159 
160 static int cx23885_start_audio_dma(struct cx23885_audio_dev *chip)
161 {
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];
166 
167 	dprintk(1, "%s()\n", __func__);
168 
169 	/* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
170 	cx_clear(AUD_INT_DMA_CTL, 0x11);
171 
172 	/* setup fifo + format - out channel */
173 	cx23885_sram_channel_setup(chip->dev, audio_ch, buf->bpl,
174 		buf->risc.dma);
175 
176 	/* sets bpl size */
177 	cx_write(AUD_INT_A_LNGTH, buf->bpl);
178 
179 	/* This is required to get good audio (1 seems to be ok) */
180 	cx_write(AUD_INT_A_MODE, 1);
181 
182 	/* reset counter */
183 	cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
184 	atomic_set(&chip->count, 0);
185 
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);
189 
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);
193 
194 	/* Clean any pending interrupt bits already set */
195 	cx_write(AUDIO_INT_INT_STAT, ~0);
196 
197 	/* enable audio irqs */
198 	cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
199 
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);
206 
207 	return 0;
208 }
209 
210 /*
211  * BOARD Specific: Resets audio DMA
212  */
213 static int cx23885_stop_audio_dma(struct cx23885_audio_dev *chip)
214 {
215 	struct cx23885_dev *dev = chip->dev;
216 	dprintk(1, "Stopping audio DMA\n");
217 
218 	/* stop dma */
219 	cx_clear(AUD_INT_DMA_CTL, 0x11);
220 
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);
225 
226 	if (audio_debug)
227 		cx23885_sram_channel_dump(chip->dev,
228 			&dev->sram_channels[AUDIO_SRAM_CHANNEL]);
229 
230 	return 0;
231 }
232 
233 /*
234  * BOARD Specific: Handles audio IRQ
235  */
236 int cx23885_audio_irq(struct cx23885_dev *dev, u32 status, u32 mask)
237 {
238 	struct cx23885_audio_dev *chip = dev->audio_dev;
239 
240 	if (0 == (status & mask))
241 		return 0;
242 
243 	cx_write(AUDIO_INT_INT_STAT, status);
244 
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]);
252 	}
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;
257 	}
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);
262 	}
263 	/* FIXME: Any other status should deserve a special handling? */
264 
265 	return 1;
266 }
267 
268 static int dsp_buffer_free(struct cx23885_audio_dev *chip)
269 {
270 	struct cx23885_riscmem *risc;
271 
272 	BUG_ON(!chip->dma_size);
273 
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);
280 
281 	chip->buf = NULL;
282 	chip->dma_size = 0;
283 
284 	return 0;
285 }
286 
287 /****************************************************************************
288 				ALSA PCM Interface
289  ****************************************************************************/
290 
291 /*
292  * Digital hardware definition
293  */
294 #define DEFAULT_FIFO_SIZE	4096
295 
296 static 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,
302 
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),
315 };
316 
317 /*
318  * audio pcm capture open callback
319  */
320 static int snd_cx23885_pcm_open(struct snd_pcm_substream *substream)
321 {
322 	struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
323 	struct snd_pcm_runtime *runtime = substream->runtime;
324 	int err;
325 
326 	if (!chip) {
327 		pr_err("BUG: cx23885 can't find device struct. Can't proceed with open\n");
328 		return -ENODEV;
329 	}
330 
331 	err = snd_pcm_hw_constraint_pow2(runtime, 0,
332 		SNDRV_PCM_HW_PARAM_PERIODS);
333 	if (err < 0)
334 		goto _error;
335 
336 	chip->substream = substream;
337 
338 	runtime->hw = snd_cx23885_digital_hw;
339 
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;
347 	}
348 
349 	return 0;
350 _error:
351 	dprintk(1, "Error opening PCM!\n");
352 	return err;
353 }
354 
355 /*
356  * audio close callback
357  */
358 static int snd_cx23885_close(struct snd_pcm_substream *substream)
359 {
360 	return 0;
361 }
362 
363 
364 /*
365  * hw_params callback
366  */
367 static int snd_cx23885_hw_params(struct snd_pcm_substream *substream,
368 			      struct snd_pcm_hw_params *hw_params)
369 {
370 	struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
371 	struct cx23885_audio_buffer *buf;
372 	int ret;
373 
374 	if (substream->runtime->dma_area) {
375 		dsp_buffer_free(chip);
376 		substream->runtime->dma_area = NULL;
377 	}
378 
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);
382 
383 	BUG_ON(!chip->dma_size);
384 	BUG_ON(chip->num_periods & (chip->num_periods-1));
385 
386 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
387 	if (NULL == buf)
388 		return -ENOMEM;
389 
390 	buf->bpl = chip->period_size;
391 	chip->buf = buf;
392 
393 	ret = cx23885_alsa_dma_init(chip,
394 			(PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
395 	if (ret < 0)
396 		goto error;
397 
398 	ret = cx23885_alsa_dma_map(chip);
399 	if (ret < 0)
400 		goto error;
401 
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;
406 
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 */
411 
412 	substream->runtime->dma_area = chip->buf->vaddr;
413 	substream->runtime->dma_bytes = chip->dma_size;
414 	substream->runtime->dma_addr = 0;
415 
416 	return 0;
417 
418 error:
419 	kfree(buf);
420 	chip->buf = NULL;
421 	return ret;
422 }
423 
424 /*
425  * hw free callback
426  */
427 static int snd_cx23885_hw_free(struct snd_pcm_substream *substream)
428 {
429 
430 	struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
431 
432 	if (substream->runtime->dma_area) {
433 		dsp_buffer_free(chip);
434 		substream->runtime->dma_area = NULL;
435 	}
436 
437 	return 0;
438 }
439 
440 /*
441  * prepare callback
442  */
443 static int snd_cx23885_prepare(struct snd_pcm_substream *substream)
444 {
445 	return 0;
446 }
447 
448 /*
449  * trigger callback
450  */
451 static int snd_cx23885_card_trigger(struct snd_pcm_substream *substream,
452 	int cmd)
453 {
454 	struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
455 	int err;
456 
457 	/* Local interrupts are already disabled by ALSA */
458 	spin_lock(&chip->lock);
459 
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;
470 	}
471 
472 	spin_unlock(&chip->lock);
473 
474 	return err;
475 }
476 
477 /*
478  * pointer callback
479  */
480 static snd_pcm_uframes_t snd_cx23885_pointer(
481 	struct snd_pcm_substream *substream)
482 {
483 	struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
484 	struct snd_pcm_runtime *runtime = substream->runtime;
485 	u16 count;
486 
487 	count = atomic_read(&chip->count);
488 
489 	return runtime->period_size * (count & (runtime->periods-1));
490 }
491 
492 /*
493  * page callback (needed for mmap)
494  */
495 static struct page *snd_cx23885_page(struct snd_pcm_substream *substream,
496 				unsigned long offset)
497 {
498 	void *pageptr = substream->runtime->dma_area + offset;
499 	return vmalloc_to_page(pageptr);
500 }
501 
502 /*
503  * operators
504  */
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,
515 };
516 
517 /*
518  * create a PCM device
519  */
520 static int snd_cx23885_pcm(struct cx23885_audio_dev *chip, int device,
521 	char *name)
522 {
523 	int err;
524 	struct snd_pcm *pcm;
525 
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);
532 
533 	return 0;
534 }
535 
536 /****************************************************************************
537 			Basic Flow for Sound Devices
538  ****************************************************************************/
539 
540 /*
541  * Alsa Constructor - Component probe
542  */
543 
544 struct cx23885_audio_dev *cx23885_audio_register(struct cx23885_dev *dev)
545 {
546 	struct snd_card *card;
547 	struct cx23885_audio_dev *chip;
548 	int err;
549 
550 	if (disable_analog_audio)
551 		return NULL;
552 
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;
557 	}
558 
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;
564 
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);
570 
571 	err = snd_cx23885_pcm(chip, 0, "CX23885 Digital");
572 	if (err < 0)
573 		goto error;
574 
575 	strcpy(card->driver, "CX23885");
576 	sprintf(card->shortname, "Conexant CX23885");
577 	sprintf(card->longname, "%s at %s", card->shortname, dev->name);
578 
579 	err = snd_card_register(card);
580 	if (err < 0)
581 		goto error;
582 
583 	dprintk(0, "registered ALSA audio device\n");
584 
585 	return chip;
586 
587 error:
588 	snd_card_free(card);
589 	pr_err("%s(): Failed to register analog audio adapter\n",
590 	       __func__);
591 
592 	return NULL;
593 }
594 
595 /*
596  * ALSA destructor
597  */
598 void cx23885_audio_unregister(struct cx23885_dev *dev)
599 {
600 	struct cx23885_audio_dev *chip = dev->audio_dev;
601 
602 	snd_card_free(chip->card);
603 }
604