]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/media/video/saa7134/saa7134-input.c
V4L/DVB (9771): tuner-xc2028: fix a small warning
[mirror_ubuntu-bionic-kernel.git] / drivers / media / video / saa7134 / saa7134-input.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 *
3 * handle saa7134 IR remotes via linux kernel input layer.
4 *
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.
9 *
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.
14 *
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
18 *
19 */
20
21#include <linux/module.h>
1da177e4
LT
22#include <linux/init.h>
23#include <linux/delay.h>
1da177e4
LT
24#include <linux/interrupt.h>
25#include <linux/input.h>
26
27#include "saa7134-reg.h"
28#include "saa7134.h"
29
ff699e6b 30static unsigned int disable_ir;
1da177e4
LT
31module_param(disable_ir, int, 0444);
32MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
33
ff699e6b 34static unsigned int ir_debug;
1da177e4
LT
35module_param(ir_debug, int, 0644);
36MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
37
ff699e6b 38static int pinnacle_remote;
b93eedb6
SP
39module_param(pinnacle_remote, int, 0644); /* Choose Pinnacle PCTV remote */
40MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
41
c408a6f6 42static int ir_rc5_remote_gap = 885;
9160723e 43module_param(ir_rc5_remote_gap, int, 0644);
c408a6f6 44static int ir_rc5_key_timeout = 115;
9160723e
HP
45module_param(ir_rc5_key_timeout, int, 0644);
46
0938e319
P
47static int repeat_delay = 500;
48module_param(repeat_delay, int, 0644);
49MODULE_PARM_DESC(repeat_delay, "delay before key repeat started");
50static int repeat_period = 33;
51module_param(repeat_period, int, 0644);
ac9bb7f5 52MODULE_PARM_DESC(repeat_period, "repeat period between "
0938e319
P
53 "keypresses when key is down");
54
e8018c9e
AMT
55static unsigned int disable_other_ir;
56module_param(disable_other_ir, int, 0644);
57MODULE_PARM_DESC(disable_other_ir, "disable full codes of "
58 "alternative remotes from other manufacturers");
59
1da177e4
LT
60#define dprintk(fmt, arg...) if (ir_debug) \
61 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
ac9cd976
RC
62#define i2cdprintk(fmt, arg...) if (ir_debug) \
63 printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg)
1da177e4 64
622ecb30 65/* Helper functions for RC5 and NEC decoding at GPIO16 or GPIO18 */
9160723e 66static int saa7134_rc5_irq(struct saa7134_dev *dev);
622ecb30
MCC
67static int saa7134_nec_irq(struct saa7134_dev *dev);
68static void nec_task(unsigned long data);
69static void saa7134_nec_timer(unsigned long data);
9160723e 70
ac9cd976 71/* -------------------- GPIO generic keycode builder -------------------- */
1da177e4
LT
72
73static int build_key(struct saa7134_dev *dev)
74{
9160723e 75 struct card_ir *ir = dev->remote;
1da177e4
LT
76 u32 gpio, data;
77
0938e319
P
78 /* here comes the additional handshake steps for some cards */
79 switch (dev->board) {
80 case SAA7134_BOARD_GOTVIEW_7135:
81 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
82 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
83 break;
84 }
1da177e4
LT
85 /* rising SAA7134_GPIO_GPRESCAN reads the status */
86 saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
87 saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
88
89 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
4ac97914
MCC
90 if (ir->polling) {
91 if (ir->last_gpio == gpio)
92 return 0;
93 ir->last_gpio = gpio;
94 }
1da177e4 95
4ac97914 96 data = ir_extract_bits(gpio, ir->mask_keycode);
1da177e4
LT
97 dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
98 gpio, ir->mask_keycode, data);
99
0602fbb2
PM
100 if (ir->polling) {
101 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
102 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
103 ir_input_keydown(ir->dev, &ir->ir, data, data);
104 } else {
105 ir_input_nokey(ir->dev, &ir->ir);
106 }
1da177e4 107 }
0602fbb2
PM
108 else { /* IRQ driven mode - handle key press and release in one go */
109 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
110 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
111 ir_input_keydown(ir->dev, &ir->ir, data, data);
112 ir_input_nokey(ir->dev, &ir->ir);
113 }
114 }
115
1da177e4
LT
116 return 0;
117}
118
ac9cd976
RC
119/* --------------------- Chip specific I2C key builders ----------------- */
120
ba340b40
BR
121static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir, u32 *ir_key,
122 u32 *ir_raw)
123{
124 unsigned char b;
125 int gpio;
126
127 /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
128 struct saa7134_dev *dev = ir->c.adapter->algo_data;
129 if (dev == NULL) {
130 dprintk("get_key_msi_tvanywhere_plus: "
131 "gir->c.adapter->algo_data is NULL!\n");
132 return -EIO;
133 }
134
135 /* rising SAA7134_GPIO_GPRESCAN reads the status */
136
137 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
138 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
139
140 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
141
142 /* GPIO&0x40 is pulsed low when a button is pressed. Don't do
143 I2C receive if gpio&0x40 is not low. */
144
145 if (gpio & 0x40)
146 return 0; /* No button press */
147
148 /* GPIO says there is a button press. Get it. */
149
150 if (1 != i2c_master_recv(&ir->c, &b, 1)) {
151 i2cdprintk("read error\n");
152 return -EIO;
153 }
154
155 /* No button press */
156
157 if (b == 0xff)
158 return 0;
159
160 /* Button pressed */
161
162 dprintk("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
163 *ir_key = b;
164 *ir_raw = b;
165 return 1;
166}
167
ac9cd976
RC
168static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
169{
170 unsigned char b;
171
172 /* poll IR chip */
173 if (1 != i2c_master_recv(&ir->c,&b,1)) {
174 i2cdprintk("read error\n");
175 return -EIO;
176 }
177
178 /* no button press */
179 if (b==0)
180 return 0;
181
182 /* repeating */
183 if (b & 0x80)
184 return 1;
185
186 *ir_key = b;
187 *ir_raw = b;
188 return 1;
189}
190
177aaaf8
TG
191static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
192{
193 unsigned char buf[5], cod4, code3, code4;
194
195 /* poll IR chip */
196 if (5 != i2c_master_recv(&ir->c,buf,5))
197 return -EIO;
198
199 cod4 = buf[4];
200 code4 = (cod4 >> 2);
201 code3 = buf[3];
202 if (code3 == 0)
203 /* no key pressed */
204 return 0;
205
206 /* return key */
207 *ir_key = code4;
208 *ir_raw = code4;
209 return 1;
210}
211
e8018c9e
AMT
212
213static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
214{
215 unsigned char data[12];
216 u32 gpio;
217
218 struct saa7134_dev *dev = ir->c.adapter->algo_data;
219
220 /* rising SAA7134_GPIO_GPRESCAN reads the status */
221 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
222 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
223
224 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
225
616f8878 226 if (0x400000 & ~gpio)
e8018c9e
AMT
227 return 0; /* No button press */
228
229 ir->c.addr = 0x5a >> 1;
230
231 if (12 != i2c_master_recv(&ir->c, data, 12)) {
232 i2cdprintk("read error\n");
233 return -EIO;
234 }
235 /* IR of this card normally decode signals NEC-standard from
236 * - Sven IHOO MT 5.1R remote. xxyye718
237 * - Sven DVD HD-10xx remote. xxyyf708
238 * - BBK ...
239 * - mayby others
240 * So, skip not our, if disable full codes mode.
241 */
242 if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
243 return 0;
244
245 *ir_key = data[9];
246 *ir_raw = data[9];
247
248 return 1;
249}
250
1c22dad8
MCC
251/* Common (grey or coloured) pinnacle PCTV remote handling
252 *
253 */
254static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
255 int parity_offset, int marker, int code_modulo)
256{
257 unsigned char b[4];
258 unsigned int start = 0,parity = 0,code = 0;
259
260 /* poll IR chip */
261 if (4 != i2c_master_recv(&ir->c, b, 4)) {
262 i2cdprintk("read error\n");
263 return -EIO;
264 }
265
266 for (start = 0; start < ARRAY_SIZE(b); start++) {
267 if (b[start] == marker) {
268 code=b[(start+parity_offset + 1) % 4];
269 parity=b[(start+parity_offset) % 4];
270 }
271 }
272
273 /* Empty Request */
274 if (parity == 0)
275 return 0;
276
277 /* Repeating... */
278 if (ir->old == parity)
279 return 0;
280
281 ir->old = parity;
282
283 /* drop special codes when a key is held down a long time for the grey controller
284 In this case, the second bit of the code is asserted */
285 if (marker == 0xfe && (code & 0x40))
286 return 0;
287
288 code %= code_modulo;
289
290 *ir_raw = code;
291 *ir_key = code;
292
293 i2cdprintk("Pinnacle PCTV key %02x\n", code);
294
295 return 1;
296}
297
298/* The grey pinnacle PCTV remote
299 *
300 * There are one issue with this remote:
301 * - I2c packet does not change when the same key is pressed quickly. The workaround
302 * is to hold down each key for about half a second, so that another code is generated
303 * in the i2c packet, and the function can distinguish key presses.
304 *
305 * Sylvain Pasche <sylvain.pasche@gmail.com>
306 */
307static int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
308{
309
310 return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff);
311}
312
313
314/* The new pinnacle PCTV remote (with the colored buttons)
315 *
316 * Ricardo Cerqueira <v4l@cerqueira.org>
317 */
318static int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
319{
320 /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
321 *
322 * this is the only value that results in 42 unique
323 * codes < 128
324 */
325
326 return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88);
327}
328
1da177e4
LT
329void saa7134_input_irq(struct saa7134_dev *dev)
330{
9160723e 331 struct card_ir *ir = dev->remote;
1da177e4 332
622ecb30
MCC
333 if (ir->nec_gpio) {
334 saa7134_nec_irq(dev);
335 } else if (!ir->polling && !ir->rc5_gpio) {
1da177e4 336 build_key(dev);
9160723e
HP
337 } else if (ir->rc5_gpio) {
338 saa7134_rc5_irq(dev);
339 }
1da177e4
LT
340}
341
342static void saa7134_input_timer(unsigned long data)
343{
b4ba7884 344 struct saa7134_dev *dev = (struct saa7134_dev *)data;
9160723e 345 struct card_ir *ir = dev->remote;
1da177e4
LT
346
347 build_key(dev);
b4ba7884 348 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
1da177e4
LT
349}
350
c458473e 351void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
b07b4783
DT
352{
353 if (ir->polling) {
b4ba7884
DT
354 setup_timer(&ir->timer, saa7134_input_timer,
355 (unsigned long)dev);
b07b4783
DT
356 ir->timer.expires = jiffies + HZ;
357 add_timer(&ir->timer);
9160723e
HP
358 } else if (ir->rc5_gpio) {
359 /* set timer_end for code completion */
360 init_timer(&ir->timer_end);
361 ir->timer_end.function = ir_rc5_timer_end;
362 ir->timer_end.data = (unsigned long)ir;
363 init_timer(&ir->timer_keyup);
364 ir->timer_keyup.function = ir_rc5_timer_keyup;
365 ir->timer_keyup.data = (unsigned long)ir;
366 ir->shift_by = 2;
367 ir->start = 0x2;
368 ir->addr = 0x17;
369 ir->rc5_key_timeout = ir_rc5_key_timeout;
370 ir->rc5_remote_gap = ir_rc5_remote_gap;
622ecb30
MCC
371 } else if (ir->nec_gpio) {
372 setup_timer(&ir->timer_keyup, saa7134_nec_timer,
373 (unsigned long)dev);
374 tasklet_init(&ir->tlet, nec_task, (unsigned long)dev);
b07b4783
DT
375 }
376}
377
c458473e 378void saa7134_ir_stop(struct saa7134_dev *dev)
b07b4783
DT
379{
380 if (dev->remote->polling)
381 del_timer_sync(&dev->remote->timer);
382}
383
1da177e4
LT
384int saa7134_input_init1(struct saa7134_dev *dev)
385{
9160723e 386 struct card_ir *ir;
b7df3910 387 struct input_dev *input_dev;
1da177e4
LT
388 IR_KEYTAB_TYPE *ir_codes = NULL;
389 u32 mask_keycode = 0;
390 u32 mask_keydown = 0;
391 u32 mask_keyup = 0;
392 int polling = 0;
9160723e 393 int rc5_gpio = 0;
622ecb30 394 int nec_gpio = 0;
1da177e4 395 int ir_type = IR_TYPE_OTHER;
b07b4783 396 int err;
1da177e4 397
cb2444df 398 if (dev->has_remote != SAA7134_REMOTE_GPIO)
1da177e4
LT
399 return -ENODEV;
400 if (disable_ir)
401 return -ENODEV;
402
403 /* detect & configure */
404 switch (dev->board) {
405 case SAA7134_BOARD_FLYVIDEO2000:
406 case SAA7134_BOARD_FLYVIDEO3000:
4ac97914 407 case SAA7134_BOARD_FLYTVPLATINUM_FM:
6af90ab5 408 case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
4c0f631e 409 ir_codes = ir_codes_flyvideo;
1da177e4
LT
410 mask_keycode = 0xEC00000;
411 mask_keydown = 0x0040000;
412 break;
413 case SAA7134_BOARD_CINERGY400:
414 case SAA7134_BOARD_CINERGY600:
415 case SAA7134_BOARD_CINERGY600_MK3:
4c0f631e 416 ir_codes = ir_codes_cinergy;
1da177e4
LT
417 mask_keycode = 0x00003f;
418 mask_keyup = 0x040000;
419 break;
420 case SAA7134_BOARD_ECS_TVP3XP:
421 case SAA7134_BOARD_ECS_TVP3XP_4CB5:
4c0f631e 422 ir_codes = ir_codes_eztv;
330a115a
MCC
423 mask_keycode = 0x00017c;
424 mask_keyup = 0x000002;
1da177e4 425 polling = 50; // ms
330a115a
MCC
426 break;
427 case SAA7134_BOARD_KWORLD_XPERT:
1da177e4 428 case SAA7134_BOARD_AVACSSMARTTV:
b639f9d2 429 ir_codes = ir_codes_pixelview;
1da177e4
LT
430 mask_keycode = 0x00001F;
431 mask_keyup = 0x000020;
432 polling = 50; // ms
433 break;
434 case SAA7134_BOARD_MD2819:
ac19ecc6 435 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
1da177e4
LT
436 case SAA7134_BOARD_AVERMEDIA_305:
437 case SAA7134_BOARD_AVERMEDIA_307:
ac19ecc6
MCC
438 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
439 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
3ac706d2 440 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
ac19ecc6 441 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
d2761f22 442 case SAA7134_BOARD_AVERMEDIA_M102:
b639f9d2 443 ir_codes = ir_codes_avermedia;
1da177e4
LT
444 mask_keycode = 0x0007C8;
445 mask_keydown = 0x000010;
446 polling = 50; // ms
447 /* Set GPIO pin2 to high to enable the IR controller */
448 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
449 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
450 break;
36f6bb97
MCC
451 case SAA7134_BOARD_AVERMEDIA_M135A:
452 ir_codes = ir_codes_avermedia_m135a;
453 mask_keydown = 0x0040000;
454 mask_keycode = 0x00013f;
455 nec_gpio = 1;
456 break;
450efcfd 457 case SAA7134_BOARD_AVERMEDIA_777:
29e0f1a1 458 case SAA7134_BOARD_AVERMEDIA_A16AR:
450efcfd 459 ir_codes = ir_codes_avermedia;
460 mask_keycode = 0x02F200;
461 mask_keydown = 0x000400;
462 polling = 50; // ms
463 /* Without this we won't receive key up events */
464 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
465 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
4dd7406e 466 break;
6e501a3f
TF
467 case SAA7134_BOARD_AVERMEDIA_A16D:
468 ir_codes = ir_codes_avermedia_a16d;
469 mask_keycode = 0x02F200;
470 mask_keydown = 0x000400;
471 polling = 50; /* ms */
472 /* Without this we won't receive key up events */
473 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
474 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
475 break;
4ac97914 476 case SAA7134_BOARD_KWORLD_TERMINATOR:
b639f9d2 477 ir_codes = ir_codes_pixelview;
dc2286cf
JW
478 mask_keycode = 0x00001f;
479 mask_keyup = 0x000060;
480 polling = 50; // ms
481 break;
ac19ecc6
MCC
482 case SAA7134_BOARD_MANLI_MTV001:
483 case SAA7134_BOARD_MANLI_MTV002:
b34dddbe
DB
484 ir_codes = ir_codes_manli;
485 mask_keycode = 0x001f00;
486 mask_keyup = 0x004000;
487 polling = 50; /* ms */
488 break;
a8ff417e 489 case SAA7134_BOARD_BEHOLD_409FM:
e8018c9e
AMT
490 case SAA7134_BOARD_BEHOLD_401:
491 case SAA7134_BOARD_BEHOLD_403:
492 case SAA7134_BOARD_BEHOLD_403FM:
493 case SAA7134_BOARD_BEHOLD_405:
494 case SAA7134_BOARD_BEHOLD_405FM:
495 case SAA7134_BOARD_BEHOLD_407:
496 case SAA7134_BOARD_BEHOLD_407FM:
497 case SAA7134_BOARD_BEHOLD_409:
498 case SAA7134_BOARD_BEHOLD_505FM:
499 case SAA7134_BOARD_BEHOLD_507_9FM:
4c0f631e 500 ir_codes = ir_codes_manli;
b34dddbe
DB
501 mask_keycode = 0x003f00;
502 mask_keyup = 0x004000;
503 polling = 50; /* ms */
504 break;
505 case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
506 ir_codes = ir_codes_behold_columbus;
507 mask_keycode = 0x003f00;
ac19ecc6 508 mask_keyup = 0x004000;
ac19ecc6
MCC
509 polling = 50; // ms
510 break;
17ce1ff9 511 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
4c0f631e 512 ir_codes = ir_codes_pctv_sedna;
c3d93192
PM
513 mask_keycode = 0x001f00;
514 mask_keyup = 0x004000;
515 polling = 50; // ms
516 break;
4ac97914 517 case SAA7134_BOARD_GOTVIEW_7135:
4c0f631e 518 ir_codes = ir_codes_gotview7135;
0938e319 519 mask_keycode = 0x0003CC;
6b961440 520 mask_keydown = 0x000010;
0938e319
P
521 polling = 5; /* ms */
522 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
6b961440 523 break;
1da177e4 524 case SAA7134_BOARD_VIDEOMATE_TV_PVR:
2a9a9a84 525 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
330a115a 526 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
4c0f631e 527 ir_codes = ir_codes_videomate_tv_pvr;
1da177e4
LT
528 mask_keycode = 0x00003F;
529 mask_keyup = 0x400000;
530 polling = 50; // ms
531 break;
b04c1baf
MM
532 case SAA7134_BOARD_PROTEUS_2309:
533 ir_codes = ir_codes_proteus_2309;
534 mask_keycode = 0x00007F;
535 mask_keyup = 0x000080;
536 polling = 50; // ms
537 break;
4ac97914
MCC
538 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
539 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
4c0f631e 540 ir_codes = ir_codes_videomate_tv_pvr;
fea095fe
NS
541 mask_keycode = 0x003F00;
542 mask_keyup = 0x040000;
543 break;
f5c965ab 544 case SAA7134_BOARD_FLYDVBS_LR300:
3d8466ec 545 case SAA7134_BOARD_FLYDVBT_LR301:
a8029170 546 case SAA7134_BOARD_FLYDVBTDUO:
3d8466ec
GG
547 ir_codes = ir_codes_flydvb;
548 mask_keycode = 0x0001F00;
549 mask_keydown = 0x0040000;
550 break;
9160723e 551 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
904ab884 552 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
0b17d0ed 553 case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
9160723e
HP
554 ir_codes = ir_codes_asus_pc39;
555 mask_keydown = 0x0040000;
556 rc5_gpio = 1;
557 break;
c36c459a
JPS
558 case SAA7134_BOARD_ENCORE_ENLTV:
559 case SAA7134_BOARD_ENCORE_ENLTV_FM:
560 ir_codes = ir_codes_encore_enltv;
561 mask_keycode = 0x00007f;
562 mask_keyup = 0x040000;
563 polling = 50; // ms
564 break;
bf1ece6a
MCC
565 case SAA7134_BOARD_ENCORE_ENLTV_FM53:
566 ir_codes = ir_codes_encore_enltv_fm53;
567 mask_keydown = 0x0040000;
568 mask_keycode = 0x00007f;
569 nec_gpio = 1;
570 break;
480f75ac
TW
571 case SAA7134_BOARD_10MOONSTVMASTER3:
572 ir_codes = ir_codes_encore_enltv;
573 mask_keycode = 0x5f80000;
574 mask_keyup = 0x8000000;
575 polling = 50; //ms
576 break;
f0ba356c
AP
577 case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
578 ir_codes = ir_codes_genius_tvgo_a11mce;
579 mask_keycode = 0xff;
580 mask_keydown = 0xf00000;
581 polling = 50; /* ms */
582 break;
9b000191
MCC
583 case SAA7134_BOARD_REAL_ANGEL_220:
584 ir_codes = ir_codes_real_audio_220_32_keys;
585 mask_keycode = 0x3f00;
586 mask_keyup = 0x4000;
587 polling = 50; /* ms */
588 break;
1da177e4
LT
589 }
590 if (NULL == ir_codes) {
591 printk("%s: Oops: IR config error [card=%d]\n",
592 dev->name, dev->board);
593 return -ENODEV;
594 }
595
b7df3910
DT
596 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
597 input_dev = input_allocate_device();
598 if (!ir || !input_dev) {
b07b4783
DT
599 err = -ENOMEM;
600 goto err_out_free;
b7df3910 601 }
1da177e4 602
d271d1c2
DT
603 ir->dev = input_dev;
604
1da177e4
LT
605 /* init hardware-specific stuff */
606 ir->mask_keycode = mask_keycode;
607 ir->mask_keydown = mask_keydown;
608 ir->mask_keyup = mask_keyup;
4ac97914 609 ir->polling = polling;
9160723e 610 ir->rc5_gpio = rc5_gpio;
622ecb30 611 ir->nec_gpio = nec_gpio;
1da177e4
LT
612
613 /* init input device */
614 snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
615 saa7134_boards[dev->board].name);
616 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
617 pci_name(dev->pci));
618
b7df3910
DT
619 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
620 input_dev->name = ir->name;
621 input_dev->phys = ir->phys;
622 input_dev->id.bustype = BUS_PCI;
623 input_dev->id.version = 1;
1da177e4 624 if (dev->pci->subsystem_vendor) {
b7df3910
DT
625 input_dev->id.vendor = dev->pci->subsystem_vendor;
626 input_dev->id.product = dev->pci->subsystem_device;
1da177e4 627 } else {
b7df3910
DT
628 input_dev->id.vendor = dev->pci->vendor;
629 input_dev->id.product = dev->pci->device;
1da177e4 630 }
2c8a3a33 631 input_dev->dev.parent = &dev->pci->dev;
1da177e4 632
1da177e4 633 dev->remote = ir;
b07b4783
DT
634 saa7134_ir_start(dev, ir);
635
636 err = input_register_device(ir->dev);
637 if (err)
638 goto err_out_stop;
1da177e4 639
0938e319
P
640 /* the remote isn't as bouncy as a keyboard */
641 ir->dev->rep[REP_DELAY] = repeat_delay;
642 ir->dev->rep[REP_PERIOD] = repeat_period;
643
1da177e4 644 return 0;
b07b4783
DT
645
646 err_out_stop:
647 saa7134_ir_stop(dev);
648 dev->remote = NULL;
649 err_out_free:
650 input_free_device(input_dev);
651 kfree(ir);
652 return err;
1da177e4
LT
653}
654
655void saa7134_input_fini(struct saa7134_dev *dev)
656{
657 if (NULL == dev->remote)
658 return;
659
b07b4783 660 saa7134_ir_stop(dev);
b7df3910 661 input_unregister_device(dev->remote->dev);
1da177e4
LT
662 kfree(dev->remote);
663 dev->remote = NULL;
664}
665
ac9cd976
RC
666void saa7134_set_i2c_ir(struct saa7134_dev *dev, struct IR_i2c *ir)
667{
668 if (disable_ir) {
cb2444df 669 dprintk("Found supported i2c remote, but IR has been disabled\n");
ac9cd976
RC
670 ir->get_key=NULL;
671 return;
672 }
673
674 switch (dev->board) {
675 case SAA7134_BOARD_PINNACLE_PCTV_110i:
eb591af3 676 case SAA7134_BOARD_PINNACLE_PCTV_310i:
ac9cd976 677 snprintf(ir->c.name, sizeof(ir->c.name), "Pinnacle PCTV");
b93eedb6
SP
678 if (pinnacle_remote == 0) {
679 ir->get_key = get_key_pinnacle_color;
680 ir->ir_codes = ir_codes_pinnacle_color;
681 } else {
682 ir->get_key = get_key_pinnacle_grey;
683 ir->ir_codes = ir_codes_pinnacle_grey;
684 }
ac9cd976
RC
685 break;
686 case SAA7134_BOARD_UPMOST_PURPLE_TV:
687 snprintf(ir->c.name, sizeof(ir->c.name), "Purple TV");
688 ir->get_key = get_key_purpletv;
689 ir->ir_codes = ir_codes_purpletv;
690 break;
ba340b40
BR
691 case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
692 snprintf(ir->c.name, sizeof(ir->c.name), "MSI TV@nywhere Plus");
693 ir->get_key = get_key_msi_tvanywhere_plus;
694 ir->ir_codes = ir_codes_msi_tvanywhere_plus;
695 break;
177aaaf8
TG
696 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
697 snprintf(ir->c.name, sizeof(ir->c.name), "HVR 1110");
698 ir->get_key = get_key_hvr1110;
699 ir->ir_codes = ir_codes_hauppauge_new;
700 break;
e8018c9e
AMT
701 case SAA7134_BOARD_BEHOLD_607_9FM:
702 case SAA7134_BOARD_BEHOLD_M6:
878cf2a5
DB
703 case SAA7134_BOARD_BEHOLD_M63:
704 case SAA7134_BOARD_BEHOLD_M6_EXTRA:
02505271 705 case SAA7134_BOARD_BEHOLD_H6:
e8018c9e
AMT
706 snprintf(ir->c.name, sizeof(ir->c.name), "BeholdTV");
707 ir->get_key = get_key_beholdm6xx;
708 ir->ir_codes = ir_codes_behold;
709 break;
ac9cd976
RC
710 default:
711 dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board);
712 break;
713 }
714
715}
9160723e
HP
716
717static int saa7134_rc5_irq(struct saa7134_dev *dev)
718{
719 struct card_ir *ir = dev->remote;
720 struct timeval tv;
721 u32 gap;
722 unsigned long current_jiffies, timeout;
723
724 /* get time of bit */
725 current_jiffies = jiffies;
726 do_gettimeofday(&tv);
727
728 /* avoid overflow with gap >1s */
729 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
730 gap = 200000;
731 } else {
732 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
733 tv.tv_usec - ir->base_time.tv_usec;
734 }
735
736 /* active code => add bit */
737 if (ir->active) {
738 /* only if in the code (otherwise spurious IRQ or timer
739 late) */
740 if (ir->last_bit < 28) {
741 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
742 ir_rc5_remote_gap;
743 ir->code |= 1 << ir->last_bit;
744 }
745 /* starting new code */
746 } else {
747 ir->active = 1;
748 ir->code = 0;
749 ir->base_time = tv;
750 ir->last_bit = 0;
751
752 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
753 mod_timer(&ir->timer_end, timeout);
754 }
755
756 return 1;
757}
758
622ecb30
MCC
759
760/* On NEC protocol, One has 2.25 ms, and zero has 1.125 ms
761 The first pulse (start) has 9 + 4.5 ms
1da177e4 762 */
622ecb30
MCC
763
764static void saa7134_nec_timer(unsigned long data)
765{
766 struct saa7134_dev *dev = (struct saa7134_dev *) data;
767 struct card_ir *ir = dev->remote;
768
769 dprintk("Cancel key repeat\n");
770
771 ir_input_nokey(ir->dev, &ir->ir);
772}
773
774static void nec_task(unsigned long data)
775{
776 struct saa7134_dev *dev = (struct saa7134_dev *) data;
777 struct card_ir *ir;
778 struct timeval tv;
779 int count, pulse, oldpulse, gap;
780 u32 ircode = 0, not_code = 0;
781 int ngap = 0;
782
783 if (!data) {
784 printk(KERN_ERR "saa713x/ir: Can't recover dev struct\n");
785 /* GPIO will be kept disabled */
786 return;
787 }
788
789 ir = dev->remote;
790
791 /* rising SAA7134_GPIO_GPRESCAN reads the status */
792 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
793 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
794
795 oldpulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
796 pulse = oldpulse;
797
798 do_gettimeofday(&tv);
799 ir->base_time = tv;
800
801 /* Decode NEC pulsecode. This code can take up to 76.5 ms to run.
802 Unfortunately, using IRQ to decode pulse didn't work, since it uses
803 a pulse train of 38KHz. This means one pulse on each 52 us
804 */
805 do {
806 /* Wait until the end of pulse/space or 5 ms */
807 for (count = 0; count < 500; count++) {
808 udelay(10);
809 /* rising SAA7134_GPIO_GPRESCAN reads the status */
810 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
811 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
812 pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)
813 & ir->mask_keydown;
814 if (pulse != oldpulse)
815 break;
816 }
817
818 do_gettimeofday(&tv);
819 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
820 tv.tv_usec - ir->base_time.tv_usec;
821
822 if (!pulse) {
823 /* Bit 0 has 560 us, while bit 1 has 1120 us.
824 Do something only if bit == 1
825 */
826 if (ngap && (gap > 560 + 280)) {
827 unsigned int shift = ngap - 1;
828
829 /* Address first, then command */
830 if (shift < 8) {
831 shift += 8;
832 ircode |= 1 << shift;
833 } else if (shift < 16) {
834 not_code |= 1 << shift;
835 } else if (shift < 24) {
836 shift -= 16;
837 ircode |= 1 << shift;
838 } else {
839 shift -= 24;
840 not_code |= 1 << shift;
841 }
842 }
843 ngap++;
844 }
845
846
847 ir->base_time = tv;
848
849 /* TIMEOUT - Long pulse */
850 if (gap >= 5000)
851 break;
852 oldpulse = pulse;
853 } while (ngap < 32);
854
855 if (ngap == 32) {
856 /* FIXME: should check if not_code == ~ircode */
857 ir->code = ir_extract_bits(ircode, ir->mask_keycode);
858
859 dprintk("scancode = 0x%02x (code = 0x%02x, notcode= 0x%02x)\n",
860 ir->code, ircode, not_code);
861
862 ir_input_keydown(ir->dev, &ir->ir, ir->code, ir->code);
863 } else
864 dprintk("Repeat last key\n");
865
866 /* Keep repeating the last key */
867 mod_timer(&ir->timer_keyup, jiffies + msecs_to_jiffies(150));
868
869 saa_setl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
870}
871
872static int saa7134_nec_irq(struct saa7134_dev *dev)
873{
874 struct card_ir *ir = dev->remote;
875
876 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
877 tasklet_schedule(&ir->tlet);
878
879 return 1;
880}