3 * Device driver for GPIO attached remote control interfaces
4 * on Conexant 2388x based TV/DVB cards.
6 * Copyright (c) 2003 Pavel Machek
7 * Copyright (c) 2004 Gerd Knorr
8 * Copyright (c) 2004, 2005 Chris Pascoe
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
23 #include <linux/init.h>
24 #include <linux/hrtimer.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
27 #include <linux/module.h>
29 #include <media/rc-core.h>
31 #define MODULE_NAME "cx88xx"
33 /* ---------------------------------------------------------------------- */
36 struct cx88_core
*core
;
44 /* sample from gpio pin 16 */
47 /* poll external decoder */
57 static unsigned int ir_samplerate
= 4;
58 module_param(ir_samplerate
, uint
, 0444);
59 MODULE_PARM_DESC(ir_samplerate
, "IR samplerate in kHz, 1 - 20, default 4");
62 module_param(ir_debug
, int, 0644); /* debug level [IR] */
63 MODULE_PARM_DESC(ir_debug
, "enable debug messages [IR]");
65 #define ir_dprintk(fmt, arg...) do { \
67 printk(KERN_DEBUG "%s IR: " fmt, ir->core->name, ##arg);\
70 #define dprintk(fmt, arg...) do { \
72 printk(KERN_DEBUG "cx88 IR: " fmt, ##arg); \
75 /* ---------------------------------------------------------------------- */
77 static void cx88_ir_handle_key(struct cx88_IR
*ir
)
79 struct cx88_core
*core
= ir
->core
;
80 u32 gpio
, data
, auxgpio
;
83 gpio
= cx_read(ir
->gpio_addr
);
84 switch (core
->boardnr
) {
85 case CX88_BOARD_NPGTECH_REALTV_TOP10FM
:
87 * This board apparently uses a combination of 2 GPIO
88 * to represent the keys. Additionally, the second GPIO
89 * can be used for parity.
94 * gpio = 0x758, auxgpio = 0xe5 or 0xf5
96 * gpio = 0x758, auxgpio = 0xed or 0xfd
99 auxgpio
= cx_read(MO_GP1_IO
);
100 /* Take out the parity part */
101 gpio
= (gpio
& 0x7fd) + (auxgpio
& 0xef);
103 case CX88_BOARD_WINFAST_DTV1000
:
104 case CX88_BOARD_WINFAST_DTV1800H
:
105 case CX88_BOARD_WINFAST_DTV1800H_XC4000
:
106 case CX88_BOARD_WINFAST_DTV2000H_PLUS
:
107 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL
:
108 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F36
:
109 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F43
:
110 gpio
= (gpio
& 0x6ff) | ((cx_read(MO_GP1_IO
) << 8) & 0x900);
117 if (ir
->last_gpio
== auxgpio
)
119 ir
->last_gpio
= auxgpio
;
123 data
= ir_extract_bits(gpio
, ir
->mask_keycode
);
124 ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
126 ir
->polling
? "poll" : "irq",
127 (gpio
& ir
->mask_keydown
) ? " down" : "",
128 (gpio
& ir
->mask_keyup
) ? " up" : "");
130 if (ir
->core
->boardnr
== CX88_BOARD_NORWOOD_MICRO
) {
131 u32 gpio_key
= cx_read(MO_GP0_IO
);
133 data
= (data
<< 4) | ((gpio_key
& 0xf0) >> 4);
135 rc_keydown(ir
->dev
, RC_TYPE_UNKNOWN
, data
, 0);
137 } else if (ir
->core
->boardnr
== CX88_BOARD_PROLINK_PLAYTVPVR
||
138 ir
->core
->boardnr
== CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO
) {
139 /* bit cleared on keydown, NEC scancode, 0xAAAACC, A = 0x866b */
144 addr
= (data
>> 8) & 0xffff;
145 cmd
= (data
>> 0) & 0x00ff;
146 scancode
= RC_SCANCODE_NECX(addr
, cmd
);
148 if (0 == (gpio
& ir
->mask_keyup
))
149 rc_keydown_notimeout(ir
->dev
, RC_TYPE_NECX
, scancode
,
154 } else if (ir
->mask_keydown
) {
155 /* bit set on keydown */
156 if (gpio
& ir
->mask_keydown
)
157 rc_keydown_notimeout(ir
->dev
, RC_TYPE_UNKNOWN
, data
, 0);
161 } else if (ir
->mask_keyup
) {
162 /* bit cleared on keydown */
163 if (0 == (gpio
& ir
->mask_keyup
))
164 rc_keydown_notimeout(ir
->dev
, RC_TYPE_UNKNOWN
, data
, 0);
169 /* can't distinguish keydown/up :-/ */
170 rc_keydown_notimeout(ir
->dev
, RC_TYPE_UNKNOWN
, data
, 0);
175 static enum hrtimer_restart
cx88_ir_work(struct hrtimer
*timer
)
177 unsigned long missed
;
178 struct cx88_IR
*ir
= container_of(timer
, struct cx88_IR
, timer
);
180 cx88_ir_handle_key(ir
);
181 missed
= hrtimer_forward_now(&ir
->timer
, ir
->polling
* 1000000);
183 ir_dprintk("Missed ticks %ld\n", missed
- 1);
185 return HRTIMER_RESTART
;
188 static int __cx88_ir_start(void *priv
)
190 struct cx88_core
*core
= priv
;
193 if (!core
|| !core
->ir
)
199 hrtimer_init(&ir
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
200 ir
->timer
.function
= cx88_ir_work
;
201 hrtimer_start(&ir
->timer
, ir
->polling
* 1000000,
205 core
->pci_irqmask
|= PCI_INT_IR_SMPINT
;
206 cx_write(MO_DDS_IO
, 0x33F286 * ir_samplerate
); /* samplerate */
207 cx_write(MO_DDSCFG_IO
, 0x5); /* enable */
212 static void __cx88_ir_stop(void *priv
)
214 struct cx88_core
*core
= priv
;
217 if (!core
|| !core
->ir
)
222 cx_write(MO_DDSCFG_IO
, 0x0);
223 core
->pci_irqmask
&= ~PCI_INT_IR_SMPINT
;
227 hrtimer_cancel(&ir
->timer
);
230 int cx88_ir_start(struct cx88_core
*core
)
233 return __cx88_ir_start(core
);
237 EXPORT_SYMBOL(cx88_ir_start
);
239 void cx88_ir_stop(struct cx88_core
*core
)
242 __cx88_ir_stop(core
);
244 EXPORT_SYMBOL(cx88_ir_stop
);
246 static int cx88_ir_open(struct rc_dev
*rc
)
248 struct cx88_core
*core
= rc
->priv
;
251 return __cx88_ir_start(core
);
254 static void cx88_ir_close(struct rc_dev
*rc
)
256 struct cx88_core
*core
= rc
->priv
;
259 if (!core
->ir
->users
)
260 __cx88_ir_stop(core
);
263 /* ---------------------------------------------------------------------- */
265 int cx88_ir_init(struct cx88_core
*core
, struct pci_dev
*pci
)
269 char *ir_codes
= NULL
;
270 u64 rc_type
= RC_BIT_OTHER
;
272 u32 hardware_mask
= 0; /* For devices with a hardware mask, when
273 * used with a full-code IR table
276 ir
= kzalloc(sizeof(*ir
), GFP_KERNEL
);
277 dev
= rc_allocate_device();
283 /* detect & configure */
284 switch (core
->boardnr
) {
285 case CX88_BOARD_DNTV_LIVE_DVB_T
:
286 case CX88_BOARD_KWORLD_DVB_T
:
287 case CX88_BOARD_KWORLD_DVB_T_CX22702
:
288 ir_codes
= RC_MAP_DNTV_LIVE_DVB_T
;
289 ir
->gpio_addr
= MO_GP1_IO
;
290 ir
->mask_keycode
= 0x1f;
291 ir
->mask_keyup
= 0x60;
292 ir
->polling
= 50; /* ms */
294 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1
:
295 ir_codes
= RC_MAP_CINERGY_1400
;
296 ir
->sampling
= 0xeb04; /* address */
298 case CX88_BOARD_HAUPPAUGE
:
299 case CX88_BOARD_HAUPPAUGE_DVB_T1
:
300 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1
:
301 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1
:
302 case CX88_BOARD_HAUPPAUGE_HVR1100
:
303 case CX88_BOARD_HAUPPAUGE_HVR3000
:
304 case CX88_BOARD_HAUPPAUGE_HVR4000
:
305 case CX88_BOARD_HAUPPAUGE_HVR4000LITE
:
306 case CX88_BOARD_PCHDTV_HD3000
:
307 case CX88_BOARD_PCHDTV_HD5500
:
308 case CX88_BOARD_HAUPPAUGE_IRONLY
:
309 ir_codes
= RC_MAP_HAUPPAUGE
;
312 case CX88_BOARD_WINFAST_DTV2000H
:
313 case CX88_BOARD_WINFAST_DTV2000H_J
:
314 case CX88_BOARD_WINFAST_DTV1800H
:
315 case CX88_BOARD_WINFAST_DTV1800H_XC4000
:
316 case CX88_BOARD_WINFAST_DTV2000H_PLUS
:
317 ir_codes
= RC_MAP_WINFAST
;
318 ir
->gpio_addr
= MO_GP0_IO
;
319 ir
->mask_keycode
= 0x8f8;
320 ir
->mask_keyup
= 0x100;
321 ir
->polling
= 50; /* ms */
323 case CX88_BOARD_WINFAST2000XP_EXPERT
:
324 case CX88_BOARD_WINFAST_DTV1000
:
325 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL
:
326 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F36
:
327 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F43
:
328 ir_codes
= RC_MAP_WINFAST
;
329 ir
->gpio_addr
= MO_GP0_IO
;
330 ir
->mask_keycode
= 0x8f8;
331 ir
->mask_keyup
= 0x100;
332 ir
->polling
= 1; /* ms */
334 case CX88_BOARD_IODATA_GVBCTV7E
:
335 ir_codes
= RC_MAP_IODATA_BCTV7E
;
336 ir
->gpio_addr
= MO_GP0_IO
;
337 ir
->mask_keycode
= 0xfd;
338 ir
->mask_keydown
= 0x02;
339 ir
->polling
= 5; /* ms */
341 case CX88_BOARD_PROLINK_PLAYTVPVR
:
342 case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO
:
344 * It seems that this hardware is paired with NEC extended
345 * address 0x866b. So, unfortunately, its usage with other
346 * IR's with different address won't work. Still, there are
347 * other IR's from the same manufacturer that works, like the
348 * 002-T mini RC, provided with newer PV hardware
350 ir_codes
= RC_MAP_PIXELVIEW_MK12
;
351 rc_type
= RC_BIT_NECX
;
352 ir
->gpio_addr
= MO_GP1_IO
;
353 ir
->mask_keyup
= 0x80;
354 ir
->polling
= 10; /* ms */
355 hardware_mask
= 0x3f; /* Hardware returns only 6 bits from command part */
357 case CX88_BOARD_PROLINK_PV_8000GT
:
358 case CX88_BOARD_PROLINK_PV_GLOBAL_XTREME
:
359 ir_codes
= RC_MAP_PIXELVIEW_NEW
;
360 ir
->gpio_addr
= MO_GP1_IO
;
361 ir
->mask_keycode
= 0x3f;
362 ir
->mask_keyup
= 0x80;
363 ir
->polling
= 1; /* ms */
365 case CX88_BOARD_KWORLD_LTV883
:
366 ir_codes
= RC_MAP_PIXELVIEW
;
367 ir
->gpio_addr
= MO_GP1_IO
;
368 ir
->mask_keycode
= 0x1f;
369 ir
->mask_keyup
= 0x60;
370 ir
->polling
= 1; /* ms */
372 case CX88_BOARD_ADSTECH_DVB_T_PCI
:
373 ir_codes
= RC_MAP_ADSTECH_DVB_T_PCI
;
374 ir
->gpio_addr
= MO_GP1_IO
;
375 ir
->mask_keycode
= 0xbf;
376 ir
->mask_keyup
= 0x40;
377 ir
->polling
= 50; /* ms */
379 case CX88_BOARD_MSI_TVANYWHERE_MASTER
:
380 ir_codes
= RC_MAP_MSI_TVANYWHERE
;
381 ir
->gpio_addr
= MO_GP1_IO
;
382 ir
->mask_keycode
= 0x1f;
383 ir
->mask_keyup
= 0x40;
384 ir
->polling
= 1; /* ms */
386 case CX88_BOARD_AVERTV_303
:
387 case CX88_BOARD_AVERTV_STUDIO_303
:
388 ir_codes
= RC_MAP_AVERTV_303
;
389 ir
->gpio_addr
= MO_GP2_IO
;
390 ir
->mask_keycode
= 0xfb;
391 ir
->mask_keydown
= 0x02;
392 ir
->polling
= 50; /* ms */
394 case CX88_BOARD_OMICOM_SS4_PCI
:
395 case CX88_BOARD_SATTRADE_ST4200
:
396 case CX88_BOARD_TBS_8920
:
397 case CX88_BOARD_TBS_8910
:
398 case CX88_BOARD_PROF_7300
:
399 case CX88_BOARD_PROF_7301
:
400 case CX88_BOARD_PROF_6200
:
401 ir_codes
= RC_MAP_TBS_NEC
;
402 ir
->sampling
= 0xff00; /* address */
404 case CX88_BOARD_TEVII_S464
:
405 case CX88_BOARD_TEVII_S460
:
406 case CX88_BOARD_TEVII_S420
:
407 ir_codes
= RC_MAP_TEVII_NEC
;
408 ir
->sampling
= 0xff00; /* address */
410 case CX88_BOARD_DNTV_LIVE_DVB_T_PRO
:
411 ir_codes
= RC_MAP_DNTV_LIVE_DVBT_PRO
;
412 ir
->sampling
= 0xff00; /* address */
414 case CX88_BOARD_NORWOOD_MICRO
:
415 ir_codes
= RC_MAP_NORWOOD
;
416 ir
->gpio_addr
= MO_GP1_IO
;
417 ir
->mask_keycode
= 0x0e;
418 ir
->mask_keyup
= 0x80;
419 ir
->polling
= 50; /* ms */
421 case CX88_BOARD_NPGTECH_REALTV_TOP10FM
:
422 ir_codes
= RC_MAP_NPGTECH
;
423 ir
->gpio_addr
= MO_GP0_IO
;
424 ir
->mask_keycode
= 0xfa;
425 ir
->polling
= 50; /* ms */
427 case CX88_BOARD_PINNACLE_PCTV_HD_800i
:
428 ir_codes
= RC_MAP_PINNACLE_PCTV_HD
;
431 case CX88_BOARD_POWERCOLOR_REAL_ANGEL
:
432 ir_codes
= RC_MAP_POWERCOLOR_REAL_ANGEL
;
433 ir
->gpio_addr
= MO_GP2_IO
;
434 ir
->mask_keycode
= 0x7e;
435 ir
->polling
= 100; /* ms */
437 case CX88_BOARD_TWINHAN_VP1027_DVBS
:
438 ir_codes
= RC_MAP_TWINHAN_VP1027_DVBS
;
439 ir
->sampling
= 0xff00; /* address */
449 * The usage of mask_keycode were very convenient, due to several
450 * reasons. Among others, the scancode tables were using the scancode
451 * as the index elements. So, the less bits it was used, the smaller
452 * the table were stored. After the input changes, the better is to use
453 * the full scancodes, since it allows replacing the IR remote by
454 * another one. Unfortunately, there are still some hardware, like
455 * Pixelview Ultra Pro, where only part of the scancode is sent via
456 * GPIO. So, there's no way to get the full scancode. Due to that,
457 * hardware_mask were introduced here: it represents those hardware
458 * that has such limits.
460 if (hardware_mask
&& !ir
->mask_keycode
)
461 ir
->mask_keycode
= hardware_mask
;
463 /* init input device */
464 snprintf(ir
->name
, sizeof(ir
->name
), "cx88 IR (%s)", core
->board
.name
);
465 snprintf(ir
->phys
, sizeof(ir
->phys
), "pci-%s/ir0", pci_name(pci
));
467 dev
->input_name
= ir
->name
;
468 dev
->input_phys
= ir
->phys
;
469 dev
->input_id
.bustype
= BUS_PCI
;
470 dev
->input_id
.version
= 1;
471 if (pci
->subsystem_vendor
) {
472 dev
->input_id
.vendor
= pci
->subsystem_vendor
;
473 dev
->input_id
.product
= pci
->subsystem_device
;
475 dev
->input_id
.vendor
= pci
->vendor
;
476 dev
->input_id
.product
= pci
->device
;
478 dev
->dev
.parent
= &pci
->dev
;
479 dev
->map_name
= ir_codes
;
480 dev
->driver_name
= MODULE_NAME
;
482 dev
->open
= cx88_ir_open
;
483 dev
->close
= cx88_ir_close
;
484 dev
->scancode_mask
= hardware_mask
;
487 dev
->driver_type
= RC_DRIVER_IR_RAW
;
488 dev
->timeout
= 10 * 1000 * 1000; /* 10 ms */
490 dev
->driver_type
= RC_DRIVER_SCANCODE
;
491 dev
->allowed_protocols
= rc_type
;
498 err
= rc_register_device(dev
);
511 int cx88_ir_fini(struct cx88_core
*core
)
513 struct cx88_IR
*ir
= core
->ir
;
515 /* skip detach on non attached boards */
520 rc_unregister_device(ir
->dev
);
528 /* ---------------------------------------------------------------------- */
530 void cx88_ir_irq(struct cx88_core
*core
)
532 struct cx88_IR
*ir
= core
->ir
;
534 unsigned int todo
, bits
;
535 struct ir_raw_event ev
;
537 if (!ir
|| !ir
->sampling
)
541 * Samples are stored in a 32 bit register, oldest sample in
542 * the msb. A set bit represents space and an unset bit
543 * represents a pulse.
545 samples
= cx_read(MO_SAMPLE_IO
);
547 if (samples
== 0xff && ir
->dev
->idle
)
550 init_ir_raw_event(&ev
);
551 for (todo
= 32; todo
> 0; todo
-= bits
) {
552 ev
.pulse
= samples
& 0x80000000 ? false : true;
553 bits
= min(todo
, 32U - fls(ev
.pulse
? samples
: ~samples
));
554 ev
.duration
= (bits
* (NSEC_PER_SEC
/ 1000)) / ir_samplerate
;
555 ir_raw_event_store_with_filter(ir
->dev
, &ev
);
558 ir_raw_event_handle(ir
->dev
);
561 static int get_key_pvr2000(struct IR_i2c
*ir
, enum rc_type
*protocol
,
562 u32
*scancode
, u8
*toggle
)
567 flags
= i2c_smbus_read_byte_data(ir
->c
, 0x10);
569 dprintk("read error\n");
573 if (0 == (flags
& 0x80))
576 /* read actual key code */
577 code
= i2c_smbus_read_byte_data(ir
->c
, 0x00);
579 dprintk("read error\n");
583 dprintk("IR Key/Flags: (0x%02x/0x%02x)\n",
584 code
& 0xff, flags
& 0xff);
586 *protocol
= RC_TYPE_UNKNOWN
;
587 *scancode
= code
& 0xff;
592 void cx88_i2c_init_ir(struct cx88_core
*core
)
594 struct i2c_board_info info
;
595 const unsigned short default_addr_list
[] = {
599 const unsigned short pvr2000_addr_list
[] = {
603 const unsigned short *addr_list
= default_addr_list
;
604 const unsigned short *addrp
;
605 /* Instantiate the IR receiver device, if present */
606 if (core
->i2c_rc
!= 0)
609 memset(&info
, 0, sizeof(struct i2c_board_info
));
610 strlcpy(info
.type
, "ir_video", I2C_NAME_SIZE
);
612 switch (core
->boardnr
) {
613 case CX88_BOARD_LEADTEK_PVR2000
:
614 addr_list
= pvr2000_addr_list
;
615 core
->init_data
.name
= "cx88 Leadtek PVR 2000 remote";
616 core
->init_data
.type
= RC_BIT_UNKNOWN
;
617 core
->init_data
.get_key
= get_key_pvr2000
;
618 core
->init_data
.ir_codes
= RC_MAP_EMPTY
;
623 * We can't call i2c_new_probed_device() because it uses
624 * quick writes for probing and at least some RC receiver
625 * devices only reply to reads.
626 * Also, Hauppauge XVR needs to be specified, as address 0x71
627 * conflicts with another remote type used with saa7134
629 for (addrp
= addr_list
; *addrp
!= I2C_CLIENT_END
; addrp
++) {
630 info
.platform_data
= NULL
;
631 memset(&core
->init_data
, 0, sizeof(core
->init_data
));
633 if (*addrp
== 0x71) {
635 core
->init_data
.name
= "cx88 Hauppauge XVR remote";
636 core
->init_data
.ir_codes
= RC_MAP_HAUPPAUGE
;
637 core
->init_data
.type
= RC_BIT_RC5
| RC_BIT_RC6_MCE
|
639 core
->init_data
.internal_get_key_func
= IR_KBD_GET_KEY_HAUP_XVR
;
641 info
.platform_data
= &core
->init_data
;
643 if (i2c_smbus_xfer(&core
->i2c_adap
, *addrp
, 0,
645 I2C_SMBUS_QUICK
, NULL
) >= 0) {
647 i2c_new_device(&core
->i2c_adap
, &info
);
653 /* ---------------------------------------------------------------------- */
655 MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
656 MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
657 MODULE_LICENSE("GPL");