radio-sf16fmr2.c 10.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/* SF16FMR2 radio driver for Linux radio support
 * heavily based on fmi driver...
 * (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com
 *
 * Notes on the hardware
 *
 *  Frequency control is done digitally -- ie out(port,encodefreq(95.8));
 *  No volume control - only mute/unmute - you have to use line volume
 *
 *  For read stereo/mono you must wait 0.1 sec after set frequency and
 *  card unmuted so I set frequency on unmute
 *  Signal handling seem to work only on autoscanning (not implemented)
13 14
 *
 *  Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
L
Linus Torvalds 已提交
15 16 17 18
 */

#include <linux/module.h>	/* Modules 			*/
#include <linux/init.h>		/* Initdata			*/
19
#include <linux/ioport.h>	/* request_region		*/
L
Linus Torvalds 已提交
20
#include <linux/delay.h>	/* udelay			*/
21
#include <linux/videodev2.h>	/* kernel radio structs		*/
22
#include <linux/mutex.h>
23 24 25 26
#include <linux/version.h>      /* for KERNEL_VERSION MACRO     */
#include <linux/io.h>		/* outb, outb_p			*/
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
L
Linus Torvalds 已提交
27

28 29 30 31 32 33 34 35 36 37
MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
MODULE_LICENSE("GPL");

static int io = 0x384;
static int radio_nr = -1;

module_param(io, int, 0);
MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
module_param(radio_nr, int, 0);
38

39 40
#define RADIO_VERSION KERNEL_VERSION(0,0,2)

41 42
#define AUD_VOL_INDEX 1

L
Linus Torvalds 已提交
43 44 45 46 47 48 49 50 51 52
#undef DEBUG
//#define DEBUG 1

#ifdef DEBUG
# define  debug_print(s) printk s
#else
# define  debug_print(s)
#endif

/* this should be static vars for module size */
53
struct fmr2
L
Linus Torvalds 已提交
54
{
55 56 57 58
	struct v4l2_device v4l2_dev;
	struct video_device vdev;
	struct mutex lock;
	int io;
59
	int curvol; /* 0-15 */
L
Linus Torvalds 已提交
60 61 62 63 64 65
	int mute;
	int stereo; /* card is producing stereo audio */
	unsigned long curfreq; /* freq in kHz */
	int card_type;
};

66
static struct fmr2 fmr2_card;
L
Linus Torvalds 已提交
67 68

/* hw precision is 12.5 kHz
69
 * It is only useful to give freq in interval of 200 (=0.0125Mhz),
L
Linus Torvalds 已提交
70 71
 * other bits will be truncated
 */
72 73 74
#define RSF16_ENCODE(x)	((x) / 200 + 856)
#define RSF16_MINFREQ (87 * 16000)
#define RSF16_MAXFREQ (108 * 16000)
L
Linus Torvalds 已提交
75

76
static inline void wait(int n, int io)
L
Linus Torvalds 已提交
77
{
78 79
	for (; n; --n)
		inb(io);
L
Linus Torvalds 已提交
80 81
}

82
static void outbits(int bits, unsigned int data, int nWait, int io)
L
Linus Torvalds 已提交
83 84
{
	int bit;
85 86 87 88 89 90 91 92 93

	for (; --bits >= 0;) {
		bit = (data >> bits) & 1;
		outb(bit, io);
		wait(nWait, io);
		outb(bit | 2, io);
		wait(nWait, io);
		outb(bit, io);
		wait(nWait, io);
L
Linus Torvalds 已提交
94 95 96
	}
}

97
static inline void fmr2_mute(int io)
L
Linus Torvalds 已提交
98
{
99 100
	outb(0x00, io);
	wait(4, io);
L
Linus Torvalds 已提交
101 102
}

103
static inline void fmr2_unmute(int io)
L
Linus Torvalds 已提交
104
{
105 106
	outb(0x04, io);
	wait(4, io);
L
Linus Torvalds 已提交
107 108
}

