radio-zoltrix.c 10.5 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
	if (zol_setfreq(zol, f->frequency) != 0)
270
		return -EINVAL;
271 272
	return 0;
}
273

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

	f->type = V4L2_TUNER_RADIO;
	f->frequency = zol->curfreq;
	return 0;
}
L
Linus Torvalds 已提交
283

284 285 286
static int vidioc_queryctrl(struct file *file, void *priv,
					struct v4l2_queryctrl *qc)
{
287 288 289 290 291
	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);
292 293 294 295 296 297 298
	}
	return -EINVAL;
}

static int vidioc_g_ctrl(struct file *file, void *priv,
				struct v4l2_control *ctrl)
{
299
	struct zoltrix *zol = video_drvdata(file);
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

	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)
{
315
	struct zoltrix *zol = video_drvdata(file);
316 317 318 319 320 321 322

	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
		if (ctrl->value)
			zol_mute(zol);
		else {
			zol_unmute(zol);
323
			zol_setvol(zol, zol->curvol);
324
		}
325 326
		return 0;
	case V4L2_CID_AUDIO_VOLUME:
327
		zol_setvol(zol, ctrl->value / 4096);
328 329 330
		return 0;
	}
	zol->stereo = 1;
331
	if (zol_setfreq(zol, zol->curfreq) != 0)
332
		return -EINVAL;
333 334
#if 0
/* FIXME: Implement stereo/mono switch on V4L2 */
335 336 337 338 339 340 341 342
	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);
	}
343
#endif
344 345
	return -EINVAL;
}
346

347 348 349 350
static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
{
	*i = 0;
	return 0;
L
Linus Torvalds 已提交
351 352
}

353
static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
L
Linus Torvalds 已提交
354
{
355
	return i ? -EINVAL : 0;
356 357
}

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

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

373
static const struct v4l2_file_operations zoltrix_fops =
L
Linus Torvalds 已提交
374 375
{
	.owner		= THIS_MODULE,
376
	.ioctl		= video_ioctl2,
L
Linus Torvalds 已提交
377 378
};

379
static const struct v4l2_ioctl_ops zoltrix_ioctl_ops = {
380 381 382 383 384 385 386 387 388 389 390 391
	.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 已提交
392 393 394 395
};

static int __init zoltrix_init(void)
{
396 397 398 399 400 401 402
	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) {
403
		v4l2_err(v4l2_dev, "You must set an I/O address with io=0x20c or 0x30c\n");
L
Linus Torvalds 已提交
404 405
		return -EINVAL;
	}
406 407
	if (zol->io != 0x20c && zol->io != 0x30c) {
		v4l2_err(v4l2_dev, "invalid port, try 0x20c or 0x30c\n");
L
Linus Torvalds 已提交
408 409 410
		return -ENXIO;
	}

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

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
	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 已提交
433 434
		return -EINVAL;
	}
435
	v4l2_info(v4l2_dev, "Zoltrix Radio Plus card driver.\n");
L
Linus Torvalds 已提交
436

437
	mutex_init(&zol->lock);
438

L
Linus Torvalds 已提交
439 440 441 442
	/* mute card - prevents noisy bootups */

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

443 444
	outb(0, zol->io);
	outb(0, zol->io);
L
Linus Torvalds 已提交
445
	msleep(20);
446
	inb(zol->io + 3);
L
Linus Torvalds 已提交
447

448 449
	zol->curvol = 0;
	zol->stereo = 1;
L
Linus Torvalds 已提交
450 451 452 453

	return 0;
}

454
static void __exit zoltrix_exit(void)
L
Linus Torvalds 已提交
455
{
456 457 458 459 460
	struct zoltrix *zol = &zoltrix_card;

	video_unregister_device(&zol->vdev);
	v4l2_device_unregister(&zol->v4l2_dev);
	release_region(zol->io, 2);
L
Linus Torvalds 已提交
461 462 463
}

module_init(zoltrix_init);
464
module_exit(zoltrix_exit);
L
Linus Torvalds 已提交
465