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