3 * handle saa7134 IR remotes via linux kernel input layer.
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.
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.
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
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/input.h>
27 #include "saa7134-reg.h"
30 static unsigned int disable_ir;
31 module_param(disable_ir, int, 0444);
32 MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
34 static unsigned int ir_debug;
35 module_param(ir_debug, int, 0644);
36 MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
38 static int pinnacle_remote;
39 module_param(pinnacle_remote, int, 0644); /* Choose Pinnacle PCTV remote */
40 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 module_param(ir_rc5_key_timeout, int, 0644);
47 static int repeat_delay = 500;
48 module_param(repeat_delay, int, 0644);
49 MODULE_PARM_DESC(repeat_delay, "delay before key repeat started");
50 static int repeat_period = 33;
51 module_param(repeat_period, int, 0644);
52 MODULE_PARM_DESC(repeat_period, "repeat period between "
53 "keypresses when key is down");
55 static unsigned int disable_other_ir;
56 module_param(disable_other_ir, int, 0644);
57 MODULE_PARM_DESC(disable_other_ir, "disable full codes of "
58 "alternative remotes from other manufacturers");
60 #define dprintk(fmt, arg...) if (ir_debug) \
61 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
62 #define i2cdprintk(fmt, arg...) if (ir_debug) \
63 printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg)
66 static int saa7134_rc5_irq(struct saa7134_dev *dev);
68 /* -------------------- GPIO generic keycode builder -------------------- */
70 static int build_key(struct saa7134_dev *dev)
72 struct card_ir *ir = dev->remote;
75 /* here comes the additional handshake steps for some cards */
77 case SAA7134_BOARD_GOTVIEW_7135:
78 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
79 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
82 /* rising SAA7134_GPIO_GPRESCAN reads the status */
83 saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
84 saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
86 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
88 if (ir->last_gpio == gpio)
93 data = ir_extract_bits(gpio, ir->mask_keycode);
94 dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
95 gpio, ir->mask_keycode, data);
98 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
99 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
100 ir_input_keydown(ir->dev, &ir->ir, data, data);
102 ir_input_nokey(ir->dev, &ir->ir);
105 else { /* IRQ driven mode - handle key press and release in one go */
106 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
107 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
108 ir_input_keydown(ir->dev, &ir->ir, data, data);
109 ir_input_nokey(ir->dev, &ir->ir);
116 /* --------------------- Chip specific I2C key builders ----------------- */
118 static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
123 if (1 != i2c_master_recv(&ir->c,&b,1)) {
124 i2cdprintk("read error\n");
128 /* no button press */
141 static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
143 unsigned char buf[5], cod4, code3, code4;
146 if (5 != i2c_master_recv(&ir->c,buf,5))
163 static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
165 unsigned char data[12];
168 struct saa7134_dev *dev = ir->c.adapter->algo_data;
170 /* rising SAA7134_GPIO_GPRESCAN reads the status */
171 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
172 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
174 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
176 if (0x400000 & ~gpio)
177 return 0; /* No button press */
179 ir->c.addr = 0x5a >> 1;
181 if (12 != i2c_master_recv(&ir->c, data, 12)) {
182 i2cdprintk("read error\n");
185 /* IR of this card normally decode signals NEC-standard from
186 * - Sven IHOO MT 5.1R remote. xxyye718
187 * - Sven DVD HD-10xx remote. xxyyf708
190 * So, skip not our, if disable full codes mode.
192 if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
201 void saa7134_input_irq(struct saa7134_dev *dev)
203 struct card_ir *ir = dev->remote;
205 if (!ir->polling && !ir->rc5_gpio) {
207 } else if (ir->rc5_gpio) {
208 saa7134_rc5_irq(dev);
212 static void saa7134_input_timer(unsigned long data)
214 struct saa7134_dev *dev = (struct saa7134_dev *)data;
215 struct card_ir *ir = dev->remote;
218 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
221 void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
224 setup_timer(&ir->timer, saa7134_input_timer,
226 ir->timer.expires = jiffies + HZ;
227 add_timer(&ir->timer);
228 } else if (ir->rc5_gpio) {
229 /* set timer_end for code completion */
230 init_timer(&ir->timer_end);
231 ir->timer_end.function = ir_rc5_timer_end;
232 ir->timer_end.data = (unsigned long)ir;
233 init_timer(&ir->timer_keyup);
234 ir->timer_keyup.function = ir_rc5_timer_keyup;
235 ir->timer_keyup.data = (unsigned long)ir;
239 ir->rc5_key_timeout = ir_rc5_key_timeout;
240 ir->rc5_remote_gap = ir_rc5_remote_gap;
244 void saa7134_ir_stop(struct saa7134_dev *dev)
246 if (dev->remote->polling)
247 del_timer_sync(&dev->remote->timer);
250 int saa7134_input_init1(struct saa7134_dev *dev)
253 struct input_dev *input_dev;
254 IR_KEYTAB_TYPE *ir_codes = NULL;
255 u32 mask_keycode = 0;
256 u32 mask_keydown = 0;
260 int ir_type = IR_TYPE_OTHER;
263 if (dev->has_remote != SAA7134_REMOTE_GPIO)
268 /* detect & configure */
269 switch (dev->board) {
270 case SAA7134_BOARD_FLYVIDEO2000:
271 case SAA7134_BOARD_FLYVIDEO3000:
272 case SAA7134_BOARD_FLYTVPLATINUM_FM:
273 case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
274 ir_codes = ir_codes_flyvideo;
275 mask_keycode = 0xEC00000;
276 mask_keydown = 0x0040000;
278 case SAA7134_BOARD_CINERGY400:
279 case SAA7134_BOARD_CINERGY600:
280 case SAA7134_BOARD_CINERGY600_MK3:
281 ir_codes = ir_codes_cinergy;
282 mask_keycode = 0x00003f;
283 mask_keyup = 0x040000;
285 case SAA7134_BOARD_ECS_TVP3XP:
286 case SAA7134_BOARD_ECS_TVP3XP_4CB5:
287 ir_codes = ir_codes_eztv;
288 mask_keycode = 0x00017c;
289 mask_keyup = 0x000002;
292 case SAA7134_BOARD_KWORLD_XPERT:
293 case SAA7134_BOARD_AVACSSMARTTV:
294 ir_codes = ir_codes_pixelview;
295 mask_keycode = 0x00001F;
296 mask_keyup = 0x000020;
299 case SAA7134_BOARD_MD2819:
300 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
301 case SAA7134_BOARD_AVERMEDIA_305:
302 case SAA7134_BOARD_AVERMEDIA_307:
303 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
304 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
305 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
306 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
307 case SAA7134_BOARD_AVERMEDIA_M102:
308 ir_codes = ir_codes_avermedia;
309 mask_keycode = 0x0007C8;
310 mask_keydown = 0x000010;
312 /* Set GPIO pin2 to high to enable the IR controller */
313 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
314 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
316 case SAA7134_BOARD_AVERMEDIA_777:
317 case SAA7134_BOARD_AVERMEDIA_A16AR:
318 ir_codes = ir_codes_avermedia;
319 mask_keycode = 0x02F200;
320 mask_keydown = 0x000400;
322 /* Without this we won't receive key up events */
323 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
324 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
326 case SAA7134_BOARD_AVERMEDIA_A16D:
327 ir_codes = ir_codes_avermedia_a16d;
328 mask_keycode = 0x02F200;
329 mask_keydown = 0x000400;
330 polling = 50; /* ms */
331 /* Without this we won't receive key up events */
332 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
333 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
335 case SAA7134_BOARD_KWORLD_TERMINATOR:
336 ir_codes = ir_codes_pixelview;
337 mask_keycode = 0x00001f;
338 mask_keyup = 0x000060;
341 case SAA7134_BOARD_MANLI_MTV001:
342 case SAA7134_BOARD_MANLI_MTV002:
343 ir_codes = ir_codes_manli;
344 mask_keycode = 0x001f00;
345 mask_keyup = 0x004000;
346 polling = 50; /* ms */
348 case SAA7134_BOARD_BEHOLD_409FM:
349 case SAA7134_BOARD_BEHOLD_401:
350 case SAA7134_BOARD_BEHOLD_403:
351 case SAA7134_BOARD_BEHOLD_403FM:
352 case SAA7134_BOARD_BEHOLD_405:
353 case SAA7134_BOARD_BEHOLD_405FM:
354 case SAA7134_BOARD_BEHOLD_407:
355 case SAA7134_BOARD_BEHOLD_407FM:
356 case SAA7134_BOARD_BEHOLD_409:
357 case SAA7134_BOARD_BEHOLD_505FM:
358 case SAA7134_BOARD_BEHOLD_507_9FM:
359 ir_codes = ir_codes_manli;
360 mask_keycode = 0x003f00;
361 mask_keyup = 0x004000;
362 polling = 50; /* ms */
364 case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
365 ir_codes = ir_codes_behold_columbus;
366 mask_keycode = 0x003f00;
367 mask_keyup = 0x004000;
370 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
371 ir_codes = ir_codes_pctv_sedna;
372 mask_keycode = 0x001f00;
373 mask_keyup = 0x004000;
376 case SAA7134_BOARD_GOTVIEW_7135:
377 ir_codes = ir_codes_gotview7135;
378 mask_keycode = 0x0003CC;
379 mask_keydown = 0x000010;
380 polling = 5; /* ms */
381 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
383 case SAA7134_BOARD_VIDEOMATE_TV_PVR:
384 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
385 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
386 ir_codes = ir_codes_videomate_tv_pvr;
387 mask_keycode = 0x00003F;
388 mask_keyup = 0x400000;
391 case SAA7134_BOARD_PROTEUS_2309:
392 ir_codes = ir_codes_proteus_2309;
393 mask_keycode = 0x00007F;
394 mask_keyup = 0x000080;
397 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
398 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
399 ir_codes = ir_codes_videomate_tv_pvr;
400 mask_keycode = 0x003F00;
401 mask_keyup = 0x040000;
403 case SAA7134_BOARD_FLYDVBS_LR300:
404 case SAA7134_BOARD_FLYDVBT_LR301:
405 case SAA7134_BOARD_FLYDVBTDUO:
406 ir_codes = ir_codes_flydvb;
407 mask_keycode = 0x0001F00;
408 mask_keydown = 0x0040000;
410 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
411 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
412 ir_codes = ir_codes_asus_pc39;
413 mask_keydown = 0x0040000;
416 case SAA7134_BOARD_ENCORE_ENLTV:
417 case SAA7134_BOARD_ENCORE_ENLTV_FM:
418 ir_codes = ir_codes_encore_enltv;
419 mask_keycode = 0x00007f;
420 mask_keyup = 0x040000;
423 case SAA7134_BOARD_10MOONSTVMASTER3:
424 ir_codes = ir_codes_encore_enltv;
425 mask_keycode = 0x5f80000;
426 mask_keyup = 0x8000000;
429 case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
430 ir_codes = ir_codes_genius_tvgo_a11mce;
432 mask_keydown = 0xf00000;
433 polling = 50; /* ms */
436 if (NULL == ir_codes) {
437 printk("%s: Oops: IR config error [card=%d]\n",
438 dev->name, dev->board);
442 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
443 input_dev = input_allocate_device();
444 if (!ir || !input_dev) {
451 /* init hardware-specific stuff */
452 ir->mask_keycode = mask_keycode;
453 ir->mask_keydown = mask_keydown;
454 ir->mask_keyup = mask_keyup;
455 ir->polling = polling;
456 ir->rc5_gpio = rc5_gpio;
458 /* init input device */
459 snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
460 saa7134_boards[dev->board].name);
461 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
464 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
465 input_dev->name = ir->name;
466 input_dev->phys = ir->phys;
467 input_dev->id.bustype = BUS_PCI;
468 input_dev->id.version = 1;
469 if (dev->pci->subsystem_vendor) {
470 input_dev->id.vendor = dev->pci->subsystem_vendor;
471 input_dev->id.product = dev->pci->subsystem_device;
473 input_dev->id.vendor = dev->pci->vendor;
474 input_dev->id.product = dev->pci->device;
476 input_dev->dev.parent = &dev->pci->dev;
479 saa7134_ir_start(dev, ir);
481 err = input_register_device(ir->dev);
485 /* the remote isn't as bouncy as a keyboard */
486 ir->dev->rep[REP_DELAY] = repeat_delay;
487 ir->dev->rep[REP_PERIOD] = repeat_period;
492 saa7134_ir_stop(dev);
495 input_free_device(input_dev);
500 void saa7134_input_fini(struct saa7134_dev *dev)
502 if (NULL == dev->remote)
505 saa7134_ir_stop(dev);
506 input_unregister_device(dev->remote->dev);
511 void saa7134_set_i2c_ir(struct saa7134_dev *dev, struct IR_i2c *ir)
514 dprintk("Found supported i2c remote, but IR has been disabled\n");
519 switch (dev->board) {
520 case SAA7134_BOARD_PINNACLE_PCTV_110i:
521 case SAA7134_BOARD_PINNACLE_PCTV_310i:
522 snprintf(ir->c.name, sizeof(ir->c.name), "Pinnacle PCTV");
523 if (pinnacle_remote == 0) {
524 ir->get_key = get_key_pinnacle_color;
525 ir->ir_codes = ir_codes_pinnacle_color;
527 ir->get_key = get_key_pinnacle_grey;
528 ir->ir_codes = ir_codes_pinnacle_grey;
531 case SAA7134_BOARD_UPMOST_PURPLE_TV:
532 snprintf(ir->c.name, sizeof(ir->c.name), "Purple TV");
533 ir->get_key = get_key_purpletv;
534 ir->ir_codes = ir_codes_purpletv;
536 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
537 snprintf(ir->c.name, sizeof(ir->c.name), "HVR 1110");
538 ir->get_key = get_key_hvr1110;
539 ir->ir_codes = ir_codes_hauppauge_new;
541 case SAA7134_BOARD_BEHOLD_607_9FM:
542 case SAA7134_BOARD_BEHOLD_M6:
543 case SAA7134_BOARD_BEHOLD_H6:
544 snprintf(ir->c.name, sizeof(ir->c.name), "BeholdTV");
545 ir->get_key = get_key_beholdm6xx;
546 ir->ir_codes = ir_codes_behold;
549 dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board);
555 static int saa7134_rc5_irq(struct saa7134_dev *dev)
557 struct card_ir *ir = dev->remote;
560 unsigned long current_jiffies, timeout;
562 /* get time of bit */
563 current_jiffies = jiffies;
564 do_gettimeofday(&tv);
566 /* avoid overflow with gap >1s */
567 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
570 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
571 tv.tv_usec - ir->base_time.tv_usec;
574 /* active code => add bit */
576 /* only if in the code (otherwise spurious IRQ or timer
578 if (ir->last_bit < 28) {
579 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
581 ir->code |= 1 << ir->last_bit;
583 /* starting new code */
590 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
591 mod_timer(&ir->timer_end, timeout);
597 /* ----------------------------------------------------------------------