109
static inline int fmr2_stereo_mode(int io)
L
Linus Torvalds 已提交
110
{
111 112 113 114 115
	int n = inb(io);

	outb(6, io);
	inb(io);
	n = ((n >> 3) & 1) ^ 1;
L
Linus Torvalds 已提交
116 117 118 119
	debug_print((KERN_DEBUG "stereo: %d\n", n));
	return n;
}

120
static int fmr2_product_info(struct fmr2 *dev)
L
Linus Torvalds 已提交
121
{
122 123
	int n = inb(dev->io);

L
Linus Torvalds 已提交
124
	n &= 0xC1;
125
	if (n == 0) {
L
Linus Torvalds 已提交
126 127 128 129 130
		/* this should support volume set */
		dev->card_type = 12;
		return 0;
	}
	/* not volume (mine is 11) */
131
	dev->card_type = (n == 128) ? 11 : 0;
L
Linus Torvalds 已提交
132 133 134
	return n;
}

135
static inline int fmr2_getsigstr(struct fmr2 *dev)
L
Linus Torvalds 已提交
136
{
137 138 139 140 141 142 143
	/* !!! works only if scanning freq */
	int res = 0xffff;

	outb(5, dev->io);
	wait(4, dev->io);
	if (!(inb(dev->io) & 1))
		res = 0;
L
Linus Torvalds 已提交
144 145 146 147 148
	debug_print((KERN_DEBUG "signal: %d\n", res));
	return res;
}

/* set frequency and unmute card */
149
static int fmr2_setfreq(struct fmr2 *dev)
L
Linus Torvalds 已提交
150 151 152
{
	unsigned long freq = dev->curfreq;

153
	fmr2_mute(dev->io);
L
Linus Torvalds 已提交
154 155 156 157 158

	/* 0x42 for mono output
	 * 0x102 forward scanning
	 * 0x182 scansione avanti
	 */
159 160
	outbits(9, 0x2, 3, dev->io);
	outbits(16, RSF16_ENCODE(freq), 2, dev->io);
L
Linus Torvalds 已提交
161

162
	fmr2_unmute(dev->io);
L
Linus Torvalds 已提交
163 164 165 166 167 168

	/* wait 0.11 sec */
	msleep(110);

	/* NOTE if mute this stop radio
	   you must set freq on unmute */
169
	dev->stereo = fmr2_stereo_mode(dev->io);
L
Linus Torvalds 已提交
170 171 172 173
	return 0;
}

/* !!! not tested, in my card this does't work !!! */
174
static int fmr2_setvolume(struct fmr2 *dev)
L
Linus Torvalds 已提交
175
{
176 177 178 179
	int vol[16] = { 0x021, 0x084, 0x090, 0x104,
			0x110, 0x204, 0x210, 0x402,
			0x404, 0x408, 0x410, 0x801,
			0x802, 0x804, 0x808, 0x810 };
180
	int i, a;
181
	int n = vol[dev->curvol & 0x0f];
L
Linus Torvalds 已提交
182

183 184
	if (dev->card_type != 11)
		return 1;
L
Linus Torvalds 已提交
185

186
	for (i = 12; --i >= 0; ) {
187 188 189 190 191 192 193
		a = ((n >> i) & 1) << 6; /* if (a==0) a = 0; else a = 0x40; */
		outb(a | 4, dev->io);
		wait(4, dev->io);
		outb(a | 0x24, dev->io);
		wait(4, dev->io);
		outb(a | 4, dev->io);
		wait(4, dev->io);
L
Linus Torvalds 已提交
194
	}
195
	for (i = 6; --i >= 0; ) {
L
Linus Torvalds 已提交
196
		a = ((0x18 >> i) & 1) << 6;
197 198 199 200 201 202
		outb(a | 4, dev->io);
		wait(4, dev->io);
		outb(a | 0x24, dev->io);
		wait(4, dev->io);
		outb(a | 4, dev->io);
		wait(4, dev->io);
L
Linus Torvalds 已提交
203
	}
204 205
	wait(4, dev->io);
	outb(0x14, dev->io);
L
Linus Torvalds 已提交
206 207 208
	return 0;
}

