treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157
[sfrench/cifs-2.6.git] / drivers / media / pci / saa7134 / saa7134-input.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  * handle saa7134 IR remotes via linux kernel input layer.
5  */
6
7 #include "saa7134.h"
8 #include "saa7134-reg.h"
9
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/interrupt.h>
14 #include <linux/slab.h>
15
16 #define MODULE_NAME "saa7134"
17
18 static unsigned int disable_ir;
19 module_param(disable_ir, int, 0444);
20 MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
21
22 static unsigned int ir_debug;
23 module_param(ir_debug, int, 0644);
24 MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
25
26 static int pinnacle_remote;
27 module_param(pinnacle_remote, int, 0644);    /* Choose Pinnacle PCTV remote */
28 MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
29
30 #define input_dbg(fmt, arg...) do { \
31         if (ir_debug) \
32                 printk(KERN_DEBUG pr_fmt("input: " fmt), ## arg); \
33         } while (0)
34 #define ir_dbg(ir, fmt, arg...) do { \
35         if (ir_debug) \
36                 printk(KERN_DEBUG pr_fmt("ir %s: " fmt), ir->rc->device_name, \
37                        ## arg); \
38         } while (0)
39
40 /* Helper function for raw decoding at GPIO16 or GPIO18 */
41 static int saa7134_raw_decode_irq(struct saa7134_dev *dev);
42
43 /* -------------------- GPIO generic keycode builder -------------------- */
44
45 static int build_key(struct saa7134_dev *dev)
46 {
47         struct saa7134_card_ir *ir = dev->remote;
48         u32 gpio, data;
49
50         /* here comes the additional handshake steps for some cards */
51         switch (dev->board) {
52         case SAA7134_BOARD_GOTVIEW_7135:
53                 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
54                 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
55                 break;
56         }
57         /* rising SAA7134_GPIO_GPRESCAN reads the status */
58         saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
59         saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
60
61         gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
62         if (ir->polling) {
63                 if (ir->last_gpio == gpio)
64                         return 0;
65                 ir->last_gpio = gpio;
66         }
67
68         data = ir_extract_bits(gpio, ir->mask_keycode);
69         input_dbg("build_key gpio=0x%x mask=0x%x data=%d\n",
70                 gpio, ir->mask_keycode, data);
71
72         switch (dev->board) {
73         case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
74                 if (data == ir->mask_keycode)
75                         rc_keyup(ir->dev);
76                 else
77                         rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
78                                              0);
79                 return 0;
80         }
81
82         if (ir->polling) {
83                 if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
84                     (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
85                         rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
86                                              0);
87                 } else {
88                         rc_keyup(ir->dev);
89                 }
90         }
91         else {  /* IRQ driven mode - handle key press and release in one go */
92                 if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
93                     (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
94                         rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
95                                              0);
96                         rc_keyup(ir->dev);
97                 }
98         }
99
100         return 0;
101 }
102
103 /* --------------------- Chip specific I2C key builders ----------------- */
104
105 static int get_key_flydvb_trio(struct IR_i2c *ir, enum rc_proto *protocol,
106                                u32 *scancode, u8 *toggle)
107 {
108         int gpio, rc;
109         int attempt = 0;
110         unsigned char b;
111
112         /* We need this to access GPI Used by the saa_readl macro. */
113         struct saa7134_dev *dev = ir->c->adapter->algo_data;
114
115         if (dev == NULL) {
116                 ir_dbg(ir, "get_key_flydvb_trio: ir->c->adapter->algo_data is NULL!\n");
117                 return -EIO;
118         }
119
120         /* rising SAA7134_GPIGPRESCAN reads the status */
121         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
122         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
123
124         gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
125
126         if (0x40000 & ~gpio)
127                 return 0; /* No button press */
128
129         /* poll IR chip */
130         /* weak up the IR chip */
131         b = 0;
132
133         while (1 != i2c_master_send(ir->c, &b, 1)) {
134                 if ((attempt++) < 10) {
135                         /*
136                          * wait a bit for next attempt -
137                          * I don't know how make it better
138                          */
139                         msleep(10);
140                         continue;
141                 }
142                 ir_dbg(ir, "send wake up byte to pic16C505 (IR chip)failed %dx\n",
143                        attempt);
144                 return -EIO;
145         }
146         rc = i2c_master_recv(ir->c, &b, 1);
147         if (rc != 1) {
148                 ir_dbg(ir, "read error\n");
149                 if (rc < 0)
150                         return rc;
151                 return -EIO;
152         }
153
154         *protocol = RC_PROTO_UNKNOWN;
155         *scancode = b;
156         *toggle = 0;
157         return 1;
158 }
159
160 static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir,
161                                        enum rc_proto *protocol,
162                                        u32 *scancode, u8 *toggle)
163 {
164         unsigned char b;
165         int gpio, rc;
166
167         /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
168         struct saa7134_dev *dev = ir->c->adapter->algo_data;
169         if (dev == NULL) {
170                 ir_dbg(ir, "get_key_msi_tvanywhere_plus: ir->c->adapter->algo_data is NULL!\n");
171                 return -EIO;
172         }
173
174         /* rising SAA7134_GPIO_GPRESCAN reads the status */
175
176         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
177         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
178
179         gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
180
181         /* GPIO&0x40 is pulsed low when a button is pressed. Don't do
182            I2C receive if gpio&0x40 is not low. */
183
184         if (gpio & 0x40)
185                 return 0;       /* No button press */
186
187         /* GPIO says there is a button press. Get it. */
188
189         rc = i2c_master_recv(ir->c, &b, 1);
190         if (rc != 1) {
191                 ir_dbg(ir, "read error\n");
192                 if (rc < 0)
193                         return rc;
194                 return -EIO;
195         }
196
197         /* No button press */
198
199         if (b == 0xff)
200                 return 0;
201
202         /* Button pressed */
203
204         input_dbg("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
205         *protocol = RC_PROTO_UNKNOWN;
206         *scancode = b;
207         *toggle = 0;
208         return 1;
209 }
210
211 /* copied and modified from get_key_msi_tvanywhere_plus() */
212 static int get_key_kworld_pc150u(struct IR_i2c *ir, enum rc_proto *protocol,
213                                  u32 *scancode, u8 *toggle)
214 {
215         unsigned char b;
216         unsigned int gpio;
217         int rc;
218
219         /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
220         struct saa7134_dev *dev = ir->c->adapter->algo_data;
221         if (dev == NULL) {
222                 ir_dbg(ir, "get_key_kworld_pc150u: ir->c->adapter->algo_data is NULL!\n");
223                 return -EIO;
224         }
225
226         /* rising SAA7134_GPIO_GPRESCAN reads the status */
227
228         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
229         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
230
231         gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
232
233         /* GPIO&0x100 is pulsed low when a button is pressed. Don't do
234            I2C receive if gpio&0x100 is not low. */
235
236         if (gpio & 0x100)
237                 return 0;       /* No button press */
238
239         /* GPIO says there is a button press. Get it. */
240
241         rc = i2c_master_recv(ir->c, &b, 1);
242         if (rc != 1) {
243                 ir_dbg(ir, "read error\n");
244                 if (rc < 0)
245                         return rc;
246                 return -EIO;
247         }
248
249         /* No button press */
250
251         if (b == 0xff)
252                 return 0;
253
254         /* Button pressed */
255
256         input_dbg("get_key_kworld_pc150u: Key = 0x%02X\n", b);
257         *protocol = RC_PROTO_UNKNOWN;
258         *scancode = b;
259         *toggle = 0;
260         return 1;
261 }
262
263 static int get_key_purpletv(struct IR_i2c *ir, enum rc_proto *protocol,
264                             u32 *scancode, u8 *toggle)
265 {
266         int rc;
267         unsigned char b;
268
269         /* poll IR chip */
270         rc = i2c_master_recv(ir->c, &b, 1);
271         if (rc != 1) {
272                 ir_dbg(ir, "read error\n");
273                 if (rc < 0)
274                         return rc;
275                 return -EIO;
276         }
277
278         /* no button press */
279         if (b==0)
280                 return 0;
281
282         /* repeating */
283         if (b & 0x80)
284                 return 1;
285
286         *protocol = RC_PROTO_UNKNOWN;
287         *scancode = b;
288         *toggle = 0;
289         return 1;
290 }
291
292 static int get_key_beholdm6xx(struct IR_i2c *ir, enum rc_proto *protocol,
293                               u32 *scancode, u8 *toggle)
294 {
295         int rc;
296         unsigned char data[12];
297         u32 gpio;
298
299         struct saa7134_dev *dev = ir->c->adapter->algo_data;
300
301         /* rising SAA7134_GPIO_GPRESCAN reads the status */
302         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
303         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
304
305         gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
306
307         if (0x400000 & ~gpio)
308                 return 0; /* No button press */
309
310         ir->c->addr = 0x5a >> 1;
311
312         rc = i2c_master_recv(ir->c, data, 12);
313         if (rc != 12) {
314                 ir_dbg(ir, "read error\n");
315                 if (rc < 0)
316                         return rc;
317                 return -EIO;
318         }
319
320         if (data[9] != (unsigned char)(~data[8]))
321                 return 0;
322
323         *protocol = RC_PROTO_NECX;
324         *scancode = RC_SCANCODE_NECX(data[11] << 8 | data[10], data[9]);
325         *toggle = 0;
326         return 1;
327 }
328
329 /* Common (grey or coloured) pinnacle PCTV remote handling
330  *
331  */
332 static int get_key_pinnacle(struct IR_i2c *ir, enum rc_proto *protocol,
333                             u32 *scancode, u8 *toggle, int parity_offset,
334                             int marker, int code_modulo)
335 {
336         int rc;
337         unsigned char b[4];
338         unsigned int start = 0,parity = 0,code = 0;
339
340         /* poll IR chip */
341         rc = i2c_master_recv(ir->c, b, 4);
342         if (rc != 4) {
343                 ir_dbg(ir, "read error\n");
344                 if (rc < 0)
345                         return rc;
346                 return -EIO;
347         }
348
349         for (start = 0; start < ARRAY_SIZE(b); start++) {
350                 if (b[start] == marker) {
351                         code=b[(start+parity_offset + 1) % 4];
352                         parity=b[(start+parity_offset) % 4];
353                 }
354         }
355
356         /* Empty Request */
357         if (parity == 0)
358                 return 0;
359
360         /* Repeating... */
361         if (ir->old == parity)
362                 return 0;
363
364         ir->old = parity;
365
366         /* drop special codes when a key is held down a long time for the grey controller
367            In this case, the second bit of the code is asserted */
368         if (marker == 0xfe && (code & 0x40))
369                 return 0;
370
371         code %= code_modulo;
372
373         *protocol = RC_PROTO_UNKNOWN;
374         *scancode = code;
375         *toggle = 0;
376
377         ir_dbg(ir, "Pinnacle PCTV key %02x\n", code);
378         return 1;
379 }
380
381 /* The grey pinnacle PCTV remote
382  *
383  *  There are one issue with this remote:
384  *   - I2c packet does not change when the same key is pressed quickly. The workaround
385  *     is to hold down each key for about half a second, so that another code is generated
386  *     in the i2c packet, and the function can distinguish key presses.
387  *
388  * Sylvain Pasche <sylvain.pasche@gmail.com>
389  */
390 static int get_key_pinnacle_grey(struct IR_i2c *ir, enum rc_proto *protocol,
391                                  u32 *scancode, u8 *toggle)
392 {
393
394         return get_key_pinnacle(ir, protocol, scancode, toggle, 1, 0xfe, 0xff);
395 }
396
397
398 /* The new pinnacle PCTV remote (with the colored buttons)
399  *
400  * Ricardo Cerqueira <v4l@cerqueira.org>
401  */
402 static int get_key_pinnacle_color(struct IR_i2c *ir, enum rc_proto *protocol,
403                                   u32 *scancode, u8 *toggle)
404 {
405         /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
406          *
407          * this is the only value that results in 42 unique
408          * codes < 128
409          */
410
411         return get_key_pinnacle(ir, protocol, scancode, toggle, 2, 0x80, 0x88);
412 }
413
414 void saa7134_input_irq(struct saa7134_dev *dev)
415 {
416         struct saa7134_card_ir *ir;
417
418         if (!dev || !dev->remote)
419                 return;
420
421         ir = dev->remote;
422         if (!ir->running)
423                 return;
424
425         if (!ir->polling && !ir->raw_decode) {
426                 build_key(dev);
427         } else if (ir->raw_decode) {
428                 saa7134_raw_decode_irq(dev);
429         }
430 }
431
432 static void saa7134_input_timer(struct timer_list *t)
433 {
434         struct saa7134_card_ir *ir = from_timer(ir, t, timer);
435         struct saa7134_dev *dev = ir->dev->priv;
436
437         build_key(dev);
438         mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
439 }
440
441 int saa7134_ir_open(struct rc_dev *rc)
442 {
443         struct saa7134_dev *dev = rc->priv;
444         struct saa7134_card_ir *ir = dev->remote;
445
446         /* Moved here from saa7134_input_init1() because the latter
447          * is not called on device resume */
448         switch (dev->board) {
449         case SAA7134_BOARD_MD2819:
450         case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
451         case SAA7134_BOARD_AVERMEDIA_305:
452         case SAA7134_BOARD_AVERMEDIA_307:
453         case SAA7134_BOARD_AVERMEDIA_505:
454         case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
455         case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
456         case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
457         case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
458         case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
459         case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
460         case SAA7134_BOARD_AVERMEDIA_M102:
461         case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
462                 /* Without this we won't receive key up events */
463                 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
464                 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
465                 break;
466         case SAA7134_BOARD_AVERMEDIA_777:
467         case SAA7134_BOARD_AVERMEDIA_A16AR:
468                 /* Without this we won't receive key up events */
469                 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
470                 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
471                 break;
472         case SAA7134_BOARD_AVERMEDIA_A16D:
473                 /* Without this we won't receive key up events */
474                 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
475                 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
476                 break;
477         case SAA7134_BOARD_GOTVIEW_7135:
478                 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
479                 break;
480         }
481
482         ir->running = true;
483
484         if (ir->polling) {
485                 timer_setup(&ir->timer, saa7134_input_timer, 0);
486                 ir->timer.expires = jiffies + HZ;
487                 add_timer(&ir->timer);
488         }
489
490         return 0;
491 }
492
493 void saa7134_ir_close(struct rc_dev *rc)
494 {
495         struct saa7134_dev *dev = rc->priv;
496         struct saa7134_card_ir *ir = dev->remote;
497
498         if (ir->polling)
499                 del_timer_sync(&ir->timer);
500
501         ir->running = false;
502 }
503
504 int saa7134_input_init1(struct saa7134_dev *dev)
505 {
506         struct saa7134_card_ir *ir;
507         struct rc_dev *rc;
508         char *ir_codes = NULL;
509         u32 mask_keycode = 0;
510         u32 mask_keydown = 0;
511         u32 mask_keyup   = 0;
512         unsigned polling = 0;
513         bool raw_decode  = false;
514         int err;
515
516         if (dev->has_remote != SAA7134_REMOTE_GPIO)
517                 return -ENODEV;
518         if (disable_ir)
519                 return -ENODEV;
520
521         /* detect & configure */
522         switch (dev->board) {
523         case SAA7134_BOARD_FLYVIDEO2000:
524         case SAA7134_BOARD_FLYVIDEO3000:
525         case SAA7134_BOARD_FLYTVPLATINUM_FM:
526         case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
527         case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
528                 ir_codes     = RC_MAP_FLYVIDEO;
529                 mask_keycode = 0xEC00000;
530                 mask_keydown = 0x0040000;
531                 break;
532         case SAA7134_BOARD_CINERGY400:
533         case SAA7134_BOARD_CINERGY600:
534         case SAA7134_BOARD_CINERGY600_MK3:
535                 ir_codes     = RC_MAP_CINERGY;
536                 mask_keycode = 0x00003f;
537                 mask_keyup   = 0x040000;
538                 break;
539         case SAA7134_BOARD_ECS_TVP3XP:
540         case SAA7134_BOARD_ECS_TVP3XP_4CB5:
541                 ir_codes     = RC_MAP_EZTV;
542                 mask_keycode = 0x00017c;
543                 mask_keyup   = 0x000002;
544                 polling      = 50; // ms
545                 break;
546         case SAA7134_BOARD_KWORLD_XPERT:
547         case SAA7134_BOARD_AVACSSMARTTV:
548                 ir_codes     = RC_MAP_PIXELVIEW;
549                 mask_keycode = 0x00001F;
550                 mask_keyup   = 0x000020;
551                 polling      = 50; // ms
552                 break;
553         case SAA7134_BOARD_MD2819:
554         case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
555         case SAA7134_BOARD_AVERMEDIA_305:
556         case SAA7134_BOARD_AVERMEDIA_307:
557         case SAA7134_BOARD_AVERMEDIA_505:
558         case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
559         case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
560         case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
561         case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
562         case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
563         case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
564         case SAA7134_BOARD_AVERMEDIA_M102:
565         case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
566                 ir_codes     = RC_MAP_AVERMEDIA;
567                 mask_keycode = 0x0007C8;
568                 mask_keydown = 0x000010;
569                 polling      = 50; // ms
570                 /* GPIO stuff moved to saa7134_ir_open() */
571                 break;
572         case SAA7134_BOARD_AVERMEDIA_M135A:
573                 ir_codes     = RC_MAP_AVERMEDIA_M135A;
574                 mask_keydown = 0x0040000;       /* Enable GPIO18 line on both edges */
575                 mask_keyup   = 0x0040000;
576                 mask_keycode = 0xffff;
577                 raw_decode   = true;
578                 break;
579         case SAA7134_BOARD_AVERMEDIA_M733A:
580                 ir_codes     = RC_MAP_AVERMEDIA_M733A_RM_K6;
581                 mask_keydown = 0x0040000;
582                 mask_keyup   = 0x0040000;
583                 mask_keycode = 0xffff;
584                 raw_decode   = true;
585                 break;
586         case SAA7134_BOARD_AVERMEDIA_777:
587         case SAA7134_BOARD_AVERMEDIA_A16AR:
588                 ir_codes     = RC_MAP_AVERMEDIA;
589                 mask_keycode = 0x02F200;
590                 mask_keydown = 0x000400;
591                 polling      = 50; // ms
592                 /* GPIO stuff moved to saa7134_ir_open() */
593                 break;
594         case SAA7134_BOARD_AVERMEDIA_A16D:
595                 ir_codes     = RC_MAP_AVERMEDIA_A16D;
596                 mask_keycode = 0x02F200;
597                 mask_keydown = 0x000400;
598                 polling      = 50; /* ms */
599                 /* GPIO stuff moved to saa7134_ir_open() */
600                 break;
601         case SAA7134_BOARD_KWORLD_TERMINATOR:
602                 ir_codes     = RC_MAP_PIXELVIEW;
603                 mask_keycode = 0x00001f;
604                 mask_keyup   = 0x000060;
605                 polling      = 50; // ms
606                 break;
607         case SAA7134_BOARD_MANLI_MTV001:
608         case SAA7134_BOARD_MANLI_MTV002:
609                 ir_codes     = RC_MAP_MANLI;
610                 mask_keycode = 0x001f00;
611                 mask_keyup   = 0x004000;
612                 polling      = 50; /* ms */
613                 break;
614         case SAA7134_BOARD_BEHOLD_409FM:
615         case SAA7134_BOARD_BEHOLD_401:
616         case SAA7134_BOARD_BEHOLD_403:
617         case SAA7134_BOARD_BEHOLD_403FM:
618         case SAA7134_BOARD_BEHOLD_405:
619         case SAA7134_BOARD_BEHOLD_405FM:
620         case SAA7134_BOARD_BEHOLD_407:
621         case SAA7134_BOARD_BEHOLD_407FM:
622         case SAA7134_BOARD_BEHOLD_409:
623         case SAA7134_BOARD_BEHOLD_505FM:
624         case SAA7134_BOARD_BEHOLD_505RDS_MK5:
625         case SAA7134_BOARD_BEHOLD_505RDS_MK3:
626         case SAA7134_BOARD_BEHOLD_507_9FM:
627         case SAA7134_BOARD_BEHOLD_507RDS_MK3:
628         case SAA7134_BOARD_BEHOLD_507RDS_MK5:
629                 ir_codes     = RC_MAP_MANLI;
630                 mask_keycode = 0x003f00;
631                 mask_keyup   = 0x004000;
632                 polling      = 50; /* ms */
633                 break;
634         case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
635                 ir_codes     = RC_MAP_BEHOLD_COLUMBUS;
636                 mask_keycode = 0x003f00;
637                 mask_keyup   = 0x004000;
638                 polling      = 50; // ms
639                 break;
640         case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
641                 ir_codes     = RC_MAP_PCTV_SEDNA;
642                 mask_keycode = 0x001f00;
643                 mask_keyup   = 0x004000;
644                 polling      = 50; // ms
645                 break;
646         case SAA7134_BOARD_GOTVIEW_7135:
647                 ir_codes     = RC_MAP_GOTVIEW7135;
648                 mask_keycode = 0x0003CC;
649                 mask_keydown = 0x000010;
650                 polling      = 5; /* ms */
651                 /* GPIO stuff moved to saa7134_ir_open() */
652                 break;
653         case SAA7134_BOARD_VIDEOMATE_TV_PVR:
654         case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
655         case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
656                 ir_codes     = RC_MAP_VIDEOMATE_TV_PVR;
657                 mask_keycode = 0x00003F;
658                 mask_keyup   = 0x400000;
659                 polling      = 50; // ms
660                 break;
661         case SAA7134_BOARD_PROTEUS_2309:
662                 ir_codes     = RC_MAP_PROTEUS_2309;
663                 mask_keycode = 0x00007F;
664                 mask_keyup   = 0x000080;
665                 polling      = 50; // ms
666                 break;
667         case SAA7134_BOARD_VIDEOMATE_DVBT_300:
668         case SAA7134_BOARD_VIDEOMATE_DVBT_200:
669                 ir_codes     = RC_MAP_VIDEOMATE_TV_PVR;
670                 mask_keycode = 0x003F00;
671                 mask_keyup   = 0x040000;
672                 break;
673         case SAA7134_BOARD_FLYDVBS_LR300:
674         case SAA7134_BOARD_FLYDVBT_LR301:
675         case SAA7134_BOARD_FLYDVBTDUO:
676                 ir_codes     = RC_MAP_FLYDVB;
677                 mask_keycode = 0x0001F00;
678                 mask_keydown = 0x0040000;
679                 break;
680         case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
681         case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
682         case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
683                 ir_codes     = RC_MAP_ASUS_PC39;
684                 mask_keydown = 0x0040000;       /* Enable GPIO18 line on both edges */
685                 mask_keyup   = 0x0040000;
686                 mask_keycode = 0xffff;
687                 raw_decode   = true;
688                 break;
689         case SAA7134_BOARD_ASUSTeK_PS3_100:
690                 ir_codes     = RC_MAP_ASUS_PS3_100;
691                 mask_keydown = 0x0040000;
692                 mask_keyup   = 0x0040000;
693                 mask_keycode = 0xffff;
694                 raw_decode   = true;
695                 break;
696         case SAA7134_BOARD_ENCORE_ENLTV:
697         case SAA7134_BOARD_ENCORE_ENLTV_FM:
698                 ir_codes     = RC_MAP_ENCORE_ENLTV;
699                 mask_keycode = 0x00007f;
700                 mask_keyup   = 0x040000;
701                 polling      = 50; // ms
702                 break;
703         case SAA7134_BOARD_ENCORE_ENLTV_FM53:
704         case SAA7134_BOARD_ENCORE_ENLTV_FM3:
705                 ir_codes     = RC_MAP_ENCORE_ENLTV_FM53;
706                 mask_keydown = 0x0040000;       /* Enable GPIO18 line on both edges */
707                 mask_keyup   = 0x0040000;
708                 mask_keycode = 0xffff;
709                 raw_decode   = true;
710                 break;
711         case SAA7134_BOARD_10MOONSTVMASTER3:
712                 ir_codes     = RC_MAP_ENCORE_ENLTV;
713                 mask_keycode = 0x5f80000;
714                 mask_keyup   = 0x8000000;
715                 polling      = 50; //ms
716                 break;
717         case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
718                 ir_codes     = RC_MAP_GENIUS_TVGO_A11MCE;
719                 mask_keycode = 0xff;
720                 mask_keydown = 0xf00000;
721                 polling = 50; /* ms */
722                 break;
723         case SAA7134_BOARD_REAL_ANGEL_220:
724                 ir_codes     = RC_MAP_REAL_AUDIO_220_32_KEYS;
725                 mask_keycode = 0x3f00;
726                 mask_keyup   = 0x4000;
727                 polling = 50; /* ms */
728                 break;
729         case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
730                 ir_codes     = RC_MAP_KWORLD_PLUS_TV_ANALOG;
731                 mask_keycode = 0x7f;
732                 polling = 40; /* ms */
733                 break;
734         case SAA7134_BOARD_VIDEOMATE_S350:
735                 ir_codes     = RC_MAP_VIDEOMATE_S350;
736                 mask_keycode = 0x003f00;
737                 mask_keydown = 0x040000;
738                 break;
739         case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
740                 ir_codes     = RC_MAP_WINFAST;
741                 mask_keycode = 0x5f00;
742                 mask_keyup   = 0x020000;
743                 polling      = 50; /* ms */
744                 break;
745         case SAA7134_BOARD_VIDEOMATE_M1F:
746                 ir_codes     = RC_MAP_VIDEOMATE_K100;
747                 mask_keycode = 0x0ff00;
748                 mask_keyup   = 0x040000;
749                 break;
750         case SAA7134_BOARD_HAUPPAUGE_HVR1150:
751         case SAA7134_BOARD_HAUPPAUGE_HVR1120:
752                 ir_codes     = RC_MAP_HAUPPAUGE;
753                 mask_keydown = 0x0040000;       /* Enable GPIO18 line on both edges */
754                 mask_keyup   = 0x0040000;
755                 mask_keycode = 0xffff;
756                 raw_decode   = true;
757                 break;
758         case SAA7134_BOARD_LEADTEK_WINFAST_TV2100_FM:
759                 ir_codes     = RC_MAP_LEADTEK_Y04G0051;
760                 mask_keydown = 0x0040000;       /* Enable GPIO18 line on both edges */
761                 mask_keyup   = 0x0040000;
762                 mask_keycode = 0xffff;
763                 raw_decode   = true;
764                 break;
765         }
766         if (NULL == ir_codes) {
767                 pr_err("Oops: IR config error [card=%d]\n", dev->board);
768                 return -ENODEV;
769         }
770
771         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
772         rc = rc_allocate_device(RC_DRIVER_SCANCODE);
773         if (!ir || !rc) {
774                 err = -ENOMEM;
775                 goto err_out_free;
776         }
777
778         ir->dev = rc;
779         dev->remote = ir;
780
781         /* init hardware-specific stuff */
782         ir->mask_keycode = mask_keycode;
783         ir->mask_keydown = mask_keydown;
784         ir->mask_keyup   = mask_keyup;
785         ir->polling      = polling;
786         ir->raw_decode   = raw_decode;
787
788         /* init input device */
789         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
790                  pci_name(dev->pci));
791
792         rc->priv = dev;
793         rc->open = saa7134_ir_open;
794         rc->close = saa7134_ir_close;
795         if (raw_decode) {
796                 rc->driver_type = RC_DRIVER_IR_RAW;
797                 rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
798         }
799
800         rc->device_name = saa7134_boards[dev->board].name;
801         rc->input_phys = ir->phys;
802         rc->input_id.bustype = BUS_PCI;
803         rc->input_id.version = 1;
804         if (dev->pci->subsystem_vendor) {
805                 rc->input_id.vendor  = dev->pci->subsystem_vendor;
806                 rc->input_id.product = dev->pci->subsystem_device;
807         } else {
808                 rc->input_id.vendor  = dev->pci->vendor;
809                 rc->input_id.product = dev->pci->device;
810         }
811         rc->dev.parent = &dev->pci->dev;
812         rc->map_name = ir_codes;
813         rc->driver_name = MODULE_NAME;
814         rc->min_timeout = 1;
815         rc->timeout = IR_DEFAULT_TIMEOUT;
816         rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
817
818         err = rc_register_device(rc);
819         if (err)
820                 goto err_out_free;
821
822         return 0;
823
824 err_out_free:
825         rc_free_device(rc);
826         dev->remote = NULL;
827         kfree(ir);
828         return err;
829 }
830
831 void saa7134_input_fini(struct saa7134_dev *dev)
832 {
833         if (NULL == dev->remote)
834                 return;
835
836         rc_unregister_device(dev->remote->dev);
837         kfree(dev->remote);
838         dev->remote = NULL;
839 }
840
841 void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
842 {
843         struct i2c_board_info info;
844         struct i2c_msg msg_msi = {
845                 .addr = 0x50,
846                 .flags = I2C_M_RD,
847                 .len = 0,
848                 .buf = NULL,
849         };
850         int rc;
851
852         if (disable_ir) {
853                 input_dbg("IR has been disabled, not probing for i2c remote\n");
854                 return;
855         }
856
857         memset(&info, 0, sizeof(struct i2c_board_info));
858         memset(&dev->init_data, 0, sizeof(dev->init_data));
859         strscpy(info.type, "ir_video", I2C_NAME_SIZE);
860
861         switch (dev->board) {
862         case SAA7134_BOARD_PINNACLE_PCTV_110i:
863         case SAA7134_BOARD_PINNACLE_PCTV_310i:
864                 dev->init_data.name = "Pinnacle PCTV";
865                 if (pinnacle_remote == 0) {
866                         dev->init_data.get_key = get_key_pinnacle_color;
867                         dev->init_data.ir_codes = RC_MAP_PINNACLE_COLOR;
868                         info.addr = 0x47;
869                 } else {
870                         dev->init_data.get_key = get_key_pinnacle_grey;
871                         dev->init_data.ir_codes = RC_MAP_PINNACLE_GREY;
872                         info.addr = 0x47;
873                 }
874                 break;
875         case SAA7134_BOARD_UPMOST_PURPLE_TV:
876                 dev->init_data.name = "Purple TV";
877                 dev->init_data.get_key = get_key_purpletv;
878                 dev->init_data.ir_codes = RC_MAP_PURPLETV;
879                 info.addr = 0x7a;
880                 break;
881         case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
882                 dev->init_data.name = "MSI TV@nywhere Plus";
883                 dev->init_data.get_key = get_key_msi_tvanywhere_plus;
884                 dev->init_data.ir_codes = RC_MAP_MSI_TVANYWHERE_PLUS;
885                 /*
886                  * MSI TV@nyware Plus requires more frequent polling
887                  * otherwise it will miss some keypresses
888                  */
889                 dev->init_data.polling_interval = 50;
890                 info.addr = 0x30;
891                 /* MSI TV@nywhere Plus controller doesn't seem to
892                    respond to probes unless we read something from
893                    an existing device. Weird...
894                    REVISIT: might no longer be needed */
895                 rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
896                 input_dbg("probe 0x%02x @ %s: %s\n",
897                         msg_msi.addr, dev->i2c_adap.name,
898                         (1 == rc) ? "yes" : "no");
899                 break;
900         case SAA7134_BOARD_SNAZIO_TVPVR_PRO:
901                 dev->init_data.name = "SnaZio* TVPVR PRO";
902                 dev->init_data.get_key = get_key_msi_tvanywhere_plus;
903                 dev->init_data.ir_codes = RC_MAP_MSI_TVANYWHERE_PLUS;
904                 /*
905                  * MSI TV@nyware Plus requires more frequent polling
906                  * otherwise it will miss some keypresses
907                  */
908                 dev->init_data.polling_interval = 50;
909                 info.addr = 0x30;
910                 /*
911                  * MSI TV@nywhere Plus controller doesn't seem to
912                  *  respond to probes unless we read something from
913                  *  an existing device. Weird...
914                  * REVISIT: might no longer be needed
915                  */
916                 rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
917                 input_dbg("probe 0x%02x @ %s: %s\n",
918                         msg_msi.addr, dev->i2c_adap.name,
919                         (rc == 1) ? "yes" : "no");
920                 break;
921         case SAA7134_BOARD_KWORLD_PC150U:
922                 /* copied and modified from MSI TV@nywhere Plus */
923                 dev->init_data.name = "Kworld PC150-U";
924                 dev->init_data.get_key = get_key_kworld_pc150u;
925                 dev->init_data.ir_codes = RC_MAP_KWORLD_PC150U;
926                 info.addr = 0x30;
927                 /* MSI TV@nywhere Plus controller doesn't seem to
928                    respond to probes unless we read something from
929                    an existing device. Weird...
930                    REVISIT: might no longer be needed */
931                 rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
932                 input_dbg("probe 0x%02x @ %s: %s\n",
933                         msg_msi.addr, dev->i2c_adap.name,
934                         (1 == rc) ? "yes" : "no");
935                 break;
936         case SAA7134_BOARD_HAUPPAUGE_HVR1110:
937                 dev->init_data.name = saa7134_boards[dev->board].name;
938                 dev->init_data.ir_codes = RC_MAP_HAUPPAUGE;
939                 dev->init_data.type = RC_PROTO_BIT_RC5 |
940                                 RC_PROTO_BIT_RC6_MCE | RC_PROTO_BIT_RC6_6A_32;
941                 dev->init_data.internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
942                 info.addr = 0x71;
943                 break;
944         case SAA7134_BOARD_BEHOLD_607FM_MK3:
945         case SAA7134_BOARD_BEHOLD_607FM_MK5:
946         case SAA7134_BOARD_BEHOLD_609FM_MK3:
947         case SAA7134_BOARD_BEHOLD_609FM_MK5:
948         case SAA7134_BOARD_BEHOLD_607RDS_MK3:
949         case SAA7134_BOARD_BEHOLD_607RDS_MK5:
950         case SAA7134_BOARD_BEHOLD_609RDS_MK3:
951         case SAA7134_BOARD_BEHOLD_609RDS_MK5:
952         case SAA7134_BOARD_BEHOLD_M6:
953         case SAA7134_BOARD_BEHOLD_M63:
954         case SAA7134_BOARD_BEHOLD_M6_EXTRA:
955         case SAA7134_BOARD_BEHOLD_H6:
956         case SAA7134_BOARD_BEHOLD_X7:
957         case SAA7134_BOARD_BEHOLD_H7:
958         case SAA7134_BOARD_BEHOLD_A7:
959                 dev->init_data.name = "BeholdTV";
960                 dev->init_data.get_key = get_key_beholdm6xx;
961                 dev->init_data.ir_codes = RC_MAP_BEHOLD;
962                 dev->init_data.type = RC_PROTO_BIT_NECX;
963                 info.addr = 0x2d;
964                 break;
965         case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
966         case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
967                 info.addr = 0x40;
968                 break;
969         case SAA7134_BOARD_AVERMEDIA_A706:
970                 info.addr = 0x41;
971                 break;
972         case SAA7134_BOARD_FLYDVB_TRIO:
973                 dev->init_data.name = "FlyDVB Trio";
974                 dev->init_data.get_key = get_key_flydvb_trio;
975                 dev->init_data.ir_codes = RC_MAP_FLYDVB;
976                 info.addr = 0x0b;
977                 break;
978         default:
979                 input_dbg("No I2C IR support for board %x\n", dev->board);
980                 return;
981         }
982
983         if (dev->init_data.name)
984                 info.platform_data = &dev->init_data;
985         i2c_new_device(&dev->i2c_adap, &info);
986 }
987
988 static int saa7134_raw_decode_irq(struct saa7134_dev *dev)
989 {
990         struct saa7134_card_ir *ir = dev->remote;
991         int space;
992
993         /* Generate initial event */
994         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
995         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
996         space = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
997         ir_raw_event_store_edge(dev->remote->dev, !space);
998
999         return 1;
1000 }