tm6000-alsa.c 12.0 KB
Newer Older
1 2
/*
 *
3
 *  Support for audio capture for tm5600/6000/6010
4
 *    (c) 2007-2008 Mauro Carvalho Chehab
5
 *
6
 *  Based on cx88-alsa.c
7 8
 *
 *  This program is free software; you can redistribute it and/or modify
9 10
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
11 12 13 14 15 16
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/interrupt.h>
17
#include <linux/usb.h>
T
Tejun Heo 已提交
18
#include <linux/slab.h>
19
#include <linux/vmalloc.h>
20

21
#include <linux/delay.h>
22 23 24 25 26 27 28
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/control.h>
#include <sound/initval.h>


29 30 31 32
#include "tm6000.h"
#include "tm6000-regs.h"

#undef dprintk
33

34 35 36 37
#define dprintk(level, fmt, arg...) do {				   \
	if (debug >= level)						   \
		printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg); \
	} while (0)
38 39 40 41 42 43

/****************************************************************************
			Module global static vars
 ****************************************************************************/

static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
44

45
static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
46 47

module_param_array(enable, bool, NULL, 0444);
48
MODULE_PARM_DESC(enable, "Enable tm6000x soundcard. default enabled.");
49 50

module_param_array(index, int, NULL, 0444);
51
MODULE_PARM_DESC(index, "Index value for tm6000x capture interface(s).");
52 53 54 55 56 57


/****************************************************************************
				Module macros
 ****************************************************************************/

58
MODULE_DESCRIPTION("ALSA driver module for tm5600/tm6000/tm6010 based TV cards");
59
MODULE_AUTHOR("Mauro Carvalho Chehab");
60
MODULE_LICENSE("GPL");
61
MODULE_SUPPORTED_DEVICE("{{Trident,tm5600},"
62 63
			"{{Trident,tm6000},"
			"{{Trident,tm6010}");
64
static unsigned int debug;
65 66
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "enable debug messages");
67 68 69 70 71 72 73 74 75

/****************************************************************************
			Module specific funtions
 ****************************************************************************/

/*
 * BOARD Specific: Sets audio DMA
 */

76
static int _tm6000_start_audio_dma(struct snd_tm6000_card *chip)
77
{
78
	struct tm6000_core *core = chip->core;
79

80 81
	dprintk(1, "Starting audio DMA\n");

82
	/* Enables audio */
83
	tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x40, 0x40);
84

85 86
	tm6000_set_audio_bitrate(core, 48000);

87 88 89 90 91 92
	return 0;
}

/*
 * BOARD Specific: Resets audio DMA
 */
93
static int _tm6000_stop_audio_dma(struct snd_tm6000_card *chip)
94
{
95
	struct tm6000_core *core = chip->core;
96

97 98
	dprintk(1, "Stopping audio DMA\n");

99
	/* Disables audio */
100
	tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x00, 0x40);
101 102 103 104

	return 0;
}

105
static void dsp_buffer_free(struct snd_pcm_substream *substream)
106
{
107
	struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
108

109
	dprintk(2, "Freeing buffer\n");
110

111 112 113 114 115 116 117 118 119 120
	vfree(substream->runtime->dma_area);
	substream->runtime->dma_area = NULL;
	substream->runtime->dma_bytes = 0;
}

static int dsp_buffer_alloc(struct snd_pcm_substream *substream, int size)
{
	struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);

	dprintk(2, "Allocating buffer\n");
121

122 123 124
	if (substream->runtime->dma_area) {
		if (substream->runtime->dma_bytes > size)
			return 0;
125

126 127 128 129 130 131
		dsp_buffer_free(substream);
	}

	substream->runtime->dma_area = vmalloc(size);
	if (!substream->runtime->dma_area)
		return -ENOMEM;
132

133 134 135
	substream->runtime->dma_bytes = size;

	return 0;
136 137
}

138

139 140 141 142 143 144 145 146
/****************************************************************************
				ALSA PCM Interface
 ****************************************************************************/

/*
 * Digital hardware definition
 */
#define DEFAULT_FIFO_SIZE	4096
147 148

