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 = 0;
31 module_param(disable_ir, int, 0444);
32 MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
34 static unsigned int ir_debug = 0;
35 module_param(ir_debug, int, 0644);
36 MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
38 static int pinnacle_remote = 0;
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 #define dprintk(fmt, arg...) if (ir_debug) \
56 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
57 #define i2cdprintk(fmt, arg...) if (ir_debug) \
58 printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg)
61 static int saa7134_rc5_irq(struct saa7134_dev *dev);
63 /* -------------------- GPIO generic keycode builder -------------------- */
65 static int build_key(struct saa7134_dev *dev)
67 struct card_ir *ir = dev->remote;
70 /* here comes the additional handshake steps for some cards */
72 case SAA7134_BOARD_GOTVIEW_7135:
73 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
74 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
77 /* rising SAA7134_GPIO_GPRESCAN reads the status */
78 saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
79 saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
81 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
83 if (ir->last_gpio == gpio)
88 data = ir_extract_bits(gpio, ir->mask_keycode);
89 dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
90 gpio, ir->mask_keycode, data);
93 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
94 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
95 ir_input_keydown(ir->dev, &ir->ir, data, data);
97 ir_input_nokey(ir->dev, &ir->ir);
100 else { /* IRQ driven mode - handle key press and release in one go */
101 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
102 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
103 ir_input_keydown(ir->dev, &ir->ir, data, data);
104 ir_input_nokey(ir->dev, &ir->ir);
111 /* --------------------- Chip specific I2C key builders ----------------- */
113 static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
118 if (1 != i2c_master_recv(&ir->c,&b,1)) {
119 i2cdprintk("read error\n");
123 /* no button press */
136 static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
138 unsigned char buf[5], cod4, code3, code4;
141 if (5 != i2c_master_recv(&ir->c,buf,5))
157 void saa7134_input_irq(struct saa7134_dev *dev)
159 struct card_ir *ir = dev->remote;
161 if (!ir->polling && !ir->rc5_gpio) {
163 } else if (ir->rc5_gpio) {
164 saa7134_rc5_irq(dev);
168 static void saa7134_input_timer(unsigned long data)
170 struct saa7134_dev *dev = (struct saa7134_dev *)data;
171 struct card_ir *ir = dev->remote;
174 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
177 void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
180 setup_timer(&ir->timer, saa7134_input_timer,
182 ir->timer.expires = jiffies + HZ;
183 add_timer(&ir->timer);
184 } else if (ir->rc5_gpio) {
185 /* set timer_end for code completion */
186 init_timer(&ir->timer_end);
187 ir->timer_end.function = ir_rc5_timer_end;
188 ir->timer_end.data = (unsigned long)ir;
189 init_timer(&ir->timer_keyup);
190 ir->timer_keyup.function = ir_rc5_timer_keyup;
191 ir->timer_keyup.data = (unsigned long)ir;
195 ir->rc5_key_timeout = ir_rc5_key_timeout;
196 ir->rc5_remote_gap = ir_rc5_remote_gap;
200 void saa7134_ir_stop(struct saa7134_dev *dev)
202 if (dev->remote->polling)
203 del_timer_sync(&dev->remote->timer);
206 int saa7134_input_init1(struct saa7134_dev *dev)
209 struct input_dev *input_dev;
210 IR_KEYTAB_TYPE *ir_codes = NULL;
211 u32 mask_keycode = 0;
212 u32 mask_keydown = 0;
216 int ir_type = IR_TYPE_OTHER;
219 if (dev->has_remote != SAA7134_REMOTE_GPIO)
224 /* detect & configure */
225 switch (dev->board) {
226 case SAA7134_BOARD_FLYVIDEO2000:
227 case SAA7134_BOARD_FLYVIDEO3000:
228 case SAA7134_BOARD_FLYTVPLATINUM_FM:
229 case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
230 ir_codes = ir_codes_flyvideo;
231 mask_keycode = 0xEC00000;
232 mask_keydown = 0x0040000;
234 case SAA7134_BOARD_CINERGY400:
235 case SAA7134_BOARD_CINERGY600:
236 case SAA7134_BOARD_CINERGY600_MK3:
237 ir_codes = ir_codes_cinergy;
238 mask_keycode = 0x00003f;
239 mask_keyup = 0x040000;
241 case SAA7134_BOARD_ECS_TVP3XP:
242 case SAA7134_BOARD_ECS_TVP3XP_4CB5:
243 ir_codes = ir_codes_eztv;
244 mask_keycode = 0x00017c;
245 mask_keyup = 0x000002;
248 case SAA7134_BOARD_KWORLD_XPERT:
249 case SAA7134_BOARD_AVACSSMARTTV:
250 ir_codes = ir_codes_pixelview;
251 mask_keycode = 0x00001F;
252 mask_keyup = 0x000020;
255 case SAA7134_BOARD_MD2819:
256 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
257 case SAA7134_BOARD_AVERMEDIA_305:
258 case SAA7134_BOARD_AVERMEDIA_307:
259 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
260 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;
264 mask_keycode = 0x0007C8;
265 mask_keydown = 0x000010;
267 /* Set GPIO pin2 to high to enable the IR controller */
268 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
269 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
271 case SAA7134_BOARD_AVERMEDIA_777:
272 case SAA7134_BOARD_AVERMEDIA_A16AR:
273 ir_codes = ir_codes_avermedia;
274 mask_keycode = 0x02F200;
275 mask_keydown = 0x000400;
277 /* Without this we won't receive key up events */
278 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
279 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
281 case SAA7134_BOARD_KWORLD_TERMINATOR:
282 ir_codes = ir_codes_pixelview;
283 mask_keycode = 0x00001f;
284 mask_keyup = 0x000060;
287 case SAA7134_BOARD_MANLI_MTV001:
288 case SAA7134_BOARD_MANLI_MTV002:
289 case SAA7134_BOARD_BEHOLD_409FM:
290 ir_codes = ir_codes_manli;
291 mask_keycode = 0x001f00;
292 mask_keyup = 0x004000;
295 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
296 ir_codes = ir_codes_pctv_sedna;
297 mask_keycode = 0x001f00;
298 mask_keyup = 0x004000;
301 case SAA7134_BOARD_GOTVIEW_7135:
302 ir_codes = ir_codes_gotview7135;
303 mask_keycode = 0x0003CC;
304 mask_keydown = 0x000010;
305 polling = 5; /* ms */
306 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
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;
312 mask_keycode = 0x00003F;
313 mask_keyup = 0x400000;
316 case SAA7134_BOARD_PROTEUS_2309:
317 ir_codes = ir_codes_proteus_2309;
318 mask_keycode = 0x00007F;
319 mask_keyup = 0x000080;
322 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
323 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
324 ir_codes = ir_codes_videomate_tv_pvr;
325 mask_keycode = 0x003F00;
326 mask_keyup = 0x040000;
328 case SAA7134_BOARD_FLYDVBS_LR300:
329 case SAA7134_BOARD_FLYDVBT_LR301:
330 case SAA7134_BOARD_FLYDVBTDUO:
331 ir_codes = ir_codes_flydvb;
332 mask_keycode = 0x0001F00;
333 mask_keydown = 0x0040000;
335 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
336 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
337 ir_codes = ir_codes_asus_pc39;
338 mask_keydown = 0x0040000;
341 case SAA7134_BOARD_ENCORE_ENLTV:
342 case SAA7134_BOARD_ENCORE_ENLTV_FM:
343 ir_codes = ir_codes_encore_enltv;
344 mask_keycode = 0x00007f;
345 mask_keyup = 0x040000;
348 case SAA7134_BOARD_10MOONSTVMASTER3:
349 ir_codes = ir_codes_encore_enltv;
350 mask_keycode = 0x5f80000;
351 mask_keyup = 0x8000000;
355 if (NULL == ir_codes) {
356 printk("%s: Oops: IR config error [card=%d]\n",
357 dev->name, dev->board);
361 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
362 input_dev = input_allocate_device();
363 if (!ir || !input_dev) {
370 /* init hardware-specific stuff */
371 ir->mask_keycode = mask_keycode;
372 ir->mask_keydown = mask_keydown;
373 ir->mask_keyup = mask_keyup;
374 ir->polling = polling;
375 ir->rc5_gpio = rc5_gpio;
377 /* init input device */
378 snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
379 saa7134_boards[dev->board].name);
380 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
383 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
384 input_dev->name = ir->name;
385 input_dev->phys = ir->phys;
386 input_dev->id.bustype = BUS_PCI;
387 input_dev->id.version = 1;
388 if (dev->pci->subsystem_vendor) {
389 input_dev->id.vendor = dev->pci->subsystem_vendor;
390 input_dev->id.product = dev->pci->subsystem_device;
392 input_dev->id.vendor = dev->pci->vendor;
393 input_dev->id.product = dev->pci->device;
395 input_dev->dev.parent = &dev->pci->dev;
398 saa7134_ir_start(dev, ir);
400 err = input_register_device(ir->dev);
404 /* the remote isn't as bouncy as a keyboard */
405 ir->dev->rep[REP_DELAY] = repeat_delay;
406 ir->dev->rep[REP_PERIOD] = repeat_period;
411 saa7134_ir_stop(dev);
414 input_free_device(input_dev);
419 void saa7134_input_fini(struct saa7134_dev *dev)
421 if (NULL == dev->remote)
424 saa7134_ir_stop(dev);
425 input_unregister_device(dev->remote->dev);
430 void saa7134_set_i2c_ir(struct saa7134_dev *dev, struct IR_i2c *ir)
433 dprintk("Found supported i2c remote, but IR has been disabled\n");
438 switch (dev->board) {
439 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 if (pinnacle_remote == 0) {
443 ir->get_key = get_key_pinnacle_color;
444 ir->ir_codes = ir_codes_pinnacle_color;
446 ir->get_key = get_key_pinnacle_grey;
447 ir->ir_codes = ir_codes_pinnacle_grey;
450 case SAA7134_BOARD_UPMOST_PURPLE_TV:
451 snprintf(ir->c.name, sizeof(ir->c.name), "Purple TV");
452 ir->get_key = get_key_purpletv;
453 ir->ir_codes = ir_codes_purpletv;
455 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
456 snprintf(ir->c.name, sizeof(ir->c.name), "HVR 1110");
457 ir->get_key = get_key_hvr1110;
458 ir->ir_codes = ir_codes_hauppauge_new;
461 dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board);
467 static int saa7134_rc5_irq(struct saa7134_dev *dev)
469 struct card_ir *ir = dev->remote;
472 unsigned long current_jiffies, timeout;
474 /* get time of bit */
475 current_jiffies = jiffies;
476 do_gettimeofday(&tv);
478 /* avoid overflow with gap >1s */
479 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
482 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
483 tv.tv_usec - ir->base_time.tv_usec;
486 /* active code => add bit */
488 /* only if in the code (otherwise spurious IRQ or timer
490 if (ir->last_bit < 28) {
491 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
493 ir->code |= 1 << ir->last_bit;
495 /* starting new code */
502 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
503 mod_timer(&ir->timer_end, timeout);
509 /* ----------------------------------------------------------------------