Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / input / keyboard / bf54x-keys.c
1 /*
2  * File:         drivers/input/keyboard/bf54x-keys.c
3  * Based on:
4  * Author:       Michael Hennerich <hennerich@blackfin.uclinux.org>
5  *
6  * Created:
7  * Description:  keypad driver for Analog Devices Blackfin BF54x Processors
8  *
9  *
10  * Modified:
11  *               Copyright 2007 Analog Devices Inc.
12  *
13  * Bugs:         Enter bugs at http://blackfin.uclinux.org/
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, see the file COPYING, or write
27  * to the Free Software Foundation, Inc.,
28  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29  */
30
31 #include <linux/module.h>
32 #include <linux/version.h>
33
34 #include <linux/init.h>
35 #include <linux/fs.h>
36 #include <linux/interrupt.h>
37 #include <linux/irq.h>
38 #include <linux/sched.h>
39 #include <linux/pm.h>
40 #include <linux/sysctl.h>
41 #include <linux/proc_fs.h>
42 #include <linux/delay.h>
43 #include <linux/platform_device.h>
44 #include <linux/input.h>
45
46 #include <asm/portmux.h>
47 #include <asm/mach/bf54x_keys.h>
48
49 #define DRV_NAME        "bf54x-keys"
50 #define TIME_SCALE      100     /* 100 ns */
51 #define MAX_MULT        (0xFF * TIME_SCALE)
52 #define MAX_RC          8       /* Max Row/Col */
53
54 static const u16 per_rows[] = {
55         P_KEY_ROW7,
56         P_KEY_ROW6,
57         P_KEY_ROW5,
58         P_KEY_ROW4,
59         P_KEY_ROW3,
60         P_KEY_ROW2,
61         P_KEY_ROW1,
62         P_KEY_ROW0,
63         0
64 };
65
66 static const u16 per_cols[] = {
67         P_KEY_COL7,
68         P_KEY_COL6,
69         P_KEY_COL5,
70         P_KEY_COL4,
71         P_KEY_COL3,
72         P_KEY_COL2,
73         P_KEY_COL1,
74         P_KEY_COL0,
75         0
76 };
77
78 struct bf54x_kpad {
79         struct input_dev *input;
80         int irq;
81         unsigned short lastkey;
82         unsigned short *keycode;
83         struct timer_list timer;
84         unsigned int keyup_test_jiffies;
85 };
86
87 static inline int bfin_kpad_find_key(struct bf54x_kpad *bf54x_kpad,
88                         struct input_dev *input, u16 keyident)
89 {
90         u16 i;
91
92         for (i = 0; i < input->keycodemax; i++)
93                 if (bf54x_kpad->keycode[i + input->keycodemax] == keyident)
94                         return bf54x_kpad->keycode[i];
95         return -1;
96 }
97
98 static inline void bfin_keycodecpy(unsigned short *keycode,
99                         const unsigned int *pdata_kc,
100                         unsigned short keymapsize)
101 {
102         unsigned int i;
103
104         for (i = 0; i < keymapsize; i++) {
105                 keycode[i] = pdata_kc[i] & 0xffff;
106                 keycode[i + keymapsize] = pdata_kc[i] >> 16;
107         }
108 }
109
110 static inline u16 bfin_kpad_get_prescale(u32 timescale)
111 {
112         u32 sclk = get_sclk();
113
114         return ((((sclk / 1000) * timescale) / 1024) - 1);
115 }
116
117 static inline u16 bfin_kpad_get_keypressed(struct bf54x_kpad *bf54x_kpad)
118 {
119         return (bfin_read_KPAD_STAT() & KPAD_PRESSED);
120 }
121
122 static inline void bfin_kpad_clear_irq(void)
123 {
124         bfin_write_KPAD_STAT(0xFFFF);
125         bfin_write_KPAD_ROWCOL(0xFFFF);
126 }
127
128 static void bfin_kpad_timer(unsigned long data)
129 {
130         struct platform_device *pdev =  (struct platform_device *) data;
131         struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
132
133         if (bfin_kpad_get_keypressed(bf54x_kpad)) {
134                 /* Try again later */
135                 mod_timer(&bf54x_kpad->timer,
136                           jiffies + bf54x_kpad->keyup_test_jiffies);
137                 return;
138         }
139
140         input_report_key(bf54x_kpad->input, bf54x_kpad->lastkey, 0);
141         input_sync(bf54x_kpad->input);
142
143         /* Clear IRQ Status */
144
145         bfin_kpad_clear_irq();
146         enable_irq(bf54x_kpad->irq);
147 }
148
149 static irqreturn_t bfin_kpad_isr(int irq, void *dev_id)
150 {
151         struct platform_device *pdev = dev_id;
152         struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
153         struct input_dev *input = bf54x_kpad->input;
154         int key;
155         u16 rowcol = bfin_read_KPAD_ROWCOL();
156
157         key = bfin_kpad_find_key(bf54x_kpad, input, rowcol);
158
159         input_report_key(input, key, 1);
160         input_sync(input);
161
162         if (bfin_kpad_get_keypressed(bf54x_kpad)) {
163                 disable_irq(bf54x_kpad->irq);
164                 bf54x_kpad->lastkey = key;
165                 mod_timer(&bf54x_kpad->timer,
166                           jiffies + bf54x_kpad->keyup_test_jiffies);
167         } else {
168                 input_report_key(input, key, 0);
169                 input_sync(input);
170
171                 bfin_kpad_clear_irq();
172         }
173
174         return IRQ_HANDLED;
175 }
176
177 static int __devinit bfin_kpad_probe(struct platform_device *pdev)
178 {
179         struct bf54x_kpad *bf54x_kpad;
180         struct bfin_kpad_platform_data *pdata = pdev->dev.platform_data;
181         struct input_dev *input;
182         int i, error;
183
184         if (!pdata->rows || !pdata->cols || !pdata->keymap) {
185                 printk(KERN_ERR DRV_NAME
186                         ": No rows, cols or keymap from pdata\n");
187                 return -EINVAL;
188         }
189
190         if (!pdata->keymapsize ||
191             pdata->keymapsize > (pdata->rows * pdata->cols)) {
192                 printk(KERN_ERR DRV_NAME ": Invalid keymapsize\n");
193                 return -EINVAL;
194         }
195
196         bf54x_kpad = kzalloc(sizeof(struct bf54x_kpad), GFP_KERNEL);
197         if (!bf54x_kpad)
198                 return -ENOMEM;
199
200         platform_set_drvdata(pdev, bf54x_kpad);
201
202         /* Allocate memory for keymap followed by private LUT */
203         bf54x_kpad->keycode = kmalloc(pdata->keymapsize *
204                                         sizeof(unsigned short) * 2, GFP_KERNEL);
205         if (!bf54x_kpad->keycode) {
206                 error = -ENOMEM;
207                 goto out;
208         }
209
210         if (!pdata->debounce_time || !pdata->debounce_time > MAX_MULT ||
211             !pdata->coldrive_time || !pdata->coldrive_time > MAX_MULT) {
212                 printk(KERN_ERR DRV_NAME
213                         ": Invalid Debounce/Columdrive Time from pdata\n");
214                 bfin_write_KPAD_MSEL(0xFF0);    /* Default MSEL */
215         } else {
216                 bfin_write_KPAD_MSEL(
217                         ((pdata->debounce_time / TIME_SCALE)
218                                                 & DBON_SCALE) |
219                         (((pdata->coldrive_time / TIME_SCALE) << 8)
220                                                 & COLDRV_SCALE));
221
222         }
223
224         if (!pdata->keyup_test_interval)
225                 bf54x_kpad->keyup_test_jiffies = msecs_to_jiffies(50);
226         else
227                 bf54x_kpad->keyup_test_jiffies =
228                         msecs_to_jiffies(pdata->keyup_test_interval);
229
230         if (peripheral_request_list((u16 *)&per_rows[MAX_RC - pdata->rows],
231                                     DRV_NAME)) {
232                 printk(KERN_ERR DRV_NAME
233                         ": Requesting Peripherals failed\n");
234                 error = -EFAULT;
235                 goto out0;
236         }
237
238         if (peripheral_request_list((u16 *)&per_cols[MAX_RC - pdata->cols],
239                                     DRV_NAME)) {
240                 printk(KERN_ERR DRV_NAME
241                         ": Requesting Peripherals failed\n");
242                 error = -EFAULT;
243                 goto out1;
244         }
245
246         bf54x_kpad->irq = platform_get_irq(pdev, 0);
247         if (bf54x_kpad->irq < 0) {
248                 error = -ENODEV;
249                 goto out2;
250         }
251
252         error = request_irq(bf54x_kpad->irq, bfin_kpad_isr,
253                                  IRQF_SAMPLE_RANDOM, DRV_NAME, pdev);
254         if (error) {
255                 printk(KERN_ERR DRV_NAME
256                         ": unable to claim irq %d; error %d\n",
257                         bf54x_kpad->irq, error);
258                 goto out2;
259         }
260
261         input = input_allocate_device();
262         if (!input) {
263                 error = -ENOMEM;
264                 goto out3;
265         }
266
267         bf54x_kpad->input = input;
268
269         input->name = pdev->name;
270         input->phys = "bf54x-keys/input0";
271         input->dev.parent = &pdev->dev;
272
273         input_set_drvdata(input, bf54x_kpad);
274
275         input->id.bustype = BUS_HOST;
276         input->id.vendor = 0x0001;
277         input->id.product = 0x0001;
278         input->id.version = 0x0100;
279
280         input->keycodesize = sizeof(unsigned short);
281         input->keycodemax = pdata->keymapsize;
282         input->keycode = bf54x_kpad->keycode;
283
284         bfin_keycodecpy(bf54x_kpad->keycode, pdata->keymap, pdata->keymapsize);
285
286         /* setup input device */
287         __set_bit(EV_KEY, input->evbit);
288
289         if (pdata->repeat)
290                 __set_bit(EV_REP, input->evbit);
291
292         for (i = 0; i < input->keycodemax; i++)
293                 __set_bit(bf54x_kpad->keycode[i] & KEY_MAX, input->keybit);
294         __clear_bit(KEY_RESERVED, input->keybit);
295
296         error = input_register_device(input);
297         if (error) {
298                 printk(KERN_ERR DRV_NAME
299                         ": Unable to register input device (%d)\n", error);
300                 goto out4;
301         }
302
303         /* Init Keypad Key Up/Release test timer */
304
305         setup_timer(&bf54x_kpad->timer, bfin_kpad_timer, (unsigned long) pdev);
306
307         bfin_write_KPAD_PRESCALE(bfin_kpad_get_prescale(TIME_SCALE));
308
309         bfin_write_KPAD_CTL((((pdata->cols - 1) << 13) & KPAD_COLEN) |
310                                 (((pdata->rows - 1) << 10) & KPAD_ROWEN) |
311                                 (2 & KPAD_IRQMODE));
312
313         bfin_write_KPAD_CTL(bfin_read_KPAD_CTL() | KPAD_EN);
314
315         device_init_wakeup(&pdev->dev, 1);
316
317         printk(KERN_ERR DRV_NAME
318                 ": Blackfin BF54x Keypad registered IRQ %d\n", bf54x_kpad->irq);
319
320         return 0;
321
322 out4:
323         input_free_device(input);
324 out3:
325         free_irq(bf54x_kpad->irq, pdev);
326 out2:
327         peripheral_free_list((u16 *)&per_cols[MAX_RC - pdata->cols]);
328 out1:
329         peripheral_free_list((u16 *)&per_rows[MAX_RC - pdata->rows]);
330 out0:
331         kfree(bf54x_kpad->keycode);
332 out:
333         kfree(bf54x_kpad);
334         platform_set_drvdata(pdev, NULL);
335
336         return error;
337 }
338
339 static int __devexit bfin_kpad_remove(struct platform_device *pdev)
340 {
341         struct bfin_kpad_platform_data *pdata = pdev->dev.platform_data;
342         struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
343
344         del_timer_sync(&bf54x_kpad->timer);
345         free_irq(bf54x_kpad->irq, pdev);
346
347         input_unregister_device(bf54x_kpad->input);
348
349         peripheral_free_list((u16 *)&per_rows[MAX_RC - pdata->rows]);
350         peripheral_free_list((u16 *)&per_cols[MAX_RC - pdata->cols]);
351
352         kfree(bf54x_kpad->keycode);
353         kfree(bf54x_kpad);
354         platform_set_drvdata(pdev, NULL);
355
356         return 0;
357 }
358
359 #ifdef CONFIG_PM
360 static int bfin_kpad_suspend(struct platform_device *pdev, pm_message_t state)
361 {
362         struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
363
364         if (device_may_wakeup(&pdev->dev))
365                 enable_irq_wake(bf54x_kpad->irq);
366
367         return 0;
368 }
369
370 static int bfin_kpad_resume(struct platform_device *pdev)
371 {
372         struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
373
374         if (device_may_wakeup(&pdev->dev))
375                 disable_irq_wake(bf54x_kpad->irq);
376
377         return 0;
378 }
379 #else
380 # define bfin_kpad_suspend NULL
381 # define bfin_kpad_resume  NULL
382 #endif
383
384 struct platform_driver bfin_kpad_device_driver = {
385         .driver         = {
386                 .name   = DRV_NAME,
387                 .owner  = THIS_MODULE,
388         },
389         .probe          = bfin_kpad_probe,
390         .remove         = __devexit_p(bfin_kpad_remove),
391         .suspend        = bfin_kpad_suspend,
392         .resume         = bfin_kpad_resume,
393 };
394
395 static int __init bfin_kpad_init(void)
396 {
397         return platform_driver_register(&bfin_kpad_device_driver);
398 }
399
400 static void __exit bfin_kpad_exit(void)
401 {
402         platform_driver_unregister(&bfin_kpad_device_driver);
403 }
404
405 module_init(bfin_kpad_init);
406 module_exit(bfin_kpad_exit);
407
408 MODULE_LICENSE("GPL");
409 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
410 MODULE_DESCRIPTION("Keypad driver for BF54x Processors");
411 MODULE_ALIAS("platform:bf54x-keys");