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>
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>
30 #include "tm6000-regs.h"
34 #define dprintk(level, fmt, arg...) do { \
36 printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg); \
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 /****************************************************************************
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},"
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");
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);
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");
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);
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
)
129 dsp_buffer_free(substream
);
132 substream
->runtime
->dma_area
= vmalloc(size
);
133 if (!substream
->runtime
->dma_area
)
136 substream
->runtime
->dma_bytes
= size
;
142 /****************************************************************************
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
,
163 .period_bytes_min
= 64,
164 .period_bytes_max
= 12544,
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
;
179 err
= snd_pcm_hw_constraint_pow2(runtime
, 0,
180 SNDRV_PCM_HW_PARAM_PERIODS
);
184 chip
->substream
= substream
;
186 runtime
->hw
= snd_tm6000_digital_hw
;
190 dprintk(1, "Error opening PCM!\n");
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
);
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
;
219 if (atomic_read(&core
->stream_started
) == 0)
222 if (!size
|| !substream
) {
223 dprintk(1, "substream was NULL\n");
227 runtime
= substream
->runtime
;
228 if (!runtime
|| !runtime
->dma_area
) {
229 dprintk(1, "runtime was NULL\n");
233 buf_pos
= chip
->buf_pos
;
234 stride
= runtime
->frame_bits
>> 3;
237 dprintk(1, "stride is zero\n");
241 length
= size
/ stride
;
243 dprintk(1, "%s: length was zero\n", __func__
);
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
);
257 memcpy(runtime
->dma_area
+ buf_pos
* stride
, buf
,
261 snd_pcm_stream_lock(substream
);
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
;
275 snd_pcm_stream_unlock(substream
);
279 snd_pcm_period_elapsed(substream
);
287 static int snd_tm6000_hw_params(struct snd_pcm_substream
*substream
,
288 struct snd_pcm_hw_params
*hw_params
)
292 size
= params_period_bytes(hw_params
) * params_periods(hw_params
);
294 rc
= dsp_buffer_alloc(substream
, size
);
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
);
320 static int snd_tm6000_prepare(struct snd_pcm_substream
*substream
)
322 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
325 chip
->period_pos
= 0;
334 static void audio_trigger(struct work_struct
*work
)
336 struct tm6000_core
*core
= container_of(work
, struct tm6000_core
,
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
);
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
;
356 case SNDRV_PCM_TRIGGER_START
:
357 atomic_set(&core
->stream_started
, 1);
359 case SNDRV_PCM_TRIGGER_STOP
:
360 atomic_set(&core
->stream_started
, 0);
366 schedule_work(&core
->wq_trigger
);
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
;
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
;
419 if (devnr
>= SNDRV_CARDS
)
425 rc
= snd_card_create(index
[devnr
], "tm6000", THIS_MODULE
, 0, &card
);
427 snd_printk(KERN_ERR
"cannot create card instance %d\n", devnr
);
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
);
450 spin_lock_init(&chip
->reg_lock
);
452 rc
= snd_pcm_new(card
, "TM6000 Audio", 0, 0, 1, &pcm
);
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
);
467 dprintk(1,"Registered audio driver for %s\n", card
->longname
);
479 static int tm6000_audio_fini(struct tm6000_core
*dev
)
481 struct snd_tm6000_card
*chip
= dev
->adev
;
492 snd_card_free(chip
->card
);
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
);