1 // SPDX-License-Identifier: GPL-2.0
2 // Support for audio capture for tm5600/6000/6010
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>
14 #include <linux/delay.h>
15 #include <sound/core.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
18 #include <sound/control.h>
19 #include <sound/initval.h>
23 #include "tm6000-regs.h"
27 #define dprintk(level, fmt, arg...) do { \
29 printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg); \
32 /****************************************************************************
33 Module global static vars
34 ****************************************************************************/
36 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
38 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
40 module_param_array(enable, bool, NULL, 0444);
41 MODULE_PARM_DESC(enable, "Enable tm6000x soundcard. default enabled.");
43 module_param_array(index, int, NULL, 0444);
44 MODULE_PARM_DESC(index, "Index value for tm6000x capture interface(s).");
47 /****************************************************************************
49 ****************************************************************************/
51 MODULE_DESCRIPTION("ALSA driver module for tm5600/tm6000/tm6010 based TV cards");
52 MODULE_AUTHOR("Mauro Carvalho Chehab");
53 MODULE_LICENSE("GPL v2");
54 MODULE_SUPPORTED_DEVICE("{{Trident,tm5600},{{Trident,tm6000},{{Trident,tm6010}");
55 static unsigned int debug;
56 module_param(debug, int, 0644);
57 MODULE_PARM_DESC(debug, "enable debug messages");
59 /****************************************************************************
60 Module specific functions
61 ****************************************************************************/
64 * BOARD Specific: Sets audio DMA
67 static int _tm6000_start_audio_dma(struct snd_tm6000_card *chip)
69 struct tm6000_core *core = chip->core;
71 dprintk(1, "Starting audio DMA\n");
74 tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x40, 0x40);
76 tm6000_set_audio_bitrate(core, 48000);
82 * BOARD Specific: Resets audio DMA
84 static int _tm6000_stop_audio_dma(struct snd_tm6000_card *chip)
86 struct tm6000_core *core = chip->core;
88 dprintk(1, "Stopping audio DMA\n");
91 tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x00, 0x40);
96 /****************************************************************************
98 ****************************************************************************/
101 * Digital hardware definition
103 #define DEFAULT_FIFO_SIZE 4096
105 static const struct snd_pcm_hardware snd_tm6000_digital_hw = {
106 .info = SNDRV_PCM_INFO_BATCH |
107 SNDRV_PCM_INFO_MMAP |
108 SNDRV_PCM_INFO_INTERLEAVED |
109 SNDRV_PCM_INFO_BLOCK_TRANSFER |
110 SNDRV_PCM_INFO_MMAP_VALID,
111 .formats = SNDRV_PCM_FMTBIT_S16_LE,
113 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
118 .period_bytes_min = 64,
119 .period_bytes_max = 12544,
122 .buffer_bytes_max = 62720 * 8,
126 * audio pcm capture open callback
128 static int snd_tm6000_pcm_open(struct snd_pcm_substream *substream)
130 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
131 struct snd_pcm_runtime *runtime = substream->runtime;
134 err = snd_pcm_hw_constraint_pow2(runtime, 0,
135 SNDRV_PCM_HW_PARAM_PERIODS);
139 chip->substream = substream;
141 runtime->hw = snd_tm6000_digital_hw;
142 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
146 dprintk(1, "Error opening PCM!\n");
151 * audio close callback
153 static int snd_tm6000_close(struct snd_pcm_substream *substream)
155 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
156 struct tm6000_core *core = chip->core;
158 if (atomic_read(&core->stream_started) > 0) {
159 atomic_set(&core->stream_started, 0);
160 schedule_work(&core->wq_trigger);
166 static int tm6000_fillbuf(struct tm6000_core *core, char *buf, int size)
168 struct snd_tm6000_card *chip = core->adev;
169 struct snd_pcm_substream *substream = chip->substream;
170 struct snd_pcm_runtime *runtime;
171 int period_elapsed = 0;
172 unsigned int stride, buf_pos;
175 if (atomic_read(&core->stream_started) == 0)
178 if (!size || !substream) {
179 dprintk(1, "substream was NULL\n");
183 runtime = substream->runtime;
184 if (!runtime || !runtime->dma_area) {
185 dprintk(1, "runtime was NULL\n");
189 buf_pos = chip->buf_pos;
190 stride = runtime->frame_bits >> 3;
193 dprintk(1, "stride is zero\n");
197 length = size / stride;
199 dprintk(1, "%s: length was zero\n", __func__);
203 dprintk(1, "Copying %d bytes at %p[%d] - buf size=%d x %d\n", size,
204 runtime->dma_area, buf_pos,
205 (unsigned int)runtime->buffer_size, stride);
207 if (buf_pos + length >= runtime->buffer_size) {
208 unsigned int cnt = runtime->buffer_size - buf_pos;
209 memcpy(runtime->dma_area + buf_pos * stride, buf, cnt * stride);
210 memcpy(runtime->dma_area, buf + cnt * stride,
211 length * stride - cnt * stride);
213 memcpy(runtime->dma_area + buf_pos * stride, buf,
216 snd_pcm_stream_lock(substream);
218 chip->buf_pos += length;
219 if (chip->buf_pos >= runtime->buffer_size)
220 chip->buf_pos -= runtime->buffer_size;
222 chip->period_pos += length;
223 if (chip->period_pos >= runtime->period_size) {
224 chip->period_pos -= runtime->period_size;
228 snd_pcm_stream_unlock(substream);
231 snd_pcm_period_elapsed(substream);
239 static int snd_tm6000_prepare(struct snd_pcm_substream *substream)
241 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
244 chip->period_pos = 0;
253 static void audio_trigger(struct work_struct *work)
255 struct tm6000_core *core = container_of(work, struct tm6000_core,
257 struct snd_tm6000_card *chip = core->adev;
259 if (atomic_read(&core->stream_started)) {
260 dprintk(1, "starting capture");
261 _tm6000_start_audio_dma(chip);
263 dprintk(1, "stopping capture");
264 _tm6000_stop_audio_dma(chip);
268 static int snd_tm6000_card_trigger(struct snd_pcm_substream *substream, int cmd)
270 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
271 struct tm6000_core *core = chip->core;
275 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
276 case SNDRV_PCM_TRIGGER_RESUME:
277 case SNDRV_PCM_TRIGGER_START:
278 atomic_set(&core->stream_started, 1);
280 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
281 case SNDRV_PCM_TRIGGER_SUSPEND:
282 case SNDRV_PCM_TRIGGER_STOP:
283 atomic_set(&core->stream_started, 0);
289 schedule_work(&core->wq_trigger);
296 static snd_pcm_uframes_t snd_tm6000_pointer(struct snd_pcm_substream *substream)
298 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
300 return chip->buf_pos;
306 static const struct snd_pcm_ops snd_tm6000_pcm_ops = {
307 .open = snd_tm6000_pcm_open,
308 .close = snd_tm6000_close,
309 .prepare = snd_tm6000_prepare,
310 .trigger = snd_tm6000_card_trigger,
311 .pointer = snd_tm6000_pointer,
315 * create a PCM device
318 /* FIXME: Control interface - How to control volume/mute? */
320 /****************************************************************************
321 Basic Flow for Sound Devices
322 ****************************************************************************/
325 * Alsa Constructor - Component probe
327 static int tm6000_audio_init(struct tm6000_core *dev)
329 struct snd_card *card;
330 struct snd_tm6000_card *chip;
339 if (devnr >= SNDRV_CARDS)
345 rc = snd_card_new(&dev->udev->dev, index[devnr], "tm6000",
346 THIS_MODULE, 0, &card);
348 snd_printk(KERN_ERR "cannot create card instance %d\n", devnr);
351 strscpy(card->driver, "tm6000-alsa", sizeof(card->driver));
352 strscpy(card->shortname, "TM5600/60x0", sizeof(card->shortname));
353 sprintf(card->longname, "TM5600/60x0 Audio at bus %d device %d",
354 dev->udev->bus->busnum, dev->udev->devnum);
356 sprintf(component, "USB%04x:%04x",
357 le16_to_cpu(dev->udev->descriptor.idVendor),
358 le16_to_cpu(dev->udev->descriptor.idProduct));
359 snd_component_add(card, component);
361 chip = kzalloc(sizeof(struct snd_tm6000_card), GFP_KERNEL);
370 spin_lock_init(&chip->reg_lock);
372 rc = snd_pcm_new(card, "TM6000 Audio", 0, 0, 1, &pcm);
377 pcm->private_data = chip;
378 strscpy(pcm->name, "Trident TM5600/60x0", sizeof(pcm->name));
380 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_tm6000_pcm_ops);
381 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
383 INIT_WORK(&dev->wq_trigger, audio_trigger);
384 rc = snd_card_register(card);
388 dprintk(1, "Registered audio driver for %s\n", card->longname);
400 static int tm6000_audio_fini(struct tm6000_core *dev)
402 struct snd_tm6000_card *chip;
414 snd_card_free(chip->card);
422 static struct tm6000_ops audio_ops = {
423 .type = TM6000_AUDIO,
424 .name = "TM6000 Audio Extension",
425 .init = tm6000_audio_init,
426 .fini = tm6000_audio_fini,
427 .fillbuf = tm6000_fillbuf,
430 static int __init tm6000_alsa_register(void)
432 return tm6000_register_extension(&audio_ops);
435 static void __exit tm6000_alsa_unregister(void)
437 tm6000_unregister_extension(&audio_ops);
440 module_init(tm6000_alsa_register);
441 module_exit(tm6000_alsa_unregister);