static struct snd_pcm_hardware snd_tm6000_digital_hw = {
149 150
	.info = SNDRV_PCM_INFO_BATCH |
		SNDRV_PCM_INFO_MMAP |
151 152 153 154 155
		SNDRV_PCM_INFO_INTERLEAVED |
		SNDRV_PCM_INFO_BLOCK_TRANSFER |
		SNDRV_PCM_INFO_MMAP_VALID,
	.formats = SNDRV_PCM_FMTBIT_S16_LE,

156
	.rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
157 158
	.rate_min = 48000,
	.rate_max = 48000,
159 160
	.channels_min = 2,
	.channels_max = 2,
D
Dmitri Belimov 已提交
161 162
	.period_bytes_min = 64,
	.period_bytes_max = 12544,
163
	.periods_min = 2,
D
Dmitri Belimov 已提交
164
	.periods_max = 98,
165
	.buffer_bytes_max = 62720 * 8,
166 167 168 169 170
};

/*
 * audio pcm capture open callback
 */
171
static int snd_tm6000_pcm_open(struct snd_pcm_substream *substream)
172
{
173
	struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
174 175 176
	struct snd_pcm_runtime *runtime = substream->runtime;
	int err;

177 178
	err = snd_pcm_hw_constraint_pow2(runtime, 0,
					 SNDRV_PCM_HW_PARAM_PERIODS);
179 180 181 182 183
	if (err < 0)
		goto _error;

	chip->substream = substream;

184
	runtime->hw = snd_tm6000_digital_hw;
185
	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
186 187 188

	return 0;
_error:
189
	dprintk(1, "Error opening PCM!\n");
190 191 192 193 194 195
	return err;
}

/*
 * audio close callback
 */
196
static int snd_tm6000_close(struct snd_pcm_substream *substream)
197
{
198 199 200 201 202 203 204 205
	struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
	struct tm6000_core *core = chip->core;

	if (atomic_read(&core->stream_started) > 0) {
		atomic_set(&core->stream_started, 0);
		schedule_work(&core->wq_trigger);
	}

206 207 208
	return 0;
}

209 210
static int tm6000_fillbuf(struct tm6000_core *core, char *buf, int size)
{
211 212 213 214 215
	struct snd_tm6000_card *chip = core->adev;
	struct snd_pcm_substream *substream = chip->substream;
	struct snd_pcm_runtime *runtime;
	int period_elapsed = 0;
	unsigned int stride, buf_pos;
D
Dmitri Belimov 已提交
216
	int length;
217

218 219 220
	if (atomic_read(&core->stream_started) == 0)
		return 0;

D
Dmitri Belimov 已提交
221 222
	if (!size || !substream) {
		dprintk(1, "substream was NULL\n");
223
		return -EINVAL;
D
Dmitri Belimov 已提交
224
	}
225 226

	runtime = substream->runtime;
D
Dmitri Belimov 已提交
227 228
	if (!runtime || !runtime->dma_area) {
		dprintk(1, "runtime was NULL\n");
229
		return -EINVAL;
D
Dmitri Belimov 已提交
230
	}
231 232 233 234

	buf_pos = chip->buf_pos;
	stride = runtime->frame_bits >> 3;

D
Dmitri Belimov 已提交
235 236 237 238 239 240 241 242 243 244 245
	if (stride == 0) {
		dprintk(1, "stride is zero\n");
		return -EINVAL;
	}

	length = size / stride;
	if (length == 0) {
		dprintk(1, "%s: length was zero\n", __func__);
		return -EINVAL;
	}

246 247 248 249
	dprintk(1, "Copying %d bytes at %p[%d] - buf size=%d x %d\n", size,
		runtime->dma_area, buf_pos,
		(unsigned int)runtime->buffer_size, stride);

D
Dmitri Belimov 已提交
250 251 252 253 254
	if (buf_pos + length >= runtime->buffer_size) {
		unsigned int cnt = runtime->buffer_size - buf_pos;
		memcpy(runtime->dma_area + buf_pos * stride, buf, cnt * stride);
		memcpy(runtime->dma_area, buf + cnt * stride,
			length * stride - cnt * stride);
255
	} else
D
Dmitri Belimov 已提交
256 257
		memcpy(runtime->dma_area + buf_pos * stride, buf,
			length * stride);
258

259
	snd_pcm_stream_lock(substream);
260

D
Dmitri Belimov 已提交
261 262 263 264 265
	chip->buf_pos += length;
	if (chip->buf_pos >= runtime->buffer_size)
		chip->buf_pos -= runtime->buffer_size;

	chip->period_pos += length;
266 267 268 269
	if (chip->period_pos >= runtime->period_size) {
		chip->period_pos -= runtime->period_size;
		period_elapsed = 1;
	}
270

271
	snd_pcm_stream_unlock(substream);
D
Dmitri Belimov 已提交
272

273 274
	if (period_elapsed)
		snd_pcm_period_elapsed(substream);
275 276 277 278

	return 0;
}

