radio-zoltrix.c 10.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/* zoltrix radio plus driver for Linux radio support
 * (c) 1998 C. van Schaik <carl@leg.uct.ac.za>
 *
4
 * BUGS
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16
 *  Due to the inconsistency in reading from the signal flags
 *  it is difficult to get an accurate tuned signal.
 *
 *  It seems that the card is not linear to 0 volume. It cuts off
 *  at a low volume, and it is not possible (at least I have not found)
 *  to get fine volume control over the low volume range.
 *
 *  Some code derived from code by Romolo Manfredini
 *				   romolo@bicnet.it
 *
 * 1999-05-06 - (C. van Schaik)
 *	      - Make signal strength and stereo scans
17
 *		kinder to cpu while in delay
L
Linus Torvalds 已提交
18 19 20 21 22 23 24 25 26
 * 1999-01-05 - (C. van Schaik)
 *	      - Changed tuning to 1/160Mhz accuracy
 *	      - Added stereo support
 *		(card defaults to stereo)
 *		(can explicitly force mono on the card)
 *		(can detect if station is in stereo)
 *	      - Added unmute function
 *	      - Reworked ioctl functions
 * 2002-07-15 - Fix Stereo typo
27 28 29
 *
 * 2006-07-24 - Converted to V4L2 API
 *		by Mauro Carvalho Chehab <mchehab@infradead.org>
L
Linus Torvalds 已提交
30 31 32 33
 */

#include <linux/module.h>	/* Modules                        */
#include <linux/init.h>		/* Initdata                       */
34
#include <linux/ioport.h>	/* request_region		  */
L
Linus Torvalds 已提交
35
#include <linux/delay.h>	/* udelay, msleep                 */
36
#include <linux/videodev2.h>	/* kernel radio structs           */
37 38 39 40
#include <linux/mutex.h>
#include <linux/version.h>      /* for KERNEL_VERSION MACRO     */
#include <linux/io.h>		/* outb, outb_p                   */
#include <media/v4l2-device.h>
41
#include <media/v4l2-ioctl.h>
L
Linus Torvalds 已提交
42

43 44 45
MODULE_AUTHOR("C.van Schaik");
MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
MODULE_LICENSE("GPL");
46

L
Linus Torvalds 已提交
47 48 49 50 51 52 53
#ifndef CONFIG_RADIO_ZOLTRIX_PORT
#define CONFIG_RADIO_ZOLTRIX_PORT -1
#endif

static int io = CONFIG_RADIO_ZOLTRIX_PORT;
static int radio_nr = -1;

54 55 56 57 58 59 60 61 62 63
module_param(io, int, 0);
MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
module_param(radio_nr, int, 0);

#define RADIO_VERSION KERNEL_VERSION(0, 0, 2)

struct zoltrix {
	struct v4l2_device v4l2_dev;
	struct video_device vdev;
	int io;
L
Linus Torvalds 已提交
64 65 66 67
	int curvol;
	unsigned long curfreq;
	int muted;
	unsigned int stereo;
68
	struct mutex lock;
L
Linus Torvalds 已提交
69 70
};

71 72 73
static struct zoltrix zoltrix_card;

static int zol_setvol(struct zoltrix *zol, int vol)
L
Linus Torvalds 已提交
74
{
75 76
	zol->curvol = vol;
	if (zol->muted)
L
Linus Torvalds 已提交
77 78
		return 0;

79
	mutex_lock(&zol->lock);
L
Linus Torvalds 已提交
80
	if (vol == 0) {
81 82 83 84
		outb(0, zol->io);
		outb(0, zol->io);
		inb(zol->io + 3);    /* Zoltrix needs to be read to confirm */
		mutex_unlock(&zol->lock);
L
Linus Torvalds 已提交
85 86 87
		return 0;
	}

88
	outb(zol->curvol-1, zol->io);
L
Linus Torvalds 已提交
89
	msleep(10);
90 91
	inb(zol->io + 2);
	mutex_unlock(&zol->lock);
L
Linus Torvalds 已提交
92 93 94
	return 0;
}

95
static void zol_mute(struct zoltrix *zol)
L
Linus Torvalds 已提交
96
{
97 98 99 100 101 102
	zol->muted = 1;
	mutex_lock(&zol->lock);
	outb(0, zol->io);
	outb(0, zol->io);
	inb(zol->io + 3);            /* Zoltrix needs to be read to confirm */
	mutex_unlock(&zol->lock);
L
Linus Torvalds 已提交
103 104
}

105
static void zol_unmute(struct zoltrix *zol)
L
Linus Torvalds 已提交
106
{
107 108
	zol->muted = 0;
	zol_setvol(zol, zol->curvol);
L
Linus Torvalds 已提交
109 110
}

