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/hrtimer.h>
27 #include <linux/input.h>
28 #include <linux/pci.h>
29 #include <linux/slab.h>
30 #include <linux/module.h>
33 #include <media/ir-core.h>
35 #define MODULE_NAME "cx88xx"
37 /* ---------------------------------------------------------------------- */
40 struct cx88_core *core;
41 struct input_dev *input;
42 struct ir_dev_props props;
49 /* sample from gpio pin 16 */
52 /* poll external decoder */
62 static unsigned ir_samplerate = 4;
63 module_param(ir_samplerate, uint, 0444);
64 MODULE_PARM_DESC(ir_samplerate, "IR samplerate in kHz, 1 - 20, default 4");
67 module_param(ir_debug, int, 0644); /* debug level [IR] */
68 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
70 #define ir_dprintk(fmt, arg...) if (ir_debug) \
71 printk(KERN_DEBUG "%s IR: " fmt , ir->core->name , ##arg)
73 /* ---------------------------------------------------------------------- */
75 static void cx88_ir_handle_key(struct cx88_IR *ir)
77 struct cx88_core *core = ir->core;
78 u32 gpio, data, auxgpio;
81 gpio = cx_read(ir->gpio_addr);
82 switch (core->boardnr) {
83 case CX88_BOARD_NPGTECH_REALTV_TOP10FM:
84 /* This board apparently uses a combination of 2 GPIO
85 to represent the keys. Additionally, the second GPIO
86 can be used for parity.
91 gpio = 0x758, auxgpio = 0xe5 or 0xf5
93 gpio = 0x758, auxgpio = 0xed or 0xfd
96 auxgpio = cx_read(MO_GP1_IO);
97 /* Take out the parity part */
98 gpio=(gpio & 0x7fd) + (auxgpio & 0xef);
100 case CX88_BOARD_WINFAST_DTV1000:
101 case CX88_BOARD_WINFAST_DTV1800H:
102 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL:
103 gpio = (gpio & 0x6ff) | ((cx_read(MO_GP1_IO) << 8) & 0x900);
110 if (ir->last_gpio == auxgpio)
112 ir->last_gpio = auxgpio;
116 data = ir_extract_bits(gpio, ir->mask_keycode);
117 ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
119 ir->polling ? "poll" : "irq",
120 (gpio & ir->mask_keydown) ? " down" : "",
121 (gpio & ir->mask_keyup) ? " up" : "");
123 if (ir->core->boardnr == CX88_BOARD_NORWOOD_MICRO) {
124 u32 gpio_key = cx_read(MO_GP0_IO);
126 data = (data << 4) | ((gpio_key & 0xf0) >> 4);
128 ir_keydown(ir->input, data, 0);
130 } else if (ir->mask_keydown) {
131 /* bit set on keydown */
132 if (gpio & ir->mask_keydown)
133 ir_keydown_notimeout(ir->input, data, 0);
137 } else if (ir->mask_keyup) {
138 /* bit cleared on keydown */
139 if (0 == (gpio & ir->mask_keyup))
140 ir_keydown_notimeout(ir->input, data, 0);
145 /* can't distinguish keydown/up :-/ */
146 ir_keydown_notimeout(ir->input, data, 0);
151 static enum hrtimer_restart cx88_ir_work(struct hrtimer *timer)
153 unsigned long missed;
154 struct cx88_IR *ir = container_of(timer, struct cx88_IR, timer);
156 cx88_ir_handle_key(ir);
157 missed = hrtimer_forward_now(&ir->timer,
158 ktime_set(0, ir->polling * 1000000));
160 ir_dprintk("Missed ticks %ld\n", missed - 1);
162 return HRTIMER_RESTART;
165 static int __cx88_ir_start(void *priv)
167 struct cx88_core *core = priv;
170 if (!core || !core->ir)
176 hrtimer_init(&ir->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
177 ir->timer.function = cx88_ir_work;
178 hrtimer_start(&ir->timer,
179 ktime_set(0, ir->polling * 1000000),
183 core->pci_irqmask |= PCI_INT_IR_SMPINT;
184 cx_write(MO_DDS_IO, 0x33F286 * ir_samplerate); /* samplerate */
185 cx_write(MO_DDSCFG_IO, 0x5); /* enable */
190 static void __cx88_ir_stop(void *priv)
192 struct cx88_core *core = priv;
195 if (!core || !core->ir)
200 cx_write(MO_DDSCFG_IO, 0x0);
201 core->pci_irqmask &= ~PCI_INT_IR_SMPINT;
205 hrtimer_cancel(&ir->timer);
208 int cx88_ir_start(struct cx88_core *core)
211 return __cx88_ir_start(core);
216 void cx88_ir_stop(struct cx88_core *core)
219 __cx88_ir_stop(core);
222 static int cx88_ir_open(void *priv)
224 struct cx88_core *core = priv;
227 return __cx88_ir_start(core);
230 static void cx88_ir_close(void *priv)
232 struct cx88_core *core = priv;
235 if (!core->ir->users)
236 __cx88_ir_stop(core);
239 /* ---------------------------------------------------------------------- */
241 int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
244 struct input_dev *input_dev;
245 char *ir_codes = NULL;
246 u64 ir_type = IR_TYPE_OTHER;
248 u32 hardware_mask = 0; /* For devices with a hardware mask, when
249 * used with a full-code IR table
252 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
253 input_dev = input_allocate_device();
254 if (!ir || !input_dev)
257 ir->input = input_dev;
259 /* detect & configure */
260 switch (core->boardnr) {
261 case CX88_BOARD_DNTV_LIVE_DVB_T:
262 case CX88_BOARD_KWORLD_DVB_T:
263 case CX88_BOARD_KWORLD_DVB_T_CX22702:
264 ir_codes = RC_MAP_DNTV_LIVE_DVB_T;
265 ir->gpio_addr = MO_GP1_IO;
266 ir->mask_keycode = 0x1f;
267 ir->mask_keyup = 0x60;
268 ir->polling = 50; /* ms */
270 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
271 ir_codes = RC_MAP_CINERGY_1400;
272 ir->sampling = 0xeb04; /* address */
274 case CX88_BOARD_HAUPPAUGE:
275 case CX88_BOARD_HAUPPAUGE_DVB_T1:
276 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
277 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
278 case CX88_BOARD_HAUPPAUGE_HVR1100:
279 case CX88_BOARD_HAUPPAUGE_HVR3000:
280 case CX88_BOARD_HAUPPAUGE_HVR4000:
281 case CX88_BOARD_HAUPPAUGE_HVR4000LITE:
282 case CX88_BOARD_PCHDTV_HD3000:
283 case CX88_BOARD_PCHDTV_HD5500:
284 case CX88_BOARD_HAUPPAUGE_IRONLY:
285 ir_codes = RC_MAP_HAUPPAUGE_NEW;
288 case CX88_BOARD_WINFAST_DTV2000H:
289 case CX88_BOARD_WINFAST_DTV2000H_J:
290 case CX88_BOARD_WINFAST_DTV1800H:
291 ir_codes = RC_MAP_WINFAST;
292 ir->gpio_addr = MO_GP0_IO;
293 ir->mask_keycode = 0x8f8;
294 ir->mask_keyup = 0x100;
295 ir->polling = 50; /* ms */
297 case CX88_BOARD_WINFAST2000XP_EXPERT:
298 case CX88_BOARD_WINFAST_DTV1000:
299 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL:
300 ir_codes = RC_MAP_WINFAST;
301 ir->gpio_addr = MO_GP0_IO;
302 ir->mask_keycode = 0x8f8;
303 ir->mask_keyup = 0x100;
304 ir->polling = 1; /* ms */
306 case CX88_BOARD_IODATA_GVBCTV7E:
307 ir_codes = RC_MAP_IODATA_BCTV7E;
308 ir->gpio_addr = MO_GP0_IO;
309 ir->mask_keycode = 0xfd;
310 ir->mask_keydown = 0x02;
311 ir->polling = 5; /* ms */
313 case CX88_BOARD_PROLINK_PLAYTVPVR:
314 case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
316 * It seems that this hardware is paired with NEC extended
317 * address 0x866b. So, unfortunately, its usage with other
318 * IR's with different address won't work. Still, there are
319 * other IR's from the same manufacturer that works, like the
320 * 002-T mini RC, provided with newer PV hardware
322 ir_codes = RC_MAP_PIXELVIEW_MK12;
323 ir->gpio_addr = MO_GP1_IO;
324 ir->mask_keyup = 0x80;
325 ir->polling = 10; /* ms */
326 hardware_mask = 0x3f; /* Hardware returns only 6 bits from command part */
328 case CX88_BOARD_PROLINK_PV_8000GT:
329 case CX88_BOARD_PROLINK_PV_GLOBAL_XTREME:
330 ir_codes = RC_MAP_PIXELVIEW_NEW;
331 ir->gpio_addr = MO_GP1_IO;
332 ir->mask_keycode = 0x3f;
333 ir->mask_keyup = 0x80;
334 ir->polling = 1; /* ms */
336 case CX88_BOARD_KWORLD_LTV883:
337 ir_codes = RC_MAP_PIXELVIEW;
338 ir->gpio_addr = MO_GP1_IO;
339 ir->mask_keycode = 0x1f;
340 ir->mask_keyup = 0x60;
341 ir->polling = 1; /* ms */
343 case CX88_BOARD_ADSTECH_DVB_T_PCI:
344 ir_codes = RC_MAP_ADSTECH_DVB_T_PCI;
345 ir->gpio_addr = MO_GP1_IO;
346 ir->mask_keycode = 0xbf;
347 ir->mask_keyup = 0x40;
348 ir->polling = 50; /* ms */
350 case CX88_BOARD_MSI_TVANYWHERE_MASTER:
351 ir_codes = RC_MAP_MSI_TVANYWHERE;
352 ir->gpio_addr = MO_GP1_IO;
353 ir->mask_keycode = 0x1f;
354 ir->mask_keyup = 0x40;
355 ir->polling = 1; /* ms */
357 case CX88_BOARD_AVERTV_303:
358 case CX88_BOARD_AVERTV_STUDIO_303:
359 ir_codes = RC_MAP_AVERTV_303;
360 ir->gpio_addr = MO_GP2_IO;
361 ir->mask_keycode = 0xfb;
362 ir->mask_keydown = 0x02;
363 ir->polling = 50; /* ms */
365 case CX88_BOARD_OMICOM_SS4_PCI:
366 case CX88_BOARD_SATTRADE_ST4200:
367 case CX88_BOARD_TBS_8920:
368 case CX88_BOARD_TBS_8910:
369 case CX88_BOARD_PROF_7300:
370 case CX88_BOARD_PROF_7301:
371 case CX88_BOARD_PROF_6200:
372 ir_codes = RC_MAP_TBS_NEC;
373 ir->sampling = 0xff00; /* address */
375 case CX88_BOARD_TEVII_S460:
376 case CX88_BOARD_TEVII_S420:
377 ir_codes = RC_MAP_TEVII_NEC;
378 ir->sampling = 0xff00; /* address */
380 case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
381 ir_codes = RC_MAP_DNTV_LIVE_DVBT_PRO;
382 ir->sampling = 0xff00; /* address */
384 case CX88_BOARD_NORWOOD_MICRO:
385 ir_codes = RC_MAP_NORWOOD;
386 ir->gpio_addr = MO_GP1_IO;
387 ir->mask_keycode = 0x0e;
388 ir->mask_keyup = 0x80;
389 ir->polling = 50; /* ms */
391 case CX88_BOARD_NPGTECH_REALTV_TOP10FM:
392 ir_codes = RC_MAP_NPGTECH;
393 ir->gpio_addr = MO_GP0_IO;
394 ir->mask_keycode = 0xfa;
395 ir->polling = 50; /* ms */
397 case CX88_BOARD_PINNACLE_PCTV_HD_800i:
398 ir_codes = RC_MAP_PINNACLE_PCTV_HD;
401 case CX88_BOARD_POWERCOLOR_REAL_ANGEL:
402 ir_codes = RC_MAP_POWERCOLOR_REAL_ANGEL;
403 ir->gpio_addr = MO_GP2_IO;
404 ir->mask_keycode = 0x7e;
405 ir->polling = 100; /* ms */
407 case CX88_BOARD_TWINHAN_VP1027_DVBS:
408 ir_codes = RC_MAP_TWINHAN_VP1027_DVBS;
409 ir_type = IR_TYPE_NEC;
410 ir->sampling = 0xff00; /* address */
420 * The usage of mask_keycode were very convenient, due to several
421 * reasons. Among others, the scancode tables were using the scancode
422 * as the index elements. So, the less bits it was used, the smaller
423 * the table were stored. After the input changes, the better is to use
424 * the full scancodes, since it allows replacing the IR remote by
425 * another one. Unfortunately, there are still some hardware, like
426 * Pixelview Ultra Pro, where only part of the scancode is sent via
427 * GPIO. So, there's no way to get the full scancode. Due to that,
428 * hardware_mask were introduced here: it represents those hardware
429 * that has such limits.
431 if (hardware_mask && !ir->mask_keycode)
432 ir->mask_keycode = hardware_mask;
434 /* init input device */
435 snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)", core->board.name);
436 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(pci));
438 input_dev->name = ir->name;
439 input_dev->phys = ir->phys;
440 input_dev->id.bustype = BUS_PCI;
441 input_dev->id.version = 1;
442 if (pci->subsystem_vendor) {
443 input_dev->id.vendor = pci->subsystem_vendor;
444 input_dev->id.product = pci->subsystem_device;
446 input_dev->id.vendor = pci->vendor;
447 input_dev->id.product = pci->device;
449 input_dev->dev.parent = &pci->dev;
450 /* record handles to ourself */
455 ir_type = IR_TYPE_ALL;
456 ir->props.driver_type = RC_DRIVER_IR_RAW;
457 ir->props.timeout = 10 * 1000 * 1000; /* 10 ms */
459 ir->props.driver_type = RC_DRIVER_SCANCODE;
461 ir->props.priv = core;
462 ir->props.open = cx88_ir_open;
463 ir->props.close = cx88_ir_close;
464 ir->props.scanmask = hardware_mask;
465 ir->props.allowed_protos = ir_type;
468 err = ir_input_register(ir->input, ir_codes, &ir->props, MODULE_NAME);
480 int cx88_ir_fini(struct cx88_core *core)
482 struct cx88_IR *ir = core->ir;
484 /* skip detach on non attached boards */
489 ir_input_unregister(ir->input);
497 /* ---------------------------------------------------------------------- */
499 void cx88_ir_irq(struct cx88_core *core)
501 struct cx88_IR *ir = core->ir;
504 struct ir_raw_event ev;
505 struct ir_input_dev *irdev;
507 if (!ir || !ir->sampling)
511 * Samples are stored in a 32 bit register, oldest sample in
512 * the msb. A set bit represents space and an unset bit
513 * represents a pulse.
515 samples = cx_read(MO_SAMPLE_IO);
516 irdev = input_get_drvdata(ir->input);
518 if (samples == 0xff && irdev->idle)
521 init_ir_raw_event(&ev);
522 for (todo = 32; todo > 0; todo -= bits) {
523 ev.pulse = samples & 0x80000000 ? false : true;
524 bits = min(todo, 32U - fls(ev.pulse ? samples : ~samples));
525 ev.duration = (bits * NSEC_PER_SEC) / (1000 * ir_samplerate);
526 ir_raw_event_store_with_filter(ir->input, &ev);
529 ir_raw_event_handle(ir->input);
532 void cx88_i2c_init_ir(struct cx88_core *core)
534 struct i2c_board_info info;
535 const unsigned short addr_list[] = {
539 const unsigned short *addrp;
540 /* Instantiate the IR receiver device, if present */
541 if (0 != core->i2c_rc)
544 memset(&info, 0, sizeof(struct i2c_board_info));
545 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
548 * We can't call i2c_new_probed_device() because it uses
549 * quick writes for probing and at least some RC receiver
550 * devices only reply to reads.
551 * Also, Hauppauge XVR needs to be specified, as address 0x71
552 * conflicts with another remote type used with saa7134
554 for (addrp = addr_list; *addrp != I2C_CLIENT_END; addrp++) {
555 info.platform_data = NULL;
556 memset(&core->init_data, 0, sizeof(core->init_data));
558 if (*addrp == 0x71) {
560 core->init_data.name = "cx88 Hauppauge XVR remote";
561 core->init_data.ir_codes = RC_MAP_HAUPPAUGE_NEW;
562 core->init_data.type = IR_TYPE_RC5;
563 core->init_data.internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
565 info.platform_data = &core->init_data;
567 if (i2c_smbus_xfer(&core->i2c_adap, *addrp, 0,
569 I2C_SMBUS_QUICK, NULL) >= 0) {
571 i2c_new_device(&core->i2c_adap, &info);
577 /* ---------------------------------------------------------------------- */
579 MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
580 MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
581 MODULE_LICENSE("GPL");