279 280 281
/*
 * hw_params callback
 */
282 283
static int snd_tm6000_hw_params(struct snd_pcm_substream *substream,
			      struct snd_pcm_hw_params *hw_params)
284
{
285
	int size, rc;
286

287
	size = params_period_bytes(hw_params) * params_periods(hw_params);
288

289 290 291
	rc = dsp_buffer_alloc(substream, size);
	if (rc < 0)
		return rc;
292 293

	return 0;
294 295 296 297 298
}

/*
 * hw free callback
 */
299
static int snd_tm6000_hw_free(struct snd_pcm_substream *substream)
300
{
301
	struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
302
	struct tm6000_core *core = chip->core;
303

304 305 306 307
	if (atomic_read(&core->stream_started) > 0) {
		atomic_set(&core->stream_started, 0);
		schedule_work(&core->wq_trigger);
	}
308

309
	dsp_buffer_free(substream);
310 311 312 313 314 315
	return 0;
}

/*
 * prepare callback
 */
316
static int snd_tm6000_prepare(struct snd_pcm_substream *substream)
317
{
318 319 320 321 322
	struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);

	chip->buf_pos = 0;
	chip->period_pos = 0;

323 324 325
	return 0;
}

326

327 328 329
/*
 * trigger callback
 */
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
static void audio_trigger(struct work_struct *work)
{
	struct tm6000_core *core = container_of(work, struct tm6000_core,
						wq_trigger);
	struct snd_tm6000_card *chip = core->adev;

	if (atomic_read(&core->stream_started)) {
		dprintk(1, "starting capture");
		_tm6000_start_audio_dma(chip);
	} else {
		dprintk(1, "stopping capture");
		_tm6000_stop_audio_dma(chip);
	}
}

345
static int snd_tm6000_card_trigger(struct snd_pcm_substream *substream, int cmd)
346
{
347
	struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
348 349
	struct tm6000_core *core = chip->core;
	int err = 0;
350 351

	switch (cmd) {
352 353
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
	case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
354
	case SNDRV_PCM_TRIGGER_START:
355
		atomic_set(&core->stream_started, 1);
356
		break;
357 358
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
	case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
359
	case SNDRV_PCM_TRIGGER_STOP:
360
		atomic_set(&core->stream_started, 0);
361 362
		break;
	default:
363
		err = -EINVAL;
364 365
		break;
	}
366
	schedule_work(&core->wq_trigger);
367 368 369 370 371 372

	return err;
}
/*
 * pointer callback
 */
373
static snd_pcm_uframes_t snd_tm6000_pointer(struct snd_pcm_substream *substream)
374
{
375
	struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
376

377
	return chip->buf_pos;
378 379
}

380 381 382 383 384 385 386 387
static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
					     unsigned long offset)
{
	void *pageptr = subs->runtime->dma_area + offset;

	return vmalloc_to_page(pageptr);
}

388 389 390
/*
 * operators
 */
391 392 393
static struct snd_pcm_ops snd_tm6000_pcm_ops = {
	.open = snd_tm6000_pcm_open,
	.close = snd_tm6000_close,
394
	.ioctl = snd_pcm_lib_ioctl,
395 396 397 398 399
	.hw_params = snd_tm6000_hw_params,
	.hw_free = snd_tm6000_hw_free,
	.prepare = snd_tm6000_prepare,
	.trigger = snd_tm6000_card_trigger,
	.pointer = snd_tm6000_pointer,
400
	.page = snd_pcm_get_vmalloc_page,
401 402 403 404 405 406
};