209 210
static int vidioc_querycap(struct file *file, void  *priv,
					struct v4l2_capability *v)
L
Linus Torvalds 已提交
211
{
212 213
	strlcpy(v->driver, "radio-sf16fmr2", sizeof(v->driver));
	strlcpy(v->card, "SF16-FMR2 radio", sizeof(v->card));
214
	strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
215
	v->version = RADIO_VERSION;
216
	v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
217 218 219 220 221 222
	return 0;
}

static int vidioc_g_tuner(struct file *file, void *priv,
					struct v4l2_tuner *v)
{
223
	struct fmr2 *fmr2 = video_drvdata(file);
L
Linus Torvalds 已提交
224

225 226 227
	if (v->index > 0)
		return -EINVAL;

228
	strlcpy(v->name, "FM", sizeof(v->name));
229 230
	v->type = V4L2_TUNER_RADIO;

231 232
	v->rangelow = RSF16_MINFREQ;
	v->rangehigh = RSF16_MAXFREQ;
233 234 235 236
	v->rxsubchans = fmr2->stereo ? V4L2_TUNER_SUB_STEREO :
					V4L2_TUNER_SUB_MONO;
	v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
	v->audmode = V4L2_TUNER_MODE_STEREO;
237
	mutex_lock(&fmr2->lock);
238
	v->signal = fmr2_getsigstr(fmr2);
239
	mutex_unlock(&fmr2->lock);
240 241
	return 0;
}
242

243 244 245
static int vidioc_s_tuner(struct file *file, void *priv,
					struct v4l2_tuner *v)
{
246
	return v->index ? -EINVAL : 0;
247
}
248

249 250 251
static int vidioc_s_frequency(struct file *file, void *priv,
					struct v4l2_frequency *f)
{
252
	struct fmr2 *fmr2 = video_drvdata(file);
253

254 255
	if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
		return -EINVAL;
256
	if (f->frequency < RSF16_MINFREQ ||
257
			f->frequency > RSF16_MAXFREQ)
258
		return -EINVAL;
259 260 261
	/* rounding in steps of 200 to match the freq
	   that will be used */
	fmr2->curfreq = (f->frequency / 200) * 200;
262 263 264

	/* set card freq (if not muted) */
	if (fmr2->curvol && !fmr2->mute) {
265
		mutex_lock(&fmr2->lock);
266
		fmr2_setfreq(fmr2);
267
		mutex_unlock(&fmr2->lock);
268 269 270
	}
	return 0;
}
271

272 273 274
static int vidioc_g_frequency(struct file *file, void *priv,
					struct v4l2_frequency *f)
{
275
	struct fmr2 *fmr2 = video_drvdata(file);
276

277 278
	if (f->tuner != 0)
		return -EINVAL;
279 280 281 282
	f->type = V4L2_TUNER_RADIO;
	f->frequency = fmr2->curfreq;
	return 0;
}
283

284 285 286
static int vidioc_queryctrl(struct file *file, void *priv,
					struct v4l2_queryctrl *qc)
{
287
	struct fmr2 *fmr2 = video_drvdata(file);
288

289 290 291 292 293 294 295 296
	switch (qc->id) {
	case V4L2_CID_AUDIO_MUTE:
		return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
	case V4L2_CID_AUDIO_VOLUME:
		/* Only card_type == 11 implements volume */
		if (fmr2->card_type == 11)
			return v4l2_ctrl_query_fill(qc, 0, 15, 1, 0);
		return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
297 298 299 300 301 302 303
	}
	return -EINVAL;
}

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

	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
		ctrl->value = fmr2->mute;
		return 0;
	case V4L2_CID_AUDIO_VOLUME:
		ctrl->value = fmr2->curvol;
		return 0;
	}
	return -EINVAL;
}

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

	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
		fmr2->mute = ctrl->value;
		break;
	case V4L2_CID_AUDIO_VOLUME:
