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