/*
 * create a PCM device
 */

407
/* FIXME: Control interface - How to control volume/mute? */
408 409 410 411 412 413 414 415

/****************************************************************************
			Basic Flow for Sound Devices
 ****************************************************************************/

/*
 * Alsa Constructor - Component probe
 */
416
static int tm6000_audio_init(struct tm6000_core *dev)
417
{
418 419 420 421 422 423 424 425 426
	struct snd_card		*card;
	struct snd_tm6000_card	*chip;
	int			rc;
	static int		devnr;
	char			component[14];
	struct snd_pcm		*pcm;

	if (!dev)
		return 0;
427

428
	if (devnr >= SNDRV_CARDS)
429 430
		return -ENODEV;

431
	if (!enable[devnr])
432 433
		return -ENOENT;

434
	rc = snd_card_create(index[devnr], "tm6000", THIS_MODULE, 0, &card);
435
	if (rc < 0) {
436
		snd_printk(KERN_ERR "cannot create card instance %d\n", devnr);
437
		return rc;
438
	}
439 440 441 442 443 444 445 446 447 448
	strcpy(card->driver, "tm6000-alsa");
	strcpy(card->shortname, "TM5600/60x0");
	sprintf(card->longname, "TM5600/60x0 Audio at bus %d device %d",
		dev->udev->bus->busnum, dev->udev->devnum);

	sprintf(component, "USB%04x:%04x",
		le16_to_cpu(dev->udev->descriptor.idVendor),
		le16_to_cpu(dev->udev->descriptor.idProduct));
	snd_component_add(card, component);
	snd_card_set_dev(card, &dev->udev->dev);
449

450
	chip = kzalloc(sizeof(struct snd_tm6000_card), GFP_KERNEL);
451 452 453 454 455
	if (!chip) {
		rc = -ENOMEM;
		goto error;
	}

456 457 458
	chip->core = dev;
	chip->card = card;
	dev->adev = chip;
459
	spin_lock_init(&chip->reg_lock);
460

461
	rc = snd_pcm_new(card, "TM6000 Audio", 0, 0, 1, &pcm);
462
	if (rc < 0)
463
		goto error_chip;
464

465
	pcm->info_flags = 0;
466
	pcm->private_data = chip;
467 468
	strcpy(pcm->name, "Trident TM5600/60x0");

469
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_tm6000_pcm_ops);
470

471
	INIT_WORK(&dev->wq_trigger, audio_trigger);
472 473
	rc = snd_card_register(card);
	if (rc < 0)
474
		goto error_chip;
475

476
	dprintk(1, "Registered audio driver for %s\n", card->longname);
477 478 479

	return 0;

480 481 482
error_chip:
	kfree(chip);
	dev->adev = NULL;
483 484 485
error:
	snd_card_free(card);
	return rc;
486 487
}

488 489
static int tm6000_audio_fini(struct tm6000_core *dev)
{
490
	struct snd_tm6000_card *chip;
491 492 493

	if (!dev)
		return 0;
494
	chip = dev->adev;
495 496 497 498 499 500 501 502 503 504 505 506

	if (!chip)
		return 0;

	if (!chip->card)
		return 0;

	snd_card_free(chip->card);
	chip->card = NULL;
	kfree(chip);
	dev->adev = NULL;

507 508 509
	return 0;
}

510
static struct tm6000_ops audio_ops = {
511
	.type	= TM6000_AUDIO,
512 513 514
	.name	= "TM6000 Audio Extension",
	.init	= tm6000_audio_init,
	.fini	= tm6000_audio_fini,
515
	.fillbuf = tm6000_fillbuf,
516 517 518 519 520 521 522 523 524 525 526 527 528 529
};

static int __init tm6000_alsa_register(void)
{
	return tm6000_register_extension(&audio_ops);
}

static void __exit tm6000_alsa_unregister(void)
{
	tm6000_unregister_extension(&audio_ops);
}

module_init(tm6000_alsa_register);
module_exit(tm6000_alsa_unregister);