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>
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 static unsigned int debug;
55 module_param(debug, int, 0644);
56 MODULE_PARM_DESC(debug, "enable debug messages");
58 /****************************************************************************
59 Module specific functions
60 ****************************************************************************/
63 * BOARD Specific: Sets audio DMA
66 static int _tm6000_start_audio_dma(struct snd_tm6000_card *chip)
68 struct tm6000_core *core = chip->core;
70 dprintk(1, "Starting audio DMA\n");
73 tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x40, 0x40);
75 tm6000_set_audio_bitrate(core, 48000);
81 * BOARD Specific: Resets audio DMA
83 static int _tm6000_stop_audio_dma(struct snd_tm6000_card *chip)
85 struct tm6000_core *core = chip->core;
87 dprintk(1, "Stopping audio DMA\n");
90 tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x00, 0x40);
95 /****************************************************************************
97 ****************************************************************************/
100 * Digital hardware definition
102 #define DEFAULT_FIFO_SIZE 4096
104 static const struct snd_pcm_hardware snd_tm6000_digital_hw = {
105 .info = SNDRV_PCM_INFO_BATCH |
106 SNDRV_PCM_INFO_MMAP |
107 SNDRV_PCM_INFO_INTERLEAVED |
108 SNDRV_PCM_INFO_BLOCK_TRANSFER |
109 SNDRV_PCM_INFO_MMAP_VALID,
110 .formats = SNDRV_PCM_FMTBIT_S16_LE,
112 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
117 .period_bytes_min = 64,
118 .period_bytes_max = 12544,
121 .buffer_bytes_max = 62720 * 8,
125 * audio pcm capture open callback
127 static int snd_tm6000_pcm_open(struct snd_pcm_substream *substream)
129 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
130 struct snd_pcm_runtime *runtime = substream->runtime;
133 err = snd_pcm_hw_constraint_pow2(runtime, 0,
134 SNDRV_PCM_HW_PARAM_PERIODS);
138 chip->substream = substream;
140 runtime->hw = snd_tm6000_digital_hw;
141 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
145 dprintk(1, "Error opening PCM!\n");
150 * audio close callback
152 static int snd_tm6000_close(struct snd_pcm_substream *substream)
154 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
155 struct tm6000_core *core = chip->core;
157 if (atomic_read(&core->stream_started) > 0) {
158 atomic_set(&core->stream_started, 0);
159 schedule_work(&core->wq_trigger);
165 static int tm6000_fillbuf(struct tm6000_core *core, char *buf, int size)
167 struct snd_tm6000_card *chip = core->adev;
168 struct snd_pcm_substream *substream = chip->substream;
169 struct snd_pcm_runtime *runtime;
170 int period_elapsed = 0;
171 unsigned int stride, buf_pos;
174 if (atomic_read(&core->stream_started) == 0)
177 if (!size || !substream) {
178 dprintk(1, "substream was NULL\n");
182 runtime = substream->runtime;
183 if (!runtime || !runtime->dma_area) {
184 dprintk(1, "runtime was NULL\n");
188 buf_pos = chip->buf_pos;
189 stride = runtime->frame_bits >> 3;
192 dprintk(1, "stride is zero\n");
196 length = size / stride;
198 dprintk(1, "%s: length was zero\n", __func__);
202 dprintk(1, "Copying %d bytes at %p[%d] - buf size=%d x %d\n", size,
203 runtime->dma_area, buf_pos,
204 (unsigned int)runtime->buffer_size, stride);
206 if (buf_pos + length >= runtime->buffer_size) {
207 unsigned int cnt = runtime->buffer_size - buf_pos;
208 memcpy(runtime->dma_area + buf_pos * stride, buf, cnt * stride);
209 memcpy(runtime->dma_area, buf + cnt * stride,
210 length * stride - cnt * stride);
212 memcpy(runtime->dma_area + buf_pos * stride, buf,
215 snd_pcm_stream_lock(substream);
217 chip->buf_pos += length;
218 if (chip->buf_pos >= runtime->buffer_size)
219 chip->buf_pos -= runtime->buffer_size;
221 chip->period_pos += length;
222 if (chip->period_pos >= runtime->period_size) {
223 chip->period_pos -= runtime->period_size;
227 snd_pcm_stream_unlock(substream);
230 snd_pcm_period_elapsed(substream);
238 static int snd_tm6000_prepare(struct snd_pcm_substream *substream)
240 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
243 chip->period_pos = 0;
252 static void audio_trigger(struct work_struct *work)
254 struct tm6000_core *core = container_of(work, struct tm6000_core,
256 struct snd_tm6000_card *chip = core->adev;
258 if (atomic_read(&core->stream_started)) {
259 dprintk(1, "starting capture");
260 _tm6000_start_audio_dma(chip);
262 dprintk(1, "stopping capture");
263 _tm6000_stop_audio_dma(chip);
267 static int snd_tm6000_card_trigger(struct snd_pcm_substream *substream, int cmd)
269 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
270 struct tm6000_core *core = chip->core;
274 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
275 case SNDRV_PCM_TRIGGER_RESUME:
276 case SNDRV_PCM_TRIGGER_START:
277 atomic_set(&core->stream_started, 1);
279 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
280 case SNDRV_PCM_TRIGGER_SUSPEND:
281 case SNDRV_PCM_TRIGGER_STOP:
282 atomic_set(&core->stream_started, 0);
288 schedule_work(&core->wq_trigger);
295 static snd_pcm_uframes_t snd_tm6000_pointer(struct snd_pcm_substream *substream)
297 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
299 return chip->buf_pos;
305 static const struct snd_pcm_ops snd_tm6000_pcm_ops = {
306 .open = snd_tm6000_pcm_open,
307 .close = snd_tm6000_close,
308 .prepare = snd_tm6000_prepare,
309 .trigger = snd_tm6000_card_trigger,
310 .pointer = snd_tm6000_pointer,
314 * create a PCM device
317 /* FIXME: Control interface - How to control volume/mute? */
319 /****************************************************************************
320 Basic Flow for Sound Devices
321 ****************************************************************************/
324 * Alsa Constructor - Component probe
326 static int tm6000_audio_init(struct tm6000_core *dev)
328 struct snd_card *card;
329 struct snd_tm6000_card *chip;
338 if (devnr >= SNDRV_CARDS)
344 rc = snd_card_new(&dev->udev->dev, index[devnr], "tm6000",
345 THIS_MODULE, 0, &card);
347 snd_printk(KERN_ERR "cannot create card instance %d\n", devnr);
350 strscpy(card->driver, "tm6000-alsa", sizeof(card->driver));
351 strscpy(card->shortname, "TM5600/60x0", sizeof(card->shortname));
352 sprintf(card->longname, "TM5600/60x0 Audio at bus %d device %d",
353 dev->udev->bus->busnum, dev->udev->devnum);
355 sprintf(component, "USB%04x:%04x",
356 le16_to_cpu(dev->udev->descriptor.idVendor),
357 le16_to_cpu(dev->udev->descriptor.idProduct));
358 snd_component_add(card, component);
360 chip = kzalloc(sizeof(struct snd_tm6000_card), GFP_KERNEL);
369 spin_lock_init(&chip->reg_lock);
371 rc = snd_pcm_new(card, "TM6000 Audio", 0, 0, 1, &pcm);
376 pcm->private_data = chip;
377 strscpy(pcm->name, "Trident TM5600/60x0", sizeof(pcm->name));
379 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_tm6000_pcm_ops);
380 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
382 INIT_WORK(&dev->wq_trigger, audio_trigger);
383 rc = snd_card_register(card);
387 dprintk(1, "Registered audio driver for %s\n", card->longname);
399 static int tm6000_audio_fini(struct tm6000_core *dev)
401 struct snd_tm6000_card *chip;
413 snd_card_free(chip->card);
421 static struct tm6000_ops audio_ops = {
422 .type = TM6000_AUDIO,
423 .name = "TM6000 Audio Extension",
424 .init = tm6000_audio_init,
425 .fini = tm6000_audio_fini,
426 .fillbuf = tm6000_fillbuf,
429 static int __init tm6000_alsa_register(void)
431 return tm6000_register_extension(&audio_ops);
434 static void __exit tm6000_alsa_unregister(void)
436 tm6000_unregister_extension(&audio_ops);
439 module_init(tm6000_alsa_register);
440 module_exit(tm6000_alsa_unregister);