Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / media / usb / tm6000 / tm6000-alsa.c
blob3a2df36ef1db1b127f6720b0d3d1c0a84ed16245
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>
4 //
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>
22 #include "tm6000.h"
23 #include "tm6000-regs.h"
25 #undef dprintk
27 #define dprintk(level, fmt, arg...) do { \
28 if (debug >= level) \
29 printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg); \
30 } while (0)
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 /****************************************************************************
48 Module macros
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");
73 /* Enables audio */
74 tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x40, 0x40);
76 tm6000_set_audio_bitrate(core, 48000);
78 return 0;
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");
90 /* Disables audio */
91 tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x00, 0x40);
93 return 0;
96 /****************************************************************************
97 ALSA PCM Interface
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,
114 .rate_min = 48000,
115 .rate_max = 48000,
116 .channels_min = 2,
117 .channels_max = 2,
118 .period_bytes_min = 64,
119 .period_bytes_max = 12544,
120 .periods_min = 2,
121 .periods_max = 98,
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;
132 int err;
134 err = snd_pcm_hw_constraint_pow2(runtime, 0,
135 SNDRV_PCM_HW_PARAM_PERIODS);
136 if (err < 0)
137 goto _error;
139 chip->substream = substream;
141 runtime->hw = snd_tm6000_digital_hw;
142 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
144 return 0;
145 _error:
146 dprintk(1, "Error opening PCM!\n");
147 return err;
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);
163 return 0;
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;
173 int length;
175 if (atomic_read(&core->stream_started) == 0)
176 return 0;
178 if (!size || !substream) {
179 dprintk(1, "substream was NULL\n");
180 return -EINVAL;
183 runtime = substream->runtime;
184 if (!runtime || !runtime->dma_area) {
185 dprintk(1, "runtime was NULL\n");
186 return -EINVAL;
189 buf_pos = chip->buf_pos;
190 stride = runtime->frame_bits >> 3;
192 if (stride == 0) {
193 dprintk(1, "stride is zero\n");
194 return -EINVAL;
197 length = size / stride;
198 if (length == 0) {
199 dprintk(1, "%s: length was zero\n", __func__);
200 return -EINVAL;
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);
212 } else
213 memcpy(runtime->dma_area + buf_pos * stride, buf,
214 length * stride);
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;
225 period_elapsed = 1;
228 snd_pcm_stream_unlock(substream);
230 if (period_elapsed)
231 snd_pcm_period_elapsed(substream);
233 return 0;
237 * prepare callback
239 static int snd_tm6000_prepare(struct snd_pcm_substream *substream)
241 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
243 chip->buf_pos = 0;
244 chip->period_pos = 0;
246 return 0;
251 * trigger callback
253 static void audio_trigger(struct work_struct *work)
255 struct tm6000_core *core = container_of(work, struct tm6000_core,
256 wq_trigger);
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);
262 } else {
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;
272 int err = 0;
274 switch (cmd) {
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);
279 break;
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);
284 break;
285 default:
286 err = -EINVAL;
287 break;
289 schedule_work(&core->wq_trigger);
291 return err;
294 * pointer callback
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;
304 * operators
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;
331 int rc;
332 static int devnr;
333 char component[14];
334 struct snd_pcm *pcm;
336 if (!dev)
337 return 0;
339 if (devnr >= SNDRV_CARDS)
340 return -ENODEV;
342 if (!enable[devnr])
343 return -ENOENT;
345 rc = snd_card_new(&dev->udev->dev, index[devnr], "tm6000",
346 THIS_MODULE, 0, &card);
347 if (rc < 0) {
348 snd_printk(KERN_ERR "cannot create card instance %d\n", devnr);
349 return rc;
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);
362 if (!chip) {
363 rc = -ENOMEM;
364 goto error;
367 chip->core = dev;
368 chip->card = card;
369 dev->adev = chip;
370 spin_lock_init(&chip->reg_lock);
372 rc = snd_pcm_new(card, "TM6000 Audio", 0, 0, 1, &pcm);
373 if (rc < 0)
374 goto error_chip;
376 pcm->info_flags = 0;
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);
385 if (rc < 0)
386 goto error_chip;
388 dprintk(1, "Registered audio driver for %s\n", card->longname);
390 return 0;
392 error_chip:
393 kfree(chip);
394 dev->adev = NULL;
395 error:
396 snd_card_free(card);
397 return rc;
400 static int tm6000_audio_fini(struct tm6000_core *dev)
402 struct snd_tm6000_card *chip;
404 if (!dev)
405 return 0;
406 chip = dev->adev;
408 if (!chip)
409 return 0;
411 if (!chip->card)
412 return 0;
414 snd_card_free(chip->card);
415 chip->card = NULL;
416 kfree(chip);
417 dev->adev = NULL;
419 return 0;
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);