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.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/input.h>
28 #include <linux/pci.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
33 #include <media/ir-common.h>
35 /* ---------------------------------------------------------------------- */
38 struct cx88_core
*core
;
39 struct input_dev
*input
;
40 struct ir_input_state ir
;
44 /* sample from gpio pin 16 */
48 unsigned long release
;
50 /* poll external decoder */
52 struct work_struct work
;
53 struct timer_list timer
;
61 static int ir_debug
= 0;
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...) if (ir_debug) \
66 printk(KERN_DEBUG "%s IR: " fmt , ir->core->name , ##arg)
68 /* ---------------------------------------------------------------------- */
70 static void cx88_ir_handle_key(struct cx88_IR
*ir
)
72 struct cx88_core
*core
= ir
->core
;
73 u32 gpio
, data
, auxgpio
;
76 gpio
= cx_read(ir
->gpio_addr
);
77 if (core
->board
== CX88_BOARD_NPGTECH_REALTV_TOP10FM
) {
78 /* This board apparently uses a combination of 2 GPIO
79 to represent the keys. Additionally, the second GPIO
80 can be used for parity.
85 gpio = 0x758, auxgpio = 0xe5 or 0xf5
87 gpio = 0x758, auxgpio = 0xed or 0xfd
90 auxgpio
= cx_read(MO_GP1_IO
);
91 /* Take out the parity part */
92 gpio
=(gpio
& 0x7fd) + (auxgpio
& 0xef);
93 } else if (core
->board
== CX88_BOARD_WINFAST_DTV1000
) {
94 gpio
= (gpio
& 0x6ff) | ((cx_read(MO_GP1_IO
) << 8) & 0x900);
100 if (ir
->last_gpio
== auxgpio
)
102 ir
->last_gpio
= auxgpio
;
106 data
= ir_extract_bits(gpio
, ir
->mask_keycode
);
107 ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
109 ir
->polling
? "poll" : "irq",
110 (gpio
& ir
->mask_keydown
) ? " down" : "",
111 (gpio
& ir
->mask_keyup
) ? " up" : "");
113 if (ir
->core
->board
== CX88_BOARD_NORWOOD_MICRO
) {
114 u32 gpio_key
= cx_read(MO_GP0_IO
);
116 data
= (data
<< 4) | ((gpio_key
& 0xf0) >> 4);
118 ir_input_keydown(ir
->input
, &ir
->ir
, data
, data
);
119 ir_input_nokey(ir
->input
, &ir
->ir
);
121 } else if (ir
->mask_keydown
) {
122 /* bit set on keydown */
123 if (gpio
& ir
->mask_keydown
) {
124 ir_input_keydown(ir
->input
, &ir
->ir
, data
, data
);
126 ir_input_nokey(ir
->input
, &ir
->ir
);
129 } else if (ir
->mask_keyup
) {
130 /* bit cleared on keydown */
131 if (0 == (gpio
& ir
->mask_keyup
)) {
132 ir_input_keydown(ir
->input
, &ir
->ir
, data
, data
);
134 ir_input_nokey(ir
->input
, &ir
->ir
);
138 /* can't distinguish keydown/up :-/ */
139 ir_input_keydown(ir
->input
, &ir
->ir
, data
, data
);
140 ir_input_nokey(ir
->input
, &ir
->ir
);
144 static void ir_timer(unsigned long data
)
146 struct cx88_IR
*ir
= (struct cx88_IR
*)data
;
148 schedule_work(&ir
->work
);
151 static void cx88_ir_work(struct work_struct
*work
)
153 struct cx88_IR
*ir
= container_of(work
, struct cx88_IR
, work
);
155 cx88_ir_handle_key(ir
);
156 mod_timer(&ir
->timer
, jiffies
+ msecs_to_jiffies(ir
->polling
));
159 static void cx88_ir_start(struct cx88_core
*core
, struct cx88_IR
*ir
)
162 setup_timer(&ir
->timer
, ir_timer
, (unsigned long)ir
);
163 INIT_WORK(&ir
->work
, cx88_ir_work
);
164 schedule_work(&ir
->work
);
167 core
->pci_irqmask
|= (1 << 18); /* IR_SMP_INT */
168 cx_write(MO_DDS_IO
, 0xa80a80); /* 4 kHz sample rate */
169 cx_write(MO_DDSCFG_IO
, 0x5); /* enable */
173 static void cx88_ir_stop(struct cx88_core
*core
, struct cx88_IR
*ir
)
176 cx_write(MO_DDSCFG_IO
, 0x0);
177 core
->pci_irqmask
&= ~(1 << 18);
181 del_timer_sync(&ir
->timer
);
182 flush_scheduled_work();
186 /* ---------------------------------------------------------------------- */
188 int cx88_ir_init(struct cx88_core
*core
, struct pci_dev
*pci
)
191 struct input_dev
*input_dev
;
192 IR_KEYTAB_TYPE
*ir_codes
= NULL
;
193 int ir_type
= IR_TYPE_OTHER
;
196 ir
= kzalloc(sizeof(*ir
), GFP_KERNEL
);
197 input_dev
= input_allocate_device();
198 if (!ir
|| !input_dev
)
201 ir
->input
= input_dev
;
203 /* detect & configure */
204 switch (core
->board
) {
205 case CX88_BOARD_DNTV_LIVE_DVB_T
:
206 case CX88_BOARD_KWORLD_DVB_T
:
207 case CX88_BOARD_KWORLD_DVB_T_CX22702
:
208 ir_codes
= ir_codes_dntv_live_dvb_t
;
209 ir
->gpio_addr
= MO_GP1_IO
;
210 ir
->mask_keycode
= 0x1f;
211 ir
->mask_keyup
= 0x60;
212 ir
->polling
= 50; /* ms */
214 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1
:
215 ir_codes
= ir_codes_cinergy_1400
;
216 ir_type
= IR_TYPE_PD
;
217 ir
->sampling
= 0xeb04; /* address */
219 case CX88_BOARD_HAUPPAUGE
:
220 case CX88_BOARD_HAUPPAUGE_DVB_T1
:
221 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1
:
222 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1
:
223 case CX88_BOARD_HAUPPAUGE_HVR1100
:
224 case CX88_BOARD_HAUPPAUGE_HVR3000
:
225 ir_codes
= ir_codes_hauppauge_new
;
226 ir_type
= IR_TYPE_RC5
;
229 case CX88_BOARD_WINFAST_DTV2000H
:
230 ir_codes
= ir_codes_winfast
;
231 ir
->gpio_addr
= MO_GP0_IO
;
232 ir
->mask_keycode
= 0x8f8;
233 ir
->mask_keyup
= 0x100;
234 ir
->polling
= 50; /* ms */
236 case CX88_BOARD_WINFAST2000XP_EXPERT
:
237 case CX88_BOARD_WINFAST_DTV1000
:
238 ir_codes
= ir_codes_winfast
;
239 ir
->gpio_addr
= MO_GP0_IO
;
240 ir
->mask_keycode
= 0x8f8;
241 ir
->mask_keyup
= 0x100;
242 ir
->polling
= 1; /* ms */
244 case CX88_BOARD_IODATA_GVBCTV7E
:
245 ir_codes
= ir_codes_iodata_bctv7e
;
246 ir
->gpio_addr
= MO_GP0_IO
;
247 ir
->mask_keycode
= 0xfd;
248 ir
->mask_keydown
= 0x02;
249 ir
->polling
= 5; /* ms */
251 case CX88_BOARD_PROLINK_PLAYTVPVR
:
252 case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO
:
253 ir_codes
= ir_codes_pixelview
;
254 ir
->gpio_addr
= MO_GP1_IO
;
255 ir
->mask_keycode
= 0x1f;
256 ir
->mask_keyup
= 0x80;
257 ir
->polling
= 1; /* ms */
259 case CX88_BOARD_KWORLD_LTV883
:
260 ir_codes
= ir_codes_pixelview
;
261 ir
->gpio_addr
= MO_GP1_IO
;
262 ir
->mask_keycode
= 0x1f;
263 ir
->mask_keyup
= 0x60;
264 ir
->polling
= 1; /* ms */
266 case CX88_BOARD_ADSTECH_DVB_T_PCI
:
267 ir_codes
= ir_codes_adstech_dvb_t_pci
;
268 ir
->gpio_addr
= MO_GP1_IO
;
269 ir
->mask_keycode
= 0xbf;
270 ir
->mask_keyup
= 0x40;
271 ir
->polling
= 50; /* ms */
273 case CX88_BOARD_MSI_TVANYWHERE_MASTER
:
274 ir_codes
= ir_codes_msi_tvanywhere
;
275 ir
->gpio_addr
= MO_GP1_IO
;
276 ir
->mask_keycode
= 0x1f;
277 ir
->mask_keyup
= 0x40;
278 ir
->polling
= 1; /* ms */
280 case CX88_BOARD_AVERTV_303
:
281 case CX88_BOARD_AVERTV_STUDIO_303
:
282 ir_codes
= ir_codes_avertv_303
;
283 ir
->gpio_addr
= MO_GP2_IO
;
284 ir
->mask_keycode
= 0xfb;
285 ir
->mask_keydown
= 0x02;
286 ir
->polling
= 50; /* ms */
288 case CX88_BOARD_DNTV_LIVE_DVB_T_PRO
:
289 ir_codes
= ir_codes_dntv_live_dvbt_pro
;
290 ir_type
= IR_TYPE_PD
;
291 ir
->sampling
= 0xff00; /* address */
293 case CX88_BOARD_NORWOOD_MICRO
:
294 ir_codes
= ir_codes_norwood
;
295 ir
->gpio_addr
= MO_GP1_IO
;
296 ir
->mask_keycode
= 0x0e;
297 ir
->mask_keyup
= 0x80;
298 ir
->polling
= 50; /* ms */
300 case CX88_BOARD_NPGTECH_REALTV_TOP10FM
:
301 ir_codes
= ir_codes_npgtech
;
302 ir
->gpio_addr
= MO_GP0_IO
;
303 ir
->mask_keycode
= 0xfa;
304 ir
->polling
= 50; /* ms */
308 if (NULL
== ir_codes
) {
313 /* init input device */
314 snprintf(ir
->name
, sizeof(ir
->name
), "cx88 IR (%s)",
315 cx88_boards
[core
->board
].name
);
316 snprintf(ir
->phys
, sizeof(ir
->phys
), "pci-%s/ir0", pci_name(pci
));
318 ir_input_init(input_dev
, &ir
->ir
, ir_type
, ir_codes
);
319 input_dev
->name
= ir
->name
;
320 input_dev
->phys
= ir
->phys
;
321 input_dev
->id
.bustype
= BUS_PCI
;
322 input_dev
->id
.version
= 1;
323 if (pci
->subsystem_vendor
) {
324 input_dev
->id
.vendor
= pci
->subsystem_vendor
;
325 input_dev
->id
.product
= pci
->subsystem_device
;
327 input_dev
->id
.vendor
= pci
->vendor
;
328 input_dev
->id
.product
= pci
->device
;
330 input_dev
->dev
.parent
= &pci
->dev
;
331 /* record handles to ourself */
335 cx88_ir_start(core
, ir
);
338 err
= input_register_device(ir
->input
);
345 cx88_ir_stop(core
, ir
);
348 input_free_device(input_dev
);
353 int cx88_ir_fini(struct cx88_core
*core
)
355 struct cx88_IR
*ir
= core
->ir
;
357 /* skip detach on non attached boards */
361 cx88_ir_stop(core
, ir
);
362 input_unregister_device(ir
->input
);
370 /* ---------------------------------------------------------------------- */
372 void cx88_ir_irq(struct cx88_core
*core
)
374 struct cx88_IR
*ir
= core
->ir
;
383 samples
= cx_read(MO_SAMPLE_IO
);
384 if (0 != samples
&& 0xffffffff != samples
) {
385 /* record sample data */
386 if (ir
->scount
< ARRAY_SIZE(ir
->samples
))
387 ir
->samples
[ir
->scount
++] = samples
;
391 /* nothing to sample */
392 if (ir
->ir
.keypressed
&& time_after(jiffies
, ir
->release
))
393 ir_input_nokey(ir
->input
, &ir
->ir
);
397 /* have a complete sample */
398 if (ir
->scount
< ARRAY_SIZE(ir
->samples
))
399 ir
->samples
[ir
->scount
++] = samples
;
400 for (i
= 0; i
< ir
->scount
; i
++)
401 ir
->samples
[i
] = ~ir
->samples
[i
];
403 ir_dump_samples(ir
->samples
, ir
->scount
);
406 switch (core
->board
) {
407 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1
:
408 case CX88_BOARD_DNTV_LIVE_DVB_T_PRO
:
409 ircode
= ir_decode_pulsedistance(ir
->samples
, ir
->scount
, 1, 4);
411 if (ircode
== 0xffffffff) { /* decoding error */
412 ir_dprintk("pulse distance decoding error\n");
416 ir_dprintk("pulse distance decoded: %x\n", ircode
);
418 if (ircode
== 0) { /* key still pressed */
419 ir_dprintk("pulse distance decoded repeat code\n");
420 ir
->release
= jiffies
+ msecs_to_jiffies(120);
424 if ((ircode
& 0xffff) != (ir
->sampling
& 0xffff)) { /* wrong address */
425 ir_dprintk("pulse distance decoded wrong address\n");
429 if (((~ircode
>> 24) & 0xff) != ((ircode
>> 16) & 0xff)) { /* wrong checksum */
430 ir_dprintk("pulse distance decoded wrong check sum\n");
434 ir_dprintk("Key Code: %x\n", (ircode
>> 16) & 0x7f);
436 ir_input_keydown(ir
->input
, &ir
->ir
, (ircode
>> 16) & 0x7f, (ircode
>> 16) & 0xff);
437 ir
->release
= jiffies
+ msecs_to_jiffies(120);
439 case CX88_BOARD_HAUPPAUGE
:
440 case CX88_BOARD_HAUPPAUGE_DVB_T1
:
441 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1
:
442 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1
:
443 case CX88_BOARD_HAUPPAUGE_HVR1100
:
444 case CX88_BOARD_HAUPPAUGE_HVR3000
:
445 ircode
= ir_decode_biphase(ir
->samples
, ir
->scount
, 5, 7);
446 ir_dprintk("biphase decoded: %x\n", ircode
);
447 if ((ircode
& 0xfffff000) != 0x3000)
449 ir_input_keydown(ir
->input
, &ir
->ir
, ircode
& 0x3f, ircode
);
450 ir
->release
= jiffies
+ msecs_to_jiffies(120);
458 /* ---------------------------------------------------------------------- */
460 MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
461 MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
462 MODULE_LICENSE("GPL");