2 * File: drivers/input/keyboard/bf54x-keys.c
4 * Author: Michael Hennerich <hennerich@blackfin.uclinux.org>
7 * Description: keypad driver for Analog Devices Blackfin BF54x Processors
11 * Copyright 2007-2008 Analog Devices Inc.
13 * Bugs: Enter bugs at http://blackfin.uclinux.org/
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.
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.
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
31 #include <linux/module.h>
34 #include <linux/interrupt.h>
35 #include <linux/irq.h>
36 #include <linux/slab.h>
37 #include <linux/sched.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>
45 #include <asm/portmux.h>
46 #include <mach/bf54x_keys.h>
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 */
53 static const u16 per_rows
[] = {
65 static const u16 per_cols
[] = {
78 struct input_dev
*input
;
80 unsigned short lastkey
;
81 unsigned short *keycode
;
82 struct timer_list timer
;
83 unsigned int keyup_test_jiffies
;
84 unsigned short kpad_msel
;
85 unsigned short kpad_prescale
;
86 unsigned short kpad_ctl
;
89 static inline int bfin_kpad_find_key(struct bf54x_kpad
*bf54x_kpad
,
90 struct input_dev
*input
, u16 keyident
)
94 for (i
= 0; i
< input
->keycodemax
; i
++)
95 if (bf54x_kpad
->keycode
[i
+ input
->keycodemax
] == keyident
)
96 return bf54x_kpad
->keycode
[i
];
100 static inline void bfin_keycodecpy(unsigned short *keycode
,
101 const unsigned int *pdata_kc
,
102 unsigned short keymapsize
)
106 for (i
= 0; i
< keymapsize
; i
++) {
107 keycode
[i
] = pdata_kc
[i
] & 0xffff;
108 keycode
[i
+ keymapsize
] = pdata_kc
[i
] >> 16;
112 static inline u16
bfin_kpad_get_prescale(u32 timescale
)
114 u32 sclk
= get_sclk();
116 return ((((sclk
/ 1000) * timescale
) / 1024) - 1);
119 static inline u16
bfin_kpad_get_keypressed(struct bf54x_kpad
*bf54x_kpad
)
121 return (bfin_read_KPAD_STAT() & KPAD_PRESSED
);
124 static inline void bfin_kpad_clear_irq(void)
126 bfin_write_KPAD_STAT(0xFFFF);
127 bfin_write_KPAD_ROWCOL(0xFFFF);
130 static void bfin_kpad_timer(unsigned long data
)
132 struct platform_device
*pdev
= (struct platform_device
*) data
;
133 struct bf54x_kpad
*bf54x_kpad
= platform_get_drvdata(pdev
);
135 if (bfin_kpad_get_keypressed(bf54x_kpad
)) {
136 /* Try again later */
137 mod_timer(&bf54x_kpad
->timer
,
138 jiffies
+ bf54x_kpad
->keyup_test_jiffies
);
142 input_report_key(bf54x_kpad
->input
, bf54x_kpad
->lastkey
, 0);
143 input_sync(bf54x_kpad
->input
);
145 /* Clear IRQ Status */
147 bfin_kpad_clear_irq();
148 enable_irq(bf54x_kpad
->irq
);
151 static irqreturn_t
bfin_kpad_isr(int irq
, void *dev_id
)
153 struct platform_device
*pdev
= dev_id
;
154 struct bf54x_kpad
*bf54x_kpad
= platform_get_drvdata(pdev
);
155 struct input_dev
*input
= bf54x_kpad
->input
;
157 u16 rowcol
= bfin_read_KPAD_ROWCOL();
159 key
= bfin_kpad_find_key(bf54x_kpad
, input
, rowcol
);
161 input_report_key(input
, key
, 1);
164 if (bfin_kpad_get_keypressed(bf54x_kpad
)) {
165 disable_irq_nosync(bf54x_kpad
->irq
);
166 bf54x_kpad
->lastkey
= key
;
167 mod_timer(&bf54x_kpad
->timer
,
168 jiffies
+ bf54x_kpad
->keyup_test_jiffies
);
170 input_report_key(input
, key
, 0);
173 bfin_kpad_clear_irq();
179 static int bfin_kpad_probe(struct platform_device
*pdev
)
181 struct bf54x_kpad
*bf54x_kpad
;
182 struct bfin_kpad_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
183 struct input_dev
*input
;
186 if (!pdata
->rows
|| !pdata
->cols
|| !pdata
->keymap
) {
187 dev_err(&pdev
->dev
, "no rows, cols or keymap from pdata\n");
191 if (!pdata
->keymapsize
||
192 pdata
->keymapsize
> (pdata
->rows
* pdata
->cols
)) {
193 dev_err(&pdev
->dev
, "invalid keymapsize\n");
197 bf54x_kpad
= kzalloc(sizeof(struct bf54x_kpad
), GFP_KERNEL
);
201 platform_set_drvdata(pdev
, bf54x_kpad
);
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
) {
211 if (!pdata
->debounce_time
|| pdata
->debounce_time
> MAX_MULT
||
212 !pdata
->coldrive_time
|| pdata
->coldrive_time
> MAX_MULT
) {
214 "invalid platform debounce/columndrive time\n");
215 bfin_write_KPAD_MSEL(0xFF0); /* Default MSEL */
217 bfin_write_KPAD_MSEL(
218 ((pdata
->debounce_time
/ TIME_SCALE
)
220 (((pdata
->coldrive_time
/ TIME_SCALE
) << 8)
225 if (!pdata
->keyup_test_interval
)
226 bf54x_kpad
->keyup_test_jiffies
= msecs_to_jiffies(50);
228 bf54x_kpad
->keyup_test_jiffies
=
229 msecs_to_jiffies(pdata
->keyup_test_interval
);
231 if (peripheral_request_list((u16
*)&per_rows
[MAX_RC
- pdata
->rows
],
233 dev_err(&pdev
->dev
, "requesting peripherals failed\n");
238 if (peripheral_request_list((u16
*)&per_cols
[MAX_RC
- pdata
->cols
],
240 dev_err(&pdev
->dev
, "requesting peripherals failed\n");
245 bf54x_kpad
->irq
= platform_get_irq(pdev
, 0);
246 if (bf54x_kpad
->irq
< 0) {
251 error
= request_irq(bf54x_kpad
->irq
, bfin_kpad_isr
,
254 dev_err(&pdev
->dev
, "unable to claim irq %d\n",
259 input
= input_allocate_device();
265 bf54x_kpad
->input
= input
;
267 input
->name
= pdev
->name
;
268 input
->phys
= "bf54x-keys/input0";
269 input
->dev
.parent
= &pdev
->dev
;
271 input
->id
.bustype
= BUS_HOST
;
272 input
->id
.vendor
= 0x0001;
273 input
->id
.product
= 0x0001;
274 input
->id
.version
= 0x0100;
276 input
->keycodesize
= sizeof(unsigned short);
277 input
->keycodemax
= pdata
->keymapsize
;
278 input
->keycode
= bf54x_kpad
->keycode
;
280 bfin_keycodecpy(bf54x_kpad
->keycode
, pdata
->keymap
, pdata
->keymapsize
);
282 /* setup input device */
283 __set_bit(EV_KEY
, input
->evbit
);
286 __set_bit(EV_REP
, input
->evbit
);
288 for (i
= 0; i
< input
->keycodemax
; i
++)
289 if (bf54x_kpad
->keycode
[i
] <= KEY_MAX
)
290 __set_bit(bf54x_kpad
->keycode
[i
], input
->keybit
);
291 __clear_bit(KEY_RESERVED
, input
->keybit
);
293 error
= input_register_device(input
);
295 dev_err(&pdev
->dev
, "unable to register input device\n");
299 /* Init Keypad Key Up/Release test timer */
301 setup_timer(&bf54x_kpad
->timer
, bfin_kpad_timer
, (unsigned long) pdev
);
303 bfin_write_KPAD_PRESCALE(bfin_kpad_get_prescale(TIME_SCALE
));
305 bfin_write_KPAD_CTL((((pdata
->cols
- 1) << 13) & KPAD_COLEN
) |
306 (((pdata
->rows
- 1) << 10) & KPAD_ROWEN
) |
309 bfin_write_KPAD_CTL(bfin_read_KPAD_CTL() | KPAD_EN
);
311 device_init_wakeup(&pdev
->dev
, 1);
316 input_free_device(input
);
318 free_irq(bf54x_kpad
->irq
, pdev
);
320 peripheral_free_list((u16
*)&per_cols
[MAX_RC
- pdata
->cols
]);
322 peripheral_free_list((u16
*)&per_rows
[MAX_RC
- pdata
->rows
]);
324 kfree(bf54x_kpad
->keycode
);
331 static int bfin_kpad_remove(struct platform_device
*pdev
)
333 struct bfin_kpad_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
334 struct bf54x_kpad
*bf54x_kpad
= platform_get_drvdata(pdev
);
336 del_timer_sync(&bf54x_kpad
->timer
);
337 free_irq(bf54x_kpad
->irq
, pdev
);
339 input_unregister_device(bf54x_kpad
->input
);
341 peripheral_free_list((u16
*)&per_rows
[MAX_RC
- pdata
->rows
]);
342 peripheral_free_list((u16
*)&per_cols
[MAX_RC
- pdata
->cols
]);
344 kfree(bf54x_kpad
->keycode
);
351 static int bfin_kpad_suspend(struct platform_device
*pdev
, pm_message_t state
)
353 struct bf54x_kpad
*bf54x_kpad
= platform_get_drvdata(pdev
);
355 bf54x_kpad
->kpad_msel
= bfin_read_KPAD_MSEL();
356 bf54x_kpad
->kpad_prescale
= bfin_read_KPAD_PRESCALE();
357 bf54x_kpad
->kpad_ctl
= bfin_read_KPAD_CTL();
359 if (device_may_wakeup(&pdev
->dev
))
360 enable_irq_wake(bf54x_kpad
->irq
);
365 static int bfin_kpad_resume(struct platform_device
*pdev
)
367 struct bf54x_kpad
*bf54x_kpad
= platform_get_drvdata(pdev
);
369 bfin_write_KPAD_MSEL(bf54x_kpad
->kpad_msel
);
370 bfin_write_KPAD_PRESCALE(bf54x_kpad
->kpad_prescale
);
371 bfin_write_KPAD_CTL(bf54x_kpad
->kpad_ctl
);
373 if (device_may_wakeup(&pdev
->dev
))
374 disable_irq_wake(bf54x_kpad
->irq
);
379 # define bfin_kpad_suspend NULL
380 # define bfin_kpad_resume NULL
383 static struct platform_driver bfin_kpad_device_driver
= {
387 .probe
= bfin_kpad_probe
,
388 .remove
= bfin_kpad_remove
,
389 .suspend
= bfin_kpad_suspend
,
390 .resume
= bfin_kpad_resume
,
392 module_platform_driver(bfin_kpad_device_driver
);
394 MODULE_LICENSE("GPL");
395 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
396 MODULE_DESCRIPTION("Keypad driver for BF54x Processors");
397 MODULE_ALIAS("platform:bf54x-keys");