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 #define dprintk(fmt, arg...) if (ir_debug) \
48 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
49 #define i2cdprintk(fmt, arg...) if (ir_debug) \
50 printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg)
53 static int saa7134_rc5_irq(struct saa7134_dev
*dev
);
55 /* -------------------- GPIO generic keycode builder -------------------- */
57 static int build_key(struct saa7134_dev
*dev
)
59 struct card_ir
*ir
= dev
->remote
;
62 /* rising SAA7134_GPIO_GPRESCAN reads the status */
63 saa_clearb(SAA7134_GPIO_GPMODE3
,SAA7134_GPIO_GPRESCAN
);
64 saa_setb(SAA7134_GPIO_GPMODE3
,SAA7134_GPIO_GPRESCAN
);
66 gpio
= saa_readl(SAA7134_GPIO_GPSTATUS0
>> 2);
68 if (ir
->last_gpio
== gpio
)
73 data
= ir_extract_bits(gpio
, ir
->mask_keycode
);
74 dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
75 gpio
, ir
->mask_keycode
, data
);
78 if ((ir
->mask_keydown
&& (0 != (gpio
& ir
->mask_keydown
))) ||
79 (ir
->mask_keyup
&& (0 == (gpio
& ir
->mask_keyup
)))) {
80 ir_input_keydown(ir
->dev
, &ir
->ir
, data
, data
);
82 ir_input_nokey(ir
->dev
, &ir
->ir
);
85 else { /* IRQ driven mode - handle key press and release in one go */
86 if ((ir
->mask_keydown
&& (0 != (gpio
& ir
->mask_keydown
))) ||
87 (ir
->mask_keyup
&& (0 == (gpio
& ir
->mask_keyup
)))) {
88 ir_input_keydown(ir
->dev
, &ir
->ir
, data
, data
);
89 ir_input_nokey(ir
->dev
, &ir
->ir
);
96 /* --------------------- Chip specific I2C key builders ----------------- */
98 static int get_key_purpletv(struct IR_i2c
*ir
, u32
*ir_key
, u32
*ir_raw
)
103 if (1 != i2c_master_recv(&ir
->c
,&b
,1)) {
104 i2cdprintk("read error\n");
108 /* no button press */
121 static int get_key_hvr1110(struct IR_i2c
*ir
, u32
*ir_key
, u32
*ir_raw
)
123 unsigned char buf
[5], cod4
, code3
, code4
;
126 if (5 != i2c_master_recv(&ir
->c
,buf
,5))
142 void saa7134_input_irq(struct saa7134_dev
*dev
)
144 struct card_ir
*ir
= dev
->remote
;
146 if (!ir
->polling
&& !ir
->rc5_gpio
) {
148 } else if (ir
->rc5_gpio
) {
149 saa7134_rc5_irq(dev
);
153 static void saa7134_input_timer(unsigned long data
)
155 struct saa7134_dev
*dev
= (struct saa7134_dev
*)data
;
156 struct card_ir
*ir
= dev
->remote
;
159 mod_timer(&ir
->timer
, jiffies
+ msecs_to_jiffies(ir
->polling
));
162 static void saa7134_ir_start(struct saa7134_dev
*dev
, struct card_ir
*ir
)
165 setup_timer(&ir
->timer
, saa7134_input_timer
,
167 ir
->timer
.expires
= jiffies
+ HZ
;
168 add_timer(&ir
->timer
);
169 } else if (ir
->rc5_gpio
) {
170 /* set timer_end for code completion */
171 init_timer(&ir
->timer_end
);
172 ir
->timer_end
.function
= ir_rc5_timer_end
;
173 ir
->timer_end
.data
= (unsigned long)ir
;
174 init_timer(&ir
->timer_keyup
);
175 ir
->timer_keyup
.function
= ir_rc5_timer_keyup
;
176 ir
->timer_keyup
.data
= (unsigned long)ir
;
180 ir
->rc5_key_timeout
= ir_rc5_key_timeout
;
181 ir
->rc5_remote_gap
= ir_rc5_remote_gap
;
185 static void saa7134_ir_stop(struct saa7134_dev
*dev
)
187 if (dev
->remote
->polling
)
188 del_timer_sync(&dev
->remote
->timer
);
191 int saa7134_input_init1(struct saa7134_dev
*dev
)
194 struct input_dev
*input_dev
;
195 IR_KEYTAB_TYPE
*ir_codes
= NULL
;
196 u32 mask_keycode
= 0;
197 u32 mask_keydown
= 0;
201 int ir_type
= IR_TYPE_OTHER
;
204 if (dev
->has_remote
!= SAA7134_REMOTE_GPIO
)
209 /* detect & configure */
210 switch (dev
->board
) {
211 case SAA7134_BOARD_FLYVIDEO2000
:
212 case SAA7134_BOARD_FLYVIDEO3000
:
213 case SAA7134_BOARD_FLYTVPLATINUM_FM
:
214 case SAA7134_BOARD_FLYTVPLATINUM_MINI2
:
215 ir_codes
= ir_codes_flyvideo
;
216 mask_keycode
= 0xEC00000;
217 mask_keydown
= 0x0040000;
219 case SAA7134_BOARD_CINERGY400
:
220 case SAA7134_BOARD_CINERGY600
:
221 case SAA7134_BOARD_CINERGY600_MK3
:
222 ir_codes
= ir_codes_cinergy
;
223 mask_keycode
= 0x00003f;
224 mask_keyup
= 0x040000;
226 case SAA7134_BOARD_ECS_TVP3XP
:
227 case SAA7134_BOARD_ECS_TVP3XP_4CB5
:
228 ir_codes
= ir_codes_eztv
;
229 mask_keycode
= 0x00017c;
230 mask_keyup
= 0x000002;
233 case SAA7134_BOARD_KWORLD_XPERT
:
234 case SAA7134_BOARD_AVACSSMARTTV
:
235 ir_codes
= ir_codes_pixelview
;
236 mask_keycode
= 0x00001F;
237 mask_keyup
= 0x000020;
240 case SAA7134_BOARD_MD2819
:
241 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT
:
242 case SAA7134_BOARD_AVERMEDIA_305
:
243 case SAA7134_BOARD_AVERMEDIA_307
:
244 case SAA7134_BOARD_AVERMEDIA_STUDIO_305
:
245 case SAA7134_BOARD_AVERMEDIA_STUDIO_307
:
246 case SAA7134_BOARD_AVERMEDIA_STUDIO_507
:
247 case SAA7134_BOARD_AVERMEDIA_GO_007_FM
:
248 ir_codes
= ir_codes_avermedia
;
249 mask_keycode
= 0x0007C8;
250 mask_keydown
= 0x000010;
252 /* Set GPIO pin2 to high to enable the IR controller */
253 saa_setb(SAA7134_GPIO_GPMODE0
, 0x4);
254 saa_setb(SAA7134_GPIO_GPSTATUS0
, 0x4);
256 case SAA7134_BOARD_AVERMEDIA_777
:
257 case SAA7134_BOARD_AVERMEDIA_A16AR
:
258 ir_codes
= ir_codes_avermedia
;
259 mask_keycode
= 0x02F200;
260 mask_keydown
= 0x000400;
262 /* Without this we won't receive key up events */
263 saa_setb(SAA7134_GPIO_GPMODE1
, 0x1);
264 saa_setb(SAA7134_GPIO_GPSTATUS1
, 0x1);
266 case SAA7134_BOARD_KWORLD_TERMINATOR
:
267 ir_codes
= ir_codes_pixelview
;
268 mask_keycode
= 0x00001f;
269 mask_keyup
= 0x000060;
272 case SAA7134_BOARD_MANLI_MTV001
:
273 case SAA7134_BOARD_MANLI_MTV002
:
274 case SAA7134_BOARD_BEHOLD_409FM
:
275 ir_codes
= ir_codes_manli
;
276 mask_keycode
= 0x001f00;
277 mask_keyup
= 0x004000;
280 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS
:
281 ir_codes
= ir_codes_pctv_sedna
;
282 mask_keycode
= 0x001f00;
283 mask_keyup
= 0x004000;
286 case SAA7134_BOARD_GOTVIEW_7135
:
287 ir_codes
= ir_codes_gotview7135
;
288 mask_keycode
= 0x0003EC;
289 mask_keyup
= 0x008000;
290 mask_keydown
= 0x000010;
293 case SAA7134_BOARD_VIDEOMATE_TV_PVR
:
294 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS
:
295 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII
:
296 ir_codes
= ir_codes_videomate_tv_pvr
;
297 mask_keycode
= 0x00003F;
298 mask_keyup
= 0x400000;
301 case SAA7134_BOARD_PROTEUS_2309
:
302 ir_codes
= ir_codes_proteus_2309
;
303 mask_keycode
= 0x00007F;
304 mask_keyup
= 0x000080;
307 case SAA7134_BOARD_VIDEOMATE_DVBT_300
:
308 case SAA7134_BOARD_VIDEOMATE_DVBT_200
:
309 ir_codes
= ir_codes_videomate_tv_pvr
;
310 mask_keycode
= 0x003F00;
311 mask_keyup
= 0x040000;
313 case SAA7134_BOARD_FLYDVBS_LR300
:
314 case SAA7134_BOARD_FLYDVBT_LR301
:
315 case SAA7134_BOARD_FLYDVBTDUO
:
316 ir_codes
= ir_codes_flydvb
;
317 mask_keycode
= 0x0001F00;
318 mask_keydown
= 0x0040000;
320 case SAA7134_BOARD_ASUSTeK_P7131_DUAL
:
321 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA
:
322 ir_codes
= ir_codes_asus_pc39
;
323 mask_keydown
= 0x0040000;
326 case SAA7134_BOARD_ENCORE_ENLTV
:
327 case SAA7134_BOARD_ENCORE_ENLTV_FM
:
328 ir_codes
= ir_codes_encore_enltv
;
329 mask_keycode
= 0x00007f;
330 mask_keyup
= 0x040000;
333 case SAA7134_BOARD_10MOONSTVMASTER3
:
334 ir_codes
= ir_codes_encore_enltv
;
335 mask_keycode
= 0x5f80000;
336 mask_keyup
= 0x8000000;
340 if (NULL
== ir_codes
) {
341 printk("%s: Oops: IR config error [card=%d]\n",
342 dev
->name
, dev
->board
);
346 ir
= kzalloc(sizeof(*ir
), GFP_KERNEL
);
347 input_dev
= input_allocate_device();
348 if (!ir
|| !input_dev
) {
355 /* init hardware-specific stuff */
356 ir
->mask_keycode
= mask_keycode
;
357 ir
->mask_keydown
= mask_keydown
;
358 ir
->mask_keyup
= mask_keyup
;
359 ir
->polling
= polling
;
360 ir
->rc5_gpio
= rc5_gpio
;
362 /* init input device */
363 snprintf(ir
->name
, sizeof(ir
->name
), "saa7134 IR (%s)",
364 saa7134_boards
[dev
->board
].name
);
365 snprintf(ir
->phys
, sizeof(ir
->phys
), "pci-%s/ir0",
368 ir_input_init(input_dev
, &ir
->ir
, ir_type
, ir_codes
);
369 input_dev
->name
= ir
->name
;
370 input_dev
->phys
= ir
->phys
;
371 input_dev
->id
.bustype
= BUS_PCI
;
372 input_dev
->id
.version
= 1;
373 if (dev
->pci
->subsystem_vendor
) {
374 input_dev
->id
.vendor
= dev
->pci
->subsystem_vendor
;
375 input_dev
->id
.product
= dev
->pci
->subsystem_device
;
377 input_dev
->id
.vendor
= dev
->pci
->vendor
;
378 input_dev
->id
.product
= dev
->pci
->device
;
380 input_dev
->dev
.parent
= &dev
->pci
->dev
;
383 saa7134_ir_start(dev
, ir
);
385 err
= input_register_device(ir
->dev
);
392 saa7134_ir_stop(dev
);
395 input_free_device(input_dev
);
400 void saa7134_input_fini(struct saa7134_dev
*dev
)
402 if (NULL
== dev
->remote
)
405 saa7134_ir_stop(dev
);
406 input_unregister_device(dev
->remote
->dev
);
411 void saa7134_set_i2c_ir(struct saa7134_dev
*dev
, struct IR_i2c
*ir
)
414 dprintk("Found supported i2c remote, but IR has been disabled\n");
419 switch (dev
->board
) {
420 case SAA7134_BOARD_PINNACLE_PCTV_110i
:
421 case SAA7134_BOARD_PINNACLE_PCTV_310i
:
422 snprintf(ir
->c
.name
, sizeof(ir
->c
.name
), "Pinnacle PCTV");
423 if (pinnacle_remote
== 0) {
424 ir
->get_key
= get_key_pinnacle_color
;
425 ir
->ir_codes
= ir_codes_pinnacle_color
;
427 ir
->get_key
= get_key_pinnacle_grey
;
428 ir
->ir_codes
= ir_codes_pinnacle_grey
;
431 case SAA7134_BOARD_UPMOST_PURPLE_TV
:
432 snprintf(ir
->c
.name
, sizeof(ir
->c
.name
), "Purple TV");
433 ir
->get_key
= get_key_purpletv
;
434 ir
->ir_codes
= ir_codes_purpletv
;
436 case SAA7134_BOARD_HAUPPAUGE_HVR1110
:
437 snprintf(ir
->c
.name
, sizeof(ir
->c
.name
), "HVR 1110");
438 ir
->get_key
= get_key_hvr1110
;
439 ir
->ir_codes
= ir_codes_hauppauge_new
;
442 dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev
->board
);
448 static int saa7134_rc5_irq(struct saa7134_dev
*dev
)
450 struct card_ir
*ir
= dev
->remote
;
453 unsigned long current_jiffies
, timeout
;
455 /* get time of bit */
456 current_jiffies
= jiffies
;
457 do_gettimeofday(&tv
);
459 /* avoid overflow with gap >1s */
460 if (tv
.tv_sec
- ir
->base_time
.tv_sec
> 1) {
463 gap
= 1000000 * (tv
.tv_sec
- ir
->base_time
.tv_sec
) +
464 tv
.tv_usec
- ir
->base_time
.tv_usec
;
467 /* active code => add bit */
469 /* only if in the code (otherwise spurious IRQ or timer
471 if (ir
->last_bit
< 28) {
472 ir
->last_bit
= (gap
- ir_rc5_remote_gap
/ 2) /
474 ir
->code
|= 1 << ir
->last_bit
;
476 /* starting new code */
483 timeout
= current_jiffies
+ (500 + 30 * HZ
) / 1000;
484 mod_timer(&ir
->timer_end
, timeout
);
490 /* ----------------------------------------------------------------------