Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / media / pci / cx88 / cx88-input.c
blobce0ef0b8186f5f01012c43e5e050e67ea8e75090
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
4 * Device driver for GPIO attached remote control interfaces
5 * on Conexant 2388x based TV/DVB cards.
7 * Copyright (c) 2003 Pavel Machek
8 * Copyright (c) 2004 Gerd Knorr
9 * Copyright (c) 2004, 2005 Chris Pascoe
12 #include "cx88.h"
14 #include <linux/init.h>
15 #include <linux/hrtimer.h>
16 #include <linux/pci.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
20 #include <media/rc-core.h>
22 #define MODULE_NAME "cx88xx"
24 /* ---------------------------------------------------------------------- */
26 struct cx88_IR {
27 struct cx88_core *core;
28 struct rc_dev *dev;
30 int users;
32 char name[32];
33 char phys[32];
35 /* sample from gpio pin 16 */
36 u32 sampling;
38 /* poll external decoder */
39 int polling;
40 struct hrtimer timer;
41 u32 gpio_addr;
42 u32 last_gpio;
43 u32 mask_keycode;
44 u32 mask_keydown;
45 u32 mask_keyup;
48 static unsigned int ir_samplerate = 4;
49 module_param(ir_samplerate, uint, 0444);
50 MODULE_PARM_DESC(ir_samplerate, "IR samplerate in kHz, 1 - 20, default 4");
52 static int ir_debug;
53 module_param(ir_debug, int, 0644); /* debug level [IR] */
54 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
56 #define ir_dprintk(fmt, arg...) do { \
57 if (ir_debug) \
58 printk(KERN_DEBUG "%s IR: " fmt, ir->core->name, ##arg);\
59 } while (0)
61 #define dprintk(fmt, arg...) do { \
62 if (ir_debug) \
63 printk(KERN_DEBUG "cx88 IR: " fmt, ##arg); \
64 } while (0)
66 /* ---------------------------------------------------------------------- */
68 static void cx88_ir_handle_key(struct cx88_IR *ir)
70 struct cx88_core *core = ir->core;
71 u32 gpio, data, auxgpio;
73 /* read gpio value */
74 gpio = cx_read(ir->gpio_addr);
75 switch (core->boardnr) {
76 case 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.
82 * Example:
84 * for key "5"
85 * gpio = 0x758, auxgpio = 0xe5 or 0xf5
86 * for key "Power"
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 break;
94 case CX88_BOARD_WINFAST_DTV1000:
95 case CX88_BOARD_WINFAST_DTV1800H:
96 case CX88_BOARD_WINFAST_DTV1800H_XC4000:
97 case CX88_BOARD_WINFAST_DTV2000H_PLUS:
98 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL:
99 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F36:
100 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F43:
101 gpio = (gpio & 0x6ff) | ((cx_read(MO_GP1_IO) << 8) & 0x900);
102 auxgpio = gpio;
103 break;
104 default:
105 auxgpio = gpio;
107 if (ir->polling) {
108 if (ir->last_gpio == auxgpio)
109 return;
110 ir->last_gpio = auxgpio;
113 /* extract data */
114 data = ir_extract_bits(gpio, ir->mask_keycode);
115 ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
116 gpio, data,
117 ir->polling ? "poll" : "irq",
118 (gpio & ir->mask_keydown) ? " down" : "",
119 (gpio & ir->mask_keyup) ? " up" : "");
121 if (ir->core->boardnr == CX88_BOARD_NORWOOD_MICRO) {
122 u32 gpio_key = cx_read(MO_GP0_IO);
124 data = (data << 4) | ((gpio_key & 0xf0) >> 4);
126 rc_keydown(ir->dev, RC_PROTO_UNKNOWN, data, 0);
128 } else if (ir->core->boardnr == CX88_BOARD_PROLINK_PLAYTVPVR ||
129 ir->core->boardnr == CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO) {
130 /* bit cleared on keydown, NEC scancode, 0xAAAACC, A = 0x866b */
131 u16 addr;
132 u8 cmd;
133 u32 scancode;
135 addr = (data >> 8) & 0xffff;
136 cmd = (data >> 0) & 0x00ff;
137 scancode = RC_SCANCODE_NECX(addr, cmd);
139 if (0 == (gpio & ir->mask_keyup))
140 rc_keydown_notimeout(ir->dev, RC_PROTO_NECX, scancode,
142 else
143 rc_keyup(ir->dev);
145 } else if (ir->mask_keydown) {
146 /* bit set on keydown */
147 if (gpio & ir->mask_keydown)
148 rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
150 else
151 rc_keyup(ir->dev);
153 } else if (ir->mask_keyup) {
154 /* bit cleared on keydown */
155 if (0 == (gpio & ir->mask_keyup))
156 rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
158 else
159 rc_keyup(ir->dev);
161 } else {
162 /* can't distinguish keydown/up :-/ */
163 rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data, 0);
164 rc_keyup(ir->dev);
168 static enum hrtimer_restart cx88_ir_work(struct hrtimer *timer)
170 u64 missed;
171 struct cx88_IR *ir = container_of(timer, struct cx88_IR, timer);
173 cx88_ir_handle_key(ir);
174 missed = hrtimer_forward_now(&ir->timer,
175 ktime_set(0, ir->polling * 1000000));
176 if (missed > 1)
177 ir_dprintk("Missed ticks %llu\n", missed - 1);
179 return HRTIMER_RESTART;
182 static int __cx88_ir_start(void *priv)
184 struct cx88_core *core = priv;
185 struct cx88_IR *ir;
187 if (!core || !core->ir)
188 return -EINVAL;
190 ir = core->ir;
192 if (ir->polling) {
193 hrtimer_init(&ir->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
194 ir->timer.function = cx88_ir_work;
195 hrtimer_start(&ir->timer,
196 ktime_set(0, ir->polling * 1000000),
197 HRTIMER_MODE_REL);
199 if (ir->sampling) {
200 core->pci_irqmask |= PCI_INT_IR_SMPINT;
201 cx_write(MO_DDS_IO, 0x33F286 * ir_samplerate); /* samplerate */
202 cx_write(MO_DDSCFG_IO, 0x5); /* enable */
204 return 0;
207 static void __cx88_ir_stop(void *priv)
209 struct cx88_core *core = priv;
210 struct cx88_IR *ir;
212 if (!core || !core->ir)
213 return;
215 ir = core->ir;
216 if (ir->sampling) {
217 cx_write(MO_DDSCFG_IO, 0x0);
218 core->pci_irqmask &= ~PCI_INT_IR_SMPINT;
221 if (ir->polling)
222 hrtimer_cancel(&ir->timer);
225 int cx88_ir_start(struct cx88_core *core)
227 if (core->ir->users)
228 return __cx88_ir_start(core);
230 return 0;
232 EXPORT_SYMBOL(cx88_ir_start);
234 void cx88_ir_stop(struct cx88_core *core)
236 if (core->ir->users)
237 __cx88_ir_stop(core);
239 EXPORT_SYMBOL(cx88_ir_stop);
241 static int cx88_ir_open(struct rc_dev *rc)
243 struct cx88_core *core = rc->priv;
245 core->ir->users++;
246 return __cx88_ir_start(core);
249 static void cx88_ir_close(struct rc_dev *rc)
251 struct cx88_core *core = rc->priv;
253 core->ir->users--;
254 if (!core->ir->users)
255 __cx88_ir_stop(core);
258 /* ---------------------------------------------------------------------- */
260 int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
262 struct cx88_IR *ir;
263 struct rc_dev *dev;
264 char *ir_codes = NULL;
265 u64 rc_proto = RC_PROTO_BIT_OTHER;
266 int err = -ENOMEM;
267 u32 hardware_mask = 0; /* For devices with a hardware mask, when
268 * used with a full-code IR table
271 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
272 dev = rc_allocate_device(RC_DRIVER_IR_RAW);
273 if (!ir || !dev)
274 goto err_out_free;
276 ir->dev = dev;
278 /* detect & configure */
279 switch (core->boardnr) {
280 case CX88_BOARD_DNTV_LIVE_DVB_T:
281 case CX88_BOARD_KWORLD_DVB_T:
282 case CX88_BOARD_KWORLD_DVB_T_CX22702:
283 ir_codes = RC_MAP_DNTV_LIVE_DVB_T;
284 ir->gpio_addr = MO_GP1_IO;
285 ir->mask_keycode = 0x1f;
286 ir->mask_keyup = 0x60;
287 ir->polling = 50; /* ms */
288 break;
289 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
290 ir_codes = RC_MAP_CINERGY_1400;
291 ir->sampling = 0xeb04; /* address */
292 break;
293 case CX88_BOARD_HAUPPAUGE:
294 case CX88_BOARD_HAUPPAUGE_DVB_T1:
295 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
296 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
297 case CX88_BOARD_HAUPPAUGE_HVR1100:
298 case CX88_BOARD_HAUPPAUGE_HVR3000:
299 case CX88_BOARD_HAUPPAUGE_HVR4000:
300 case CX88_BOARD_HAUPPAUGE_HVR4000LITE:
301 case CX88_BOARD_PCHDTV_HD3000:
302 case CX88_BOARD_PCHDTV_HD5500:
303 case CX88_BOARD_HAUPPAUGE_IRONLY:
304 ir_codes = RC_MAP_HAUPPAUGE;
305 ir->sampling = 1;
306 break;
307 case CX88_BOARD_WINFAST_DTV2000H:
308 case CX88_BOARD_WINFAST_DTV2000H_J:
309 case CX88_BOARD_WINFAST_DTV1800H:
310 case CX88_BOARD_WINFAST_DTV1800H_XC4000:
311 case CX88_BOARD_WINFAST_DTV2000H_PLUS:
312 ir_codes = RC_MAP_WINFAST;
313 ir->gpio_addr = MO_GP0_IO;
314 ir->mask_keycode = 0x8f8;
315 ir->mask_keyup = 0x100;
316 ir->polling = 50; /* ms */
317 break;
318 case CX88_BOARD_WINFAST2000XP_EXPERT:
319 case CX88_BOARD_WINFAST_DTV1000:
320 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL:
321 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F36:
322 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F43:
323 ir_codes = RC_MAP_WINFAST;
324 ir->gpio_addr = MO_GP0_IO;
325 ir->mask_keycode = 0x8f8;
326 ir->mask_keyup = 0x100;
327 ir->polling = 1; /* ms */
328 break;
329 case CX88_BOARD_IODATA_GVBCTV7E:
330 ir_codes = RC_MAP_IODATA_BCTV7E;
331 ir->gpio_addr = MO_GP0_IO;
332 ir->mask_keycode = 0xfd;
333 ir->mask_keydown = 0x02;
334 ir->polling = 5; /* ms */
335 break;
336 case CX88_BOARD_PROLINK_PLAYTVPVR:
337 case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
339 * It seems that this hardware is paired with NEC extended
340 * address 0x866b. So, unfortunately, its usage with other
341 * IR's with different address won't work. Still, there are
342 * other IR's from the same manufacturer that works, like the
343 * 002-T mini RC, provided with newer PV hardware
345 ir_codes = RC_MAP_PIXELVIEW_MK12;
346 rc_proto = RC_PROTO_BIT_NECX;
347 ir->gpio_addr = MO_GP1_IO;
348 ir->mask_keyup = 0x80;
349 ir->polling = 10; /* ms */
350 hardware_mask = 0x3f; /* Hardware returns only 6 bits from command part */
351 break;
352 case CX88_BOARD_PROLINK_PV_8000GT:
353 case CX88_BOARD_PROLINK_PV_GLOBAL_XTREME:
354 ir_codes = RC_MAP_PIXELVIEW_NEW;
355 ir->gpio_addr = MO_GP1_IO;
356 ir->mask_keycode = 0x3f;
357 ir->mask_keyup = 0x80;
358 ir->polling = 1; /* ms */
359 break;
360 case CX88_BOARD_KWORLD_LTV883:
361 ir_codes = RC_MAP_PIXELVIEW;
362 ir->gpio_addr = MO_GP1_IO;
363 ir->mask_keycode = 0x1f;
364 ir->mask_keyup = 0x60;
365 ir->polling = 1; /* ms */
366 break;
367 case CX88_BOARD_ADSTECH_DVB_T_PCI:
368 ir_codes = RC_MAP_ADSTECH_DVB_T_PCI;
369 ir->gpio_addr = MO_GP1_IO;
370 ir->mask_keycode = 0xbf;
371 ir->mask_keyup = 0x40;
372 ir->polling = 50; /* ms */
373 break;
374 case CX88_BOARD_MSI_TVANYWHERE_MASTER:
375 ir_codes = RC_MAP_MSI_TVANYWHERE;
376 ir->gpio_addr = MO_GP1_IO;
377 ir->mask_keycode = 0x1f;
378 ir->mask_keyup = 0x40;
379 ir->polling = 1; /* ms */
380 break;
381 case CX88_BOARD_AVERTV_303:
382 case CX88_BOARD_AVERTV_STUDIO_303:
383 ir_codes = RC_MAP_AVERTV_303;
384 ir->gpio_addr = MO_GP2_IO;
385 ir->mask_keycode = 0xfb;
386 ir->mask_keydown = 0x02;
387 ir->polling = 50; /* ms */
388 break;
389 case CX88_BOARD_OMICOM_SS4_PCI:
390 case CX88_BOARD_SATTRADE_ST4200:
391 case CX88_BOARD_TBS_8920:
392 case CX88_BOARD_TBS_8910:
393 case CX88_BOARD_PROF_7300:
394 case CX88_BOARD_PROF_7301:
395 case CX88_BOARD_PROF_6200:
396 ir_codes = RC_MAP_TBS_NEC;
397 ir->sampling = 0xff00; /* address */
398 break;
399 case CX88_BOARD_TEVII_S464:
400 case CX88_BOARD_TEVII_S460:
401 case CX88_BOARD_TEVII_S420:
402 ir_codes = RC_MAP_TEVII_NEC;
403 ir->sampling = 0xff00; /* address */
404 break;
405 case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
406 ir_codes = RC_MAP_DNTV_LIVE_DVBT_PRO;
407 ir->sampling = 0xff00; /* address */
408 break;
409 case CX88_BOARD_NORWOOD_MICRO:
410 ir_codes = RC_MAP_NORWOOD;
411 ir->gpio_addr = MO_GP1_IO;
412 ir->mask_keycode = 0x0e;
413 ir->mask_keyup = 0x80;
414 ir->polling = 50; /* ms */
415 break;
416 case CX88_BOARD_NPGTECH_REALTV_TOP10FM:
417 ir_codes = RC_MAP_NPGTECH;
418 ir->gpio_addr = MO_GP0_IO;
419 ir->mask_keycode = 0xfa;
420 ir->polling = 50; /* ms */
421 break;
422 case CX88_BOARD_PINNACLE_PCTV_HD_800i:
423 ir_codes = RC_MAP_PINNACLE_PCTV_HD;
424 ir->sampling = 1;
425 break;
426 case CX88_BOARD_POWERCOLOR_REAL_ANGEL:
427 ir_codes = RC_MAP_POWERCOLOR_REAL_ANGEL;
428 ir->gpio_addr = MO_GP2_IO;
429 ir->mask_keycode = 0x7e;
430 ir->polling = 100; /* ms */
431 break;
432 case CX88_BOARD_TWINHAN_VP1027_DVBS:
433 ir_codes = RC_MAP_TWINHAN_VP1027_DVBS;
434 ir->sampling = 0xff00; /* address */
435 break;
438 if (!ir_codes) {
439 err = -ENODEV;
440 goto err_out_free;
444 * The usage of mask_keycode were very convenient, due to several
445 * reasons. Among others, the scancode tables were using the scancode
446 * as the index elements. So, the less bits it was used, the smaller
447 * the table were stored. After the input changes, the better is to use
448 * the full scancodes, since it allows replacing the IR remote by
449 * another one. Unfortunately, there are still some hardware, like
450 * Pixelview Ultra Pro, where only part of the scancode is sent via
451 * GPIO. So, there's no way to get the full scancode. Due to that,
452 * hardware_mask were introduced here: it represents those hardware
453 * that has such limits.
455 if (hardware_mask && !ir->mask_keycode)
456 ir->mask_keycode = hardware_mask;
458 /* init input device */
459 snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)", core->board.name);
460 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(pci));
462 dev->device_name = ir->name;
463 dev->input_phys = ir->phys;
464 dev->input_id.bustype = BUS_PCI;
465 dev->input_id.version = 1;
466 if (pci->subsystem_vendor) {
467 dev->input_id.vendor = pci->subsystem_vendor;
468 dev->input_id.product = pci->subsystem_device;
469 } else {
470 dev->input_id.vendor = pci->vendor;
471 dev->input_id.product = pci->device;
473 dev->dev.parent = &pci->dev;
474 dev->map_name = ir_codes;
475 dev->driver_name = MODULE_NAME;
476 dev->priv = core;
477 dev->open = cx88_ir_open;
478 dev->close = cx88_ir_close;
479 dev->scancode_mask = hardware_mask;
481 if (ir->sampling) {
482 dev->timeout = MS_TO_US(10); /* 10 ms */
483 } else {
484 dev->driver_type = RC_DRIVER_SCANCODE;
485 dev->allowed_protocols = rc_proto;
488 ir->core = core;
489 core->ir = ir;
491 /* all done */
492 err = rc_register_device(dev);
493 if (err)
494 goto err_out_free;
496 return 0;
498 err_out_free:
499 rc_free_device(dev);
500 core->ir = NULL;
501 kfree(ir);
502 return err;
505 int cx88_ir_fini(struct cx88_core *core)
507 struct cx88_IR *ir = core->ir;
509 /* skip detach on non attached boards */
510 if (!ir)
511 return 0;
513 cx88_ir_stop(core);
514 rc_unregister_device(ir->dev);
515 kfree(ir);
517 /* done */
518 core->ir = NULL;
519 return 0;
522 /* ---------------------------------------------------------------------- */
524 void cx88_ir_irq(struct cx88_core *core)
526 struct cx88_IR *ir = core->ir;
527 u32 samples;
528 unsigned int todo, bits;
529 struct ir_raw_event ev = {};
531 if (!ir || !ir->sampling)
532 return;
535 * Samples are stored in a 32 bit register, oldest sample in
536 * the msb. A set bit represents space and an unset bit
537 * represents a pulse.
539 samples = cx_read(MO_SAMPLE_IO);
541 if (samples == 0xff && ir->dev->idle)
542 return;
544 for (todo = 32; todo > 0; todo -= bits) {
545 ev.pulse = samples & 0x80000000 ? false : true;
546 bits = min(todo, 32U - fls(ev.pulse ? samples : ~samples));
547 ev.duration = (bits * (USEC_PER_SEC / 1000)) / ir_samplerate;
548 ir_raw_event_store_with_filter(ir->dev, &ev);
549 samples <<= bits;
551 ir_raw_event_handle(ir->dev);
554 static int get_key_pvr2000(struct IR_i2c *ir, enum rc_proto *protocol,
555 u32 *scancode, u8 *toggle)
557 int flags, code;
559 /* poll IR chip */
560 flags = i2c_smbus_read_byte_data(ir->c, 0x10);
561 if (flags < 0) {
562 dprintk("read error\n");
563 return 0;
565 /* key pressed ? */
566 if (0 == (flags & 0x80))
567 return 0;
569 /* read actual key code */
570 code = i2c_smbus_read_byte_data(ir->c, 0x00);
571 if (code < 0) {
572 dprintk("read error\n");
573 return 0;
576 dprintk("IR Key/Flags: (0x%02x/0x%02x)\n",
577 code & 0xff, flags & 0xff);
579 *protocol = RC_PROTO_UNKNOWN;
580 *scancode = code & 0xff;
581 *toggle = 0;
582 return 1;
585 void cx88_i2c_init_ir(struct cx88_core *core)
587 struct i2c_board_info info;
588 static const unsigned short default_addr_list[] = {
589 0x18, 0x6b, 0x71,
590 I2C_CLIENT_END
592 static const unsigned short pvr2000_addr_list[] = {
593 0x18, 0x1a,
594 I2C_CLIENT_END
596 const unsigned short *addr_list = default_addr_list;
597 const unsigned short *addrp;
598 /* Instantiate the IR receiver device, if present */
599 if (core->i2c_rc != 0)
600 return;
602 memset(&info, 0, sizeof(struct i2c_board_info));
603 strscpy(info.type, "ir_video", I2C_NAME_SIZE);
605 switch (core->boardnr) {
606 case CX88_BOARD_LEADTEK_PVR2000:
607 addr_list = pvr2000_addr_list;
608 core->init_data.name = "cx88 Leadtek PVR 2000 remote";
609 core->init_data.type = RC_PROTO_BIT_UNKNOWN;
610 core->init_data.get_key = get_key_pvr2000;
611 core->init_data.ir_codes = RC_MAP_EMPTY;
612 break;
616 * We can't call i2c_new_scanned_device() because it uses
617 * quick writes for probing and at least some RC receiver
618 * devices only reply to reads.
619 * Also, Hauppauge XVR needs to be specified, as address 0x71
620 * conflicts with another remote type used with saa7134
622 for (addrp = addr_list; *addrp != I2C_CLIENT_END; addrp++) {
623 info.platform_data = NULL;
624 memset(&core->init_data, 0, sizeof(core->init_data));
626 if (*addrp == 0x71) {
627 /* Hauppauge Z8F0811 */
628 strscpy(info.type, "ir_z8f0811_haup", I2C_NAME_SIZE);
629 core->init_data.name = core->board.name;
630 core->init_data.ir_codes = RC_MAP_HAUPPAUGE;
631 core->init_data.type = RC_PROTO_BIT_RC5 |
632 RC_PROTO_BIT_RC6_MCE | RC_PROTO_BIT_RC6_6A_32;
633 core->init_data.internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
635 info.platform_data = &core->init_data;
637 if (i2c_smbus_xfer(&core->i2c_adap, *addrp, 0,
638 I2C_SMBUS_READ, 0,
639 I2C_SMBUS_QUICK, NULL) >= 0) {
640 info.addr = *addrp;
641 i2c_new_client_device(&core->i2c_adap, &info);
642 break;
647 /* ---------------------------------------------------------------------- */
649 MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
650 MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
651 MODULE_LICENSE("GPL");