1 // SPDX-License-Identifier: GPL-2.0
2 // Support for audio capture for tm5600/6000/6010
3 // Copyright (c) 2007-2008 Mauro Carvalho Chehab <mchehab@kernel.org>
5 // Based on cx88-alsa.c
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/device.h>
10 #include <linux/interrupt.h>
11 #include <linux/usb.h>
12 #include <linux/slab.h>
13 #include <linux/vmalloc.h>
15 #include <linux/delay.h>
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/control.h>
20 #include <sound/initval.h>
24 #include "tm6000-regs.h"
28 #define dprintk(level, fmt, arg...) do { \
30 printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg); \
33 /****************************************************************************
34 Module global static vars
35 ****************************************************************************/
37 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
; /* Index 0-MAX */
39 static bool enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE_PNP
;
41 module_param_array(enable
, bool, NULL
, 0444);
42 MODULE_PARM_DESC(enable
, "Enable tm6000x soundcard. default enabled.");
44 module_param_array(index
, int, NULL
, 0444);
45 MODULE_PARM_DESC(index
, "Index value for tm6000x capture interface(s).");
48 /****************************************************************************
50 ****************************************************************************/
52 MODULE_DESCRIPTION("ALSA driver module for tm5600/tm6000/tm6010 based TV cards");
53 MODULE_AUTHOR("Mauro Carvalho Chehab");
54 MODULE_LICENSE("GPL v2");
55 MODULE_SUPPORTED_DEVICE("{{Trident,tm5600},{{Trident,tm6000},{{Trident,tm6010}");
56 static unsigned int debug
;
57 module_param(debug
, int, 0644);
58 MODULE_PARM_DESC(debug
, "enable debug messages");
60 /****************************************************************************
61 Module specific funtions
62 ****************************************************************************/
65 * BOARD Specific: Sets audio DMA
68 static int _tm6000_start_audio_dma(struct snd_tm6000_card
*chip
)
70 struct tm6000_core
*core
= chip
->core
;
72 dprintk(1, "Starting audio DMA\n");
75 tm6000_set_reg_mask(core
, TM6010_REQ07_RCC_ACTIVE_IF
, 0x40, 0x40);
77 tm6000_set_audio_bitrate(core
, 48000);
83 * BOARD Specific: Resets audio DMA
85 static int _tm6000_stop_audio_dma(struct snd_tm6000_card
*chip
)
87 struct tm6000_core
*core
= chip
->core
;
89 dprintk(1, "Stopping audio DMA\n");
92 tm6000_set_reg_mask(core
, TM6010_REQ07_RCC_ACTIVE_IF
, 0x00, 0x40);
97 static void dsp_buffer_free(struct snd_pcm_substream
*substream
)
99 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
101 dprintk(2, "Freeing buffer\n");
103 vfree(substream
->runtime
->dma_area
);
104 substream
->runtime
->dma_area
= NULL
;
105 substream
->runtime
->dma_bytes
= 0;
108 static int dsp_buffer_alloc(struct snd_pcm_substream
*substream
, int size
)
110 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
112 dprintk(2, "Allocating buffer\n");
114 if (substream
->runtime
->dma_area
) {
115 if (substream
->runtime
->dma_bytes
> size
)
118 dsp_buffer_free(substream
);
121 substream
->runtime
->dma_area
= vmalloc(size
);
122 if (!substream
->runtime
->dma_area
)
125 substream
->runtime
->dma_bytes
= size
;
131 /****************************************************************************
133 ****************************************************************************/
136 * Digital hardware definition
138 #define DEFAULT_FIFO_SIZE 4096
140 static const struct snd_pcm_hardware snd_tm6000_digital_hw
= {
141 .info
= SNDRV_PCM_INFO_BATCH
|
142 SNDRV_PCM_INFO_MMAP
|
143 SNDRV_PCM_INFO_INTERLEAVED
|
144 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
145 SNDRV_PCM_INFO_MMAP_VALID
,
146 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
148 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_KNOT
,
153 .period_bytes_min
= 64,
154 .period_bytes_max
= 12544,
157 .buffer_bytes_max
= 62720 * 8,
161 * audio pcm capture open callback
163 static int snd_tm6000_pcm_open(struct snd_pcm_substream
*substream
)
165 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
166 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
169 err
= snd_pcm_hw_constraint_pow2(runtime
, 0,
170 SNDRV_PCM_HW_PARAM_PERIODS
);
174 chip
->substream
= substream
;
176 runtime
->hw
= snd_tm6000_digital_hw
;
177 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
181 dprintk(1, "Error opening PCM!\n");
186 * audio close callback
188 static int snd_tm6000_close(struct snd_pcm_substream
*substream
)
190 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
191 struct tm6000_core
*core
= chip
->core
;
193 if (atomic_read(&core
->stream_started
) > 0) {
194 atomic_set(&core
->stream_started
, 0);
195 schedule_work(&core
->wq_trigger
);
201 static int tm6000_fillbuf(struct tm6000_core
*core
, char *buf
, int size
)
203 struct snd_tm6000_card
*chip
= core
->adev
;
204 struct snd_pcm_substream
*substream
= chip
->substream
;
205 struct snd_pcm_runtime
*runtime
;
206 int period_elapsed
= 0;
207 unsigned int stride
, buf_pos
;
210 if (atomic_read(&core
->stream_started
) == 0)
213 if (!size
|| !substream
) {
214 dprintk(1, "substream was NULL\n");
218 runtime
= substream
->runtime
;
219 if (!runtime
|| !runtime
->dma_area
) {
220 dprintk(1, "runtime was NULL\n");
224 buf_pos
= chip
->buf_pos
;
225 stride
= runtime
->frame_bits
>> 3;
228 dprintk(1, "stride is zero\n");
232 length
= size
/ stride
;
234 dprintk(1, "%s: length was zero\n", __func__
);
238 dprintk(1, "Copying %d bytes at %p[%d] - buf size=%d x %d\n", size
,
239 runtime
->dma_area
, buf_pos
,
240 (unsigned int)runtime
->buffer_size
, stride
);
242 if (buf_pos
+ length
>= runtime
->buffer_size
) {
243 unsigned int cnt
= runtime
->buffer_size
- buf_pos
;
244 memcpy(runtime
->dma_area
+ buf_pos
* stride
, buf
, cnt
* stride
);
245 memcpy(runtime
->dma_area
, buf
+ cnt
* stride
,
246 length
* stride
- cnt
* stride
);
248 memcpy(runtime
->dma_area
+ buf_pos
* stride
, buf
,
251 snd_pcm_stream_lock(substream
);
253 chip
->buf_pos
+= length
;
254 if (chip
->buf_pos
>= runtime
->buffer_size
)
255 chip
->buf_pos
-= runtime
->buffer_size
;
257 chip
->period_pos
+= length
;
258 if (chip
->period_pos
>= runtime
->period_size
) {
259 chip
->period_pos
-= runtime
->period_size
;
263 snd_pcm_stream_unlock(substream
);
266 snd_pcm_period_elapsed(substream
);
274 static int snd_tm6000_hw_params(struct snd_pcm_substream
*substream
,
275 struct snd_pcm_hw_params
*hw_params
)
279 size
= params_period_bytes(hw_params
) * params_periods(hw_params
);
281 rc
= dsp_buffer_alloc(substream
, size
);
291 static int snd_tm6000_hw_free(struct snd_pcm_substream
*substream
)
293 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
294 struct tm6000_core
*core
= chip
->core
;
296 if (atomic_read(&core
->stream_started
) > 0) {
297 atomic_set(&core
->stream_started
, 0);
298 schedule_work(&core
->wq_trigger
);
301 dsp_buffer_free(substream
);
308 static int snd_tm6000_prepare(struct snd_pcm_substream
*substream
)
310 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
313 chip
->period_pos
= 0;
322 static void audio_trigger(struct work_struct
*work
)
324 struct tm6000_core
*core
= container_of(work
, struct tm6000_core
,
326 struct snd_tm6000_card
*chip
= core
->adev
;
328 if (atomic_read(&core
->stream_started
)) {
329 dprintk(1, "starting capture");
330 _tm6000_start_audio_dma(chip
);
332 dprintk(1, "stopping capture");
333 _tm6000_stop_audio_dma(chip
);
337 static int snd_tm6000_card_trigger(struct snd_pcm_substream
*substream
, int cmd
)
339 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
340 struct tm6000_core
*core
= chip
->core
;
344 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
: /* fall through */
345 case SNDRV_PCM_TRIGGER_RESUME
: /* fall through */
346 case SNDRV_PCM_TRIGGER_START
:
347 atomic_set(&core
->stream_started
, 1);
349 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
: /* fall through */
350 case SNDRV_PCM_TRIGGER_SUSPEND
: /* fall through */
351 case SNDRV_PCM_TRIGGER_STOP
:
352 atomic_set(&core
->stream_started
, 0);
358 schedule_work(&core
->wq_trigger
);
365 static snd_pcm_uframes_t
snd_tm6000_pointer(struct snd_pcm_substream
*substream
)
367 struct snd_tm6000_card
*chip
= snd_pcm_substream_chip(substream
);
369 return chip
->buf_pos
;
372 static struct page
*snd_pcm_get_vmalloc_page(struct snd_pcm_substream
*subs
,
373 unsigned long offset
)
375 void *pageptr
= subs
->runtime
->dma_area
+ offset
;
377 return vmalloc_to_page(pageptr
);
383 static const 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
,
392 .page
= snd_pcm_get_vmalloc_page
,
396 * create a PCM device
399 /* FIXME: Control interface - How to control volume/mute? */
401 /****************************************************************************
402 Basic Flow for Sound Devices
403 ****************************************************************************/
406 * Alsa Constructor - Component probe
408 static int tm6000_audio_init(struct tm6000_core
*dev
)
410 struct snd_card
*card
;
411 struct snd_tm6000_card
*chip
;
420 if (devnr
>= SNDRV_CARDS
)
426 rc
= snd_card_new(&dev
->udev
->dev
, index
[devnr
], "tm6000",
427 THIS_MODULE
, 0, &card
);
429 snd_printk(KERN_ERR
"cannot create card instance %d\n", devnr
);
432 strcpy(card
->driver
, "tm6000-alsa");
433 strcpy(card
->shortname
, "TM5600/60x0");
434 sprintf(card
->longname
, "TM5600/60x0 Audio at bus %d device %d",
435 dev
->udev
->bus
->busnum
, dev
->udev
->devnum
);
437 sprintf(component
, "USB%04x:%04x",
438 le16_to_cpu(dev
->udev
->descriptor
.idVendor
),
439 le16_to_cpu(dev
->udev
->descriptor
.idProduct
));
440 snd_component_add(card
, component
);
442 chip
= kzalloc(sizeof(struct snd_tm6000_card
), GFP_KERNEL
);
451 spin_lock_init(&chip
->reg_lock
);
453 rc
= snd_pcm_new(card
, "TM6000 Audio", 0, 0, 1, &pcm
);
458 pcm
->private_data
= chip
;
459 strcpy(pcm
->name
, "Trident TM5600/60x0");
461 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_tm6000_pcm_ops
);
463 INIT_WORK(&dev
->wq_trigger
, audio_trigger
);
464 rc
= snd_card_register(card
);
468 dprintk(1, "Registered audio driver for %s\n", card
->longname
);
480 static int tm6000_audio_fini(struct tm6000_core
*dev
)
482 struct snd_tm6000_card
*chip
;
494 snd_card_free(chip
->card
);
502 static struct tm6000_ops audio_ops
= {
503 .type
= TM6000_AUDIO
,
504 .name
= "TM6000 Audio Extension",
505 .init
= tm6000_audio_init
,
506 .fini
= tm6000_audio_fini
,
507 .fillbuf
= tm6000_fillbuf
,
510 static int __init
tm6000_alsa_register(void)
512 return tm6000_register_extension(&audio_ops
);
515 static void __exit
tm6000_alsa_unregister(void)
517 tm6000_unregister_extension(&audio_ops
);
520 module_init(tm6000_alsa_register
);
521 module_exit(tm6000_alsa_unregister
);