111
static int zol_setfreq(struct zoltrix *zol, unsigned long freq)
L
Linus Torvalds 已提交
112 113
{
	/* tunes the radio to the desired frequency */
114
	struct v4l2_device *v4l2_dev = &zol->v4l2_dev;
L
Linus Torvalds 已提交
115
	unsigned long long bitmask, f, m;
116
	unsigned int stereo = zol->stereo;
L
Linus Torvalds 已提交
117 118
	int i;

119
	if (freq == 0) {
120
		v4l2_warn(v4l2_dev, "cannot set a frequency of 0.\n");
121 122 123
		return -EINVAL;
	}

L
Linus Torvalds 已提交
124
	m = (freq / 160 - 8800) * 2;
125
	f = (unsigned long long)m + 0x4d1c;
L
Linus Torvalds 已提交
126 127 128 129

	bitmask = 0xc480402c10080000ull;
	i = 45;

130
	mutex_lock(&zol->lock);
131

132
	zol->curfreq = freq;
L
Linus Torvalds 已提交
133

134 135 136
	outb(0, zol->io);
	outb(0, zol->io);
	inb(zol->io + 3);            /* Zoltrix needs to be read to confirm */
L
Linus Torvalds 已提交
137

138 139 140 141
	outb(0x40, zol->io);
	outb(0xc0, zol->io);

	bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ (stereo << 31));
L
Linus Torvalds 已提交
142 143
	while (i--) {
		if ((bitmask & 0x8000000000000000ull) != 0) {
144
			outb(0x80, zol->io);
L
Linus Torvalds 已提交
145
			udelay(50);
146
			outb(0x00, zol->io);
L
Linus Torvalds 已提交
147
			udelay(50);
148
			outb(0x80, zol->io);
L
Linus Torvalds 已提交
149 150
			udelay(50);
		} else {
151
			outb(0xc0, zol->io);
L
Linus Torvalds 已提交
152
			udelay(50);
153
			outb(0x40, zol->io);
L
Linus Torvalds 已提交
154
			udelay(50);
155
			outb(0xc0, zol->io);
L
Linus Torvalds 已提交
156 157 158 159 160
			udelay(50);
		}
		bitmask *= 2;
	}
	/* termination sequence */
161 162 163
	outb(0x80, zol->io);
	outb(0xc0, zol->io);
	outb(0x40, zol->io);
L
Linus Torvalds 已提交
164
	udelay(1000);
165
	inb(zol->io + 2);
L
Linus Torvalds 已提交
166

167 168
	udelay(1000);

169 170 171 172
	if (zol->muted) {
		outb(0, zol->io);
		outb(0, zol->io);
		inb(zol->io + 3);
L
Linus Torvalds 已提交
173 174
		udelay(1000);
	}
175

176
	mutex_unlock(&zol->lock);
177

178 179
	if (!zol->muted)
		zol_setvol(zol, zol->curvol);
L
Linus Torvalds 已提交
180 181 182 183
	return 0;
}

/* Get signal strength */
184
static int zol_getsigstr(struct zoltrix *zol)
L
Linus Torvalds 已提交
185 186 187
{
	int a, b;

188 189 190
	mutex_lock(&zol->lock);
	outb(0x00, zol->io);         /* This stuff I found to do nothing */
	outb(zol->curvol, zol->io);
L
Linus Torvalds 已提交
191 192
	msleep(20);

193
	a = inb(zol->io);
L
Linus Torvalds 已提交
194
	msleep(10);
195
	b = inb(zol->io);
L
Linus Torvalds 已提交
196

197
	mutex_unlock(&zol->lock);
198

L
Linus Torvalds 已提交
199
	if (a != b)
200
		return 0;
L
Linus Torvalds 已提交
201

202 203
	/* I found this out by playing with a binary scanner on the card io */
	return a == 0xcf || a == 0xdf || a == 0xef;
L
Linus Torvalds 已提交
204 205
}

206
static int zol_is_stereo(struct zoltrix *zol)
L
Linus Torvalds 已提交
207 208 209
{
	int x1, x2;

210
	mutex_lock(&zol->lock);
211

212 213
	outb(0x00, zol->io);
	outb(zol->curvol, zol->io);
L
Linus Torvalds 已提交
214 215
	msleep(20);

216
	x1 = inb(zol->io);
L
Linus Torvalds 已提交
217
	msleep(10);
218
	x2 = inb(zol->io);
L
Linus Torvalds 已提交
219

220
	mutex_unlock(&zol->lock);
221

222
	return x1 == x2 && x1 == 0xcf;
L
Linus Torvalds 已提交
223 224
}

