FRV: Use generic show_interrupts()
[cris-mirror.git] / drivers / staging / tm6000 / tm6000-alsa.c
blobacb03172a8878935b57062a399f257379e1cc930
1 /*
3 * Support for audio capture for tm5600/6000/6010
4 * (c) 2007-2008 Mauro Carvalho Chehab <mchehab@redhat.com>
6 * Based on cx88-alsa.c
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/usb.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
21 #include <asm/delay.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/control.h>
26 #include <sound/initval.h>
29 #include "tm6000.h"
30 #include "tm6000-regs.h"
32 #undef dprintk
34 #define dprintk(level, fmt, arg...) do { \
35 if (debug >= level) \
36 printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg); \
37 } while (0)
39 /****************************************************************************
40 Module global static vars
41 ****************************************************************************/
43 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
45 static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
47 module_param_array(enable, bool, NULL, 0444);
48 MODULE_PARM_DESC(enable, "Enable tm6000x soundcard. default enabled.");
50 module_param_array(index, int, NULL, 0444);
51 MODULE_PARM_DESC(index, "Index value for tm6000x capture interface(s).");
54 /****************************************************************************
55 Module macros
56 ****************************************************************************/
58 MODULE_DESCRIPTION("ALSA driver module for tm5600/tm6000/tm6010 based TV cards");
59 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
60 MODULE_LICENSE("GPL");
61 MODULE_SUPPORTED_DEVICE("{{Trident,tm5600},"
62 "{{Trident,tm6000},"
63 "{{Trident,tm6010}");
64 static unsigned int debug;
65 module_param(debug, int, 0644);
66 MODULE_PARM_DESC(debug, "enable debug messages");
68 /****************************************************************************
69 Module specific funtions
70 ****************************************************************************/
73 * BOARD Specific: Sets audio DMA
76 static int _tm6000_start_audio_dma(struct snd_tm6000_card *chip)
78 struct tm6000_core *core = chip->core;
80 dprintk(1, "Starting audio DMA\n");
82 /* Enables audio */
83 tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0x40, 0x40);
85 tm6000_set_audio_bitrate(core, 48000);
87 tm6000_set_reg(core, TM6010_REQ08_R01_A_INIT, 0x80);
89 return 0;
93 * BOARD Specific: Resets audio DMA
95 static int _tm6000_stop_audio_dma(struct snd_tm6000_card *chip)
97 struct tm6000_core *core = chip->core;
99 dprintk(1, "Stopping audio DMA\n");
101 /* Disables audio */
102 tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0x00, 0x40);
104 tm6000_set_reg(core, TM6010_REQ08_R01_A_INIT, 0);
106 return 0;
109 static void dsp_buffer_free(struct snd_pcm_substream *substream)
111 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
113 dprintk(2, "Freeing buffer\n");
115 vfree(substream->runtime->dma_area);
116 substream->runtime->dma_area = NULL;
117 substream->runtime->dma_bytes = 0;
120 static int dsp_buffer_alloc(struct snd_pcm_substream *substream, int size)
122 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
124 dprintk(2, "Allocating buffer\n");
126 if (substream->runtime->dma_area) {
127 if (substream->runtime->dma_bytes > size)
128 return 0;
129 dsp_buffer_free(substream);
132 substream->runtime->dma_area = vmalloc(size);
133 if (!substream->runtime->dma_area)
134 return -ENOMEM;
136 substream->runtime->dma_bytes = size;
138 return 0;
142 /****************************************************************************
143 ALSA PCM Interface
144 ****************************************************************************/
147 * Digital hardware definition
149 #define DEFAULT_FIFO_SIZE 4096
151 static struct snd_pcm_hardware snd_tm6000_digital_hw = {
152 .info = SNDRV_PCM_INFO_MMAP |
153 SNDRV_PCM_INFO_INTERLEAVED |
154 SNDRV_PCM_INFO_BLOCK_TRANSFER |
155 SNDRV_PCM_INFO_MMAP_VALID,
156 .formats = SNDRV_PCM_FMTBIT_S16_LE,
158 .rates = SNDRV_PCM_RATE_CONTINUOUS,
159 .rate_min = 48000,
160 .rate_max = 48000,
161 .channels_min = 2,
162 .channels_max = 2,
163 .period_bytes_min = 64,
164 .period_bytes_max = 12544,
165 .periods_min = 1,
166 .periods_max = 98,
167 .buffer_bytes_max = 62720 * 8,
171 * audio pcm capture open callback
173 static int snd_tm6000_pcm_open(struct snd_pcm_substream *substream)
175 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
176 struct snd_pcm_runtime *runtime = substream->runtime;
177 int err;
179 err = snd_pcm_hw_constraint_pow2(runtime, 0,
180 SNDRV_PCM_HW_PARAM_PERIODS);
181 if (err < 0)
182 goto _error;
184 chip->substream = substream;
186 runtime->hw = snd_tm6000_digital_hw;
188 return 0;
189 _error:
190 dprintk(1, "Error opening PCM!\n");
191 return err;
195 * audio close callback
197 static int snd_tm6000_close(struct snd_pcm_substream *substream)
199 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
200 struct tm6000_core *core = chip->core;
202 if (atomic_read(&core->stream_started) > 0) {
203 atomic_set(&core->stream_started, 0);
204 schedule_work(&core->wq_trigger);
207 return 0;
210 static int tm6000_fillbuf(struct tm6000_core *core, char *buf, int size)
212 struct snd_tm6000_card *chip = core->adev;
213 struct snd_pcm_substream *substream = chip->substream;
214 struct snd_pcm_runtime *runtime;
215 int period_elapsed = 0;
216 unsigned int stride, buf_pos;
217 int length;
219 if (atomic_read(&core->stream_started) == 0)
220 return 0;
222 if (!size || !substream) {
223 dprintk(1, "substream was NULL\n");
224 return -EINVAL;
227 runtime = substream->runtime;
228 if (!runtime || !runtime->dma_area) {
229 dprintk(1, "runtime was NULL\n");
230 return -EINVAL;
233 buf_pos = chip->buf_pos;
234 stride = runtime->frame_bits >> 3;
236 if (stride == 0) {
237 dprintk(1, "stride is zero\n");
238 return -EINVAL;
241 length = size / stride;
242 if (length == 0) {
243 dprintk(1, "%s: length was zero\n", __func__);
244 return -EINVAL;
247 dprintk(1, "Copying %d bytes at %p[%d] - buf size=%d x %d\n", size,
248 runtime->dma_area, buf_pos,
249 (unsigned int)runtime->buffer_size, stride);
251 if (buf_pos + length >= runtime->buffer_size) {
252 unsigned int cnt = runtime->buffer_size - buf_pos;
253 memcpy(runtime->dma_area + buf_pos * stride, buf, cnt * stride);
254 memcpy(runtime->dma_area, buf + cnt * stride,
255 length * stride - cnt * stride);
256 } else
257 memcpy(runtime->dma_area + buf_pos * stride, buf,
258 length * stride);
260 #ifndef NO_PCM_LOCK
261 snd_pcm_stream_lock(substream);
262 #endif
264 chip->buf_pos += length;
265 if (chip->buf_pos >= runtime->buffer_size)
266 chip->buf_pos -= runtime->buffer_size;
268 chip->period_pos += length;
269 if (chip->period_pos >= runtime->period_size) {
270 chip->period_pos -= runtime->period_size;
271 period_elapsed = 1;
274 #ifndef NO_PCM_LOCK
275 snd_pcm_stream_unlock(substream);
276 #endif
278 if (period_elapsed)
279 snd_pcm_period_elapsed(substream);
281 return 0;
285 * hw_params callback
287 static int snd_tm6000_hw_params(struct snd_pcm_substream *substream,
288 struct snd_pcm_hw_params *hw_params)
290 int size, rc;
292 size = params_period_bytes(hw_params) * params_periods(hw_params);
294 rc = dsp_buffer_alloc(substream, size);
295 if (rc < 0)
296 return rc;
298 return 0;
302 * hw free callback
304 static int snd_tm6000_hw_free(struct snd_pcm_substream *substream)
306 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
307 struct tm6000_core *core = chip->core;
309 if (atomic_read(&core->stream_started) > 0) {
310 atomic_set(&core->stream_started, 0);
311 schedule_work(&core->wq_trigger);
314 return 0;
318 * prepare callback
320 static int snd_tm6000_prepare(struct snd_pcm_substream *substream)
322 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
324 chip->buf_pos = 0;
325 chip->period_pos = 0;
327 return 0;
332 * trigger callback
334 static void audio_trigger(struct work_struct *work)
336 struct tm6000_core *core = container_of(work, struct tm6000_core,
337 wq_trigger);
338 struct snd_tm6000_card *chip = core->adev;
340 if (atomic_read(&core->stream_started)) {
341 dprintk(1, "starting capture");
342 _tm6000_start_audio_dma(chip);
343 } else {
344 dprintk(1, "stopping capture");
345 _tm6000_stop_audio_dma(chip);
349 static int snd_tm6000_card_trigger(struct snd_pcm_substream *substream, int cmd)
351 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
352 struct tm6000_core *core = chip->core;
353 int err = 0;
355 switch (cmd) {
356 case SNDRV_PCM_TRIGGER_START:
357 atomic_set(&core->stream_started, 1);
358 break;
359 case SNDRV_PCM_TRIGGER_STOP:
360 atomic_set(&core->stream_started, 0);
361 break;
362 default:
363 err = -EINVAL;
364 break;
366 schedule_work(&core->wq_trigger);
368 return err;
371 * pointer callback
373 static snd_pcm_uframes_t snd_tm6000_pointer(struct snd_pcm_substream *substream)
375 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
377 return chip->buf_pos;
381 * operators
383 static struct snd_pcm_ops snd_tm6000_pcm_ops = {
384 .open = snd_tm6000_pcm_open,
385 .close = snd_tm6000_close,
386 .ioctl = snd_pcm_lib_ioctl,
387 .hw_params = snd_tm6000_hw_params,
388 .hw_free = snd_tm6000_hw_free,
389 .prepare = snd_tm6000_prepare,
390 .trigger = snd_tm6000_card_trigger,
391 .pointer = snd_tm6000_pointer,
395 * create a PCM device
398 /* FIXME: Control interface - How to control volume/mute? */
400 /****************************************************************************
401 Basic Flow for Sound Devices
402 ****************************************************************************/
405 * Alsa Constructor - Component probe
407 int tm6000_audio_init(struct tm6000_core *dev)
409 struct snd_card *card;
410 struct snd_tm6000_card *chip;
411 int rc;
412 static int devnr;
413 char component[14];
414 struct snd_pcm *pcm;
416 if (!dev)
417 return 0;
419 if (devnr >= SNDRV_CARDS)
420 return -ENODEV;
422 if (!enable[devnr])
423 return -ENOENT;
425 rc = snd_card_create(index[devnr], "tm6000", THIS_MODULE, 0, &card);
426 if (rc < 0) {
427 snd_printk(KERN_ERR "cannot create card instance %d\n", devnr);
428 return rc;
430 strcpy(card->driver, "tm6000-alsa");
431 strcpy(card->shortname, "TM5600/60x0");
432 sprintf(card->longname, "TM5600/60x0 Audio at bus %d device %d",
433 dev->udev->bus->busnum, dev->udev->devnum);
435 sprintf(component, "USB%04x:%04x",
436 le16_to_cpu(dev->udev->descriptor.idVendor),
437 le16_to_cpu(dev->udev->descriptor.idProduct));
438 snd_component_add(card, component);
439 snd_card_set_dev(card, &dev->udev->dev);
441 chip = kzalloc(sizeof(struct snd_tm6000_card), GFP_KERNEL);
442 if (!chip) {
443 rc = -ENOMEM;
444 goto error;
447 chip->core = dev;
448 chip->card = card;
449 dev->adev = chip;
450 spin_lock_init(&chip->reg_lock);
452 rc = snd_pcm_new(card, "TM6000 Audio", 0, 0, 1, &pcm);
453 if (rc < 0)
454 goto error_chip;
456 pcm->info_flags = 0;
457 pcm->private_data = chip;
458 strcpy(pcm->name, "Trident TM5600/60x0");
460 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_tm6000_pcm_ops);
462 INIT_WORK(&dev->wq_trigger, audio_trigger);
463 rc = snd_card_register(card);
464 if (rc < 0)
465 goto error_chip;
467 dprintk(1,"Registered audio driver for %s\n", card->longname);
469 return 0;
471 error_chip:
472 kfree(chip);
473 dev->adev = NULL;
474 error:
475 snd_card_free(card);
476 return rc;
479 static int tm6000_audio_fini(struct tm6000_core *dev)
481 struct snd_tm6000_card *chip = dev->adev;
483 if (!dev)
484 return 0;
486 if (!chip)
487 return 0;
489 if (!chip->card)
490 return 0;
492 snd_card_free(chip->card);
493 chip->card = NULL;
494 kfree(chip);
495 dev->adev = NULL;
497 return 0;
500 struct tm6000_ops audio_ops = {
501 .type = TM6000_AUDIO,
502 .name = "TM6000 Audio Extension",
503 .init = tm6000_audio_init,
504 .fini = tm6000_audio_fini,
505 .fillbuf = tm6000_fillbuf,
508 static int __init tm6000_alsa_register(void)
510 return tm6000_register_extension(&audio_ops);
513 static void __exit tm6000_alsa_unregister(void)
515 tm6000_unregister_extension(&audio_ops);
518 module_init(tm6000_alsa_register);
519 module_exit(tm6000_alsa_unregister);