3 * Support for audio capture for tm5600/6000/6010
4 * (c) 2007-2008 Mauro Carvalho Chehab <mchehab@redhat.com>
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>
20 #include <asm/delay.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/control.h>
25 #include <sound/initval.h>
29 #include "tm6000-regs.h"
33 #define dprintk(level, fmt, arg...) do { \
35 printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg); \
38 /****************************************************************************
39 Data type declarations - Can be moded to a header file later
40 ****************************************************************************/
42 struct snd_tm6000_card {
43 struct snd_card *card;
49 unsigned int period_size;
50 unsigned int num_periods;
52 struct tm6000_core *core;
53 struct tm6000_buffer *buf;
57 struct snd_pcm_substream *substream;
61 /****************************************************************************
62 Module global static vars
63 ****************************************************************************/
65 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
66 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
67 static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
69 module_param_array(enable, bool, NULL, 0444);
70 MODULE_PARM_DESC(enable, "Enable tm6000x soundcard. default enabled.");
72 module_param_array(index, int, NULL, 0444);
73 MODULE_PARM_DESC(index, "Index value for tm6000x capture interface(s).");
76 /****************************************************************************
78 ****************************************************************************/
80 MODULE_DESCRIPTION("ALSA driver module for tm5600/tm6000/tm6010 based TV cards");
81 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
82 MODULE_LICENSE("GPL");
83 MODULE_SUPPORTED_DEVICE("{{Trident,tm5600},"
86 static unsigned int debug;
87 module_param(debug, int, 0644);
88 MODULE_PARM_DESC(debug, "enable debug messages");
90 /****************************************************************************
91 Module specific funtions
92 ****************************************************************************/
95 * BOARD Specific: Sets audio DMA
98 static int _tm6000_start_audio_dma(struct snd_tm6000_card *chip)
100 struct tm6000_core *core = chip->core;
104 val = tm6000_get_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0x0);
106 tm6000_set_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, val);
108 tm6000_set_reg(core, TM6010_REQ08_R01_A_INIT, 0x80);
114 * BOARD Specific: Resets audio DMA
116 static int _tm6000_stop_audio_dma(struct snd_tm6000_card *chip)
118 struct tm6000_core *core = chip->core;
120 dprintk(1, "Stopping audio DMA\n");
123 val = tm6000_get_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0x0);
125 tm6000_set_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, val);
127 tm6000_set_reg(core, TM6010_REQ08_R01_A_INIT, 0);
132 static int dsp_buffer_free(struct snd_tm6000_card *chip)
134 BUG_ON(!chip->bufsize);
136 dprintk(2, "Freeing buffer\n");
138 /* FIXME: Frees buffer */
145 /****************************************************************************
147 ****************************************************************************/
150 * Digital hardware definition
152 #define DEFAULT_FIFO_SIZE 4096
154 static struct snd_pcm_hardware snd_tm6000_digital_hw = {
155 .info = SNDRV_PCM_INFO_MMAP |
156 SNDRV_PCM_INFO_INTERLEAVED |
157 SNDRV_PCM_INFO_BLOCK_TRANSFER |
158 SNDRV_PCM_INFO_MMAP_VALID,
159 .formats = SNDRV_PCM_FMTBIT_S16_LE,
161 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
166 .period_bytes_min = DEFAULT_FIFO_SIZE/4,
167 .period_bytes_max = DEFAULT_FIFO_SIZE/4,
170 .buffer_bytes_max = (1024*1024),
174 * audio pcm capture open callback
176 static int snd_tm6000_pcm_open(struct snd_pcm_substream *substream)
178 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
179 struct snd_pcm_runtime *runtime = substream->runtime;
182 err = snd_pcm_hw_constraint_pow2(runtime, 0,
183 SNDRV_PCM_HW_PARAM_PERIODS);
187 chip->substream = substream;
189 runtime->hw = snd_tm6000_digital_hw;
193 dprintk(1, "Error opening PCM!\n");
198 * audio close callback
200 static int snd_tm6000_close(struct snd_pcm_substream *substream)
208 static int snd_tm6000_hw_params(struct snd_pcm_substream *substream,
209 struct snd_pcm_hw_params *hw_params)
211 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
213 if (substream->runtime->dma_area) {
214 dsp_buffer_free(chip);
215 substream->runtime->dma_area = NULL;
218 chip->period_size = params_period_bytes(hw_params);
219 chip->num_periods = params_periods(hw_params);
220 chip->bufsize = chip->period_size * params_periods(hw_params);
222 BUG_ON(!chip->bufsize);
224 dprintk(1, "Setting buffer\n");
226 /* FIXME: Allocate buffer for audio */
235 static int snd_tm6000_hw_free(struct snd_pcm_substream *substream)
238 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
240 if (substream->runtime->dma_area) {
241 dsp_buffer_free(chip);
242 substream->runtime->dma_area = NULL;
251 static int snd_tm6000_prepare(struct snd_pcm_substream *substream)
260 static int snd_tm6000_card_trigger(struct snd_pcm_substream *substream, int cmd)
262 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
265 spin_lock(&chip->reg_lock);
268 case SNDRV_PCM_TRIGGER_START:
269 err = _tm6000_start_audio_dma(chip);
271 case SNDRV_PCM_TRIGGER_STOP:
272 err = _tm6000_stop_audio_dma(chip);
279 spin_unlock(&chip->reg_lock);
287 static snd_pcm_uframes_t snd_tm6000_pointer(struct snd_pcm_substream *substream)
289 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
290 struct snd_pcm_runtime *runtime = substream->runtime;
293 count = atomic_read(&chip->count);
295 return runtime->period_size * (count & (runtime->periods-1));
301 static struct snd_pcm_ops snd_tm6000_pcm_ops = {
302 .open = snd_tm6000_pcm_open,
303 .close = snd_tm6000_close,
304 .ioctl = snd_pcm_lib_ioctl,
305 .hw_params = snd_tm6000_hw_params,
306 .hw_free = snd_tm6000_hw_free,
307 .prepare = snd_tm6000_prepare,
308 .trigger = snd_tm6000_card_trigger,
309 .pointer = snd_tm6000_pointer,
313 * create a PCM device
315 static int __devinit snd_tm6000_pcm(struct snd_tm6000_card *chip,
316 int device, char *name)
321 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
324 pcm->private_data = chip;
325 strcpy(pcm->name, name);
326 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_tm6000_pcm_ops);
331 /* FIXME: Control interface - How to control volume/mute? */
333 /****************************************************************************
334 Basic Flow for Sound Devices
335 ****************************************************************************/
338 * Alsa Constructor - Component probe
341 int tm6000_audio_init(struct tm6000_core *dev, int idx)
343 struct snd_card *card;
344 struct snd_tm6000_card *chip;
348 if (idx >= SNDRV_CARDS)
354 rc = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
356 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
360 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
369 strcpy(card->driver, "tm6000-alsa");
370 sprintf(component, "USB%04x:%04x",
371 le16_to_cpu(dev->udev->descriptor.idVendor),
372 le16_to_cpu(dev->udev->descriptor.idProduct));
373 snd_component_add(card, component);
375 if (dev->udev->descriptor.iManufacturer)
376 len = usb_string(dev->udev,
377 dev->udev->descriptor.iManufacturer,
378 card->longname, sizeof(card->longname));
383 strlcat(card->longname, " ", sizeof(card->longname));
385 strlcat(card->longname, card->shortname, sizeof(card->longname));
387 len = strlcat(card->longname, " at ", sizeof(card->longname));
389 if (len < sizeof(card->longname))
390 usb_make_path(dev->udev, card->longname + len,
391 sizeof(card->longname) - len);
393 strlcat(card->longname,
394 dev->udev->speed == USB_SPEED_LOW ? ", low speed" :
395 dev->udev->speed == USB_SPEED_FULL ? ", full speed" :
397 sizeof(card->longname));
399 rc = snd_tm6000_pcm(chip, 0, "tm6000 Digital");
403 rc = snd_card_register(card);
415 static int tm6000_audio_fini(struct tm6000_core *dev)
420 struct tm6000_ops audio_ops = {
422 .name = "TM6000 Audio Extension",
423 .init = tm6000_audio_init,
424 .fini = tm6000_audio_fini,
427 static int __init tm6000_alsa_register(void)
429 return tm6000_register_extension(&audio_ops);
432 static void __exit tm6000_alsa_unregister(void)
434 tm6000_unregister_extension(&audio_ops);
437 module_init(tm6000_alsa_register);
438 module_exit(tm6000_alsa_unregister);