Revert "gma500: Fix dependencies"
[zen-stable.git] / drivers / staging / tm6000 / tm6000-alsa.c
blobddfd7c33361ba31ba64fb1a4ef3a4a79b5b1c260
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 <linux/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);
88 return 0;
92 * BOARD Specific: Resets audio DMA
94 static int _tm6000_stop_audio_dma(struct snd_tm6000_card *chip)
96 struct tm6000_core *core = chip->core;
98 dprintk(1, "Stopping audio DMA\n");
100 /* Disables audio */
101 tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0x00, 0x40);
103 return 0;
106 static void dsp_buffer_free(struct snd_pcm_substream *substream)
108 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
110 dprintk(2, "Freeing buffer\n");
112 vfree(substream->runtime->dma_area);
113 substream->runtime->dma_area = NULL;
114 substream->runtime->dma_bytes = 0;
117 static int dsp_buffer_alloc(struct snd_pcm_substream *substream, int size)
119 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
121 dprintk(2, "Allocating buffer\n");
123 if (substream->runtime->dma_area) {
124 if (substream->runtime->dma_bytes > size)
125 return 0;
126 dsp_buffer_free(substream);
129 substream->runtime->dma_area = vmalloc(size);
130 if (!substream->runtime->dma_area)
131 return -ENOMEM;
133 substream->runtime->dma_bytes = size;
135 return 0;
139 /****************************************************************************
140 ALSA PCM Interface
141 ****************************************************************************/
144 * Digital hardware definition
146 #define DEFAULT_FIFO_SIZE 4096
148 static struct snd_pcm_hardware snd_tm6000_digital_hw = {
149 .info = SNDRV_PCM_INFO_MMAP |
150 SNDRV_PCM_INFO_INTERLEAVED |
151 SNDRV_PCM_INFO_BLOCK_TRANSFER |
152 SNDRV_PCM_INFO_MMAP_VALID,
153 .formats = SNDRV_PCM_FMTBIT_S16_LE,
155 .rates = SNDRV_PCM_RATE_CONTINUOUS,
156 .rate_min = 48000,
157 .rate_max = 48000,
158 .channels_min = 2,
159 .channels_max = 2,
160 .period_bytes_min = 64,
161 .period_bytes_max = 12544,
162 .periods_min = 1,
163 .periods_max = 98,
164 .buffer_bytes_max = 62720 * 8,
168 * audio pcm capture open callback
170 static int snd_tm6000_pcm_open(struct snd_pcm_substream *substream)
172 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
173 struct snd_pcm_runtime *runtime = substream->runtime;
174 int err;
176 err = snd_pcm_hw_constraint_pow2(runtime, 0,
177 SNDRV_PCM_HW_PARAM_PERIODS);
178 if (err < 0)
179 goto _error;
181 chip->substream = substream;
183 runtime->hw = snd_tm6000_digital_hw;
185 return 0;
186 _error:
187 dprintk(1, "Error opening PCM!\n");
188 return err;
192 * audio close callback
194 static int snd_tm6000_close(struct snd_pcm_substream *substream)
196 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
197 struct tm6000_core *core = chip->core;
199 if (atomic_read(&core->stream_started) > 0) {
200 atomic_set(&core->stream_started, 0);
201 schedule_work(&core->wq_trigger);
204 return 0;
207 static int tm6000_fillbuf(struct tm6000_core *core, char *buf, int size)
209 struct snd_tm6000_card *chip = core->adev;
210 struct snd_pcm_substream *substream = chip->substream;
211 struct snd_pcm_runtime *runtime;
212 int period_elapsed = 0;
213 unsigned int stride, buf_pos;
214 int length;
216 if (atomic_read(&core->stream_started) == 0)
217 return 0;
219 if (!size || !substream) {
220 dprintk(1, "substream was NULL\n");
221 return -EINVAL;
224 runtime = substream->runtime;
225 if (!runtime || !runtime->dma_area) {
226 dprintk(1, "runtime was NULL\n");
227 return -EINVAL;
230 buf_pos = chip->buf_pos;
231 stride = runtime->frame_bits >> 3;
233 if (stride == 0) {
234 dprintk(1, "stride is zero\n");
235 return -EINVAL;
238 length = size / stride;
239 if (length == 0) {
240 dprintk(1, "%s: length was zero\n", __func__);
241 return -EINVAL;
244 dprintk(1, "Copying %d bytes at %p[%d] - buf size=%d x %d\n", size,
245 runtime->dma_area, buf_pos,
246 (unsigned int)runtime->buffer_size, stride);
248 if (buf_pos + length >= runtime->buffer_size) {
249 unsigned int cnt = runtime->buffer_size - buf_pos;
250 memcpy(runtime->dma_area + buf_pos * stride, buf, cnt * stride);
251 memcpy(runtime->dma_area, buf + cnt * stride,
252 length * stride - cnt * stride);
253 } else
254 memcpy(runtime->dma_area + buf_pos * stride, buf,
255 length * stride);
257 #ifndef NO_PCM_LOCK
258 snd_pcm_stream_lock(substream);
259 #endif
261 chip->buf_pos += length;
262 if (chip->buf_pos >= runtime->buffer_size)
263 chip->buf_pos -= runtime->buffer_size;
265 chip->period_pos += length;
266 if (chip->period_pos >= runtime->period_size) {
267 chip->period_pos -= runtime->period_size;
268 period_elapsed = 1;
271 #ifndef NO_PCM_LOCK
272 snd_pcm_stream_unlock(substream);
273 #endif
275 if (period_elapsed)
276 snd_pcm_period_elapsed(substream);
278 return 0;
282 * hw_params callback
284 static int snd_tm6000_hw_params(struct snd_pcm_substream *substream,
285 struct snd_pcm_hw_params *hw_params)
287 int size, rc;
289 size = params_period_bytes(hw_params) * params_periods(hw_params);
291 rc = dsp_buffer_alloc(substream, size);
292 if (rc < 0)
293 return rc;
295 return 0;
299 * hw free callback
301 static int snd_tm6000_hw_free(struct snd_pcm_substream *substream)
303 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
304 struct tm6000_core *core = chip->core;
306 if (atomic_read(&core->stream_started) > 0) {
307 atomic_set(&core->stream_started, 0);
308 schedule_work(&core->wq_trigger);
311 return 0;
315 * prepare callback
317 static int snd_tm6000_prepare(struct snd_pcm_substream *substream)
319 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
321 chip->buf_pos = 0;
322 chip->period_pos = 0;
324 return 0;
329 * trigger callback
331 static void audio_trigger(struct work_struct *work)
333 struct tm6000_core *core = container_of(work, struct tm6000_core,
334 wq_trigger);
335 struct snd_tm6000_card *chip = core->adev;
337 if (atomic_read(&core->stream_started)) {
338 dprintk(1, "starting capture");
339 _tm6000_start_audio_dma(chip);
340 } else {
341 dprintk(1, "stopping capture");
342 _tm6000_stop_audio_dma(chip);
346 static int snd_tm6000_card_trigger(struct snd_pcm_substream *substream, int cmd)
348 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
349 struct tm6000_core *core = chip->core;
350 int err = 0;
352 switch (cmd) {
353 case SNDRV_PCM_TRIGGER_START:
354 atomic_set(&core->stream_started, 1);
355 break;
356 case SNDRV_PCM_TRIGGER_STOP:
357 atomic_set(&core->stream_started, 0);
358 break;
359 default:
360 err = -EINVAL;
361 break;
363 schedule_work(&core->wq_trigger);
365 return err;
368 * pointer callback
370 static snd_pcm_uframes_t snd_tm6000_pointer(struct snd_pcm_substream *substream)
372 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
374 return chip->buf_pos;
378 * operators
380 static struct snd_pcm_ops snd_tm6000_pcm_ops = {
381 .open = snd_tm6000_pcm_open,
382 .close = snd_tm6000_close,
383 .ioctl = snd_pcm_lib_ioctl,
384 .hw_params = snd_tm6000_hw_params,
385 .hw_free = snd_tm6000_hw_free,
386 .prepare = snd_tm6000_prepare,
387 .trigger = snd_tm6000_card_trigger,
388 .pointer = snd_tm6000_pointer,
392 * create a PCM device
395 /* FIXME: Control interface - How to control volume/mute? */
397 /****************************************************************************
398 Basic Flow for Sound Devices
399 ****************************************************************************/
402 * Alsa Constructor - Component probe
404 int tm6000_audio_init(struct tm6000_core *dev)
406 struct snd_card *card;
407 struct snd_tm6000_card *chip;
408 int rc;
409 static int devnr;
410 char component[14];
411 struct snd_pcm *pcm;
413 if (!dev)
414 return 0;
416 if (devnr >= SNDRV_CARDS)
417 return -ENODEV;
419 if (!enable[devnr])
420 return -ENOENT;
422 rc = snd_card_create(index[devnr], "tm6000", THIS_MODULE, 0, &card);
423 if (rc < 0) {
424 snd_printk(KERN_ERR "cannot create card instance %d\n", devnr);
425 return rc;
427 strcpy(card->driver, "tm6000-alsa");
428 strcpy(card->shortname, "TM5600/60x0");
429 sprintf(card->longname, "TM5600/60x0 Audio at bus %d device %d",
430 dev->udev->bus->busnum, dev->udev->devnum);
432 sprintf(component, "USB%04x:%04x",
433 le16_to_cpu(dev->udev->descriptor.idVendor),
434 le16_to_cpu(dev->udev->descriptor.idProduct));
435 snd_component_add(card, component);
436 snd_card_set_dev(card, &dev->udev->dev);
438 chip = kzalloc(sizeof(struct snd_tm6000_card), GFP_KERNEL);
439 if (!chip) {
440 rc = -ENOMEM;
441 goto error;
444 chip->core = dev;
445 chip->card = card;
446 dev->adev = chip;
447 spin_lock_init(&chip->reg_lock);
449 rc = snd_pcm_new(card, "TM6000 Audio", 0, 0, 1, &pcm);
450 if (rc < 0)
451 goto error_chip;
453 pcm->info_flags = 0;
454 pcm->private_data = chip;
455 strcpy(pcm->name, "Trident TM5600/60x0");
457 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_tm6000_pcm_ops);
459 INIT_WORK(&dev->wq_trigger, audio_trigger);
460 rc = snd_card_register(card);
461 if (rc < 0)
462 goto error_chip;
464 dprintk(1, "Registered audio driver for %s\n", card->longname);
466 return 0;
468 error_chip:
469 kfree(chip);
470 dev->adev = NULL;
471 error:
472 snd_card_free(card);
473 return rc;
476 static int tm6000_audio_fini(struct tm6000_core *dev)
478 struct snd_tm6000_card *chip = dev->adev;
480 if (!dev)
481 return 0;
483 if (!chip)
484 return 0;
486 if (!chip->card)
487 return 0;
489 snd_card_free(chip->card);
490 chip->card = NULL;
491 kfree(chip);
492 dev->adev = NULL;
494 return 0;
497 struct tm6000_ops audio_ops = {
498 .type = TM6000_AUDIO,
499 .name = "TM6000 Audio Extension",
500 .init = tm6000_audio_init,
501 .fini = tm6000_audio_fini,
502 .fillbuf = tm6000_fillbuf,
505 static int __init tm6000_alsa_register(void)
507 return tm6000_register_extension(&audio_ops);
510 static void __exit tm6000_alsa_unregister(void)
512 tm6000_unregister_extension(&audio_ops);
515 module_init(tm6000_alsa_register);
516 module_exit(tm6000_alsa_unregister);