Input: add DaVinci Keypad Driver
[safe/jmp/linux-2.6] / drivers / input / keyboard / davinci_keyscan.c
1 /*
2  * DaVinci Key Scan Driver for TI platforms
3  *
4  * Copyright (C) 2009 Texas Instruments, Inc
5  *
6  * Author: Miguel Aguilar <miguel.aguilar@ridgerun.com>
7  *
8  * Intial Code: Sandeep Paulraj <s-paulraj@ti.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/types.h>
28 #include <linux/input.h>
29 #include <linux/kernel.h>
30 #include <linux/delay.h>
31 #include <linux/platform_device.h>
32 #include <linux/errno.h>
33
34 #include <asm/irq.h>
35
36 #include <mach/hardware.h>
37 #include <mach/irqs.h>
38 #include <mach/keyscan.h>
39
40 /* Key scan registers */
41 #define DAVINCI_KEYSCAN_KEYCTRL         0x0000
42 #define DAVINCI_KEYSCAN_INTENA          0x0004
43 #define DAVINCI_KEYSCAN_INTFLAG         0x0008
44 #define DAVINCI_KEYSCAN_INTCLR          0x000c
45 #define DAVINCI_KEYSCAN_STRBWIDTH       0x0010
46 #define DAVINCI_KEYSCAN_INTERVAL        0x0014
47 #define DAVINCI_KEYSCAN_CONTTIME        0x0018
48 #define DAVINCI_KEYSCAN_CURRENTST       0x001c
49 #define DAVINCI_KEYSCAN_PREVSTATE       0x0020
50 #define DAVINCI_KEYSCAN_EMUCTRL         0x0024
51 #define DAVINCI_KEYSCAN_IODFTCTRL       0x002c
52
53 /* Key Control Register (KEYCTRL) */
54 #define DAVINCI_KEYSCAN_KEYEN           0x00000001
55 #define DAVINCI_KEYSCAN_PREVMODE        0x00000002
56 #define DAVINCI_KEYSCAN_CHATOFF         0x00000004
57 #define DAVINCI_KEYSCAN_AUTODET         0x00000008
58 #define DAVINCI_KEYSCAN_SCANMODE        0x00000010
59 #define DAVINCI_KEYSCAN_OUTTYPE         0x00000020
60
61 /* Masks for the interrupts */
62 #define DAVINCI_KEYSCAN_INT_CONT        0x00000008
63 #define DAVINCI_KEYSCAN_INT_OFF         0x00000004
64 #define DAVINCI_KEYSCAN_INT_ON          0x00000002
65 #define DAVINCI_KEYSCAN_INT_CHANGE      0x00000001
66 #define DAVINCI_KEYSCAN_INT_ALL         0x0000000f
67
68 struct davinci_ks {
69         struct input_dev                *input;
70         struct davinci_ks_platform_data *pdata;
71         int                             irq;
72         void __iomem                    *base;
73         resource_size_t                 pbase;
74         size_t                          base_size;
75         unsigned short                  keymap[];
76 };
77
78 /* Initializing the kp Module */
79 static int __init davinci_ks_initialize(struct davinci_ks *davinci_ks)
80 {
81         struct device *dev = &davinci_ks->input->dev;
82         struct davinci_ks_platform_data *pdata = davinci_ks->pdata;
83         u32 matrix_ctrl;
84
85         /* Enable all interrupts */
86         __raw_writel(DAVINCI_KEYSCAN_INT_ALL,
87                      davinci_ks->base + DAVINCI_KEYSCAN_INTENA);
88
89         /* Clear interrupts if any */
90         __raw_writel(DAVINCI_KEYSCAN_INT_ALL,
91                      davinci_ks->base + DAVINCI_KEYSCAN_INTCLR);
92
93         /* Setup the scan period = strobe + interval */
94         __raw_writel(pdata->strobe,
95                      davinci_ks->base + DAVINCI_KEYSCAN_STRBWIDTH);
96         __raw_writel(pdata->interval,
97                      davinci_ks->base + DAVINCI_KEYSCAN_INTERVAL);
98         __raw_writel(0x01,
99                      davinci_ks->base + DAVINCI_KEYSCAN_CONTTIME);
100
101         /* Define matrix type */
102         switch (pdata->matrix_type) {
103         case DAVINCI_KEYSCAN_MATRIX_4X4:
104                 matrix_ctrl = 0;
105                 break;
106         case DAVINCI_KEYSCAN_MATRIX_5X3:
107                 matrix_ctrl = (1 << 6);
108                 break;
109         default:
110                 dev_err(dev->parent, "wrong matrix type\n");
111                 return -EINVAL;
112         }
113
114         /* Enable key scan module and set matrix type */
115         __raw_writel(DAVINCI_KEYSCAN_AUTODET | DAVINCI_KEYSCAN_KEYEN |
116                      matrix_ctrl, davinci_ks->base + DAVINCI_KEYSCAN_KEYCTRL);
117
118         return 0;
119 }
120
121 static irqreturn_t davinci_ks_interrupt(int irq, void *dev_id)
122 {
123         struct davinci_ks *davinci_ks = dev_id;
124         struct device *dev = &davinci_ks->input->dev;
125         unsigned short *keymap = davinci_ks->keymap;
126         int keymapsize = davinci_ks->pdata->keymapsize;
127         u32 prev_status, new_status, changed;
128         bool release;
129         int keycode = KEY_UNKNOWN;
130         int i;
131
132         /* Disable interrupt */
133         __raw_writel(0x0, davinci_ks->base + DAVINCI_KEYSCAN_INTENA);
134
135         /* Reading previous and new status of the key scan */
136         prev_status = __raw_readl(davinci_ks->base + DAVINCI_KEYSCAN_PREVSTATE);
137         new_status = __raw_readl(davinci_ks->base + DAVINCI_KEYSCAN_CURRENTST);
138
139         changed = prev_status ^ new_status;
140
141         if (changed) {
142                 /*
143                  * It goes through all bits in 'changed' to ensure
144                  * that no key changes are being missed
145                  */
146                 for (i = 0 ; i < keymapsize; i++) {
147                         if ((changed>>i) & 0x1) {
148                                 keycode = keymap[i];
149                                 release = (new_status >> i) & 0x1;
150                                 dev_dbg(dev->parent, "key %d %s\n", keycode,
151                                         release ? "released" : "pressed");
152                                 input_report_key(davinci_ks->input, keycode,
153                                                  !release);
154                                 input_sync(davinci_ks->input);
155                         }
156                 }
157                 /* Clearing interrupt */
158                 __raw_writel(DAVINCI_KEYSCAN_INT_ALL,
159                              davinci_ks->base + DAVINCI_KEYSCAN_INTCLR);
160         }
161
162         /* Enable interrupts */
163         __raw_writel(0x1, davinci_ks->base + DAVINCI_KEYSCAN_INTENA);
164
165         return IRQ_HANDLED;
166 }
167
168 static int __init davinci_ks_probe(struct platform_device *pdev)
169 {
170         struct davinci_ks *davinci_ks;
171         struct input_dev *key_dev;
172         struct resource *res, *mem;
173         struct device *dev = &pdev->dev;
174         struct davinci_ks_platform_data *pdata = pdev->dev.platform_data;
175         int error, i;
176
177         if (!pdata->keymap) {
178                 dev_dbg(dev, "no keymap from pdata\n");
179                 return -EINVAL;
180         }
181
182         davinci_ks = kzalloc(sizeof(struct davinci_ks) +
183                 sizeof(unsigned short) * pdata->keymapsize, GFP_KERNEL);
184         if (!davinci_ks) {
185                 dev_dbg(dev, "could not allocate memory for private data\n");
186                 return -ENOMEM;
187         }
188
189         memcpy(davinci_ks->keymap, pdata->keymap,
190                 sizeof(unsigned short) * pdata->keymapsize);
191
192         key_dev = input_allocate_device();
193         if (!key_dev) {
194                 dev_dbg(dev, "could not allocate input device\n");
195                 error = -ENOMEM;
196                 goto fail1;
197         }
198
199         davinci_ks->input = key_dev;
200
201         davinci_ks->irq = platform_get_irq(pdev, 0);
202         if (davinci_ks->irq < 0) {
203                 dev_err(dev, "no key scan irq\n");
204                 error = davinci_ks->irq;
205                 goto fail2;
206         }
207
208         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
209         if (!res) {
210                 dev_err(dev, "no mem resource\n");
211                 error = -EINVAL;
212                 goto fail2;
213         }
214
215         davinci_ks->pbase = res->start;
216         davinci_ks->base_size = resource_size(res);
217
218         mem = request_mem_region(davinci_ks->pbase, davinci_ks->base_size,
219                                  pdev->name);
220         if (!mem) {
221                 dev_err(dev, "key scan registers at %08x are not free\n",
222                         davinci_ks->pbase);
223                 error = -EBUSY;
224                 goto fail2;
225         }
226
227         davinci_ks->base = ioremap(davinci_ks->pbase, davinci_ks->base_size);
228         if (!davinci_ks->base) {
229                 dev_err(dev, "can't ioremap MEM resource.\n");
230                 error = -ENOMEM;
231                 goto fail3;
232         }
233
234         /* Enable auto repeat feature of Linux input subsystem */
235         if (pdata->rep)
236                 __set_bit(EV_REP, key_dev->evbit);
237
238         /* Setup input device */
239         __set_bit(EV_KEY, key_dev->evbit);
240
241         /* Setup the platform data */
242         davinci_ks->pdata = pdata;
243
244         for (i = 0; i < davinci_ks->pdata->keymapsize; i++)
245                 __set_bit(davinci_ks->pdata->keymap[i], key_dev->keybit);
246
247         key_dev->name = "davinci_keyscan";
248         key_dev->phys = "davinci_keyscan/input0";
249         key_dev->dev.parent = &pdev->dev;
250         key_dev->id.bustype = BUS_HOST;
251         key_dev->id.vendor = 0x0001;
252         key_dev->id.product = 0x0001;
253         key_dev->id.version = 0x0001;
254         key_dev->keycode = davinci_ks->keymap;
255         key_dev->keycodesize = sizeof(davinci_ks->keymap[0]);
256         key_dev->keycodemax = davinci_ks->pdata->keymapsize;
257
258         error = input_register_device(davinci_ks->input);
259         if (error < 0) {
260                 dev_err(dev, "unable to register davinci key scan device\n");
261                 goto fail4;
262         }
263
264         error = request_irq(davinci_ks->irq, davinci_ks_interrupt,
265                           IRQF_DISABLED, pdev->name, davinci_ks);
266         if (error < 0) {
267                 dev_err(dev, "unable to register davinci key scan interrupt\n");
268                 goto fail5;
269         }
270
271         error = davinci_ks_initialize(davinci_ks);
272         if (error < 0) {
273                 dev_err(dev, "unable to initialize davinci key scan device\n");
274                 goto fail6;
275         }
276
277         platform_set_drvdata(pdev, davinci_ks);
278         return 0;
279
280 fail6:
281         free_irq(davinci_ks->irq, davinci_ks);
282 fail5:
283         input_unregister_device(davinci_ks->input);
284         key_dev = NULL;
285 fail4:
286         iounmap(davinci_ks->base);
287 fail3:
288         release_mem_region(davinci_ks->pbase, davinci_ks->base_size);
289 fail2:
290         input_free_device(key_dev);
291 fail1:
292         kfree(davinci_ks);
293
294         return error;
295 }
296
297 static int __devexit davinci_ks_remove(struct platform_device *pdev)
298 {
299         struct davinci_ks *davinci_ks = platform_get_drvdata(pdev);
300
301         free_irq(davinci_ks->irq, davinci_ks);
302
303         input_unregister_device(davinci_ks->input);
304
305         iounmap(davinci_ks->base);
306         release_mem_region(davinci_ks->pbase, davinci_ks->base_size);
307
308         platform_set_drvdata(pdev, NULL);
309
310         kfree(davinci_ks);
311
312         return 0;
313 }
314
315 static struct platform_driver davinci_ks_driver = {
316         .driver = {
317                 .name = "davinci_keyscan",
318                 .owner = THIS_MODULE,
319         },
320         .remove = __devexit_p(davinci_ks_remove),
321 };
322
323 static int __init davinci_ks_init(void)
324 {
325         return platform_driver_probe(&davinci_ks_driver, davinci_ks_probe);
326 }
327 module_init(davinci_ks_init);
328
329 static void __exit davinci_ks_exit(void)
330 {
331         platform_driver_unregister(&davinci_ks_driver);
332 }
333 module_exit(davinci_ks_exit);
334
335 MODULE_AUTHOR("Miguel Aguilar");
336 MODULE_DESCRIPTION("Texas Instruments DaVinci Key Scan Driver");
337 MODULE_LICENSE("GPL");