Merge git://git.infradead.org/mtd-2.6
[linux-2.6] / drivers / media / video / saa7134 / saa7134-input.c
1 /*
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>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/input.h>
26
27 #include "saa7134-reg.h"
28 #include "saa7134.h"
29
30 static unsigned int disable_ir = 0;
31 module_param(disable_ir, int, 0444);
32 MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
33
34 static unsigned int ir_debug = 0;
35 module_param(ir_debug, int, 0644);
36 MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
37
38 static int pinnacle_remote = 0;
39 module_param(pinnacle_remote, int, 0644);    /* Choose Pinnacle PCTV remote */
40 MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
41
42 static int ir_rc5_remote_gap = 885;
43 module_param(ir_rc5_remote_gap, int, 0644);
44 static int ir_rc5_key_timeout = 115;
45 module_param(ir_rc5_key_timeout, int, 0644);
46
47 static int repeat_delay = 500;
48 module_param(repeat_delay, int, 0644);
49 MODULE_PARM_DESC(repeat_delay, "delay before key repeat started");
50 static int repeat_period = 33;
51 module_param(repeat_period, int, 0644);
52 MODULE_PARM_DESC(repeat_period, "repeat period between"
53     "keypresses when key is down");
54
55 #define dprintk(fmt, arg...)    if (ir_debug) \
56         printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
57 #define i2cdprintk(fmt, arg...)    if (ir_debug) \
58         printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg)
59
60 /** rc5 functions */
61 static int saa7134_rc5_irq(struct saa7134_dev *dev);
62
63 /* -------------------- GPIO generic keycode builder -------------------- */
64
65 static int build_key(struct saa7134_dev *dev)
66 {
67         struct card_ir *ir = dev->remote;
68         u32 gpio, data;
69
70         /* here comes the additional handshake steps for some cards */
71         switch (dev->board) {
72         case SAA7134_BOARD_GOTVIEW_7135:
73                 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
74                 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
75                 break;
76         }
77         /* rising SAA7134_GPIO_GPRESCAN reads the status */
78         saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
79         saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
80
81         gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
82         if (ir->polling) {
83                 if (ir->last_gpio == gpio)
84                         return 0;
85                 ir->last_gpio = gpio;
86         }
87
88         data = ir_extract_bits(gpio, ir->mask_keycode);
89         dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
90                 gpio, ir->mask_keycode, data);
91
92         if (ir->polling) {
93                 if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
94                     (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
95                         ir_input_keydown(ir->dev, &ir->ir, data, data);
96                 } else {
97                         ir_input_nokey(ir->dev, &ir->ir);
98                 }
99         }
100         else {  /* IRQ driven mode - handle key press and release in one go */
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                         ir_input_nokey(ir->dev, &ir->ir);
105                 }
106         }
107
108         return 0;
109 }
110
111 /* --------------------- Chip specific I2C key builders ----------------- */
112
113 static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
114 {
115         unsigned char b;
116
117         /* poll IR chip */
118         if (1 != i2c_master_recv(&ir->c,&b,1)) {
119                 i2cdprintk("read error\n");
120                 return -EIO;
121         }
122
123         /* no button press */
124         if (b==0)
125                 return 0;
126
127         /* repeating */
128         if (b & 0x80)
129                 return 1;
130
131         *ir_key = b;
132         *ir_raw = b;
133         return 1;
134 }
135
136 static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
137 {
138         unsigned char buf[5], cod4, code3, code4;
139
140         /* poll IR chip */
141         if (5 != i2c_master_recv(&ir->c,buf,5))
142                 return -EIO;
143
144         cod4    = buf[4];
145         code4   = (cod4 >> 2);
146         code3   = buf[3];
147         if (code3 == 0)
148                 /* no key pressed */
149                 return 0;
150
151         /* return key */
152         *ir_key = code4;
153         *ir_raw = code4;
154         return 1;
155 }
156
157 void saa7134_input_irq(struct saa7134_dev *dev)
158 {
159         struct card_ir *ir = dev->remote;
160
161         if (!ir->polling && !ir->rc5_gpio) {
162                 build_key(dev);
163         } else if (ir->rc5_gpio) {
164                 saa7134_rc5_irq(dev);
165         }
166 }
167
168 static void saa7134_input_timer(unsigned long data)
169 {
170         struct saa7134_dev *dev = (struct saa7134_dev *)data;
171         struct card_ir *ir = dev->remote;
172
173         build_key(dev);
174         mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
175 }
176
177 void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
178 {
179         if (ir->polling) {
180                 setup_timer(&ir->timer, saa7134_input_timer,
181                             (unsigned long)dev);
182                 ir->timer.expires  = jiffies + HZ;
183                 add_timer(&ir->timer);
184         } else if (ir->rc5_gpio) {
185                 /* set timer_end for code completion */
186                 init_timer(&ir->timer_end);
187                 ir->timer_end.function = ir_rc5_timer_end;
188                 ir->timer_end.data = (unsigned long)ir;
189                 init_timer(&ir->timer_keyup);
190                 ir->timer_keyup.function = ir_rc5_timer_keyup;
191                 ir->timer_keyup.data = (unsigned long)ir;
192                 ir->shift_by = 2;
193                 ir->start = 0x2;
194                 ir->addr = 0x17;
195                 ir->rc5_key_timeout = ir_rc5_key_timeout;
196                 ir->rc5_remote_gap = ir_rc5_remote_gap;
197         }
198 }
199
200 void saa7134_ir_stop(struct saa7134_dev *dev)
201 {
202         if (dev->remote->polling)
203                 del_timer_sync(&dev->remote->timer);
204 }
205
206 int saa7134_input_init1(struct saa7134_dev *dev)
207 {
208         struct card_ir *ir;
209         struct input_dev *input_dev;
210         IR_KEYTAB_TYPE *ir_codes = NULL;
211         u32 mask_keycode = 0;
212         u32 mask_keydown = 0;
213         u32 mask_keyup   = 0;
214         int polling      = 0;
215         int rc5_gpio     = 0;
216         int ir_type      = IR_TYPE_OTHER;
217         int err;
218
219         if (dev->has_remote != SAA7134_REMOTE_GPIO)
220                 return -ENODEV;
221         if (disable_ir)
222                 return -ENODEV;
223
224         /* detect & configure */
225         switch (dev->board) {
226         case SAA7134_BOARD_FLYVIDEO2000:
227         case SAA7134_BOARD_FLYVIDEO3000:
228         case SAA7134_BOARD_FLYTVPLATINUM_FM:
229         case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
230                 ir_codes     = ir_codes_flyvideo;
231                 mask_keycode = 0xEC00000;
232                 mask_keydown = 0x0040000;
233                 break;
234         case SAA7134_BOARD_CINERGY400:
235         case SAA7134_BOARD_CINERGY600:
236         case SAA7134_BOARD_CINERGY600_MK3:
237                 ir_codes     = ir_codes_cinergy;
238                 mask_keycode = 0x00003f;
239                 mask_keyup   = 0x040000;
240                 break;
241         case SAA7134_BOARD_ECS_TVP3XP:
242         case SAA7134_BOARD_ECS_TVP3XP_4CB5:
243                 ir_codes     = ir_codes_eztv;
244                 mask_keycode = 0x00017c;
245                 mask_keyup   = 0x000002;
246                 polling      = 50; // ms
247                 break;
248         case SAA7134_BOARD_KWORLD_XPERT:
249         case SAA7134_BOARD_AVACSSMARTTV:
250                 ir_codes     = ir_codes_pixelview;
251                 mask_keycode = 0x00001F;
252                 mask_keyup   = 0x000020;
253                 polling      = 50; // ms
254                 break;
255         case SAA7134_BOARD_MD2819:
256         case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
257         case SAA7134_BOARD_AVERMEDIA_305:
258         case SAA7134_BOARD_AVERMEDIA_307:
259         case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
260         case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
261         case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
262         case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
263                 ir_codes     = ir_codes_avermedia;
264                 mask_keycode = 0x0007C8;
265                 mask_keydown = 0x000010;
266                 polling      = 50; // ms
267                 /* Set GPIO pin2 to high to enable the IR controller */
268                 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
269                 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
270                 break;
271         case SAA7134_BOARD_AVERMEDIA_777:
272         case SAA7134_BOARD_AVERMEDIA_A16AR:
273                 ir_codes     = ir_codes_avermedia;
274                 mask_keycode = 0x02F200;
275                 mask_keydown = 0x000400;
276                 polling      = 50; // ms
277                 /* Without this we won't receive key up events */
278                 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
279                 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
280                 break;
281         case SAA7134_BOARD_KWORLD_TERMINATOR:
282                 ir_codes     = ir_codes_pixelview;
283                 mask_keycode = 0x00001f;
284                 mask_keyup   = 0x000060;
285                 polling      = 50; // ms
286                 break;
287         case SAA7134_BOARD_MANLI_MTV001:
288         case SAA7134_BOARD_MANLI_MTV002:
289         case SAA7134_BOARD_BEHOLD_409FM:
290                 ir_codes     = ir_codes_manli;
291                 mask_keycode = 0x001f00;
292                 mask_keyup   = 0x004000;
293                 polling      = 50; // ms
294                 break;
295         case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
296                 ir_codes     = ir_codes_pctv_sedna;
297                 mask_keycode = 0x001f00;
298                 mask_keyup   = 0x004000;
299                 polling      = 50; // ms
300                 break;
301         case SAA7134_BOARD_GOTVIEW_7135:
302                 ir_codes     = ir_codes_gotview7135;
303                 mask_keycode = 0x0003CC;
304                 mask_keydown = 0x000010;
305                 polling      = 5; /* ms */
306                 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
307                 break;
308         case SAA7134_BOARD_VIDEOMATE_TV_PVR:
309         case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
310         case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
311                 ir_codes     = ir_codes_videomate_tv_pvr;
312                 mask_keycode = 0x00003F;
313                 mask_keyup   = 0x400000;
314                 polling      = 50; // ms
315                 break;
316         case SAA7134_BOARD_PROTEUS_2309:
317                 ir_codes     = ir_codes_proteus_2309;
318                 mask_keycode = 0x00007F;
319                 mask_keyup   = 0x000080;
320                 polling      = 50; // ms
321                 break;
322         case SAA7134_BOARD_VIDEOMATE_DVBT_300:
323         case SAA7134_BOARD_VIDEOMATE_DVBT_200:
324                 ir_codes     = ir_codes_videomate_tv_pvr;
325                 mask_keycode = 0x003F00;
326                 mask_keyup   = 0x040000;
327                 break;
328         case SAA7134_BOARD_FLYDVBS_LR300:
329         case SAA7134_BOARD_FLYDVBT_LR301:
330         case SAA7134_BOARD_FLYDVBTDUO:
331                 ir_codes     = ir_codes_flydvb;
332                 mask_keycode = 0x0001F00;
333                 mask_keydown = 0x0040000;
334                 break;
335         case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
336         case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
337                 ir_codes     = ir_codes_asus_pc39;
338                 mask_keydown = 0x0040000;
339                 rc5_gpio = 1;
340                 break;
341         case SAA7134_BOARD_ENCORE_ENLTV:
342         case SAA7134_BOARD_ENCORE_ENLTV_FM:
343                 ir_codes     = ir_codes_encore_enltv;
344                 mask_keycode = 0x00007f;
345                 mask_keyup   = 0x040000;
346                 polling      = 50; // ms
347                 break;
348         case SAA7134_BOARD_10MOONSTVMASTER3:
349                 ir_codes     = ir_codes_encore_enltv;
350                 mask_keycode = 0x5f80000;
351                 mask_keyup   = 0x8000000;
352                 polling      = 50; //ms
353                 break;
354         }
355         if (NULL == ir_codes) {
356                 printk("%s: Oops: IR config error [card=%d]\n",
357                        dev->name, dev->board);
358                 return -ENODEV;
359         }
360
361         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
362         input_dev = input_allocate_device();
363         if (!ir || !input_dev) {
364                 err = -ENOMEM;
365                 goto err_out_free;
366         }
367
368         ir->dev = input_dev;
369
370         /* init hardware-specific stuff */
371         ir->mask_keycode = mask_keycode;
372         ir->mask_keydown = mask_keydown;
373         ir->mask_keyup   = mask_keyup;
374         ir->polling      = polling;
375         ir->rc5_gpio     = rc5_gpio;
376
377         /* init input device */
378         snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
379                  saa7134_boards[dev->board].name);
380         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
381                  pci_name(dev->pci));
382
383         ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
384         input_dev->name = ir->name;
385         input_dev->phys = ir->phys;
386         input_dev->id.bustype = BUS_PCI;
387         input_dev->id.version = 1;
388         if (dev->pci->subsystem_vendor) {
389                 input_dev->id.vendor  = dev->pci->subsystem_vendor;
390                 input_dev->id.product = dev->pci->subsystem_device;
391         } else {
392                 input_dev->id.vendor  = dev->pci->vendor;
393                 input_dev->id.product = dev->pci->device;
394         }
395         input_dev->dev.parent = &dev->pci->dev;
396
397         dev->remote = ir;
398         saa7134_ir_start(dev, ir);
399
400         err = input_register_device(ir->dev);
401         if (err)
402                 goto err_out_stop;
403
404         /* the remote isn't as bouncy as a keyboard */
405         ir->dev->rep[REP_DELAY] = repeat_delay;
406         ir->dev->rep[REP_PERIOD] = repeat_period;
407
408         return 0;
409
410  err_out_stop:
411         saa7134_ir_stop(dev);
412         dev->remote = NULL;
413  err_out_free:
414         input_free_device(input_dev);
415         kfree(ir);
416         return err;
417 }
418
419 void saa7134_input_fini(struct saa7134_dev *dev)
420 {
421         if (NULL == dev->remote)
422                 return;
423
424         saa7134_ir_stop(dev);
425         input_unregister_device(dev->remote->dev);
426         kfree(dev->remote);
427         dev->remote = NULL;
428 }
429
430 void saa7134_set_i2c_ir(struct saa7134_dev *dev, struct IR_i2c *ir)
431 {
432         if (disable_ir) {
433                 dprintk("Found supported i2c remote, but IR has been disabled\n");
434                 ir->get_key=NULL;
435                 return;
436         }
437
438         switch (dev->board) {
439         case SAA7134_BOARD_PINNACLE_PCTV_110i:
440         case SAA7134_BOARD_PINNACLE_PCTV_310i:
441                 snprintf(ir->c.name, sizeof(ir->c.name), "Pinnacle PCTV");
442                 if (pinnacle_remote == 0) {
443                         ir->get_key   = get_key_pinnacle_color;
444                         ir->ir_codes = ir_codes_pinnacle_color;
445                 } else {
446                         ir->get_key   = get_key_pinnacle_grey;
447                         ir->ir_codes = ir_codes_pinnacle_grey;
448                 }
449                 break;
450         case SAA7134_BOARD_UPMOST_PURPLE_TV:
451                 snprintf(ir->c.name, sizeof(ir->c.name), "Purple TV");
452                 ir->get_key   = get_key_purpletv;
453                 ir->ir_codes  = ir_codes_purpletv;
454                 break;
455         case SAA7134_BOARD_HAUPPAUGE_HVR1110:
456                 snprintf(ir->c.name, sizeof(ir->c.name), "HVR 1110");
457                 ir->get_key   = get_key_hvr1110;
458                 ir->ir_codes  = ir_codes_hauppauge_new;
459                 break;
460         default:
461                 dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board);
462                 break;
463         }
464
465 }
466
467 static int saa7134_rc5_irq(struct saa7134_dev *dev)
468 {
469         struct card_ir *ir = dev->remote;
470         struct timeval tv;
471         u32 gap;
472         unsigned long current_jiffies, timeout;
473
474         /* get time of bit */
475         current_jiffies = jiffies;
476         do_gettimeofday(&tv);
477
478         /* avoid overflow with gap >1s */
479         if (tv.tv_sec - ir->base_time.tv_sec > 1) {
480                 gap = 200000;
481         } else {
482                 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
483                     tv.tv_usec - ir->base_time.tv_usec;
484         }
485
486         /* active code => add bit */
487         if (ir->active) {
488                 /* only if in the code (otherwise spurious IRQ or timer
489                    late) */
490                 if (ir->last_bit < 28) {
491                         ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
492                             ir_rc5_remote_gap;
493                         ir->code |= 1 << ir->last_bit;
494                 }
495                 /* starting new code */
496         } else {
497                 ir->active = 1;
498                 ir->code = 0;
499                 ir->base_time = tv;
500                 ir->last_bit = 0;
501
502                 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
503                 mod_timer(&ir->timer_end, timeout);
504         }
505
506         return 1;
507 }
508
509 /* ----------------------------------------------------------------------
510  * Local variables:
511  * c-basic-offset: 8
512  * End:
513  */