3 * Copyright (c) 2003 Gerd Knorr
4 * Copyright (c) 2003 Pavel Machek
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * 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>
26 #include <linux/slab.h>
33 module_param(ir_debug, int, 0644);
35 static int ir_rc5_remote_gap = 885;
36 module_param(ir_rc5_remote_gap, int, 0644);
39 #define dprintk(arg...) do { \
44 #define DEVNAME "bttv-input"
46 #define MODULE_NAME "bttv"
48 /* ---------------------------------------------------------------------- */
50 static void ir_handle_key(struct bttv *btv)
52 struct bttv_ir *ir = btv->remote;
56 gpio = bttv_gpio_read(&btv->c);
58 if (ir->last_gpio == gpio)
64 data = ir_extract_bits(gpio, ir->mask_keycode);
65 dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
67 ir->polling ? "poll" : "irq",
68 (gpio & ir->mask_keydown) ? " down" : "",
69 (gpio & ir->mask_keyup) ? " up" : "");
71 if ((ir->mask_keydown && (gpio & ir->mask_keydown)) ||
72 (ir->mask_keyup && !(gpio & ir->mask_keyup))) {
73 rc_keydown_notimeout(ir->dev, data, 0);
75 /* HACK: Probably, ir->mask_keydown is missing
77 if (btv->c.type == BTTV_BOARD_WINFAST2000)
78 rc_keydown_notimeout(ir->dev, data, 0);
84 static void ir_enltv_handle_key(struct bttv *btv)
86 struct bttv_ir *ir = btv->remote;
87 u32 gpio, data, keyup;
90 gpio = bttv_gpio_read(&btv->c);
93 data = ir_extract_bits(gpio, ir->mask_keycode);
95 /* Check if it is keyup */
96 keyup = (gpio & ir->mask_keyup) ? 1 << 31 : 0;
98 if ((ir->last_gpio & 0x7f) != data) {
99 dprintk(KERN_INFO DEVNAME ": gpio=0x%x code=%d | %s\n",
101 (gpio & ir->mask_keyup) ? " up" : "up/down");
103 rc_keydown_notimeout(ir->dev, data, 0);
107 if ((ir->last_gpio & 1 << 31) == keyup)
110 dprintk(KERN_INFO DEVNAME ":(cnt) gpio=0x%x code=%d | %s\n",
112 (gpio & ir->mask_keyup) ? " up" : "down");
117 rc_keydown_notimeout(ir->dev, data, 0);
120 ir->last_gpio = data | keyup;
123 void bttv_input_irq(struct bttv *btv)
125 struct bttv_ir *ir = btv->remote;
131 static void bttv_input_timer(unsigned long data)
133 struct bttv *btv = (struct bttv*)data;
134 struct bttv_ir *ir = btv->remote;
136 if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
137 ir_enltv_handle_key(btv);
140 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
144 * FIXME: Nebula digi uses the legacy way to decode RC5, instead of relying
145 * on the rc-core way. As we need to be sure that both IRQ transitions are
146 * properly triggered, Better to touch it only with this hardware for
150 #define RC5_START(x) (((x) >> 12) & 3)
151 #define RC5_TOGGLE(x) (((x) >> 11) & 1)
152 #define RC5_ADDR(x) (((x) >> 6) & 31)
153 #define RC5_INSTR(x) ((x) & 63)
155 /* decode raw bit pattern to RC5 code */
156 static u32 bttv_rc5_decode(unsigned int code)
158 unsigned int org_code = code;
160 unsigned int rc5 = 0;
163 for (i = 0; i < 14; ++i) {
176 dprintk(KERN_INFO DEVNAME ":rc5_decode(%x) bad code\n",
181 dprintk(KERN_INFO DEVNAME ":"
182 "code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
183 "instr=%x\n", rc5, org_code, RC5_START(rc5),
184 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
188 static void bttv_rc5_timer_end(unsigned long data)
190 struct bttv_ir *ir = (struct bttv_ir *)data;
192 unsigned long current_jiffies;
197 current_jiffies = jiffies;
198 do_gettimeofday(&tv);
200 /* avoid overflow with gap >1s */
201 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
204 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
205 tv.tv_usec - ir->base_time.tv_usec;
208 /* signal we're ready to start a new code */
211 /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
213 dprintk(KERN_INFO DEVNAME ": spurious timer_end\n");
217 if (ir->last_bit < 20) {
218 /* ignore spurious codes (caused by light/other remotes) */
219 dprintk(KERN_INFO DEVNAME ": short code: %x\n", ir->code);
221 ir->code = (ir->code << ir->shift_by) | 1;
222 rc5 = bttv_rc5_decode(ir->code);
224 /* two start bits? */
225 if (RC5_START(rc5) != ir->start) {
226 printk(KERN_INFO DEVNAME ":"
227 " rc5 start bits invalid: %u\n", RC5_START(rc5));
230 } else if (RC5_ADDR(rc5) == ir->addr) {
231 u32 toggle = RC5_TOGGLE(rc5);
232 u32 instr = RC5_INSTR(rc5);
235 rc_keydown(ir->dev, instr, toggle);
236 dprintk(KERN_INFO DEVNAME ":"
237 " instruction %x, toggle %x\n",
243 static int bttv_rc5_irq(struct bttv *btv)
245 struct bttv_ir *ir = btv->remote;
249 unsigned long current_jiffies;
252 gpio = bttv_gpio_read(&btv->c);
258 /* get time of bit */
259 current_jiffies = jiffies;
260 do_gettimeofday(&tv);
262 /* avoid overflow with gap >1s */
263 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
266 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
267 tv.tv_usec - ir->base_time.tv_usec;
270 /* active code => add bit */
272 /* only if in the code (otherwise spurious IRQ or timer
274 if (ir->last_bit < 28) {
275 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
277 ir->code |= 1 << ir->last_bit;
279 /* starting new code */
286 mod_timer(&ir->timer, current_jiffies + msecs_to_jiffies(30));
289 /* toggle GPIO pin 4 to reset the irq */
290 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
291 bttv_gpio_write(&btv->c, gpio | (1 << 4));
295 /* ---------------------------------------------------------------------- */
297 static void bttv_ir_start(struct bttv *btv, struct bttv_ir *ir)
300 setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
301 ir->timer.expires = jiffies + msecs_to_jiffies(1000);
302 add_timer(&ir->timer);
303 } else if (ir->rc5_gpio) {
304 /* set timer_end for code completion */
305 setup_timer(&ir->timer, bttv_rc5_timer_end, (unsigned long)ir);
309 ir->rc5_remote_gap = ir_rc5_remote_gap;
313 static void bttv_ir_stop(struct bttv *btv)
315 if (btv->remote->polling) {
316 del_timer_sync(&btv->remote->timer);
317 flush_scheduled_work();
320 if (btv->remote->rc5_gpio) {
323 del_timer_sync(&btv->remote->timer);
324 flush_scheduled_work();
326 gpio = bttv_gpio_read(&btv->c);
327 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
332 * Get_key functions used by I2C remotes
335 static int get_key_pv951(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
340 if (1 != i2c_master_recv(ir->c, &b, 1)) {
341 dprintk(KERN_INFO DEVNAME ": read error\n");
348 dprintk(KERN_INFO DEVNAME ": key %02x\n", b);
355 /* Instantiate the I2C IR receiver device, if present */
356 void __devinit init_bttv_i2c_ir(struct bttv *btv)
358 const unsigned short addr_list[] = {
359 0x1a, 0x18, 0x64, 0x30, 0x71,
362 struct i2c_board_info info;
364 if (0 != btv->i2c_rc)
367 memset(&info, 0, sizeof(struct i2c_board_info));
368 memset(&btv->init_data, 0, sizeof(btv->init_data));
369 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
371 switch (btv->c.type) {
372 case BTTV_BOARD_PV951:
373 btv->init_data.name = "PV951";
374 btv->init_data.get_key = get_key_pv951;
375 btv->init_data.ir_codes = RC_MAP_PV951;
380 * The external IR receiver is at i2c address 0x34 (0x35 for
381 * reads). Future Hauppauge cards will have an internal
382 * receiver at 0x30 (0x31 for reads). In theory, both can be
383 * fitted, and Hauppauge suggest an external overrides an
385 * That's why we probe 0x1a (~0x34) first. CB
388 i2c_new_probed_device(&btv->c.i2c_adap, &info, addr_list, NULL);
392 if (btv->init_data.name)
393 info.platform_data = &btv->init_data;
394 i2c_new_device(&btv->c.i2c_adap, &info);
399 int __devexit fini_bttv_i2c(struct bttv *btv)
401 if (0 != btv->i2c_rc)
404 return i2c_del_adapter(&btv->c.i2c_adap);
407 int bttv_input_init(struct bttv *btv)
410 char *ir_codes = NULL;
414 if (!btv->has_remote)
417 ir = kzalloc(sizeof(*ir),GFP_KERNEL);
418 rc = rc_allocate_device();
422 /* detect & configure */
423 switch (btv->c.type) {
424 case BTTV_BOARD_AVERMEDIA:
425 case BTTV_BOARD_AVPHONE98:
426 case BTTV_BOARD_AVERMEDIA98:
427 ir_codes = RC_MAP_AVERMEDIA;
428 ir->mask_keycode = 0xf88000;
429 ir->mask_keydown = 0x010000;
430 ir->polling = 50; // ms
433 case BTTV_BOARD_AVDVBT_761:
434 case BTTV_BOARD_AVDVBT_771:
435 ir_codes = RC_MAP_AVERMEDIA_DVBT;
436 ir->mask_keycode = 0x0f00c0;
437 ir->mask_keydown = 0x000020;
438 ir->polling = 50; // ms
441 case BTTV_BOARD_PXELVWPLTVPAK:
442 ir_codes = RC_MAP_PIXELVIEW;
443 ir->mask_keycode = 0x003e00;
444 ir->mask_keyup = 0x010000;
445 ir->polling = 50; // ms
447 case BTTV_BOARD_PV_M4900:
448 case BTTV_BOARD_PV_BT878P_9B:
449 case BTTV_BOARD_PV_BT878P_PLUS:
450 ir_codes = RC_MAP_PIXELVIEW;
451 ir->mask_keycode = 0x001f00;
452 ir->mask_keyup = 0x008000;
453 ir->polling = 50; // ms
456 case BTTV_BOARD_WINFAST2000:
457 ir_codes = RC_MAP_WINFAST;
458 ir->mask_keycode = 0x1f8;
460 case BTTV_BOARD_MAGICTVIEW061:
461 case BTTV_BOARD_MAGICTVIEW063:
462 ir_codes = RC_MAP_WINFAST;
463 ir->mask_keycode = 0x0008e000;
464 ir->mask_keydown = 0x00200000;
466 case BTTV_BOARD_APAC_VIEWCOMP:
467 ir_codes = RC_MAP_APAC_VIEWCOMP;
468 ir->mask_keycode = 0x001f00;
469 ir->mask_keyup = 0x008000;
470 ir->polling = 50; // ms
472 case BTTV_BOARD_ASKEY_CPH03X:
473 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
474 case BTTV_BOARD_CONTVFMI:
475 ir_codes = RC_MAP_PIXELVIEW;
476 ir->mask_keycode = 0x001F00;
477 ir->mask_keyup = 0x006000;
478 ir->polling = 50; // ms
480 case BTTV_BOARD_NEBULA_DIGITV:
481 ir_codes = RC_MAP_NEBULA;
482 btv->custom_irq = bttv_rc5_irq;
485 case BTTV_BOARD_MACHTV_MAGICTV:
486 ir_codes = RC_MAP_APAC_VIEWCOMP;
487 ir->mask_keycode = 0x001F00;
488 ir->mask_keyup = 0x004000;
489 ir->polling = 50; /* ms */
491 case BTTV_BOARD_KOZUMI_KTV_01C:
492 ir_codes = RC_MAP_PCTV_SEDNA;
493 ir->mask_keycode = 0x001f00;
494 ir->mask_keyup = 0x006000;
495 ir->polling = 50; /* ms */
497 case BTTV_BOARD_ENLTV_FM_2:
498 ir_codes = RC_MAP_ENCORE_ENLTV2;
499 ir->mask_keycode = 0x00fd00;
500 ir->mask_keyup = 0x000080;
501 ir->polling = 1; /* ms */
502 ir->last_gpio = ir_extract_bits(bttv_gpio_read(&btv->c),
506 if (NULL == ir_codes) {
507 dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);
514 /* enable remote irq */
515 bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
516 gpio = bttv_gpio_read(&btv->c);
517 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
518 bttv_gpio_write(&btv->c, gpio | (1 << 4));
520 /* init hardware-specific stuff */
521 bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
524 /* init input device */
527 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
529 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
530 pci_name(btv->c.pci));
532 rc->input_name = ir->name;
533 rc->input_phys = ir->phys;
534 rc->input_id.bustype = BUS_PCI;
535 rc->input_id.version = 1;
536 if (btv->c.pci->subsystem_vendor) {
537 rc->input_id.vendor = btv->c.pci->subsystem_vendor;
538 rc->input_id.product = btv->c.pci->subsystem_device;
540 rc->input_id.vendor = btv->c.pci->vendor;
541 rc->input_id.product = btv->c.pci->device;
543 rc->dev.parent = &btv->c.pci->dev;
544 rc->map_name = ir_codes;
545 rc->driver_name = MODULE_NAME;
548 bttv_ir_start(btv, ir);
551 err = rc_register_device(rc);
566 void bttv_input_fini(struct bttv *btv)
568 if (btv->remote == NULL)
572 rc_unregister_device(btv->remote->dev);