[PATCH] v4l: 716: support for em28xx board family
[pandora-kernel.git] / drivers / media / video / ir-kbd-gpio.c
1 /*
2  *
3  * Copyright (c) 2003 Gerd Knorr
4  * Copyright (c) 2003 Pavel Machek
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 #include <linux/pci.h>
28
29 #include <media/ir-common.h>
30
31 #include "bttv.h"
32
33 /* ---------------------------------------------------------------------- */
34
35 static IR_KEYTAB_TYPE ir_codes_avermedia[IR_KEYTAB_SIZE] = {
36         [ 34 ] = KEY_KP0,
37         [ 40 ] = KEY_KP1,
38         [ 24 ] = KEY_KP2,
39         [ 56 ] = KEY_KP3,
40         [ 36 ] = KEY_KP4,
41         [ 20 ] = KEY_KP5,
42         [ 52 ] = KEY_KP6,
43         [ 44 ] = KEY_KP7,
44         [ 28 ] = KEY_KP8,
45         [ 60 ] = KEY_KP9,
46
47         [ 48 ] = KEY_EJECTCD,     // Unmarked on my controller
48         [  0 ] = KEY_POWER,
49         [ 18 ] = BTN_LEFT,        // DISPLAY/L
50         [ 50 ] = BTN_RIGHT,       // LOOP/R
51         [ 10 ] = KEY_MUTE,
52         [ 38 ] = KEY_RECORD,
53         [ 22 ] = KEY_PAUSE,
54         [ 54 ] = KEY_STOP,
55         [ 30 ] = KEY_VOLUMEDOWN,
56         [ 62 ] = KEY_VOLUMEUP,
57
58         [ 32 ] = KEY_TUNER,       // TV/FM
59         [ 16 ] = KEY_CD,
60         [  8 ] = KEY_VIDEO,
61         [  4 ] = KEY_AUDIO,
62         [ 12 ] = KEY_ZOOM,        // full screen
63         [  2 ] = KEY_INFO,        // preview
64         [ 42 ] = KEY_SEARCH,      // autoscan
65         [ 26 ] = KEY_STOP,        // freeze
66         [ 58 ] = KEY_RECORD,      // capture
67         [  6 ] = KEY_PLAY,        // unmarked
68         [ 46 ] = KEY_RED,         // unmarked
69         [ 14 ] = KEY_GREEN,       // unmarked
70
71         [ 33 ] = KEY_YELLOW,      // unmarked
72         [ 17 ] = KEY_CHANNELDOWN,
73         [ 49 ] = KEY_CHANNELUP,
74         [  1 ] = KEY_BLUE,        // unmarked
75 };
76
77 /* Matt Jesson <dvb@jesson.eclipse.co.uk */
78 static IR_KEYTAB_TYPE ir_codes_avermedia_dvbt[IR_KEYTAB_SIZE] = {
79         [ 0x28 ] = KEY_KP0,         //'0' / 'enter'
80         [ 0x22 ] = KEY_KP1,         //'1'
81         [ 0x12 ] = KEY_KP2,         //'2' / 'up arrow'
82         [ 0x32 ] = KEY_KP3,         //'3'
83         [ 0x24 ] = KEY_KP4,         //'4' / 'left arrow'
84         [ 0x14 ] = KEY_KP5,         //'5'
85         [ 0x34 ] = KEY_KP6,         //'6' / 'right arrow'
86         [ 0x26 ] = KEY_KP7,         //'7'
87         [ 0x16 ] = KEY_KP8,         //'8' / 'down arrow'
88         [ 0x36 ] = KEY_KP9,         //'9'
89
90         [ 0x20 ] = KEY_LIST,        // 'source'
91         [ 0x10 ] = KEY_TEXT,        // 'teletext'
92         [ 0x00 ] = KEY_POWER,       // 'power'
93         [ 0x04 ] = KEY_AUDIO,       // 'audio'
94         [ 0x06 ] = KEY_ZOOM,        // 'full screen'
95         [ 0x18 ] = KEY_VIDEO,       // 'display'
96         [ 0x38 ] = KEY_SEARCH,      // 'loop'
97         [ 0x08 ] = KEY_INFO,        // 'preview'
98         [ 0x2a ] = KEY_REWIND,      // 'backward <<'
99         [ 0x1a ] = KEY_FASTFORWARD, // 'forward >>'
100         [ 0x3a ] = KEY_RECORD,      // 'capture'
101         [ 0x0a ] = KEY_MUTE,        // 'mute'
102         [ 0x2c ] = KEY_RECORD,      // 'record'
103         [ 0x1c ] = KEY_PAUSE,       // 'pause'
104         [ 0x3c ] = KEY_STOP,        // 'stop'
105         [ 0x0c ] = KEY_PLAY,        // 'play'
106         [ 0x2e ] = KEY_RED,         // 'red'
107         [ 0x01 ] = KEY_BLUE,        // 'blue' / 'cancel'
108         [ 0x0e ] = KEY_YELLOW,      // 'yellow' / 'ok'
109         [ 0x21 ] = KEY_GREEN,       // 'green'
110         [ 0x11 ] = KEY_CHANNELDOWN, // 'channel -'
111         [ 0x31 ] = KEY_CHANNELUP,   // 'channel +'
112         [ 0x1e ] = KEY_VOLUMEDOWN,  // 'volume -'
113         [ 0x3e ] = KEY_VOLUMEUP,    // 'volume +'
114 };
115
116 /* Attila Kondoros <attila.kondoros@chello.hu> */
117 static IR_KEYTAB_TYPE ir_codes_apac_viewcomp[IR_KEYTAB_SIZE] = {
118
119         [  1 ] = KEY_KP1,
120         [  2 ] = KEY_KP2,
121         [  3 ] = KEY_KP3,
122         [  4 ] = KEY_KP4,
123         [  5 ] = KEY_KP5,
124         [  6 ] = KEY_KP6,
125         [  7 ] = KEY_KP7,
126         [  8 ] = KEY_KP8,
127         [  9 ] = KEY_KP9,
128         [  0 ] = KEY_KP0,
129         [ 23 ] = KEY_LAST,        // +100
130         [ 10 ] = KEY_LIST,        // recall
131
132
133         [ 28 ] = KEY_TUNER,       // TV/FM
134         [ 21 ] = KEY_SEARCH,      // scan
135         [ 18 ] = KEY_POWER,       // power
136         [ 31 ] = KEY_VOLUMEDOWN,  // vol up
137         [ 27 ] = KEY_VOLUMEUP,    // vol down
138         [ 30 ] = KEY_CHANNELDOWN, // chn up
139         [ 26 ] = KEY_CHANNELUP,   // chn down
140
141         [ 17 ] = KEY_VIDEO,       // video
142         [ 15 ] = KEY_ZOOM,        // full screen
143         [ 19 ] = KEY_MUTE,        // mute/unmute
144         [ 16 ] = KEY_TEXT,        // min
145
146         [ 13 ] = KEY_STOP,        // freeze
147         [ 14 ] = KEY_RECORD,      // record
148         [ 29 ] = KEY_PLAYPAUSE,   // stop
149         [ 25 ] = KEY_PLAY,        // play
150
151         [ 22 ] = KEY_GOTO,        // osd
152         [ 20 ] = KEY_REFRESH,     // default
153         [ 12 ] = KEY_KPPLUS,      // fine tune >>>>
154         [ 24 ] = KEY_KPMINUS      // fine tune <<<<
155 };
156
157 /* ---------------------------------------------------------------------- */
158
159 /* Ricardo Cerqueira <v4l@cerqueira.org> */
160 /* Weird matching, since the remote has "uncommon" keys */
161
162 static IR_KEYTAB_TYPE ir_codes_conceptronic[IR_KEYTAB_SIZE] = {
163
164         [ 30 ] = KEY_POWER,       // power
165         [ 7  ] = KEY_MEDIA,       // source
166         [ 28 ] = KEY_SEARCH,      // scan
167
168 /* FIXME: duplicate keycodes?
169  *
170  * These four keys seem to share the same GPIO as CH+, CH-, <<< and >>>
171  * The GPIO values are
172  * 6397fb for both "Scan <" and "CH -",
173  * 639ffb for "Scan >" and "CH+",
174  * 6384fb for "Tune <" and "<<<",
175  * 638cfb for "Tune >" and ">>>", regardless of the mask.
176  *
177  *      [ 23 ] = KEY_BACK,        // fm scan <<
178  *      [ 31 ] = KEY_FORWARD,     // fm scan >>
179  *
180  *      [ 4  ] = KEY_LEFT,        // fm tuning <
181  *      [ 12 ] = KEY_RIGHT,       // fm tuning >
182  *
183  * For now, these four keys are disabled. Pressing them will generate
184  * the CH+/CH-/<<</>>> events
185  */
186
187         [ 3  ] = KEY_TUNER,       // TV/FM
188
189         [ 0  ] = KEY_RECORD,
190         [ 8  ] = KEY_STOP,
191         [ 17 ] = KEY_PLAY,
192
193         [ 26 ] = KEY_PLAYPAUSE,   // freeze
194         [ 25 ] = KEY_ZOOM,        // zoom
195         [ 15 ] = KEY_TEXT,        // min
196
197         [ 1  ] = KEY_KP1,
198         [ 11 ] = KEY_KP2,
199         [ 27 ] = KEY_KP3,
200         [ 5  ] = KEY_KP4,
201         [ 9  ] = KEY_KP5,
202         [ 21 ] = KEY_KP6,
203         [ 6  ] = KEY_KP7,
204         [ 10 ] = KEY_KP8,
205         [ 18 ] = KEY_KP9,
206         [ 2  ] = KEY_KP0,
207         [ 16 ] = KEY_LAST,        // +100
208         [ 19 ] = KEY_LIST,        // recall
209
210         [ 31 ] = KEY_CHANNELUP,   // chn down
211         [ 23 ] = KEY_CHANNELDOWN, // chn up
212         [ 22 ] = KEY_VOLUMEUP,    // vol down
213         [ 20 ] = KEY_VOLUMEDOWN,  // vol up
214
215         [ 4  ] = KEY_KPMINUS,     // <<<
216         [ 14 ] = KEY_SETUP,       // function
217         [ 12 ] = KEY_KPPLUS,      // >>>
218
219         [ 13 ] = KEY_GOTO,        // mts
220         [ 29 ] = KEY_REFRESH,     // reset
221         [ 24 ] = KEY_MUTE         // mute/unmute
222 };
223
224 struct IR {
225         struct bttv_sub_device  *sub;
226         struct input_dev        *input;
227         struct ir_input_state   ir;
228         char                    name[32];
229         char                    phys[32];
230         u32                     mask_keycode;
231         u32                     mask_keydown;
232         u32                     mask_keyup;
233
234         int                     polling;
235         u32                     last_gpio;
236         struct work_struct      work;
237         struct timer_list       timer;
238 };
239
240 static int debug;
241 module_param(debug, int, 0644);    /* debug level (0,1,2) */
242
243 #define DEVNAME "ir-kbd-gpio"
244 #define dprintk(fmt, arg...)    if (debug) \
245         printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
246
247 static void ir_irq(struct bttv_sub_device *sub);
248 static int ir_probe(struct device *dev);
249 static int ir_remove(struct device *dev);
250
251 static struct bttv_sub_driver driver = {
252         .drv = {
253                 .name   = DEVNAME,
254                 .probe  = ir_probe,
255                 .remove = ir_remove,
256         },
257         .gpio_irq       = ir_irq,
258 };
259
260 /* ---------------------------------------------------------------------- */
261
262 static void ir_handle_key(struct IR *ir)
263 {
264         u32 gpio,data;
265
266         /* read gpio value */
267         gpio = bttv_gpio_read(ir->sub->core);
268         if (ir->polling) {
269                 if (ir->last_gpio == gpio)
270                         return;
271                 ir->last_gpio = gpio;
272         }
273
274         /* extract data */
275         data = ir_extract_bits(gpio, ir->mask_keycode);
276         dprintk(DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
277                 gpio, data,
278                 ir->polling               ? "poll"  : "irq",
279                 (gpio & ir->mask_keydown) ? " down" : "",
280                 (gpio & ir->mask_keyup)   ? " up"   : "");
281
282         if (ir->mask_keydown) {
283                 /* bit set on keydown */
284                 if (gpio & ir->mask_keydown) {
285                         ir_input_keydown(ir->input, &ir->ir, data, data);
286                 } else {
287                         ir_input_nokey(ir->input, &ir->ir);
288                 }
289
290         } else if (ir->mask_keyup) {
291                 /* bit cleared on keydown */
292                 if (0 == (gpio & ir->mask_keyup)) {
293                         ir_input_keydown(ir->input, &ir->ir, data, data);
294                 } else {
295                         ir_input_nokey(ir->input, &ir->ir);
296                 }
297
298         } else {
299                 /* can't disturgissh keydown/up :-/ */
300                 ir_input_keydown(ir->input, &ir->ir, data, data);
301                 ir_input_nokey(ir->input, &ir->ir);
302         }
303 }
304
305 static void ir_irq(struct bttv_sub_device *sub)
306 {
307         struct IR *ir = dev_get_drvdata(&sub->dev);
308
309         if (!ir->polling)
310                 ir_handle_key(ir);
311 }
312
313 static void ir_timer(unsigned long data)
314 {
315         struct IR *ir = (struct IR*)data;
316
317         schedule_work(&ir->work);
318 }
319
320 static void ir_work(void *data)
321 {
322         struct IR *ir = data;
323         unsigned long timeout;
324
325         ir_handle_key(ir);
326         timeout = jiffies + (ir->polling * HZ / 1000);
327         mod_timer(&ir->timer, timeout);
328 }
329
330 /* ---------------------------------------------------------------------- */
331
332 static int ir_probe(struct device *dev)
333 {
334         struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
335         struct IR *ir;
336         struct input_dev *input_dev;
337         IR_KEYTAB_TYPE *ir_codes = NULL;
338         int ir_type = IR_TYPE_OTHER;
339
340         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
341         input_dev = input_allocate_device();
342         if (!ir || !input_dev) {
343                 kfree(ir);
344                 input_free_device(input_dev);
345                 return -ENOMEM;
346         }
347
348         /* detect & configure */
349         switch (sub->core->type) {
350         case BTTV_BOARD_AVERMEDIA:
351         case BTTV_BOARD_AVPHONE98:
352         case BTTV_BOARD_AVERMEDIA98:
353                 ir_codes         = ir_codes_avermedia;
354                 ir->mask_keycode = 0xf88000;
355                 ir->mask_keydown = 0x010000;
356                 ir->polling      = 50; // ms
357                 break;
358
359         case BTTV_BOARD_AVDVBT_761:
360         case BTTV_BOARD_AVDVBT_771:
361                 ir_codes         = ir_codes_avermedia_dvbt;
362                 ir->mask_keycode = 0x0f00c0;
363                 ir->mask_keydown = 0x000020;
364                 ir->polling      = 50; // ms
365                 break;
366
367         case BTTV_BOARD_PXELVWPLTVPAK:
368                 ir_codes         = ir_codes_pixelview;
369                 ir->mask_keycode = 0x003e00;
370                 ir->mask_keyup   = 0x010000;
371                 ir->polling      = 50; // ms
372                 break;
373         case BTTV_BOARD_PV_BT878P_9B:
374         case BTTV_BOARD_PV_BT878P_PLUS:
375                 ir_codes         = ir_codes_pixelview;
376                 ir->mask_keycode = 0x001f00;
377                 ir->mask_keyup   = 0x008000;
378                 ir->polling      = 50; // ms
379                 break;
380
381         case BTTV_BOARD_WINFAST2000:
382                 ir_codes         = ir_codes_winfast;
383                 ir->mask_keycode = 0x1f8;
384                 break;
385         case BTTV_BOARD_MAGICTVIEW061:
386         case BTTV_BOARD_MAGICTVIEW063:
387                 ir_codes         = ir_codes_winfast;
388                 ir->mask_keycode = 0x0008e000;
389                 ir->mask_keydown = 0x00200000;
390                 break;
391         case BTTV_BOARD_APAC_VIEWCOMP:
392                 ir_codes         = ir_codes_apac_viewcomp;
393                 ir->mask_keycode = 0x001f00;
394                 ir->mask_keyup   = 0x008000;
395                 ir->polling      = 50; // ms
396                 break;
397         case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
398                 ir_codes         = ir_codes_conceptronic;
399                 ir->mask_keycode = 0x001F00;
400                 ir->mask_keyup   = 0x006000;
401                 ir->polling      = 50; // ms
402                 break;
403         }
404         if (NULL == ir_codes) {
405                 kfree(ir);
406                 input_free_device(input_dev);
407                 return -ENODEV;
408         }
409
410         /* init hardware-specific stuff */
411         bttv_gpio_inout(sub->core, ir->mask_keycode | ir->mask_keydown, 0);
412         ir->sub = sub;
413
414         /* init input device */
415         snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
416                  sub->core->type);
417         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
418                  pci_name(sub->core->pci));
419
420         ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
421         input_dev->name = ir->name;
422         input_dev->phys = ir->phys;
423         input_dev->id.bustype = BUS_PCI;
424         input_dev->id.version = 1;
425         if (sub->core->pci->subsystem_vendor) {
426                 input_dev->id.vendor  = sub->core->pci->subsystem_vendor;
427                 input_dev->id.product = sub->core->pci->subsystem_device;
428         } else {
429                 input_dev->id.vendor  = sub->core->pci->vendor;
430                 input_dev->id.product = sub->core->pci->device;
431         }
432         input_dev->cdev.dev = &sub->core->pci->dev;
433
434         if (ir->polling) {
435                 INIT_WORK(&ir->work, ir_work, ir);
436                 init_timer(&ir->timer);
437                 ir->timer.function = ir_timer;
438                 ir->timer.data     = (unsigned long)ir;
439                 schedule_work(&ir->work);
440         }
441
442         /* all done */
443         dev_set_drvdata(dev, ir);
444         input_register_device(ir->input);
445
446         return 0;
447 }
448
449 static int ir_remove(struct device *dev)
450 {
451         struct IR *ir = dev_get_drvdata(dev);
452
453         if (ir->polling) {
454                 del_timer(&ir->timer);
455                 flush_scheduled_work();
456         }
457
458         input_unregister_device(ir->input);
459         kfree(ir);
460         return 0;
461 }
462
463 /* ---------------------------------------------------------------------- */
464
465 MODULE_AUTHOR("Gerd Knorr, Pavel Machek");
466 MODULE_DESCRIPTION("input driver for bt8x8 gpio IR remote controls");
467 MODULE_LICENSE("GPL");
468
469 static int ir_init(void)
470 {
471         return bttv_sub_register(&driver, "remote");
472 }
473
474 static void ir_fini(void)
475 {
476         bttv_sub_unregister(&driver);
477 }
478
479 module_init(ir_init);
480 module_exit(ir_fini);
481
482
483 /*
484  * Local variables:
485  * c-basic-offset: 8
486  * End:
487  */