225 226 227 228 229
static int vidioc_querycap(struct file *file, void  *priv,
					struct v4l2_capability *v)
{
	strlcpy(v->driver, "radio-zoltrix", sizeof(v->driver));
	strlcpy(v->card, "Zoltrix Radio", sizeof(v->card));
230
	strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
231
	v->version = RADIO_VERSION;
232
	v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
233 234 235 236 237
	return 0;
}

static int vidioc_g_tuner(struct file *file, void *priv,
					struct v4l2_tuner *v)
L
Linus Torvalds 已提交
238
{
239
	struct zoltrix *zol = video_drvdata(file);
L
Linus Torvalds 已提交
240

241 242
	if (v->index > 0)
		return -EINVAL;
243

244
	strlcpy(v->name, "FM", sizeof(v->name));
245
	v->type = V4L2_TUNER_RADIO;
246 247 248
	v->rangelow = 88 * 16000;
	v->rangehigh = 108 * 16000;
	v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
249 250 251 252 253
	v->capability = V4L2_TUNER_CAP_LOW;
	if (zol_is_stereo(zol))
		v->audmode = V4L2_TUNER_MODE_STEREO;
	else
		v->audmode = V4L2_TUNER_MODE_MONO;
254
	v->signal = 0xFFFF * zol_getsigstr(zol);
255 256
	return 0;
}
257

258 259 260
static int vidioc_s_tuner(struct file *file, void *priv,
					struct v4l2_tuner *v)
{
261
	return v->index ? -EINVAL : 0;
262
}
263

264 265 266
static int vidioc_s_frequency(struct file *file, void *priv,
					struct v4l2_frequency *f)
{
267
	struct zoltrix *zol = video_drvdata(file);
268

269 270
	if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
		return -EINVAL;
271
	if (zol_setfreq(zol, f->frequency) != 0)
272
		return -EINVAL;
273 274
	return 0;
}
275

276 277 278
static int vidioc_g_frequency(struct file *file, void *priv,
					struct v4l2_frequency *f)
{
279
	struct zoltrix *zol = video_drvdata(file);
280

281 282
	if (f->tuner != 0)
		return -EINVAL;
283 284 285 286
	f->type = V4L2_TUNER_RADIO;
	f->frequency = zol->curfreq;
	return 0;
}
L
Linus Torvalds 已提交
287

288 289 290
static int vidioc_queryctrl(struct file *file, void *priv,
					struct v4l2_queryctrl *qc)
{
291 292 293 294 295
	switch (qc->id) {
	case V4L2_CID_AUDIO_MUTE:
		return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
	case V4L2_CID_AUDIO_VOLUME:
		return v4l2_ctrl_query_fill(qc, 0, 65535, 4096, 65535);
296 297 298 299 300 301 302
	}
	return -EINVAL;
}

static int vidioc_g_ctrl(struct file *file, void *priv,
				struct v4l2_control *ctrl)
{
303
	struct zoltrix *zol = video_drvdata(file);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
		ctrl->value = zol->muted;
		return 0;
	case V4L2_CID_AUDIO_VOLUME:
		ctrl->value = zol->curvol * 4096;
		return 0;
	}
	return -EINVAL;
}

static int vidioc_s_ctrl(struct file *file, void *priv,
				struct v4l2_control *ctrl)
{
319
	struct zoltrix *zol = video_drvdata(file);
320 321 322 323 324 325 326

	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
		if (ctrl->value)
			zol_mute(zol);
		else {
			zol_unmute(zol);
327
			zol_setvol(zol, zol->curvol);
328
		}
329 330
		return 0;
	case V4L2_CID_AUDIO_VOLUME:
331
		zol_setvol(zol, ctrl->value / 4096);
332 333 334
		return 0;
	}
	zol->stereo = 1;
335
	if (zol_setfreq(zol, zol->curfreq) != 0)
336
		return -EINVAL;
337 338
#if 0
/* FIXME: Implement stereo/mono switch on V4L2 */
339 340 341 342 343 344 345 346
	if (v->mode & VIDEO_SOUND_STEREO) {
		zol->stereo = 1;
		zol_setfreq(zol, zol->curfreq);
	}
	if (v->mode & VIDEO_SOUND_MONO) {
		zol->stereo = 0;
		zol_setfreq(zol, zol->curfreq);
	}
347
#endif
348 349
	return -EINVAL;
}
350

351 352 353 354
static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
{
	*i = 0;
	return 0;
L
Linus Torvalds 已提交
355 356
}

357
static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
L
Linus Torvalds 已提交
358
{
359
	return i ? -EINVAL : 0;
360 361
}

