xref: /openbmc/linux/drivers/media/pci/saa7134/saa7134-input.c (revision 8631f940b81bf0da3d375fce166d381fa8c47bb2)
1 /*
2  *
3  * handle saa7134 IR remotes via linux kernel input layer.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16 
17 #include "saa7134.h"
18 #include "saa7134-reg.h"
19 
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/interrupt.h>
24 #include <linux/slab.h>
25 
26 #define MODULE_NAME "saa7134"
27 
28 static unsigned int disable_ir;
29 module_param(disable_ir, int, 0444);
30 MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
31 
32 static unsigned int ir_debug;
33 module_param(ir_debug, int, 0644);
34 MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
35 
36 static int pinnacle_remote;
37 module_param(pinnacle_remote, int, 0644);    /* Choose Pinnacle PCTV remote */
38 MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
39 
40 #define input_dbg(fmt, arg...) do { \
41 	if (ir_debug) \
42 		printk(KERN_DEBUG pr_fmt("input: " fmt), ## arg); \
43 	} while (0)
44 #define ir_dbg(ir, fmt, arg...) do { \
45 	if (ir_debug) \
46 		printk(KERN_DEBUG pr_fmt("ir %s: " fmt), ir->rc->device_name, \
47 		       ## arg); \
48 	} while (0)
49 
50 /* Helper function for raw decoding at GPIO16 or GPIO18 */
51 static int saa7134_raw_decode_irq(struct saa7134_dev *dev);
52 
53 /* -------------------- GPIO generic keycode builder -------------------- */
54 
55 static int build_key(struct saa7134_dev *dev)
56 {
57 	struct saa7134_card_ir *ir = dev->remote;
58 	u32 gpio, data;
59 
60 	/* here comes the additional handshake steps for some cards */
61 	switch (dev->board) {
62 	case SAA7134_BOARD_GOTVIEW_7135:
63 		saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
64 		saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
65 		break;
66 	}
67 	/* rising SAA7134_GPIO_GPRESCAN reads the status */
68 	saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
69 	saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
70 
71 	gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
72 	if (ir->polling) {
73 		if (ir->last_gpio == gpio)
74 			return 0;
75 		ir->last_gpio = gpio;
76 	}
77 
78 	data = ir_extract_bits(gpio, ir->mask_keycode);
79 	input_dbg("build_key gpio=0x%x mask=0x%x data=%d\n",
80 		gpio, ir->mask_keycode, data);
81 
82 	switch (dev->board) {
83 	case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
84 		if (data == ir->mask_keycode)
85 			rc_keyup(ir->dev);
86 		else
87 			rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
88 					     0);
89 		return 0;
90 	}
91 
92 	if (ir->polling) {
93 		if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
94 		    (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
95 			rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
96 					     0);
97 		} else {
98 			rc_keyup(ir->dev);
99 		}
100 	}
101 	else {	/* IRQ driven mode - handle key press and release in one go */
102 		if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
103 		    (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
104 			rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
105 					     0);
106 			rc_keyup(ir->dev);
107 		}
108 	}
109 
110 	return 0;
111 }
112 
113 /* --------------------- Chip specific I2C key builders ----------------- */
114 
115 static int get_key_flydvb_trio(struct IR_i2c *ir, enum rc_proto *protocol,
116 			       u32 *scancode, u8 *toggle)
117 {
118 	int gpio, rc;
119 	int attempt = 0;
120 	unsigned char b;
121 
122 	/* We need this to access GPI Used by the saa_readl macro. */
123 	struct saa7134_dev *dev = ir->c->adapter->algo_data;
124 
125 	if (dev == NULL) {
126 		ir_dbg(ir, "get_key_flydvb_trio: ir->c->adapter->algo_data is NULL!\n");
127 		return -EIO;
128 	}
129 
130 	/* rising SAA7134_GPIGPRESCAN reads the status */
131 	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
132 	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
133 
134 	gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
135 
136 	if (0x40000 & ~gpio)
137 		return 0; /* No button press */
138 
139 	/* poll IR chip */
140 	/* weak up the IR chip */
141 	b = 0;
142 
143 	while (1 != i2c_master_send(ir->c, &b, 1)) {
144 		if ((attempt++) < 10) {
145 			/*
146 			 * wait a bit for next attempt -
147 			 * I don't know how make it better
148 			 */
149 			msleep(10);
150 			continue;
151 		}
152 		ir_dbg(ir, "send wake up byte to pic16C505 (IR chip)failed %dx\n",
153 		       attempt);
154 		return -EIO;
155 	}
156 	rc = i2c_master_recv(ir->c, &b, 1);
157 	if (rc != 1) {
158 		ir_dbg(ir, "read error\n");
159 		if (rc < 0)
160 			return rc;
161 		return -EIO;
162 	}
163 
164 	*protocol = RC_PROTO_UNKNOWN;
165 	*scancode = b;
166 	*toggle = 0;
167 	return 1;
168 }
169 
170 static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir,
171 				       enum rc_proto *protocol,
172 				       u32 *scancode, u8 *toggle)
173 {
174 	unsigned char b;
175 	int gpio, rc;
176 
177 	/* <dev> is needed to access GPIO. Used by the saa_readl macro. */
178 	struct saa7134_dev *dev = ir->c->adapter->algo_data;
179 	if (dev == NULL) {
180 		ir_dbg(ir, "get_key_msi_tvanywhere_plus: ir->c->adapter->algo_data is NULL!\n");
181 		return -EIO;
182 	}
183 
184 	/* rising SAA7134_GPIO_GPRESCAN reads the status */
185 
186 	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
187 	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
188 
189 	gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
190 
191 	/* GPIO&0x40 is pulsed low when a button is pressed. Don't do
192 	   I2C receive if gpio&0x40 is not low. */
193 
194 	if (gpio & 0x40)
195 		return 0;       /* No button press */
196 
197 	/* GPIO says there is a button press. Get it. */
198 
199 	rc = i2c_master_recv(ir->c, &b, 1);
200 	if (rc != 1) {
201 		ir_dbg(ir, "read error\n");
202 		if (rc < 0)
203 			return rc;
204 		return -EIO;
205 	}
206 
207 	/* No button press */
208 
209 	if (b == 0xff)
210 		return 0;
211 
212 	/* Button pressed */
213 
214 	input_dbg("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
215 	*protocol = RC_PROTO_UNKNOWN;
216 	*scancode = b;
217 	*toggle = 0;
218 	return 1;
219 }
220 
221 /* copied and modified from get_key_msi_tvanywhere_plus() */
222 static int get_key_kworld_pc150u(struct IR_i2c *ir, enum rc_proto *protocol,
223 				 u32 *scancode, u8 *toggle)
224 {
225 	unsigned char b;
226 	unsigned int gpio;
227 	int rc;
228 
229 	/* <dev> is needed to access GPIO. Used by the saa_readl macro. */
230 	struct saa7134_dev *dev = ir->c->adapter->algo_data;
231 	if (dev == NULL) {
232 		ir_dbg(ir, "get_key_kworld_pc150u: ir->c->adapter->algo_data is NULL!\n");
233 		return -EIO;
234 	}
235 
236 	/* rising SAA7134_GPIO_GPRESCAN reads the status */
237 
238 	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
239 	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
240 
241 	gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
242 
243 	/* GPIO&0x100 is pulsed low when a button is pressed. Don't do
244 	   I2C receive if gpio&0x100 is not low. */
245 
246 	if (gpio & 0x100)
247 		return 0;       /* No button press */
248 
249 	/* GPIO says there is a button press. Get it. */
250 
251 	rc = i2c_master_recv(ir->c, &b, 1);
252 	if (rc != 1) {
253 		ir_dbg(ir, "read error\n");
254 		if (rc < 0)
255 			return rc;
256 		return -EIO;
257 	}
258 
259 	/* No button press */
260 
261 	if (b == 0xff)
262 		return 0;
263 
264 	/* Button pressed */
265 
266 	input_dbg("get_key_kworld_pc150u: Key = 0x%02X\n", b);
267 	*protocol = RC_PROTO_UNKNOWN;
268 	*scancode = b;
269 	*toggle = 0;
270 	return 1;
271 }
272 
273 static int get_key_purpletv(struct IR_i2c *ir, enum rc_proto *protocol,
274 			    u32 *scancode, u8 *toggle)
275 {
276 	int rc;
277 	unsigned char b;
278 
279 	/* poll IR chip */
280 	rc = i2c_master_recv(ir->c, &b, 1);
281 	if (rc != 1) {
282 		ir_dbg(ir, "read error\n");
283 		if (rc < 0)
284 			return rc;
285 		return -EIO;
286 	}
287 
288 	/* no button press */
289 	if (b==0)
290 		return 0;
291 
292 	/* repeating */
293 	if (b & 0x80)
294 		return 1;
295 
296 	*protocol = RC_PROTO_UNKNOWN;
297 	*scancode = b;
298 	*toggle = 0;
299 	return 1;
300 }
301 
302 static int get_key_beholdm6xx(struct IR_i2c *ir, enum rc_proto *protocol,
303 			      u32 *scancode, u8 *toggle)
304 {
305 	int rc;
306 	unsigned char data[12];
307 	u32 gpio;
308 
309 	struct saa7134_dev *dev = ir->c->adapter->algo_data;
310 
311 	/* rising SAA7134_GPIO_GPRESCAN reads the status */
312 	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
313 	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
314 
315 	gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
316 
317 	if (0x400000 & ~gpio)
318 		return 0; /* No button press */
319 
320 	ir->c->addr = 0x5a >> 1;
321 
322 	rc = i2c_master_recv(ir->c, data, 12);
323 	if (rc != 12) {
324 		ir_dbg(ir, "read error\n");
325 		if (rc < 0)
326 			return rc;
327 		return -EIO;
328 	}
329 
330 	if (data[9] != (unsigned char)(~data[8]))
331 		return 0;
332 
333 	*protocol = RC_PROTO_NECX;
334 	*scancode = RC_SCANCODE_NECX(data[11] << 8 | data[10], data[9]);
335 	*toggle = 0;
336 	return 1;
337 }
338 
339 /* Common (grey or coloured) pinnacle PCTV remote handling
340  *
341  */
342 static int get_key_pinnacle(struct IR_i2c *ir, enum rc_proto *protocol,
343 			    u32 *scancode, u8 *toggle, int parity_offset,
344 			    int marker, int code_modulo)
345 {
346 	int rc;
347 	unsigned char b[4];
348 	unsigned int start = 0,parity = 0,code = 0;
349 
350 	/* poll IR chip */
351 	rc = i2c_master_recv(ir->c, b, 4);
352 	if (rc != 4) {
353 		ir_dbg(ir, "read error\n");
354 		if (rc < 0)
355 			return rc;
356 		return -EIO;
357 	}
358 
359 	for (start = 0; start < ARRAY_SIZE(b); start++) {
360 		if (b[start] == marker) {
361 			code=b[(start+parity_offset + 1) % 4];
362 			parity=b[(start+parity_offset) % 4];
363 		}
364 	}
365 
366 	/* Empty Request */
367 	if (parity == 0)
368 		return 0;
369 
370 	/* Repeating... */
371 	if (ir->old == parity)
372 		return 0;
373 
374 	ir->old = parity;
375 
376 	/* drop special codes when a key is held down a long time for the grey controller
377 	   In this case, the second bit of the code is asserted */
378 	if (marker == 0xfe && (code & 0x40))
379 		return 0;
380 
381 	code %= code_modulo;
382 
383 	*protocol = RC_PROTO_UNKNOWN;
384 	*scancode = code;
385 	*toggle = 0;
386 
387 	ir_dbg(ir, "Pinnacle PCTV key %02x\n", code);
388 	return 1;
389 }
390 
391 /* The grey pinnacle PCTV remote
392  *
393  *  There are one issue with this remote:
394  *   - I2c packet does not change when the same key is pressed quickly. The workaround
395  *     is to hold down each key for about half a second, so that another code is generated
396  *     in the i2c packet, and the function can distinguish key presses.
397  *
398  * Sylvain Pasche <sylvain.pasche@gmail.com>
399  */
400 static int get_key_pinnacle_grey(struct IR_i2c *ir, enum rc_proto *protocol,
401 				 u32 *scancode, u8 *toggle)
402 {
403 
404 	return get_key_pinnacle(ir, protocol, scancode, toggle, 1, 0xfe, 0xff);
405 }
406 
407 
408 /* The new pinnacle PCTV remote (with the colored buttons)
409  *
410  * Ricardo Cerqueira <v4l@cerqueira.org>
411  */
412 static int get_key_pinnacle_color(struct IR_i2c *ir, enum rc_proto *protocol,
413 				  u32 *scancode, u8 *toggle)
414 {
415 	/* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
416 	 *
417 	 * this is the only value that results in 42 unique
418 	 * codes < 128
419 	 */
420 
421 	return get_key_pinnacle(ir, protocol, scancode, toggle, 2, 0x80, 0x88);
422 }
423 
424 void saa7134_input_irq(struct saa7134_dev *dev)
425 {
426 	struct saa7134_card_ir *ir;
427 
428 	if (!dev || !dev->remote)
429 		return;
430 
431 	ir = dev->remote;
432 	if (!ir->running)
433 		return;
434 
435 	if (!ir->polling && !ir->raw_decode) {
436 		build_key(dev);
437 	} else if (ir->raw_decode) {
438 		saa7134_raw_decode_irq(dev);
439 	}
440 }
441 
442 static void saa7134_input_timer(struct timer_list *t)
443 {
444 	struct saa7134_card_ir *ir = from_timer(ir, t, timer);
445 	struct saa7134_dev *dev = ir->dev->priv;
446 
447 	build_key(dev);
448 	mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
449 }
450 
451 int saa7134_ir_open(struct rc_dev *rc)
452 {
453 	struct saa7134_dev *dev = rc->priv;
454 	struct saa7134_card_ir *ir = dev->remote;
455 
456 	/* Moved here from saa7134_input_init1() because the latter
457 	 * is not called on device resume */
458 	switch (dev->board) {
459 	case SAA7134_BOARD_MD2819:
460 	case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
461 	case SAA7134_BOARD_AVERMEDIA_305:
462 	case SAA7134_BOARD_AVERMEDIA_307:
463 	case SAA7134_BOARD_AVERMEDIA_505:
464 	case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
465 	case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
466 	case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
467 	case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
468 	case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
469 	case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
470 	case SAA7134_BOARD_AVERMEDIA_M102:
471 	case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
472 		/* Without this we won't receive key up events */
473 		saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
474 		saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
475 		break;
476 	case SAA7134_BOARD_AVERMEDIA_777:
477 	case SAA7134_BOARD_AVERMEDIA_A16AR:
478 		/* Without this we won't receive key up events */
479 		saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
480 		saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
481 		break;
482 	case SAA7134_BOARD_AVERMEDIA_A16D:
483 		/* Without this we won't receive key up events */
484 		saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
485 		saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
486 		break;
487 	case SAA7134_BOARD_GOTVIEW_7135:
488 		saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
489 		break;
490 	}
491 
492 	ir->running = true;
493 
494 	if (ir->polling) {
495 		timer_setup(&ir->timer, saa7134_input_timer, 0);
496 		ir->timer.expires = jiffies + HZ;
497 		add_timer(&ir->timer);
498 	}
499 
500 	return 0;
501 }
502 
503 void saa7134_ir_close(struct rc_dev *rc)
504 {
505 	struct saa7134_dev *dev = rc->priv;
506 	struct saa7134_card_ir *ir = dev->remote;
507 
508 	if (ir->polling)
509 		del_timer_sync(&ir->timer);
510 
511 	ir->running = false;
512 }
513 
514 int saa7134_input_init1(struct saa7134_dev *dev)
515 {
516 	struct saa7134_card_ir *ir;
517 	struct rc_dev *rc;
518 	char *ir_codes = NULL;
519 	u32 mask_keycode = 0;
520 	u32 mask_keydown = 0;
521 	u32 mask_keyup   = 0;
522 	unsigned polling = 0;
523 	bool raw_decode  = false;
524 	int err;
525 
526 	if (dev->has_remote != SAA7134_REMOTE_GPIO)
527 		return -ENODEV;
528 	if (disable_ir)
529 		return -ENODEV;
530 
531 	/* detect & configure */
532 	switch (dev->board) {
533 	case SAA7134_BOARD_FLYVIDEO2000:
534 	case SAA7134_BOARD_FLYVIDEO3000:
535 	case SAA7134_BOARD_FLYTVPLATINUM_FM:
536 	case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
537 	case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
538 		ir_codes     = RC_MAP_FLYVIDEO;
539 		mask_keycode = 0xEC00000;
540 		mask_keydown = 0x0040000;
541 		break;
542 	case SAA7134_BOARD_CINERGY400:
543 	case SAA7134_BOARD_CINERGY600:
544 	case SAA7134_BOARD_CINERGY600_MK3:
545 		ir_codes     = RC_MAP_CINERGY;
546 		mask_keycode = 0x00003f;
547 		mask_keyup   = 0x040000;
548 		break;
549 	case SAA7134_BOARD_ECS_TVP3XP:
550 	case SAA7134_BOARD_ECS_TVP3XP_4CB5:
551 		ir_codes     = RC_MAP_EZTV;
552 		mask_keycode = 0x00017c;
553 		mask_keyup   = 0x000002;
554 		polling      = 50; // ms
555 		break;
556 	case SAA7134_BOARD_KWORLD_XPERT:
557 	case SAA7134_BOARD_AVACSSMARTTV:
558 		ir_codes     = RC_MAP_PIXELVIEW;
559 		mask_keycode = 0x00001F;
560 		mask_keyup   = 0x000020;
561 		polling      = 50; // ms
562 		break;
563 	case SAA7134_BOARD_MD2819:
564 	case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
565 	case SAA7134_BOARD_AVERMEDIA_305:
566 	case SAA7134_BOARD_AVERMEDIA_307:
567 	case SAA7134_BOARD_AVERMEDIA_505:
568 	case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
569 	case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
570 	case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
571 	case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
572 	case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
573 	case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
574 	case SAA7134_BOARD_AVERMEDIA_M102:
575 	case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
576 		ir_codes     = RC_MAP_AVERMEDIA;
577 		mask_keycode = 0x0007C8;
578 		mask_keydown = 0x000010;
579 		polling      = 50; // ms
580 		/* GPIO stuff moved to saa7134_ir_open() */
581 		break;
582 	case SAA7134_BOARD_AVERMEDIA_M135A:
583 		ir_codes     = RC_MAP_AVERMEDIA_M135A;
584 		mask_keydown = 0x0040000;	/* Enable GPIO18 line on both edges */
585 		mask_keyup   = 0x0040000;
586 		mask_keycode = 0xffff;
587 		raw_decode   = true;
588 		break;
589 	case SAA7134_BOARD_AVERMEDIA_M733A:
590 		ir_codes     = RC_MAP_AVERMEDIA_M733A_RM_K6;
591 		mask_keydown = 0x0040000;
592 		mask_keyup   = 0x0040000;
593 		mask_keycode = 0xffff;
594 		raw_decode   = true;
595 		break;
596 	case SAA7134_BOARD_AVERMEDIA_777:
597 	case SAA7134_BOARD_AVERMEDIA_A16AR:
598 		ir_codes     = RC_MAP_AVERMEDIA;
599 		mask_keycode = 0x02F200;
600 		mask_keydown = 0x000400;
601 		polling      = 50; // ms
602 		/* GPIO stuff moved to saa7134_ir_open() */
603 		break;
604 	case SAA7134_BOARD_AVERMEDIA_A16D:
605 		ir_codes     = RC_MAP_AVERMEDIA_A16D;
606 		mask_keycode = 0x02F200;
607 		mask_keydown = 0x000400;
608 		polling      = 50; /* ms */
609 		/* GPIO stuff moved to saa7134_ir_open() */
610 		break;
611 	case SAA7134_BOARD_KWORLD_TERMINATOR:
612 		ir_codes     = RC_MAP_PIXELVIEW;
613 		mask_keycode = 0x00001f;
614 		mask_keyup   = 0x000060;
615 		polling      = 50; // ms
616 		break;
617 	case SAA7134_BOARD_MANLI_MTV001:
618 	case SAA7134_BOARD_MANLI_MTV002:
619 		ir_codes     = RC_MAP_MANLI;
620 		mask_keycode = 0x001f00;
621 		mask_keyup   = 0x004000;
622 		polling      = 50; /* ms */
623 		break;
624 	case SAA7134_BOARD_BEHOLD_409FM:
625 	case SAA7134_BOARD_BEHOLD_401:
626 	case SAA7134_BOARD_BEHOLD_403:
627 	case SAA7134_BOARD_BEHOLD_403FM:
628 	case SAA7134_BOARD_BEHOLD_405:
629 	case SAA7134_BOARD_BEHOLD_405FM:
630 	case SAA7134_BOARD_BEHOLD_407:
631 	case SAA7134_BOARD_BEHOLD_407FM:
632 	case SAA7134_BOARD_BEHOLD_409:
633 	case SAA7134_BOARD_BEHOLD_505FM:
634 	case SAA7134_BOARD_BEHOLD_505RDS_MK5:
635 	case SAA7134_BOARD_BEHOLD_505RDS_MK3:
636 	case SAA7134_BOARD_BEHOLD_507_9FM:
637 	case SAA7134_BOARD_BEHOLD_507RDS_MK3:
638 	case SAA7134_BOARD_BEHOLD_507RDS_MK5:
639 		ir_codes     = RC_MAP_MANLI;
640 		mask_keycode = 0x003f00;
641 		mask_keyup   = 0x004000;
642 		polling      = 50; /* ms */
643 		break;
644 	case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
645 		ir_codes     = RC_MAP_BEHOLD_COLUMBUS;
646 		mask_keycode = 0x003f00;
647 		mask_keyup   = 0x004000;
648 		polling      = 50; // ms
649 		break;
650 	case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
651 		ir_codes     = RC_MAP_PCTV_SEDNA;
652 		mask_keycode = 0x001f00;
653 		mask_keyup   = 0x004000;
654 		polling      = 50; // ms
655 		break;
656 	case SAA7134_BOARD_GOTVIEW_7135:
657 		ir_codes     = RC_MAP_GOTVIEW7135;
658 		mask_keycode = 0x0003CC;
659 		mask_keydown = 0x000010;
660 		polling	     = 5; /* ms */
661 		/* GPIO stuff moved to saa7134_ir_open() */
662 		break;
663 	case SAA7134_BOARD_VIDEOMATE_TV_PVR:
664 	case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
665 	case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
666 		ir_codes     = RC_MAP_VIDEOMATE_TV_PVR;
667 		mask_keycode = 0x00003F;
668 		mask_keyup   = 0x400000;
669 		polling      = 50; // ms
670 		break;
671 	case SAA7134_BOARD_PROTEUS_2309:
672 		ir_codes     = RC_MAP_PROTEUS_2309;
673 		mask_keycode = 0x00007F;
674 		mask_keyup   = 0x000080;
675 		polling      = 50; // ms
676 		break;
677 	case SAA7134_BOARD_VIDEOMATE_DVBT_300:
678 	case SAA7134_BOARD_VIDEOMATE_DVBT_200:
679 		ir_codes     = RC_MAP_VIDEOMATE_TV_PVR;
680 		mask_keycode = 0x003F00;
681 		mask_keyup   = 0x040000;
682 		break;
683 	case SAA7134_BOARD_FLYDVBS_LR300:
684 	case SAA7134_BOARD_FLYDVBT_LR301:
685 	case SAA7134_BOARD_FLYDVBTDUO:
686 		ir_codes     = RC_MAP_FLYDVB;
687 		mask_keycode = 0x0001F00;
688 		mask_keydown = 0x0040000;
689 		break;
690 	case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
691 	case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
692 	case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
693 		ir_codes     = RC_MAP_ASUS_PC39;
694 		mask_keydown = 0x0040000;	/* Enable GPIO18 line on both edges */
695 		mask_keyup   = 0x0040000;
696 		mask_keycode = 0xffff;
697 		raw_decode   = true;
698 		break;
699 	case SAA7134_BOARD_ASUSTeK_PS3_100:
700 		ir_codes     = RC_MAP_ASUS_PS3_100;
701 		mask_keydown = 0x0040000;
702 		mask_keyup   = 0x0040000;
703 		mask_keycode = 0xffff;
704 		raw_decode   = true;
705 		break;
706 	case SAA7134_BOARD_ENCORE_ENLTV:
707 	case SAA7134_BOARD_ENCORE_ENLTV_FM:
708 		ir_codes     = RC_MAP_ENCORE_ENLTV;
709 		mask_keycode = 0x00007f;
710 		mask_keyup   = 0x040000;
711 		polling      = 50; // ms
712 		break;
713 	case SAA7134_BOARD_ENCORE_ENLTV_FM53:
714 	case SAA7134_BOARD_ENCORE_ENLTV_FM3:
715 		ir_codes     = RC_MAP_ENCORE_ENLTV_FM53;
716 		mask_keydown = 0x0040000;	/* Enable GPIO18 line on both edges */
717 		mask_keyup   = 0x0040000;
718 		mask_keycode = 0xffff;
719 		raw_decode   = true;
720 		break;
721 	case SAA7134_BOARD_10MOONSTVMASTER3:
722 		ir_codes     = RC_MAP_ENCORE_ENLTV;
723 		mask_keycode = 0x5f80000;
724 		mask_keyup   = 0x8000000;
725 		polling      = 50; //ms
726 		break;
727 	case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
728 		ir_codes     = RC_MAP_GENIUS_TVGO_A11MCE;
729 		mask_keycode = 0xff;
730 		mask_keydown = 0xf00000;
731 		polling = 50; /* ms */
732 		break;
733 	case SAA7134_BOARD_REAL_ANGEL_220:
734 		ir_codes     = RC_MAP_REAL_AUDIO_220_32_KEYS;
735 		mask_keycode = 0x3f00;
736 		mask_keyup   = 0x4000;
737 		polling = 50; /* ms */
738 		break;
739 	case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
740 		ir_codes     = RC_MAP_KWORLD_PLUS_TV_ANALOG;
741 		mask_keycode = 0x7f;
742 		polling = 40; /* ms */
743 		break;
744 	case SAA7134_BOARD_VIDEOMATE_S350:
745 		ir_codes     = RC_MAP_VIDEOMATE_S350;
746 		mask_keycode = 0x003f00;
747 		mask_keydown = 0x040000;
748 		break;
749 	case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
750 		ir_codes     = RC_MAP_WINFAST;
751 		mask_keycode = 0x5f00;
752 		mask_keyup   = 0x020000;
753 		polling      = 50; /* ms */
754 		break;
755 	case SAA7134_BOARD_VIDEOMATE_M1F:
756 		ir_codes     = RC_MAP_VIDEOMATE_K100;
757 		mask_keycode = 0x0ff00;
758 		mask_keyup   = 0x040000;
759 		break;
760 	case SAA7134_BOARD_HAUPPAUGE_HVR1150:
761 	case SAA7134_BOARD_HAUPPAUGE_HVR1120:
762 		ir_codes     = RC_MAP_HAUPPAUGE;
763 		mask_keydown = 0x0040000;	/* Enable GPIO18 line on both edges */
764 		mask_keyup   = 0x0040000;
765 		mask_keycode = 0xffff;
766 		raw_decode   = true;
767 		break;
768 	case SAA7134_BOARD_LEADTEK_WINFAST_TV2100_FM:
769 		ir_codes     = RC_MAP_LEADTEK_Y04G0051;
770 		mask_keydown = 0x0040000;	/* Enable GPIO18 line on both edges */
771 		mask_keyup   = 0x0040000;
772 		mask_keycode = 0xffff;
773 		raw_decode   = true;
774 		break;
775 	}
776 	if (NULL == ir_codes) {
777 		pr_err("Oops: IR config error [card=%d]\n", dev->board);
778 		return -ENODEV;
779 	}
780 
781 	ir = kzalloc(sizeof(*ir), GFP_KERNEL);
782 	rc = rc_allocate_device(RC_DRIVER_SCANCODE);
783 	if (!ir || !rc) {
784 		err = -ENOMEM;
785 		goto err_out_free;
786 	}
787 
788 	ir->dev = rc;
789 	dev->remote = ir;
790 
791 	/* init hardware-specific stuff */
792 	ir->mask_keycode = mask_keycode;
793 	ir->mask_keydown = mask_keydown;
794 	ir->mask_keyup   = mask_keyup;
795 	ir->polling      = polling;
796 	ir->raw_decode	 = raw_decode;
797 
798 	/* init input device */
799 	snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
800 		 pci_name(dev->pci));
801 
802 	rc->priv = dev;
803 	rc->open = saa7134_ir_open;
804 	rc->close = saa7134_ir_close;
805 	if (raw_decode) {
806 		rc->driver_type = RC_DRIVER_IR_RAW;
807 		rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
808 	}
809 
810 	rc->device_name = saa7134_boards[dev->board].name;
811 	rc->input_phys = ir->phys;
812 	rc->input_id.bustype = BUS_PCI;
813 	rc->input_id.version = 1;
814 	if (dev->pci->subsystem_vendor) {
815 		rc->input_id.vendor  = dev->pci->subsystem_vendor;
816 		rc->input_id.product = dev->pci->subsystem_device;
817 	} else {
818 		rc->input_id.vendor  = dev->pci->vendor;
819 		rc->input_id.product = dev->pci->device;
820 	}
821 	rc->dev.parent = &dev->pci->dev;
822 	rc->map_name = ir_codes;
823 	rc->driver_name = MODULE_NAME;
824 	rc->min_timeout = 1;
825 	rc->timeout = IR_DEFAULT_TIMEOUT;
826 	rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
827 
828 	err = rc_register_device(rc);
829 	if (err)
830 		goto err_out_free;
831 
832 	return 0;
833 
834 err_out_free:
835 	rc_free_device(rc);
836 	dev->remote = NULL;
837 	kfree(ir);
838 	return err;
839 }
840 
841 void saa7134_input_fini(struct saa7134_dev *dev)
842 {
843 	if (NULL == dev->remote)
844 		return;
845 
846 	rc_unregister_device(dev->remote->dev);
847 	kfree(dev->remote);
848 	dev->remote = NULL;
849 }
850 
851 void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
852 {
853 	struct i2c_board_info info;
854 	struct i2c_msg msg_msi = {
855 		.addr = 0x50,
856 		.flags = I2C_M_RD,
857 		.len = 0,
858 		.buf = NULL,
859 	};
860 	int rc;
861 
862 	if (disable_ir) {
863 		input_dbg("IR has been disabled, not probing for i2c remote\n");
864 		return;
865 	}
866 
867 	memset(&info, 0, sizeof(struct i2c_board_info));
868 	memset(&dev->init_data, 0, sizeof(dev->init_data));
869 	strscpy(info.type, "ir_video", I2C_NAME_SIZE);
870 
871 	switch (dev->board) {
872 	case SAA7134_BOARD_PINNACLE_PCTV_110i:
873 	case SAA7134_BOARD_PINNACLE_PCTV_310i:
874 		dev->init_data.name = "Pinnacle PCTV";
875 		if (pinnacle_remote == 0) {
876 			dev->init_data.get_key = get_key_pinnacle_color;
877 			dev->init_data.ir_codes = RC_MAP_PINNACLE_COLOR;
878 			info.addr = 0x47;
879 		} else {
880 			dev->init_data.get_key = get_key_pinnacle_grey;
881 			dev->init_data.ir_codes = RC_MAP_PINNACLE_GREY;
882 			info.addr = 0x47;
883 		}
884 		break;
885 	case SAA7134_BOARD_UPMOST_PURPLE_TV:
886 		dev->init_data.name = "Purple TV";
887 		dev->init_data.get_key = get_key_purpletv;
888 		dev->init_data.ir_codes = RC_MAP_PURPLETV;
889 		info.addr = 0x7a;
890 		break;
891 	case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
892 		dev->init_data.name = "MSI TV@nywhere Plus";
893 		dev->init_data.get_key = get_key_msi_tvanywhere_plus;
894 		dev->init_data.ir_codes = RC_MAP_MSI_TVANYWHERE_PLUS;
895 		/*
896 		 * MSI TV@nyware Plus requires more frequent polling
897 		 * otherwise it will miss some keypresses
898 		 */
899 		dev->init_data.polling_interval = 50;
900 		info.addr = 0x30;
901 		/* MSI TV@nywhere Plus controller doesn't seem to
902 		   respond to probes unless we read something from
903 		   an existing device. Weird...
904 		   REVISIT: might no longer be needed */
905 		rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
906 		input_dbg("probe 0x%02x @ %s: %s\n",
907 			msg_msi.addr, dev->i2c_adap.name,
908 			(1 == rc) ? "yes" : "no");
909 		break;
910 	case SAA7134_BOARD_SNAZIO_TVPVR_PRO:
911 		dev->init_data.name = "SnaZio* TVPVR PRO";
912 		dev->init_data.get_key = get_key_msi_tvanywhere_plus;
913 		dev->init_data.ir_codes = RC_MAP_MSI_TVANYWHERE_PLUS;
914 		/*
915 		 * MSI TV@nyware Plus requires more frequent polling
916 		 * otherwise it will miss some keypresses
917 		 */
918 		dev->init_data.polling_interval = 50;
919 		info.addr = 0x30;
920 		/*
921 		 * MSI TV@nywhere Plus controller doesn't seem to
922 		 *  respond to probes unless we read something from
923 		 *  an existing device. Weird...
924 		 * REVISIT: might no longer be needed
925 		 */
926 		rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
927 		input_dbg("probe 0x%02x @ %s: %s\n",
928 			msg_msi.addr, dev->i2c_adap.name,
929 			(rc == 1) ? "yes" : "no");
930 		break;
931 	case SAA7134_BOARD_KWORLD_PC150U:
932 		/* copied and modified from MSI TV@nywhere Plus */
933 		dev->init_data.name = "Kworld PC150-U";
934 		dev->init_data.get_key = get_key_kworld_pc150u;
935 		dev->init_data.ir_codes = RC_MAP_KWORLD_PC150U;
936 		info.addr = 0x30;
937 		/* MSI TV@nywhere Plus controller doesn't seem to
938 		   respond to probes unless we read something from
939 		   an existing device. Weird...
940 		   REVISIT: might no longer be needed */
941 		rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
942 		input_dbg("probe 0x%02x @ %s: %s\n",
943 			msg_msi.addr, dev->i2c_adap.name,
944 			(1 == rc) ? "yes" : "no");
945 		break;
946 	case SAA7134_BOARD_HAUPPAUGE_HVR1110:
947 		dev->init_data.name = saa7134_boards[dev->board].name;
948 		dev->init_data.ir_codes = RC_MAP_HAUPPAUGE;
949 		dev->init_data.type = RC_PROTO_BIT_RC5 |
950 				RC_PROTO_BIT_RC6_MCE | RC_PROTO_BIT_RC6_6A_32;
951 		dev->init_data.internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
952 		info.addr = 0x71;
953 		break;
954 	case SAA7134_BOARD_BEHOLD_607FM_MK3:
955 	case SAA7134_BOARD_BEHOLD_607FM_MK5:
956 	case SAA7134_BOARD_BEHOLD_609FM_MK3:
957 	case SAA7134_BOARD_BEHOLD_609FM_MK5:
958 	case SAA7134_BOARD_BEHOLD_607RDS_MK3:
959 	case SAA7134_BOARD_BEHOLD_607RDS_MK5:
960 	case SAA7134_BOARD_BEHOLD_609RDS_MK3:
961 	case SAA7134_BOARD_BEHOLD_609RDS_MK5:
962 	case SAA7134_BOARD_BEHOLD_M6:
963 	case SAA7134_BOARD_BEHOLD_M63:
964 	case SAA7134_BOARD_BEHOLD_M6_EXTRA:
965 	case SAA7134_BOARD_BEHOLD_H6:
966 	case SAA7134_BOARD_BEHOLD_X7:
967 	case SAA7134_BOARD_BEHOLD_H7:
968 	case SAA7134_BOARD_BEHOLD_A7:
969 		dev->init_data.name = "BeholdTV";
970 		dev->init_data.get_key = get_key_beholdm6xx;
971 		dev->init_data.ir_codes = RC_MAP_BEHOLD;
972 		dev->init_data.type = RC_PROTO_BIT_NECX;
973 		info.addr = 0x2d;
974 		break;
975 	case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
976 	case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
977 		info.addr = 0x40;
978 		break;
979 	case SAA7134_BOARD_AVERMEDIA_A706:
980 		info.addr = 0x41;
981 		break;
982 	case SAA7134_BOARD_FLYDVB_TRIO:
983 		dev->init_data.name = "FlyDVB Trio";
984 		dev->init_data.get_key = get_key_flydvb_trio;
985 		dev->init_data.ir_codes = RC_MAP_FLYDVB;
986 		info.addr = 0x0b;
987 		break;
988 	default:
989 		input_dbg("No I2C IR support for board %x\n", dev->board);
990 		return;
991 	}
992 
993 	if (dev->init_data.name)
994 		info.platform_data = &dev->init_data;
995 	i2c_new_device(&dev->i2c_adap, &info);
996 }
997 
998 static int saa7134_raw_decode_irq(struct saa7134_dev *dev)
999 {
1000 	struct saa7134_card_ir *ir = dev->remote;
1001 	int space;
1002 
1003 	/* Generate initial event */
1004 	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1005 	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1006 	space = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
1007 	ir_raw_event_store_edge(dev->remote->dev, !space);
1008 
1009 	return 1;
1010 }
1011