saa7134-input.c 13.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 *
 * handle saa7134 IR remotes via linux kernel input layer.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/input.h>

#include "saa7134-reg.h"
#include "saa7134.h"

static unsigned int disable_ir = 0;
module_param(disable_ir, int, 0444);
MODULE_PARM_DESC(disable_ir,"disable infrared remote support");

static unsigned int ir_debug = 0;
module_param(ir_debug, int, 0644);
MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");

38 39 40 41
static int pinnacle_remote = 0;
module_param(pinnacle_remote, int, 0644);    /* Choose Pinnacle PCTV remote */
MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");

42
static int ir_rc5_remote_gap = 885;
43
module_param(ir_rc5_remote_gap, int, 0644);
44
static int ir_rc5_key_timeout = 115;
45 46
module_param(ir_rc5_key_timeout, int, 0644);

47 48 49 50 51
static int repeat_delay = 500;
module_param(repeat_delay, int, 0644);
MODULE_PARM_DESC(repeat_delay, "delay before key repeat started");
static int repeat_period = 33;
module_param(repeat_period, int, 0644);
52
MODULE_PARM_DESC(repeat_period, "repeat period between "
53 54
    "keypresses when key is down");

L
Linus Torvalds 已提交
55 56
#define dprintk(fmt, arg...)	if (ir_debug) \
	printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
57 58
#define i2cdprintk(fmt, arg...)    if (ir_debug) \
	printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg)
L
Linus Torvalds 已提交
59

60 61 62
/** rc5 functions */
static int saa7134_rc5_irq(struct saa7134_dev *dev);

63
/* -------------------- GPIO generic keycode builder -------------------- */
L
Linus Torvalds 已提交
64 65 66

static int build_key(struct saa7134_dev *dev)
{
67
	struct card_ir *ir = dev->remote;
L
Linus Torvalds 已提交
68 69
	u32 gpio, data;

70 71 72 73 74 75 76
	/* here comes the additional handshake steps for some cards */
	switch (dev->board) {
	case SAA7134_BOARD_GOTVIEW_7135:
		saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
		saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
		break;
	}
L
Linus Torvalds 已提交
77 78 79 80 81
	/* rising SAA7134_GPIO_GPRESCAN reads the status */
	saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
	saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);

	gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
82 83 84 85 86
	if (ir->polling) {
		if (ir->last_gpio == gpio)
			return 0;
		ir->last_gpio = gpio;
	}
L
Linus Torvalds 已提交
87

88
	data = ir_extract_bits(gpio, ir->mask_keycode);
L
Linus Torvalds 已提交
89 90 91
	dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
		gpio, ir->mask_keycode, data);

92 93 94 95 96 97 98
	if (ir->polling) {
		if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
		    (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
			ir_input_keydown(ir->dev, &ir->ir, data, data);
		} else {
			ir_input_nokey(ir->dev, &ir->ir);
		}
L
Linus Torvalds 已提交
99
	}
100 101 102 103 104 105 106 107
	else {	/* IRQ driven mode - handle key press and release in one go */
		if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
		    (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
			ir_input_keydown(ir->dev, &ir->ir, data, data);
			ir_input_nokey(ir->dev, &ir->ir);
		}
	}

L
Linus Torvalds 已提交
108 109 110
	return 0;
}

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
/* --------------------- Chip specific I2C key builders ----------------- */

static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
{
	unsigned char b;

	/* poll IR chip */
	if (1 != i2c_master_recv(&ir->c,&b,1)) {
		i2cdprintk("read error\n");
		return -EIO;
	}

	/* no button press */
	if (b==0)
		return 0;

	/* repeating */
	if (b & 0x80)
		return 1;

	*ir_key = b;
	*ir_raw = b;
	return 1;
}

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
{
	unsigned char buf[5], cod4, code3, code4;

	/* poll IR chip */
	if (5 != i2c_master_recv(&ir->c,buf,5))
		return -EIO;

	cod4	= buf[4];
	code4	= (cod4 >> 2);
	code3	= buf[3];
	if (code3 == 0)
		/* no key pressed */
		return 0;

	/* return key */
	*ir_key = code4;
	*ir_raw = code4;
	return 1;
}

L
Linus Torvalds 已提交
157 158
void saa7134_input_irq(struct saa7134_dev *dev)
{
159
	struct card_ir *ir = dev->remote;
L
Linus Torvalds 已提交
160

161
	if (!ir->polling && !ir->rc5_gpio) {
L
Linus Torvalds 已提交
162
		build_key(dev);
163 164 165
	} else if (ir->rc5_gpio) {
		saa7134_rc5_irq(dev);
	}
L
Linus Torvalds 已提交
166 167 168 169
}

