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