362
static int vidioc_g_audio(struct file *file, void *priv,
363 364
					struct v4l2_audio *a)
{
365 366 367
	a->index = 0;
	strlcpy(a->name, "Radio", sizeof(a->name));
	a->capability = V4L2_AUDCAP_STEREO;
368
	return 0;
L
Linus Torvalds 已提交
369 370
}

371 372 373 374 375
static int vidioc_s_audio(struct file *file, void *priv,
					struct v4l2_audio *a)
{
	return a->index ? -EINVAL : 0;
}
L
Linus Torvalds 已提交
376

377
static const struct v4l2_file_operations zoltrix_fops =
L
Linus Torvalds 已提交
378 379
{
	.owner		= THIS_MODULE,
380
	.ioctl		= video_ioctl2,
L
Linus Torvalds 已提交
381 382
};

383
static const struct v4l2_ioctl_ops zoltrix_ioctl_ops = {
384 385 386 387 388 389 390 391 392 393 394 395
	.vidioc_querycap    = vidioc_querycap,
	.vidioc_g_tuner     = vidioc_g_tuner,
	.vidioc_s_tuner     = vidioc_s_tuner,
	.vidioc_g_audio     = vidioc_g_audio,
	.vidioc_s_audio     = vidioc_s_audio,
	.vidioc_g_input     = vidioc_g_input,
	.vidioc_s_input     = vidioc_s_input,
	.vidioc_g_frequency = vidioc_g_frequency,
	.vidioc_s_frequency = vidioc_s_frequency,
	.vidioc_queryctrl   = vidioc_queryctrl,
	.vidioc_g_ctrl      = vidioc_g_ctrl,
	.vidioc_s_ctrl      = vidioc_s_ctrl,
L
Linus Torvalds 已提交
396 397 398 399
};

static int __init zoltrix_init(void)
{
400 401 402 403 404 405 406
	struct zoltrix *zol = &zoltrix_card;
	struct v4l2_device *v4l2_dev = &zol->v4l2_dev;
	int res;

	strlcpy(v4l2_dev->name, "zoltrix", sizeof(v4l2_dev->name));
	zol->io = io;
	if (zol->io == -1) {
407
		v4l2_err(v4l2_dev, "You must set an I/O address with io=0x20c or 0x30c\n");
L
Linus Torvalds 已提交
408 409
		return -EINVAL;
	}
410 411
	if (zol->io != 0x20c && zol->io != 0x30c) {
		v4l2_err(v4l2_dev, "invalid port, try 0x20c or 0x30c\n");
L
Linus Torvalds 已提交
412 413 414
		return -ENXIO;
	}

415 416
	if (!request_region(zol->io, 2, "zoltrix")) {
		v4l2_err(v4l2_dev, "port 0x%x already in use\n", zol->io);
L
Linus Torvalds 已提交
417 418 419
		return -EBUSY;
	}

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
	res = v4l2_device_register(NULL, v4l2_dev);
	if (res < 0) {
		release_region(zol->io, 2);
		v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
		return res;
	}

	strlcpy(zol->vdev.name, v4l2_dev->name, sizeof(zol->vdev.name));
	zol->vdev.v4l2_dev = v4l2_dev;
	zol->vdev.fops = &zoltrix_fops;
	zol->vdev.ioctl_ops = &zoltrix_ioctl_ops;
	zol->vdev.release = video_device_release_empty;
	video_set_drvdata(&zol->vdev, zol);

	if (video_register_device(&zol->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
		v4l2_device_unregister(v4l2_dev);
		release_region(zol->io, 2);
L
Linus Torvalds 已提交
437 438
		return -EINVAL;
	}
439
	v4l2_info(v4l2_dev, "Zoltrix Radio Plus card driver.\n");
L
Linus Torvalds 已提交
440

441
	mutex_init(&zol->lock);
442

L
Linus Torvalds 已提交
443 444 445 446
	/* mute card - prevents noisy bootups */

	/* this ensures that the volume is all the way down  */

447 448
	outb(0, zol->io);
	outb(0, zol->io);
L
Linus Torvalds 已提交
449
	msleep(20);
450
	inb(zol->io + 3);
L
Linus Torvalds 已提交
451

452 453
	zol->curvol = 0;
	zol->stereo = 1;
L
Linus Torvalds 已提交
454 455 456 457

	return 0;
}

458
static void __exit zoltrix_exit(void)
L
Linus Torvalds 已提交
459
{
460 461 462 463 464
	struct zoltrix *zol = &zoltrix_card;

	video_unregister_device(&zol->vdev);
	v4l2_device_unregister(&zol->v4l2_dev);
	release_region(zol->io, 2);
L
Linus Torvalds 已提交
465 466 467
}

module_init(zoltrix_init);
468
module_exit(zoltrix_exit);
L
Linus Torvalds 已提交
469