static void saa7134_input_timer(unsigned long data)
{
170
	struct saa7134_dev *dev = (struct saa7134_dev *)data;
171
	struct card_ir *ir = dev->remote;
L
Linus Torvalds 已提交
172 173

	build_key(dev);
174
	mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
L
Linus Torvalds 已提交
175 176
}

177
void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
178 179
{
	if (ir->polling) {
180 181
		setup_timer(&ir->timer, saa7134_input_timer,
			    (unsigned long)dev);
182 183
		ir->timer.expires  = jiffies + HZ;
		add_timer(&ir->timer);
184 185 186 187 188 189 190 191 192 193 194 195 196
	} else if (ir->rc5_gpio) {
		/* set timer_end for code completion */
		init_timer(&ir->timer_end);
		ir->timer_end.function = ir_rc5_timer_end;
		ir->timer_end.data = (unsigned long)ir;
		init_timer(&ir->timer_keyup);
		ir->timer_keyup.function = ir_rc5_timer_keyup;
		ir->timer_keyup.data = (unsigned long)ir;
		ir->shift_by = 2;
		ir->start = 0x2;
		ir->addr = 0x17;
		ir->rc5_key_timeout = ir_rc5_key_timeout;
		ir->rc5_remote_gap = ir_rc5_remote_gap;
197 198 199
	}
}

200
void saa7134_ir_stop(struct saa7134_dev *dev)
201 202 203 204 205
{
	if (dev->remote->polling)
		del_timer_sync(&dev->remote->timer);
}

