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/moduleparam.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/input.h>
28 #include "saa7134-reg.h"
31 static unsigned int disable_ir
= 0;
32 module_param(disable_ir
, int, 0444);
33 MODULE_PARM_DESC(disable_ir
,"disable infrared remote support");
35 static unsigned int ir_debug
= 0;
36 module_param(ir_debug
, int, 0644);
37 MODULE_PARM_DESC(ir_debug
,"enable debug messages [IR]");
39 static int pinnacle_remote
= 0;
40 module_param(pinnacle_remote
, int, 0644); /* Choose Pinnacle PCTV remote */
41 MODULE_PARM_DESC(pinnacle_remote
, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
43 static int ir_rc5_remote_gap
= 885;
44 module_param(ir_rc5_remote_gap
, int, 0644);
45 static int ir_rc5_key_timeout
= 115;
46 module_param(ir_rc5_key_timeout
, int, 0644);
48 #define dprintk(fmt, arg...) if (ir_debug) \
49 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
50 #define i2cdprintk(fmt, arg...) if (ir_debug) \
51 printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg)
54 static int saa7134_rc5_irq(struct saa7134_dev
*dev
);
56 /* -------------------- GPIO generic keycode builder -------------------- */
58 static int build_key(struct saa7134_dev
*dev
)
60 struct card_ir
*ir
= dev
->remote
;
63 /* rising SAA7134_GPIO_GPRESCAN reads the status */
64 saa_clearb(SAA7134_GPIO_GPMODE3
,SAA7134_GPIO_GPRESCAN
);
65 saa_setb(SAA7134_GPIO_GPMODE3
,SAA7134_GPIO_GPRESCAN
);
67 gpio
= saa_readl(SAA7134_GPIO_GPSTATUS0
>> 2);
69 if (ir
->last_gpio
== gpio
)
74 data
= ir_extract_bits(gpio
, ir
->mask_keycode
);
75 dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
76 gpio
, ir
->mask_keycode
, data
);
79 if ((ir
->mask_keydown
&& (0 != (gpio
& ir
->mask_keydown
))) ||
80 (ir
->mask_keyup
&& (0 == (gpio
& ir
->mask_keyup
)))) {
81 ir_input_keydown(ir
->dev
, &ir
->ir
, data
, data
);
83 ir_input_nokey(ir
->dev
, &ir
->ir
);
86 else { /* IRQ driven mode - handle key press and release in one go */
87 if ((ir
->mask_keydown
&& (0 != (gpio
& ir
->mask_keydown
))) ||
88 (ir
->mask_keyup
&& (0 == (gpio
& ir
->mask_keyup
)))) {
89 ir_input_keydown(ir
->dev
, &ir
->ir
, data
, data
);
90 ir_input_nokey(ir
->dev
, &ir
->ir
);
97 /* --------------------- Chip specific I2C key builders ----------------- */
99 static int get_key_purpletv(struct IR_i2c
*ir
, u32
*ir_key
, u32
*ir_raw
)
104 if (1 != i2c_master_recv(&ir
->c
,&b
,1)) {
105 i2cdprintk("read error\n");
109 /* no button press */
122 static int get_key_hvr1110(struct IR_i2c
*ir
, u32
*ir_key
, u32
*ir_raw
)
124 unsigned char buf
[5], cod4
, code3
, code4
;
127 if (5 != i2c_master_recv(&ir
->c
,buf
,5))
143 void saa7134_input_irq(struct saa7134_dev
*dev
)
145 struct card_ir
*ir
= dev
->remote
;
147 if (!ir
->polling
&& !ir
->rc5_gpio
) {
149 } else if (ir
->rc5_gpio
) {
150 saa7134_rc5_irq(dev
);
154 static void saa7134_input_timer(unsigned long data
)
156 struct saa7134_dev
*dev
= (struct saa7134_dev
*)data
;
157 struct card_ir
*ir
= dev
->remote
;
160 mod_timer(&ir
->timer
, jiffies
+ msecs_to_jiffies(ir
->polling
));
163 static void saa7134_ir_start(struct saa7134_dev
*dev
, struct card_ir
*ir
)
166 setup_timer(&ir
->timer
, saa7134_input_timer
,
168 ir
->timer
.expires
= jiffies
+ HZ
;
169 add_timer(&ir
->timer
);
170 } else if (ir
->rc5_gpio
) {
171 /* set timer_end for code completion */
172 init_timer(&ir
->timer_end
);
173 ir
->timer_end
.function
= ir_rc5_timer_end
;
174 ir
->timer_end
.data
= (unsigned long)ir
;
175 init_timer(&ir
->timer_keyup
);
176 ir
->timer_keyup
.function
= ir_rc5_timer_keyup
;
177 ir
->timer_keyup
.data
= (unsigned long)ir
;
181 ir
->rc5_key_timeout
= ir_rc5_key_timeout
;
182 ir
->rc5_remote_gap
= ir_rc5_remote_gap
;
186 static void saa7134_ir_stop(struct saa7134_dev
*dev
)
188 if (dev
->remote
->polling
)
189 del_timer_sync(&dev
->remote
->timer
);
192 int saa7134_input_init1(struct saa7134_dev
*dev
)
195 struct input_dev
*input_dev
;
196 IR_KEYTAB_TYPE
*ir_codes
= NULL
;
197 u32 mask_keycode
= 0;
198 u32 mask_keydown
= 0;
202 int ir_type
= IR_TYPE_OTHER
;
205 if (dev
->has_remote
!= SAA7134_REMOTE_GPIO
)
210 /* detect & configure */
211 switch (dev
->board
) {
212 case SAA7134_BOARD_FLYVIDEO2000
:
213 case SAA7134_BOARD_FLYVIDEO3000
:
214 case SAA7134_BOARD_FLYTVPLATINUM_FM
:
215 case SAA7134_BOARD_FLYTVPLATINUM_MINI2
:
216 ir_codes
= ir_codes_flyvideo
;
217 mask_keycode
= 0xEC00000;
218 mask_keydown
= 0x0040000;
220 case SAA7134_BOARD_CINERGY400
:
221 case SAA7134_BOARD_CINERGY600
:
222 case SAA7134_BOARD_CINERGY600_MK3
:
223 ir_codes
= ir_codes_cinergy
;
224 mask_keycode
= 0x00003f;
225 mask_keyup
= 0x040000;
227 case SAA7134_BOARD_ECS_TVP3XP
:
228 case SAA7134_BOARD_ECS_TVP3XP_4CB5
:
229 ir_codes
= ir_codes_eztv
;
230 mask_keycode
= 0x00017c;
231 mask_keyup
= 0x000002;
234 case SAA7134_BOARD_KWORLD_XPERT
:
235 case SAA7134_BOARD_AVACSSMARTTV
:
236 ir_codes
= ir_codes_pixelview
;
237 mask_keycode
= 0x00001F;
238 mask_keyup
= 0x000020;
241 case SAA7134_BOARD_MD2819
:
242 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT
:
243 case SAA7134_BOARD_AVERMEDIA_305
:
244 case SAA7134_BOARD_AVERMEDIA_307
:
245 case SAA7134_BOARD_AVERMEDIA_STUDIO_305
:
246 case SAA7134_BOARD_AVERMEDIA_STUDIO_307
:
247 case SAA7134_BOARD_AVERMEDIA_STUDIO_507
:
248 case SAA7134_BOARD_AVERMEDIA_GO_007_FM
:
249 ir_codes
= ir_codes_avermedia
;
250 mask_keycode
= 0x0007C8;
251 mask_keydown
= 0x000010;
253 /* Set GPIO pin2 to high to enable the IR controller */
254 saa_setb(SAA7134_GPIO_GPMODE0
, 0x4);
255 saa_setb(SAA7134_GPIO_GPSTATUS0
, 0x4);
257 case SAA7134_BOARD_AVERMEDIA_777
:
258 case SAA7134_BOARD_AVERMEDIA_A16AR
:
259 ir_codes
= ir_codes_avermedia
;
260 mask_keycode
= 0x02F200;
261 mask_keydown
= 0x000400;
263 /* Without this we won't receive key up events */
264 saa_setb(SAA7134_GPIO_GPMODE1
, 0x1);
265 saa_setb(SAA7134_GPIO_GPSTATUS1
, 0x1);
267 case SAA7134_BOARD_KWORLD_TERMINATOR
:
268 ir_codes
= ir_codes_pixelview
;
269 mask_keycode
= 0x00001f;
270 mask_keyup
= 0x000060;
273 case SAA7134_BOARD_MANLI_MTV001
:
274 case SAA7134_BOARD_MANLI_MTV002
:
275 case SAA7134_BOARD_BEHOLD_409FM
:
276 ir_codes
= ir_codes_manli
;
277 mask_keycode
= 0x001f00;
278 mask_keyup
= 0x004000;
281 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS
:
282 ir_codes
= ir_codes_pctv_sedna
;
283 mask_keycode
= 0x001f00;
284 mask_keyup
= 0x004000;
287 case SAA7134_BOARD_GOTVIEW_7135
:
288 ir_codes
= ir_codes_gotview7135
;
289 mask_keycode
= 0x0003EC;
290 mask_keyup
= 0x008000;
291 mask_keydown
= 0x000010;
294 case SAA7134_BOARD_VIDEOMATE_TV_PVR
:
295 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS
:
296 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII
:
297 ir_codes
= ir_codes_videomate_tv_pvr
;
298 mask_keycode
= 0x00003F;
299 mask_keyup
= 0x400000;
302 case SAA7134_BOARD_PROTEUS_2309
:
303 ir_codes
= ir_codes_proteus_2309
;
304 mask_keycode
= 0x00007F;
305 mask_keyup
= 0x000080;
308 case SAA7134_BOARD_VIDEOMATE_DVBT_300
:
309 case SAA7134_BOARD_VIDEOMATE_DVBT_200
:
310 ir_codes
= ir_codes_videomate_tv_pvr
;
311 mask_keycode
= 0x003F00;
312 mask_keyup
= 0x040000;
314 case SAA7134_BOARD_FLYDVBS_LR300
:
315 case SAA7134_BOARD_FLYDVBT_LR301
:
316 case SAA7134_BOARD_FLYDVBTDUO
:
317 ir_codes
= ir_codes_flydvb
;
318 mask_keycode
= 0x0001F00;
319 mask_keydown
= 0x0040000;
321 case SAA7134_BOARD_ASUSTeK_P7131_DUAL
:
322 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA
:
323 ir_codes
= ir_codes_asus_pc39
;
324 mask_keydown
= 0x0040000;
327 case SAA7134_BOARD_ENCORE_ENLTV
:
328 case SAA7134_BOARD_ENCORE_ENLTV_FM
:
329 ir_codes
= ir_codes_encore_enltv
;
330 mask_keycode
= 0x00007f;
331 mask_keyup
= 0x040000;
334 case SAA7134_BOARD_10MOONSTVMASTER3
:
335 ir_codes
= ir_codes_encore_enltv
;
336 mask_keycode
= 0x5f80000;
337 mask_keyup
= 0x8000000;
341 if (NULL
== ir_codes
) {
342 printk("%s: Oops: IR config error [card=%d]\n",
343 dev
->name
, dev
->board
);
347 ir
= kzalloc(sizeof(*ir
), GFP_KERNEL
);
348 input_dev
= input_allocate_device();
349 if (!ir
|| !input_dev
) {
356 /* init hardware-specific stuff */
357 ir
->mask_keycode
= mask_keycode
;
358 ir
->mask_keydown
= mask_keydown
;
359 ir
->mask_keyup
= mask_keyup
;
360 ir
->polling
= polling
;
361 ir
->rc5_gpio
= rc5_gpio
;
363 /* init input device */
364 snprintf(ir
->name
, sizeof(ir
->name
), "saa7134 IR (%s)",
365 saa7134_boards
[dev
->board
].name
);
366 snprintf(ir
->phys
, sizeof(ir
->phys
), "pci-%s/ir0",
369 ir_input_init(input_dev
, &ir
->ir
, ir_type
, ir_codes
);
370 input_dev
->name
= ir
->name
;
371 input_dev
->phys
= ir
->phys
;
372 input_dev
->id
.bustype
= BUS_PCI
;
373 input_dev
->id
.version
= 1;
374 if (dev
->pci
->subsystem_vendor
) {
375 input_dev
->id
.vendor
= dev
->pci
->subsystem_vendor
;
376 input_dev
->id
.product
= dev
->pci
->subsystem_device
;
378 input_dev
->id
.vendor
= dev
->pci
->vendor
;
379 input_dev
->id
.product
= dev
->pci
->device
;
381 input_dev
->dev
.parent
= &dev
->pci
->dev
;
384 saa7134_ir_start(dev
, ir
);
386 err
= input_register_device(ir
->dev
);
393 saa7134_ir_stop(dev
);
396 input_free_device(input_dev
);
401 void saa7134_input_fini(struct saa7134_dev
*dev
)
403 if (NULL
== dev
->remote
)
406 saa7134_ir_stop(dev
);
407 input_unregister_device(dev
->remote
->dev
);
412 void saa7134_set_i2c_ir(struct saa7134_dev
*dev
, struct IR_i2c
*ir
)
415 dprintk("Found supported i2c remote, but IR has been disabled\n");
420 switch (dev
->board
) {
421 case SAA7134_BOARD_PINNACLE_PCTV_110i
:
422 case SAA7134_BOARD_PINNACLE_PCTV_310i
:
423 snprintf(ir
->c
.name
, sizeof(ir
->c
.name
), "Pinnacle PCTV");
424 if (pinnacle_remote
== 0) {
425 ir
->get_key
= get_key_pinnacle_color
;
426 ir
->ir_codes
= ir_codes_pinnacle_color
;
428 ir
->get_key
= get_key_pinnacle_grey
;
429 ir
->ir_codes
= ir_codes_pinnacle_grey
;
432 case SAA7134_BOARD_UPMOST_PURPLE_TV
:
433 snprintf(ir
->c
.name
, sizeof(ir
->c
.name
), "Purple TV");
434 ir
->get_key
= get_key_purpletv
;
435 ir
->ir_codes
= ir_codes_purpletv
;
437 case SAA7134_BOARD_HAUPPAUGE_HVR1110
:
438 snprintf(ir
->c
.name
, sizeof(ir
->c
.name
), "HVR 1110");
439 ir
->get_key
= get_key_hvr1110
;
440 ir
->ir_codes
= ir_codes_hauppauge_new
;
443 dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev
->board
);
449 static int saa7134_rc5_irq(struct saa7134_dev
*dev
)
451 struct card_ir
*ir
= dev
->remote
;
454 unsigned long current_jiffies
, timeout
;
456 /* get time of bit */
457 current_jiffies
= jiffies
;
458 do_gettimeofday(&tv
);
460 /* avoid overflow with gap >1s */
461 if (tv
.tv_sec
- ir
->base_time
.tv_sec
> 1) {
464 gap
= 1000000 * (tv
.tv_sec
- ir
->base_time
.tv_sec
) +
465 tv
.tv_usec
- ir
->base_time
.tv_usec
;
468 /* active code => add bit */
470 /* only if in the code (otherwise spurious IRQ or timer
472 if (ir
->last_bit
< 28) {
473 ir
->last_bit
= (gap
- ir_rc5_remote_gap
/ 2) /
475 ir
->code
|= 1 << ir
->last_bit
;
477 /* starting new code */
484 timeout
= current_jiffies
+ (500 + 30 * HZ
) / 1000;
485 mod_timer(&ir
->timer_end
, timeout
);
491 /* ----------------------------------------------------------------------