2 * SuperH KEYSC Keypad Driver
4 * Copyright (C) 2008 Magnus Damm
6 * Based on gpio_keys.c, Copyright 2005 Phil Blundell
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/delay.h>
19 #include <linux/platform_device.h>
20 #include <linux/input.h>
21 #include <linux/input/sh_keysc.h>
22 #include <linux/bitmap.h>
23 #include <linux/pm_runtime.h>
25 #include <linux/slab.h>
28 unsigned char kymd
, keyout
, keyin
;
30 [SH_KEYSC_MODE_1
] = { 0, 6, 5 },
31 [SH_KEYSC_MODE_2
] = { 1, 5, 6 },
32 [SH_KEYSC_MODE_3
] = { 2, 4, 7 },
33 [SH_KEYSC_MODE_4
] = { 3, 6, 6 },
34 [SH_KEYSC_MODE_5
] = { 4, 6, 7 },
35 [SH_KEYSC_MODE_6
] = { 5, 8, 8 },
38 struct sh_keysc_priv
{
39 void __iomem
*iomem_base
;
40 DECLARE_BITMAP(last_keys
, SH_KEYSC_MAXKEYS
);
41 struct input_dev
*input
;
42 struct sh_keysc_info pdata
;
50 #define KYCR2_IRQ_LEVEL 0x10
51 #define KYCR2_IRQ_DISABLED 0x00
53 static unsigned long sh_keysc_read(struct sh_keysc_priv
*p
, int reg_nr
)
55 return ioread16(p
->iomem_base
+ (reg_nr
<< 2));
58 static void sh_keysc_write(struct sh_keysc_priv
*p
, int reg_nr
,
61 iowrite16(value
, p
->iomem_base
+ (reg_nr
<< 2));
64 static void sh_keysc_level_mode(struct sh_keysc_priv
*p
,
65 unsigned long keys_set
)
67 struct sh_keysc_info
*pdata
= &p
->pdata
;
69 sh_keysc_write(p
, KYOUTDR
, 0);
70 sh_keysc_write(p
, KYCR2
, KYCR2_IRQ_LEVEL
| (keys_set
<< 8));
72 if (pdata
->kycr2_delay
)
73 udelay(pdata
->kycr2_delay
);
76 static void sh_keysc_map_dbg(struct device
*dev
, unsigned long *map
,
81 for (k
= 0; k
< BITS_TO_LONGS(SH_KEYSC_MAXKEYS
); k
++)
82 dev_dbg(dev
, "%s[%d] 0x%lx\n", str
, k
, map
[k
]);
85 static irqreturn_t
sh_keysc_isr(int irq
, void *dev_id
)
87 struct platform_device
*pdev
= dev_id
;
88 struct sh_keysc_priv
*priv
= platform_get_drvdata(pdev
);
89 struct sh_keysc_info
*pdata
= &priv
->pdata
;
90 int keyout_nr
= sh_keysc_mode
[pdata
->mode
].keyout
;
91 int keyin_nr
= sh_keysc_mode
[pdata
->mode
].keyin
;
92 DECLARE_BITMAP(keys
, SH_KEYSC_MAXKEYS
);
93 DECLARE_BITMAP(keys0
, SH_KEYSC_MAXKEYS
);
94 DECLARE_BITMAP(keys1
, SH_KEYSC_MAXKEYS
);
95 unsigned char keyin_set
, tmp
;
98 dev_dbg(&pdev
->dev
, "isr!\n");
100 bitmap_fill(keys1
, SH_KEYSC_MAXKEYS
);
101 bitmap_zero(keys0
, SH_KEYSC_MAXKEYS
);
104 bitmap_zero(keys
, SH_KEYSC_MAXKEYS
);
107 sh_keysc_write(priv
, KYCR2
, KYCR2_IRQ_DISABLED
);
109 for (i
= 0; i
< keyout_nr
; i
++) {
112 /* drive one KEYOUT pin low, read KEYIN pins */
113 sh_keysc_write(priv
, KYOUTDR
, 0xffff ^ (3 << (i
* 2)));
114 udelay(pdata
->delay
);
115 tmp
= sh_keysc_read(priv
, KYINDR
);
117 /* set bit if key press has been detected */
118 for (k
= 0; k
< keyin_nr
; k
++) {
120 __set_bit(n
+ k
, keys
);
123 /* keep track of which KEYIN bits that have been set */
124 keyin_set
|= tmp
^ ((1 << keyin_nr
) - 1);
127 sh_keysc_level_mode(priv
, keyin_set
);
129 bitmap_complement(keys
, keys
, SH_KEYSC_MAXKEYS
);
130 bitmap_and(keys1
, keys1
, keys
, SH_KEYSC_MAXKEYS
);
131 bitmap_or(keys0
, keys0
, keys
, SH_KEYSC_MAXKEYS
);
133 sh_keysc_map_dbg(&pdev
->dev
, keys
, "keys");
135 } while (sh_keysc_read(priv
, KYCR2
) & 0x01);
137 sh_keysc_map_dbg(&pdev
->dev
, priv
->last_keys
, "last_keys");
138 sh_keysc_map_dbg(&pdev
->dev
, keys0
, "keys0");
139 sh_keysc_map_dbg(&pdev
->dev
, keys1
, "keys1");
141 for (i
= 0; i
< SH_KEYSC_MAXKEYS
; i
++) {
142 k
= pdata
->keycodes
[i
];
146 if (test_bit(i
, keys0
) == test_bit(i
, priv
->last_keys
))
149 if (test_bit(i
, keys1
) || test_bit(i
, keys0
)) {
150 input_event(priv
->input
, EV_KEY
, k
, 1);
151 __set_bit(i
, priv
->last_keys
);
154 if (!test_bit(i
, keys1
)) {
155 input_event(priv
->input
, EV_KEY
, k
, 0);
156 __clear_bit(i
, priv
->last_keys
);
160 input_sync(priv
->input
);
165 static int __devinit
sh_keysc_probe(struct platform_device
*pdev
)
167 struct sh_keysc_priv
*priv
;
168 struct sh_keysc_info
*pdata
;
169 struct resource
*res
;
170 struct input_dev
*input
;
174 if (!pdev
->dev
.platform_data
) {
175 dev_err(&pdev
->dev
, "no platform data defined\n");
181 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
183 dev_err(&pdev
->dev
, "failed to get I/O memory\n");
187 irq
= platform_get_irq(pdev
, 0);
189 dev_err(&pdev
->dev
, "failed to get irq\n");
193 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
195 dev_err(&pdev
->dev
, "failed to allocate driver data\n");
200 platform_set_drvdata(pdev
, priv
);
201 memcpy(&priv
->pdata
, pdev
->dev
.platform_data
, sizeof(priv
->pdata
));
202 pdata
= &priv
->pdata
;
204 priv
->iomem_base
= ioremap_nocache(res
->start
, resource_size(res
));
205 if (priv
->iomem_base
== NULL
) {
206 dev_err(&pdev
->dev
, "failed to remap I/O memory\n");
211 priv
->input
= input_allocate_device();
213 dev_err(&pdev
->dev
, "failed to allocate input device\n");
219 input
->evbit
[0] = BIT_MASK(EV_KEY
);
221 input
->name
= pdev
->name
;
222 input
->phys
= "sh-keysc-keys/input0";
223 input
->dev
.parent
= &pdev
->dev
;
225 input
->id
.bustype
= BUS_HOST
;
226 input
->id
.vendor
= 0x0001;
227 input
->id
.product
= 0x0001;
228 input
->id
.version
= 0x0100;
230 input
->keycode
= pdata
->keycodes
;
231 input
->keycodesize
= sizeof(pdata
->keycodes
[0]);
232 input
->keycodemax
= ARRAY_SIZE(pdata
->keycodes
);
234 error
= request_threaded_irq(irq
, NULL
, sh_keysc_isr
, IRQF_ONESHOT
,
235 dev_name(&pdev
->dev
), pdev
);
237 dev_err(&pdev
->dev
, "failed to request IRQ\n");
241 for (i
= 0; i
< SH_KEYSC_MAXKEYS
; i
++)
242 __set_bit(pdata
->keycodes
[i
], input
->keybit
);
243 __clear_bit(KEY_RESERVED
, input
->keybit
);
245 error
= input_register_device(input
);
247 dev_err(&pdev
->dev
, "failed to register input device\n");
251 pm_runtime_enable(&pdev
->dev
);
252 pm_runtime_get_sync(&pdev
->dev
);
254 sh_keysc_write(priv
, KYCR1
, (sh_keysc_mode
[pdata
->mode
].kymd
<< 8) |
256 sh_keysc_level_mode(priv
, 0);
258 device_init_wakeup(&pdev
->dev
, 1);
265 input_free_device(input
);
267 iounmap(priv
->iomem_base
);
269 platform_set_drvdata(pdev
, NULL
);
275 static int __devexit
sh_keysc_remove(struct platform_device
*pdev
)
277 struct sh_keysc_priv
*priv
= platform_get_drvdata(pdev
);
279 sh_keysc_write(priv
, KYCR2
, KYCR2_IRQ_DISABLED
);
281 input_unregister_device(priv
->input
);
282 free_irq(platform_get_irq(pdev
, 0), pdev
);
283 iounmap(priv
->iomem_base
);
285 pm_runtime_put_sync(&pdev
->dev
);
286 pm_runtime_disable(&pdev
->dev
);
288 platform_set_drvdata(pdev
, NULL
);
294 #ifdef CONFIG_PM_SLEEP
295 static int sh_keysc_suspend(struct device
*dev
)
297 struct platform_device
*pdev
= to_platform_device(dev
);
298 struct sh_keysc_priv
*priv
= platform_get_drvdata(pdev
);
299 int irq
= platform_get_irq(pdev
, 0);
300 unsigned short value
;
302 value
= sh_keysc_read(priv
, KYCR1
);
304 if (device_may_wakeup(dev
)) {
305 sh_keysc_write(priv
, KYCR1
, value
| 0x80);
306 enable_irq_wake(irq
);
308 sh_keysc_write(priv
, KYCR1
, value
& ~0x80);
309 pm_runtime_put_sync(dev
);
315 static int sh_keysc_resume(struct device
*dev
)
317 struct platform_device
*pdev
= to_platform_device(dev
);
318 int irq
= platform_get_irq(pdev
, 0);
320 if (device_may_wakeup(dev
))
321 disable_irq_wake(irq
);
323 pm_runtime_get_sync(dev
);
329 static SIMPLE_DEV_PM_OPS(sh_keysc_dev_pm_ops
,
330 sh_keysc_suspend
, sh_keysc_resume
);
332 static struct platform_driver sh_keysc_device_driver
= {
333 .probe
= sh_keysc_probe
,
334 .remove
= __devexit_p(sh_keysc_remove
),
337 .pm
= &sh_keysc_dev_pm_ops
,
341 static int __init
sh_keysc_init(void)
343 return platform_driver_register(&sh_keysc_device_driver
);
346 static void __exit
sh_keysc_exit(void)
348 platform_driver_unregister(&sh_keysc_device_driver
);
351 module_init(sh_keysc_init
);
352 module_exit(sh_keysc_exit
);
354 MODULE_AUTHOR("Magnus Damm");
355 MODULE_DESCRIPTION("SuperH KEYSC Keypad Driver");
356 MODULE_LICENSE("GPL");