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
;
81 dprintk(1, "Starting audio DMA\n");
84 val
= tm6000_get_reg(core
, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF
, 0x0);
86 tm6000_set_reg(core
, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF
, val
);
88 tm6000_set_audio_bitrate(core
, 48000);
90 tm6000_set_reg(core
, TM6010_REQ08_R01_A_INIT
, 0x80);
96 * BOARD Specific: Resets audio DMA
98 static int _tm6000_stop_audio_dma(struct snd_tm6000_card
*chip
)
100 struct tm6000_core
*core
= chip
->core
;
102 dprintk(1, "Stopping audio DMA\n");
105 val
= tm6000_get_reg(core
, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF
, 0x0);
107 tm6000_set_reg(core
, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF
, val
);
109 tm6000_set_reg(core
, TM6010_REQ08_R01_A_INIT
, 0);
114 static void dsp_buffer_free(struct snd_pcm_substream
*substream
)
116 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
118 dprintk(2, "Freeing buffer\n");
120 vfree(substream
->runtime
->dma_area
);
121 substream
->runtime
->dma_area
= NULL
;
122 substream
->runtime
->dma_bytes
= 0;
125 static int dsp_buffer_alloc(struct snd_pcm_substream
*substream
, int size
)
127 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
129 dprintk(2, "Allocating buffer\n");
131 if (substream
->runtime
->dma_area
) {
132 if (substream
->runtime
->dma_bytes
> size
)
134 dsp_buffer_free(substream
);
137 substream
->runtime
->dma_area
= vmalloc(size
);
138 if (!substream
->runtime
->dma_area
)
141 substream
->runtime
->dma_bytes
= size
;
147 /****************************************************************************
149 ****************************************************************************/
152 * Digital hardware definition
154 #define DEFAULT_FIFO_SIZE 4096
156 static struct snd_pcm_hardware snd_tm6000_digital_hw
= {
157 .info
= SNDRV_PCM_INFO_MMAP
|
158 SNDRV_PCM_INFO_INTERLEAVED
|
159 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
160 SNDRV_PCM_INFO_MMAP_VALID
,
161 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
163 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
168 .period_bytes_min
= 64,
169 .period_bytes_max
= 12544,
172 .buffer_bytes_max
= 62720 * 8,
176 * audio pcm capture open callback
178 static int snd_tm6000_pcm_open(struct snd_pcm_substream
*substream
)
180 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
181 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
184 err
= snd_pcm_hw_constraint_pow2(runtime
, 0,
185 SNDRV_PCM_HW_PARAM_PERIODS
);
189 chip
->substream
= substream
;
191 runtime
->hw
= snd_tm6000_digital_hw
;
195 dprintk(1, "Error opening PCM!\n");
200 * audio close callback
202 static int snd_tm6000_close(struct snd_pcm_substream
*substream
)
204 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
205 struct tm6000_core
*core
= chip
->core
;
207 if (atomic_read(&core
->stream_started
) > 0) {
208 atomic_set(&core
->stream_started
, 0);
209 schedule_work(&core
->wq_trigger
);
215 static int tm6000_fillbuf(struct tm6000_core
*core
, char *buf
, int size
)
217 struct snd_tm6000_card
*chip
= core
->adev
;
218 struct snd_pcm_substream
*substream
= chip
->substream
;
219 struct snd_pcm_runtime
*runtime
;
220 int period_elapsed
= 0;
221 unsigned int stride
, buf_pos
;
224 if (atomic_read(&core
->stream_started
) == 0)
227 if (!size
|| !substream
) {
228 dprintk(1, "substream was NULL\n");
232 runtime
= substream
->runtime
;
233 if (!runtime
|| !runtime
->dma_area
) {
234 dprintk(1, "runtime was NULL\n");
238 buf_pos
= chip
->buf_pos
;
239 stride
= runtime
->frame_bits
>> 3;
242 dprintk(1, "stride is zero\n");
246 length
= size
/ stride
;
248 dprintk(1, "%s: length was zero\n", __func__
);
252 dprintk(1, "Copying %d bytes at %p[%d] - buf size=%d x %d\n", size
,
253 runtime
->dma_area
, buf_pos
,
254 (unsigned int)runtime
->buffer_size
, stride
);
256 if (buf_pos
+ length
>= runtime
->buffer_size
) {
257 unsigned int cnt
= runtime
->buffer_size
- buf_pos
;
258 memcpy(runtime
->dma_area
+ buf_pos
* stride
, buf
, cnt
* stride
);
259 memcpy(runtime
->dma_area
, buf
+ cnt
* stride
,
260 length
* stride
- cnt
* stride
);
262 memcpy(runtime
->dma_area
+ buf_pos
* stride
, buf
,
266 snd_pcm_stream_lock(substream
);
269 chip
->buf_pos
+= length
;
270 if (chip
->buf_pos
>= runtime
->buffer_size
)
271 chip
->buf_pos
-= runtime
->buffer_size
;
273 chip
->period_pos
+= length
;
274 if (chip
->period_pos
>= runtime
->period_size
) {
275 chip
->period_pos
-= runtime
->period_size
;
280 snd_pcm_stream_unlock(substream
);
284 snd_pcm_period_elapsed(substream
);
292 static int snd_tm6000_hw_params(struct snd_pcm_substream
*substream
,
293 struct snd_pcm_hw_params
*hw_params
)
297 size
= params_period_bytes(hw_params
) * params_periods(hw_params
);
299 rc
= dsp_buffer_alloc(substream
, size
);
309 static int snd_tm6000_hw_free(struct snd_pcm_substream
*substream
)
311 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
312 struct tm6000_core
*core
= chip
->core
;
314 if (atomic_read(&core
->stream_started
) > 0) {
315 atomic_set(&core
->stream_started
, 0);
316 schedule_work(&core
->wq_trigger
);
325 static int snd_tm6000_prepare(struct snd_pcm_substream
*substream
)
327 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
330 chip
->period_pos
= 0;
339 static void audio_trigger(struct work_struct
*work
)
341 struct tm6000_core
*core
= container_of(work
, struct tm6000_core
,
343 struct snd_tm6000_card
*chip
= core
->adev
;
345 if (atomic_read(&core
->stream_started
)) {
346 dprintk(1, "starting capture");
347 _tm6000_start_audio_dma(chip
);
349 dprintk(1, "stopping capture");
350 _tm6000_stop_audio_dma(chip
);
354 static int snd_tm6000_card_trigger(struct snd_pcm_substream
*substream
, int cmd
)
356 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
357 struct tm6000_core
*core
= chip
->core
;
361 case SNDRV_PCM_TRIGGER_START
:
362 atomic_set(&core
->stream_started
, 1);
364 case SNDRV_PCM_TRIGGER_STOP
:
365 atomic_set(&core
->stream_started
, 0);
371 schedule_work(&core
->wq_trigger
);
378 static snd_pcm_uframes_t
snd_tm6000_pointer(struct snd_pcm_substream
*substream
)
380 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
382 return chip
->buf_pos
;
388 static struct snd_pcm_ops snd_tm6000_pcm_ops
= {
389 .open
= snd_tm6000_pcm_open
,
390 .close
= snd_tm6000_close
,
391 .ioctl
= snd_pcm_lib_ioctl
,
392 .hw_params
= snd_tm6000_hw_params
,
393 .hw_free
= snd_tm6000_hw_free
,
394 .prepare
= snd_tm6000_prepare
,
395 .trigger
= snd_tm6000_card_trigger
,
396 .pointer
= snd_tm6000_pointer
,
400 * create a PCM device
403 /* FIXME: Control interface - How to control volume/mute? */
405 /****************************************************************************
406 Basic Flow for Sound Devices
407 ****************************************************************************/
410 * Alsa Constructor - Component probe
412 int tm6000_audio_init(struct tm6000_core
*dev
)
414 struct snd_card
*card
;
415 struct snd_tm6000_card
*chip
;
424 if (devnr
>= SNDRV_CARDS
)
430 rc
= snd_card_create(index
[devnr
], "tm6000", THIS_MODULE
, 0, &card
);
432 snd_printk(KERN_ERR
"cannot create card instance %d\n", devnr
);
435 strcpy(card
->driver
, "tm6000-alsa");
436 strcpy(card
->shortname
, "TM5600/60x0");
437 sprintf(card
->longname
, "TM5600/60x0 Audio at bus %d device %d",
438 dev
->udev
->bus
->busnum
, dev
->udev
->devnum
);
440 sprintf(component
, "USB%04x:%04x",
441 le16_to_cpu(dev
->udev
->descriptor
.idVendor
),
442 le16_to_cpu(dev
->udev
->descriptor
.idProduct
));
443 snd_component_add(card
, component
);
444 snd_card_set_dev(card
, &dev
->udev
->dev
);
446 chip
= kzalloc(sizeof(struct snd_tm6000_card
), GFP_KERNEL
);
455 spin_lock_init(&chip
->reg_lock
);
457 rc
= snd_pcm_new(card
, "TM6000 Audio", 0, 0, 1, &pcm
);
462 pcm
->private_data
= chip
;
463 strcpy(pcm
->name
, "Trident TM5600/60x0");
465 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_tm6000_pcm_ops
);
467 INIT_WORK(&dev
->wq_trigger
, audio_trigger
);
468 rc
= snd_card_register(card
);
472 dprintk(1,"Registered audio driver for %s\n", card
->longname
);
484 static int tm6000_audio_fini(struct tm6000_core
*dev
)
486 struct snd_tm6000_card
*chip
= dev
->adev
;
497 snd_card_free(chip
->card
);
505 struct tm6000_ops audio_ops
= {
506 .type
= TM6000_AUDIO
,
507 .name
= "TM6000 Audio Extension",
508 .init
= tm6000_audio_init
,
509 .fini
= tm6000_audio_fini
,
510 .fillbuf
= tm6000_fillbuf
,
513 static int __init
tm6000_alsa_register(void)
515 return tm6000_register_extension(&audio_ops
);
518 static void __exit
tm6000_alsa_unregister(void)
520 tm6000_unregister_extension(&audio_ops
);
523 module_init(tm6000_alsa_register
);
524 module_exit(tm6000_alsa_unregister
);