327
		fmr2->curvol = ctrl->value;
328 329 330 331 332
		break;
	default:
		return -EINVAL;
	}

L
Linus Torvalds 已提交
333
#ifdef DEBUG
334 335 336 337
	if (fmr2->curvol && !fmr2->mute)
		printk(KERN_DEBUG "unmute\n");
	else
		printk(KERN_DEBUG "mute\n");
L
Linus Torvalds 已提交
338
#endif
339

340
	mutex_lock(&fmr2->lock);
341 342
	if (fmr2->curvol && !fmr2->mute) {
		fmr2_setvolume(fmr2);
343
		/* Set frequency and unmute card */
344 345
		fmr2_setfreq(fmr2);
	} else
346 347
		fmr2_mute(fmr2->io);
	mutex_unlock(&fmr2->lock);
348 349 350 351 352 353 354
	return 0;
}

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

357 358
static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
{
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 fmr2_fops = {
L
Linus Torvalds 已提交
378
	.owner          = THIS_MODULE,
379
	.ioctl          = video_ioctl2,
L
Linus Torvalds 已提交
380 381
};

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

static int __init fmr2_init(void)
{
399 400 401 402 403 404 405 406 407 408 409
	struct fmr2 *fmr2 = &fmr2_card;
	struct v4l2_device *v4l2_dev = &fmr2->v4l2_dev;
	int res;

	strlcpy(v4l2_dev->name, "sf16fmr2", sizeof(v4l2_dev->name));
	fmr2->io = io;
	fmr2->stereo = 1;
	mutex_init(&fmr2->lock);

	if (!request_region(fmr2->io, 2, "sf16fmr2")) {
		v4l2_err(v4l2_dev, "request_region failed!\n");
L
Linus Torvalds 已提交
410 411 412
		return -EBUSY;
	}

413 414 415 416 417
	res = v4l2_device_register(NULL, v4l2_dev);
	if (res < 0) {
		release_region(fmr2->io, 2);
		v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
		return res;
L
Linus Torvalds 已提交
418 419
	}

420 421 422 423 424 425
	strlcpy(fmr2->vdev.name, v4l2_dev->name, sizeof(fmr2->vdev.name));
	fmr2->vdev.v4l2_dev = v4l2_dev;
	fmr2->vdev.fops = &fmr2_fops;
	fmr2->vdev.ioctl_ops = &fmr2_ioctl_ops;
	fmr2->vdev.release = video_device_release_empty;
	video_set_drvdata(&fmr2->vdev, fmr2);
426

427 428 429 430 431
	if (video_register_device(&fmr2->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
		v4l2_device_unregister(v4l2_dev);
		release_region(fmr2->io, 2);
		return -EINVAL;
	}
432

433 434 435 436 437 438 439
	v4l2_info(v4l2_dev, "SF16FMR2 radio card driver at 0x%x.\n", fmr2->io);
	/* mute card - prevents noisy bootups */
	mutex_lock(&fmr2->lock);
	fmr2_mute(fmr2->io);
	fmr2_product_info(fmr2);
	mutex_unlock(&fmr2->lock);
	debug_print((KERN_DEBUG "card_type %d\n", fmr2->card_type));
L
Linus Torvalds 已提交
440 441 442
	return 0;
}

443
static void __exit fmr2_exit(void)
L
Linus Torvalds 已提交
444
{
445 446 447 448 449
	struct fmr2 *fmr2 = &fmr2_card;

	video_unregister_device(&fmr2->vdev);
	v4l2_device_unregister(&fmr2->v4l2_dev);
	release_region(fmr2->io, 2);
L
Linus Torvalds 已提交
450 451 452
}

module_init(fmr2_init);
453
module_exit(fmr2_exit);
L
Linus Torvalds 已提交
454 455 456 457 458 459 460 461 462 463 464 465

#ifndef MODULE

static int __init fmr2_setup_io(char *str)
{
	get_option(&str, &io);
	return 1;
}

__setup("sf16fmr2=", fmr2_setup_io);

#endif