Merge branch 'hwmon-for-linus' of git://jdelvare.pck.nerim.net/jdelvare-2.6
[pandora-kernel.git] / 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/moduleparam.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/input.h>
27
28 #include "saa7134-reg.h"
29 #include "saa7134.h"
30
31 static unsigned int disable_ir = 0;
32 module_param(disable_ir, int, 0444);
33 MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
34
35 static unsigned int ir_debug = 0;
36 module_param(ir_debug, int, 0644);
37 MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
38
39 static int pinnacle_remote = 0;
40 module_param(pinnacle_remote, int, 0644);    /* Choose Pinnacle PCTV remote */
41 MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
42
43 #define dprintk(fmt, arg...)    if (ir_debug) \
44         printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
45 #define i2cdprintk(fmt, arg...)    if (ir_debug) \
46         printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg)
47
48 /* -------------------- GPIO generic keycode builder -------------------- */
49
50 static int build_key(struct saa7134_dev *dev)
51 {
52         struct saa7134_ir *ir = dev->remote;
53         u32 gpio, data;
54
55         /* rising SAA7134_GPIO_GPRESCAN reads the status */
56         saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
57         saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
58
59         gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
60         if (ir->polling) {
61                 if (ir->last_gpio == gpio)
62                         return 0;
63                 ir->last_gpio = gpio;
64         }
65
66         data = ir_extract_bits(gpio, ir->mask_keycode);
67         dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
68                 gpio, ir->mask_keycode, data);
69
70         if (ir->polling) {
71                 if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
72                     (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
73                         ir_input_keydown(ir->dev, &ir->ir, data, data);
74                 } else {
75                         ir_input_nokey(ir->dev, &ir->ir);
76                 }
77         }
78         else {  /* IRQ driven mode - handle key press and release in one go */
79                 if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
80                     (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
81                         ir_input_keydown(ir->dev, &ir->ir, data, data);
82                         ir_input_nokey(ir->dev, &ir->ir);
83                 }
84         }
85
86         return 0;
87 }
88
89 /* --------------------- Chip specific I2C key builders ----------------- */
90
91 static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
92 {
93         unsigned char b;
94
95         /* poll IR chip */
96         if (1 != i2c_master_recv(&ir->c,&b,1)) {
97                 i2cdprintk("read error\n");
98                 return -EIO;
99         }
100
101         /* no button press */
102         if (b==0)
103                 return 0;
104
105         /* repeating */
106         if (b & 0x80)
107                 return 1;
108
109         *ir_key = b;
110         *ir_raw = b;
111         return 1;
112 }
113
114 static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
115 {
116         unsigned char buf[5], cod4, code3, code4;
117
118         /* poll IR chip */
119         if (5 != i2c_master_recv(&ir->c,buf,5))
120                 return -EIO;
121
122         cod4    = buf[4];
123         code4   = (cod4 >> 2);
124         code3   = buf[3];
125         if (code3 == 0)
126                 /* no key pressed */
127                 return 0;
128
129         /* return key */
130         *ir_key = code4;
131         *ir_raw = code4;
132         return 1;
133 }
134
135 void saa7134_input_irq(struct saa7134_dev *dev)
136 {
137         struct saa7134_ir *ir = dev->remote;
138
139         if (!ir->polling)
140                 build_key(dev);
141 }
142
143 static void saa7134_input_timer(unsigned long data)
144 {
145         struct saa7134_dev *dev = (struct saa7134_dev*)data;
146         struct saa7134_ir *ir = dev->remote;
147         unsigned long timeout;
148
149         build_key(dev);
150         timeout = jiffies + (ir->polling * HZ / 1000);
151         mod_timer(&ir->timer, timeout);
152 }
153
154 static void saa7134_ir_start(struct saa7134_dev *dev, struct saa7134_ir *ir)
155 {
156         if (ir->polling) {
157                 init_timer(&ir->timer);
158                 ir->timer.function = saa7134_input_timer;
159                 ir->timer.data     = (unsigned long)dev;
160                 ir->timer.expires  = jiffies + HZ;
161                 add_timer(&ir->timer);
162         }
163 }
164
165 static void saa7134_ir_stop(struct saa7134_dev *dev)
166 {
167         if (dev->remote->polling)
168                 del_timer_sync(&dev->remote->timer);
169 }
170
171 int saa7134_input_init1(struct saa7134_dev *dev)
172 {
173         struct saa7134_ir *ir;
174         struct input_dev *input_dev;
175         IR_KEYTAB_TYPE *ir_codes = NULL;
176         u32 mask_keycode = 0;
177         u32 mask_keydown = 0;
178         u32 mask_keyup   = 0;
179         int polling      = 0;
180         int ir_type      = IR_TYPE_OTHER;
181         int err;
182
183         if (dev->has_remote != SAA7134_REMOTE_GPIO)
184                 return -ENODEV;
185         if (disable_ir)
186                 return -ENODEV;
187
188         /* detect & configure */
189         switch (dev->board) {
190         case SAA7134_BOARD_FLYVIDEO2000:
191         case SAA7134_BOARD_FLYVIDEO3000:
192         case SAA7134_BOARD_FLYTVPLATINUM_FM:
193         case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
194                 ir_codes     = ir_codes_flyvideo;
195                 mask_keycode = 0xEC00000;
196                 mask_keydown = 0x0040000;
197                 break;
198         case SAA7134_BOARD_CINERGY400:
199         case SAA7134_BOARD_CINERGY600:
200         case SAA7134_BOARD_CINERGY600_MK3:
201                 ir_codes     = ir_codes_cinergy;
202                 mask_keycode = 0x00003f;
203                 mask_keyup   = 0x040000;
204                 break;
205         case SAA7134_BOARD_ECS_TVP3XP:
206         case SAA7134_BOARD_ECS_TVP3XP_4CB5:
207                 ir_codes     = ir_codes_eztv;
208                 mask_keycode = 0x00017c;
209                 mask_keyup   = 0x000002;
210                 polling      = 50; // ms
211                 break;
212         case SAA7134_BOARD_KWORLD_XPERT:
213         case SAA7134_BOARD_AVACSSMARTTV:
214                 ir_codes     = ir_codes_pixelview;
215                 mask_keycode = 0x00001F;
216                 mask_keyup   = 0x000020;
217                 polling      = 50; // ms
218                 break;
219         case SAA7134_BOARD_MD2819:
220         case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
221         case SAA7134_BOARD_AVERMEDIA_305:
222         case SAA7134_BOARD_AVERMEDIA_307:
223         case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
224         case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
225         case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
226         case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
227                 ir_codes     = ir_codes_avermedia;
228                 mask_keycode = 0x0007C8;
229                 mask_keydown = 0x000010;
230                 polling      = 50; // ms
231                 /* Set GPIO pin2 to high to enable the IR controller */
232                 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
233                 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
234                 break;
235         case SAA7134_BOARD_AVERMEDIA_777:
236         case SAA7134_BOARD_AVERMEDIA_A16AR:
237                 ir_codes     = ir_codes_avermedia;
238                 mask_keycode = 0x02F200;
239                 mask_keydown = 0x000400;
240                 polling      = 50; // ms
241                 /* Without this we won't receive key up events */
242                 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
243                 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
244                 break;
245         case SAA7134_BOARD_KWORLD_TERMINATOR:
246                 ir_codes     = ir_codes_pixelview;
247                 mask_keycode = 0x00001f;
248                 mask_keyup   = 0x000060;
249                 polling      = 50; // ms
250                 break;
251         case SAA7134_BOARD_MANLI_MTV001:
252         case SAA7134_BOARD_MANLI_MTV002:
253         case SAA7134_BOARD_BEHOLD_409FM:
254                 ir_codes     = ir_codes_manli;
255                 mask_keycode = 0x001f00;
256                 mask_keyup   = 0x004000;
257                 polling      = 50; // ms
258                 break;
259         case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
260                 ir_codes     = ir_codes_pctv_sedna;
261                 mask_keycode = 0x001f00;
262                 mask_keyup   = 0x004000;
263                 polling      = 50; // ms
264                 break;
265         case SAA7134_BOARD_GOTVIEW_7135:
266                 ir_codes     = ir_codes_gotview7135;
267                 mask_keycode = 0x0003EC;
268                 mask_keyup   = 0x008000;
269                 mask_keydown = 0x000010;
270                 polling      = 50; // ms
271                 break;
272         case SAA7134_BOARD_VIDEOMATE_TV_PVR:
273         case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
274         case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
275                 ir_codes     = ir_codes_videomate_tv_pvr;
276                 mask_keycode = 0x00003F;
277                 mask_keyup   = 0x400000;
278                 polling      = 50; // ms
279                 break;
280         case SAA7134_BOARD_PROTEUS_2309:
281                 ir_codes     = ir_codes_proteus_2309;
282                 mask_keycode = 0x00007F;
283                 mask_keyup   = 0x000080;
284                 polling      = 50; // ms
285                 break;
286         case SAA7134_BOARD_VIDEOMATE_DVBT_300:
287         case SAA7134_BOARD_VIDEOMATE_DVBT_200:
288                 ir_codes     = ir_codes_videomate_tv_pvr;
289                 mask_keycode = 0x003F00;
290                 mask_keyup   = 0x040000;
291                 break;
292         case SAA7134_BOARD_FLYDVBT_LR301:
293         case SAA7134_BOARD_FLYDVBTDUO:
294                 ir_codes     = ir_codes_flydvb;
295                 mask_keycode = 0x0001F00;
296                 mask_keydown = 0x0040000;
297                 break;
298         }
299         if (NULL == ir_codes) {
300                 printk("%s: Oops: IR config error [card=%d]\n",
301                        dev->name, dev->board);
302                 return -ENODEV;
303         }
304
305         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
306         input_dev = input_allocate_device();
307         if (!ir || !input_dev) {
308                 err = -ENOMEM;
309                 goto err_out_free;
310         }
311
312         ir->dev = input_dev;
313
314         /* init hardware-specific stuff */
315         ir->mask_keycode = mask_keycode;
316         ir->mask_keydown = mask_keydown;
317         ir->mask_keyup   = mask_keyup;
318         ir->polling      = polling;
319
320         /* init input device */
321         snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
322                  saa7134_boards[dev->board].name);
323         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
324                  pci_name(dev->pci));
325
326         ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
327         input_dev->name = ir->name;
328         input_dev->phys = ir->phys;
329         input_dev->id.bustype = BUS_PCI;
330         input_dev->id.version = 1;
331         if (dev->pci->subsystem_vendor) {
332                 input_dev->id.vendor  = dev->pci->subsystem_vendor;
333                 input_dev->id.product = dev->pci->subsystem_device;
334         } else {
335                 input_dev->id.vendor  = dev->pci->vendor;
336                 input_dev->id.product = dev->pci->device;
337         }
338         input_dev->cdev.dev = &dev->pci->dev;
339
340         dev->remote = ir;
341         saa7134_ir_start(dev, ir);
342
343         err = input_register_device(ir->dev);
344         if (err)
345                 goto err_out_stop;
346
347         return 0;
348
349  err_out_stop:
350         saa7134_ir_stop(dev);
351         dev->remote = NULL;
352  err_out_free:
353         input_free_device(input_dev);
354         kfree(ir);
355         return err;
356 }
357
358 void saa7134_input_fini(struct saa7134_dev *dev)
359 {
360         if (NULL == dev->remote)
361                 return;
362
363         saa7134_ir_stop(dev);
364         input_unregister_device(dev->remote->dev);
365         kfree(dev->remote);
366         dev->remote = NULL;
367 }
368
369 void saa7134_set_i2c_ir(struct saa7134_dev *dev, struct IR_i2c *ir)
370 {
371         if (disable_ir) {
372                 dprintk("Found supported i2c remote, but IR has been disabled\n");
373                 ir->get_key=NULL;
374                 return;
375         }
376
377         switch (dev->board) {
378         case SAA7134_BOARD_PINNACLE_PCTV_110i:
379         case SAA7134_BOARD_PINNACLE_PCTV_310i:
380                 snprintf(ir->c.name, sizeof(ir->c.name), "Pinnacle PCTV");
381                 if (pinnacle_remote == 0) {
382                         ir->get_key   = get_key_pinnacle_color;
383                         ir->ir_codes = ir_codes_pinnacle_color;
384                 } else {
385                         ir->get_key   = get_key_pinnacle_grey;
386                         ir->ir_codes = ir_codes_pinnacle_grey;
387                 }
388                 break;
389         case SAA7134_BOARD_UPMOST_PURPLE_TV:
390                 snprintf(ir->c.name, sizeof(ir->c.name), "Purple TV");
391                 ir->get_key   = get_key_purpletv;
392                 ir->ir_codes  = ir_codes_purpletv;
393                 break;
394         case SAA7134_BOARD_HAUPPAUGE_HVR1110:
395                 snprintf(ir->c.name, sizeof(ir->c.name), "HVR 1110");
396                 ir->get_key   = get_key_hvr1110;
397                 ir->ir_codes  = ir_codes_hauppauge_new;
398                 break;
399         default:
400                 dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board);
401                 break;
402         }
403
404 }
405 /* ----------------------------------------------------------------------
406  * Local variables:
407  * c-basic-offset: 8
408  * End:
409  */