L
Linus Torvalds 已提交
206 207
int saa7134_input_init1(struct saa7134_dev *dev)
{
208
	struct card_ir *ir;
209
	struct input_dev *input_dev;
L
Linus Torvalds 已提交
210 211 212 213 214
	IR_KEYTAB_TYPE *ir_codes = NULL;
	u32 mask_keycode = 0;
	u32 mask_keydown = 0;
	u32 mask_keyup   = 0;
	int polling      = 0;
215
	int rc5_gpio	 = 0;
L
Linus Torvalds 已提交
216
	int ir_type      = IR_TYPE_OTHER;
217
	int err;
L
Linus Torvalds 已提交
218

219
	if (dev->has_remote != SAA7134_REMOTE_GPIO)
L
Linus Torvalds 已提交
220 221 222 223 224 225 226 227
		return -ENODEV;
	if (disable_ir)
		return -ENODEV;

	/* detect & configure */
	switch (dev->board) {
	case SAA7134_BOARD_FLYVIDEO2000:
	case SAA7134_BOARD_FLYVIDEO3000:
228
	case SAA7134_BOARD_FLYTVPLATINUM_FM:
229
	case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
230
		ir_codes     = ir_codes_flyvideo;
L
Linus Torvalds 已提交
231 232 233 234 235 236
		mask_keycode = 0xEC00000;
		mask_keydown = 0x0040000;
		break;
	case SAA7134_BOARD_CINERGY400:
	case SAA7134_BOARD_CINERGY600:
	case SAA7134_BOARD_CINERGY600_MK3:
237
		ir_codes     = ir_codes_cinergy;
L
Linus Torvalds 已提交
238 239 240 241 242
		mask_keycode = 0x00003f;
		mask_keyup   = 0x040000;
		break;
	case SAA7134_BOARD_ECS_TVP3XP:
	case SAA7134_BOARD_ECS_TVP3XP_4CB5:
243
		ir_codes     = ir_codes_eztv;
244 245
		mask_keycode = 0x00017c;
		mask_keyup   = 0x000002;
L
Linus Torvalds 已提交
246
		polling      = 50; // ms
247 248
		break;
	case SAA7134_BOARD_KWORLD_XPERT:
L
Linus Torvalds 已提交
249
	case SAA7134_BOARD_AVACSSMARTTV:
250
		ir_codes     = ir_codes_pixelview;
L
Linus Torvalds 已提交
251 252 253 254 255
		mask_keycode = 0x00001F;
		mask_keyup   = 0x000020;
		polling      = 50; // ms
		break;
	case SAA7134_BOARD_MD2819:
256
	case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
L
Linus Torvalds 已提交
257 258
	case SAA7134_BOARD_AVERMEDIA_305:
	case SAA7134_BOARD_AVERMEDIA_307:
259 260
	case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
	case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
261
	case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
262
	case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
263
		ir_codes     = ir_codes_avermedia;
L
Linus Torvalds 已提交
264 265 266 267 268 269 270
		mask_keycode = 0x0007C8;
		mask_keydown = 0x000010;
		polling      = 50; // ms
		/* Set GPIO pin2 to high to enable the IR controller */
		saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
		saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
		break;
271
	case SAA7134_BOARD_AVERMEDIA_777:
272
	case SAA7134_BOARD_AVERMEDIA_A16AR:
273 274 275 276 277 278 279
		ir_codes     = ir_codes_avermedia;
		mask_keycode = 0x02F200;
		mask_keydown = 0x000400;
		polling      = 50; // ms
		/* Without this we won't receive key up events */
		saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
		saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
280
		break;
281
	case SAA7134_BOARD_KWORLD_TERMINATOR:
282
		ir_codes     = ir_codes_pixelview;
283 284 285 286
		mask_keycode = 0x00001f;
		mask_keyup   = 0x000060;
		polling      = 50; // ms
		break;
287 288
	case SAA7134_BOARD_MANLI_MTV001:
	case SAA7134_BOARD_MANLI_MTV002:
289
	case SAA7134_BOARD_BEHOLD_409FM:
290
		ir_codes     = ir_codes_manli;
291 292 293 294
		mask_keycode = 0x001f00;
		mask_keyup   = 0x004000;
		polling      = 50; // ms
		break;
295
	case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
296
		ir_codes     = ir_codes_pctv_sedna;
297 298 299 300
		mask_keycode = 0x001f00;
		mask_keyup   = 0x004000;
		polling      = 50; // ms
		break;
301
	case SAA7134_BOARD_GOTVIEW_7135:
302
		ir_codes     = ir_codes_gotview7135;
303
		mask_keycode = 0x0003CC;
304
		mask_keydown = 0x000010;
305 306
		polling	     = 5; /* ms */
		saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
307
		break;
L
Linus Torvalds 已提交
308
	case SAA7134_BOARD_VIDEOMATE_TV_PVR:
309
	case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
310
	case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
311
		ir_codes     = ir_codes_videomate_tv_pvr;
L
Linus Torvalds 已提交
312 313 314 315
		mask_keycode = 0x00003F;
		mask_keyup   = 0x400000;
		polling      = 50; // ms
		break;
316 317 318 319 320 321
	case SAA7134_BOARD_PROTEUS_2309:
		ir_codes     = ir_codes_proteus_2309;
		mask_keycode = 0x00007F;
		mask_keyup   = 0x000080;
		polling      = 50; // ms
		break;
322 323
	case SAA7134_BOARD_VIDEOMATE_DVBT_300:
	case SAA7134_BOARD_VIDEOMATE_DVBT_200:
324
		ir_codes     = ir_codes_videomate_tv_pvr;
325 326 327
		mask_keycode = 0x003F00;
		mask_keyup   = 0x040000;
		break;
328
	case SAA7134_BOARD_FLYDVBS_LR300:
329
	case SAA7134_BOARD_FLYDVBT_LR301:
330
	case SAA7134_BOARD_FLYDVBTDUO:
331 332 333 334
		ir_codes     = ir_codes_flydvb;
		mask_keycode = 0x0001F00;
		mask_keydown = 0x0040000;
		break;
335
	case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
336
	case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
337 338 339 340
		ir_codes     = ir_codes_asus_pc39;
		mask_keydown = 0x0040000;
		rc5_gpio = 1;
		break;
341 342 343 344 345 346 347
	case SAA7134_BOARD_ENCORE_ENLTV:
	case SAA7134_BOARD_ENCORE_ENLTV_FM:
		ir_codes     = ir_codes_encore_enltv;
		mask_keycode = 0x00007f;
		mask_keyup   = 0x040000;
		polling      = 50; // ms
		break;
348 349 350 351 352 353
	case SAA7134_BOARD_10MOONSTVMASTER3:
		ir_codes     = ir_codes_encore_enltv;
		mask_keycode = 0x5f80000;
		mask_keyup   = 0x8000000;
		polling      = 50; //ms
		break;
L
Linus Torvalds 已提交
354 355 356 357 358 359 360
	}
	if (NULL == ir_codes) {
		printk("%s: Oops: IR config error [card=%d]\n",
		       dev->name, dev->board);
		return -ENODEV;
	}

361 362 363
	ir = kzalloc(sizeof(*ir), GFP_KERNEL);
	input_dev = input_allocate_device();
	if (!ir || !input_dev) {
364 365
		err = -ENOMEM;
		goto err_out_free;
366
	}
L
Linus Torvalds 已提交
367

368 369
	ir->dev = input_dev;

L
Linus Torvalds 已提交
370 371 372 373
	/* init hardware-specific stuff */
	ir->mask_keycode = mask_keycode;
	ir->mask_keydown = mask_keydown;
	ir->mask_keyup   = mask_keyup;
374
	ir->polling      = polling;
375
	ir->rc5_gpio	 = rc5_gpio;
L
Linus Torvalds 已提交
376 377 378 379 380 381 382

	/* init input device */
	snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
		 saa7134_boards[dev->board].name);
	snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
		 pci_name(dev->pci));

383 384 385 386 387
	ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
	input_dev->name = ir->name;
	input_dev->phys = ir->phys;
	input_dev->id.bustype = BUS_PCI;
	input_dev->id.version = 1;
L
Linus Torvalds 已提交
388
	if (dev->pci->subsystem_vendor) {
389 390
		input_dev->id.vendor  = dev->pci->subsystem_vendor;
		input_dev->id.product = dev->pci->subsystem_device;
L
Linus Torvalds 已提交
391
	} else {
392 393
		input_dev->id.vendor  = dev->pci->vendor;
		input_dev->id.product = dev->pci->device;
L
Linus Torvalds 已提交
394
	}
395
	input_dev->dev.parent = &dev->pci->dev;
L
Linus Torvalds 已提交
396 397

	dev->remote = ir;
398 399 400 401 402
	saa7134_ir_start(dev, ir);

	err = input_register_device(ir->dev);
	if (err)
		goto err_out_stop;
L
Linus Torvalds 已提交
403

404 405 406 407
	/* the remote isn't as bouncy as a keyboard */
	ir->dev->rep[REP_DELAY] = repeat_delay;
	ir->dev->rep[REP_PERIOD] = repeat_period;

L
Linus Torvalds 已提交
408
	return 0;
409 410 411 412 413 414 415 416

 err_out_stop:
	saa7134_ir_stop(dev);
	dev->remote = NULL;
 err_out_free:
	input_free_device(input_dev);
	kfree(ir);
	return err;
L
Linus Torvalds 已提交
417 418 419 420 421 422 423
}

void saa7134_input_fini(struct saa7134_dev *dev)
{
	if (NULL == dev->remote)
		return;

424
	saa7134_ir_stop(dev);
425
	input_unregister_device(dev->remote->dev);
L
Linus Torvalds 已提交
426 427 428 429
	kfree(dev->remote);
	dev->remote = NULL;
}

430 431 432
void saa7134_set_i2c_ir(struct saa7134_dev *dev, struct IR_i2c *ir)
{
	if (disable_ir) {
433
		dprintk("Found supported i2c remote, but IR has been disabled\n");
434 435 436 437 438 439
		ir->get_key=NULL;
		return;
	}

	switch (dev->board) {
	case SAA7134_BOARD_PINNACLE_PCTV_110i:
440
	case SAA7134_BOARD_PINNACLE_PCTV_310i:
441
		snprintf(ir->c.name, sizeof(ir->c.name), "Pinnacle PCTV");
442 443 444 445 446 447 448
		if (pinnacle_remote == 0) {
			ir->get_key   = get_key_pinnacle_color;
			ir->ir_codes = ir_codes_pinnacle_color;
		} else {
			ir->get_key   = get_key_pinnacle_grey;
			ir->ir_codes = ir_codes_pinnacle_grey;
		}
449 450 451 452 453 454
		break;
	case SAA7134_BOARD_UPMOST_PURPLE_TV:
		snprintf(ir->c.name, sizeof(ir->c.name), "Purple TV");
		ir->get_key   = get_key_purpletv;
		ir->ir_codes  = ir_codes_purpletv;
		break;
455 456 457 458 459
	case SAA7134_BOARD_HAUPPAUGE_HVR1110:
		snprintf(ir->c.name, sizeof(ir->c.name), "HVR 1110");
		ir->get_key   = get_key_hvr1110;
		ir->ir_codes  = ir_codes_hauppauge_new;
		break;
460 461 462 463 464 465
	default:
		dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board);
		break;
	}

}
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508

static int saa7134_rc5_irq(struct saa7134_dev *dev)
{
	struct card_ir *ir = dev->remote;
	struct timeval tv;
	u32 gap;
	unsigned long current_jiffies, timeout;

	/* get time of bit */
	current_jiffies = jiffies;
	do_gettimeofday(&tv);

	/* avoid overflow with gap >1s */
	if (tv.tv_sec - ir->base_time.tv_sec > 1) {
		gap = 200000;
	} else {
		gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
		    tv.tv_usec - ir->base_time.tv_usec;
	}

	/* active code => add bit */
	if (ir->active) {
		/* only if in the code (otherwise spurious IRQ or timer
		   late) */
		if (ir->last_bit < 28) {
			ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
			    ir_rc5_remote_gap;
			ir->code |= 1 << ir->last_bit;
		}
		/* starting new code */
	} else {
		ir->active = 1;
		ir->code = 0;
		ir->base_time = tv;
		ir->last_bit = 0;

		timeout = current_jiffies + (500 + 30 * HZ) / 1000;
		mod_timer(&ir->timer_end, timeout);
	}

	return 1;
}

L
Linus Torvalds 已提交
509 510 511 512 513
/* ----------------------------------------------------------------------
 * Local variables:
 * c-basic-offset: 8
 * End:
 */