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/clk.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, 7, 7 },
38 struct sh_keysc_priv
{
39 void __iomem
*iomem_base
;
41 DECLARE_BITMAP(last_keys
, SH_KEYSC_MAXKEYS
);
42 struct input_dev
*input
;
43 struct sh_keysc_info pdata
;
51 #define KYCR2_IRQ_LEVEL 0x10
52 #define KYCR2_IRQ_DISABLED 0x00
54 static unsigned long sh_keysc_read(struct sh_keysc_priv
*p
, int reg_nr
)
56 return ioread16(p
->iomem_base
+ (reg_nr
<< 2));
59 static void sh_keysc_write(struct sh_keysc_priv
*p
, int reg_nr
,
62 iowrite16(value
, p
->iomem_base
+ (reg_nr
<< 2));
65 static void sh_keysc_level_mode(struct sh_keysc_priv
*p
,
66 unsigned long keys_set
)
68 struct sh_keysc_info
*pdata
= &p
->pdata
;
70 sh_keysc_write(p
, KYOUTDR
, 0);
71 sh_keysc_write(p
, KYCR2
, KYCR2_IRQ_LEVEL
| (keys_set
<< 8));
73 if (pdata
->kycr2_delay
)
74 udelay(pdata
->kycr2_delay
);
77 static void sh_keysc_map_dbg(struct device
*dev
, unsigned long *map
,
82 for (k
= 0; k
< BITS_TO_LONGS(SH_KEYSC_MAXKEYS
); k
++)
83 dev_dbg(dev
, "%s[%d] 0x%lx\n", str
, k
, map
[k
]);
86 static irqreturn_t
sh_keysc_isr(int irq
, void *dev_id
)
88 struct platform_device
*pdev
= dev_id
;
89 struct sh_keysc_priv
*priv
= platform_get_drvdata(pdev
);
90 struct sh_keysc_info
*pdata
= &priv
->pdata
;
91 int keyout_nr
= sh_keysc_mode
[pdata
->mode
].keyout
;
92 int keyin_nr
= sh_keysc_mode
[pdata
->mode
].keyin
;
93 DECLARE_BITMAP(keys
, SH_KEYSC_MAXKEYS
);
94 DECLARE_BITMAP(keys0
, SH_KEYSC_MAXKEYS
);
95 DECLARE_BITMAP(keys1
, SH_KEYSC_MAXKEYS
);
96 unsigned char keyin_set
, tmp
;
99 dev_dbg(&pdev
->dev
, "isr!\n");
101 bitmap_fill(keys1
, SH_KEYSC_MAXKEYS
);
102 bitmap_zero(keys0
, SH_KEYSC_MAXKEYS
);
105 bitmap_zero(keys
, SH_KEYSC_MAXKEYS
);
108 sh_keysc_write(priv
, KYCR2
, KYCR2_IRQ_DISABLED
);
110 for (i
= 0; i
< keyout_nr
; i
++) {
113 /* drive one KEYOUT pin low, read KEYIN pins */
114 sh_keysc_write(priv
, KYOUTDR
, 0xffff ^ (3 << (i
* 2)));
115 udelay(pdata
->delay
);
116 tmp
= sh_keysc_read(priv
, KYINDR
);
118 /* set bit if key press has been detected */
119 for (k
= 0; k
< keyin_nr
; k
++) {
121 __set_bit(n
+ k
, keys
);
124 /* keep track of which KEYIN bits that have been set */
125 keyin_set
|= tmp
^ ((1 << keyin_nr
) - 1);
128 sh_keysc_level_mode(priv
, keyin_set
);
130 bitmap_complement(keys
, keys
, SH_KEYSC_MAXKEYS
);
131 bitmap_and(keys1
, keys1
, keys
, SH_KEYSC_MAXKEYS
);
132 bitmap_or(keys0
, keys0
, keys
, SH_KEYSC_MAXKEYS
);
134 sh_keysc_map_dbg(&pdev
->dev
, keys
, "keys");
136 } while (sh_keysc_read(priv
, KYCR2
) & 0x01);
138 sh_keysc_map_dbg(&pdev
->dev
, priv
->last_keys
, "last_keys");
139 sh_keysc_map_dbg(&pdev
->dev
, keys0
, "keys0");
140 sh_keysc_map_dbg(&pdev
->dev
, keys1
, "keys1");
142 for (i
= 0; i
< SH_KEYSC_MAXKEYS
; i
++) {
143 k
= pdata
->keycodes
[i
];
147 if (test_bit(i
, keys0
) == test_bit(i
, priv
->last_keys
))
150 if (test_bit(i
, keys1
) || test_bit(i
, keys0
)) {
151 input_event(priv
->input
, EV_KEY
, k
, 1);
152 __set_bit(i
, priv
->last_keys
);
155 if (!test_bit(i
, keys1
)) {
156 input_event(priv
->input
, EV_KEY
, k
, 0);
157 __clear_bit(i
, priv
->last_keys
);
161 input_sync(priv
->input
);
166 static int __devinit
sh_keysc_probe(struct platform_device
*pdev
)
168 struct sh_keysc_priv
*priv
;
169 struct sh_keysc_info
*pdata
;
170 struct resource
*res
;
171 struct input_dev
*input
;
176 if (!pdev
->dev
.platform_data
) {
177 dev_err(&pdev
->dev
, "no platform data defined\n");
183 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
185 dev_err(&pdev
->dev
, "failed to get I/O memory\n");
189 irq
= platform_get_irq(pdev
, 0);
191 dev_err(&pdev
->dev
, "failed to get irq\n");
195 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
197 dev_err(&pdev
->dev
, "failed to allocate driver data\n");
202 platform_set_drvdata(pdev
, priv
);
203 memcpy(&priv
->pdata
, pdev
->dev
.platform_data
, sizeof(priv
->pdata
));
204 pdata
= &priv
->pdata
;
206 priv
->iomem_base
= ioremap_nocache(res
->start
, resource_size(res
));
207 if (priv
->iomem_base
== NULL
) {
208 dev_err(&pdev
->dev
, "failed to remap I/O memory\n");
213 snprintf(clk_name
, sizeof(clk_name
), "keysc%d", pdev
->id
);
214 priv
->clk
= clk_get(&pdev
->dev
, clk_name
);
215 if (IS_ERR(priv
->clk
)) {
216 dev_err(&pdev
->dev
, "cannot get clock \"%s\"\n", clk_name
);
217 error
= PTR_ERR(priv
->clk
);
221 priv
->input
= input_allocate_device();
223 dev_err(&pdev
->dev
, "failed to allocate input device\n");
229 input
->evbit
[0] = BIT_MASK(EV_KEY
);
231 input
->name
= pdev
->name
;
232 input
->phys
= "sh-keysc-keys/input0";
233 input
->dev
.parent
= &pdev
->dev
;
235 input
->id
.bustype
= BUS_HOST
;
236 input
->id
.vendor
= 0x0001;
237 input
->id
.product
= 0x0001;
238 input
->id
.version
= 0x0100;
240 input
->keycode
= pdata
->keycodes
;
241 input
->keycodesize
= sizeof(pdata
->keycodes
[0]);
242 input
->keycodemax
= ARRAY_SIZE(pdata
->keycodes
);
244 error
= request_irq(irq
, sh_keysc_isr
, 0, pdev
->name
, pdev
);
246 dev_err(&pdev
->dev
, "failed to request IRQ\n");
250 for (i
= 0; i
< SH_KEYSC_MAXKEYS
; i
++)
251 __set_bit(pdata
->keycodes
[i
], input
->keybit
);
252 __clear_bit(KEY_RESERVED
, input
->keybit
);
254 error
= input_register_device(input
);
256 dev_err(&pdev
->dev
, "failed to register input device\n");
260 clk_enable(priv
->clk
);
262 sh_keysc_write(priv
, KYCR1
, (sh_keysc_mode
[pdata
->mode
].kymd
<< 8) |
264 sh_keysc_level_mode(priv
, 0);
266 device_init_wakeup(&pdev
->dev
, 1);
273 input_free_device(input
);
277 iounmap(priv
->iomem_base
);
279 platform_set_drvdata(pdev
, NULL
);
285 static int __devexit
sh_keysc_remove(struct platform_device
*pdev
)
287 struct sh_keysc_priv
*priv
= platform_get_drvdata(pdev
);
289 sh_keysc_write(priv
, KYCR2
, KYCR2_IRQ_DISABLED
);
291 input_unregister_device(priv
->input
);
292 free_irq(platform_get_irq(pdev
, 0), pdev
);
293 iounmap(priv
->iomem_base
);
295 clk_disable(priv
->clk
);
298 platform_set_drvdata(pdev
, NULL
);
304 static int sh_keysc_suspend(struct device
*dev
)
306 struct platform_device
*pdev
= to_platform_device(dev
);
307 struct sh_keysc_priv
*priv
= platform_get_drvdata(pdev
);
308 int irq
= platform_get_irq(pdev
, 0);
309 unsigned short value
;
311 value
= sh_keysc_read(priv
, KYCR1
);
313 if (device_may_wakeup(dev
)) {
315 enable_irq_wake(irq
);
320 sh_keysc_write(priv
, KYCR1
, value
);
325 static int sh_keysc_resume(struct device
*dev
)
327 struct platform_device
*pdev
= to_platform_device(dev
);
328 int irq
= platform_get_irq(pdev
, 0);
330 if (device_may_wakeup(dev
))
331 disable_irq_wake(irq
);
336 static const struct dev_pm_ops sh_keysc_dev_pm_ops
= {
337 .suspend
= sh_keysc_suspend
,
338 .resume
= sh_keysc_resume
,
341 struct platform_driver sh_keysc_device_driver
= {
342 .probe
= sh_keysc_probe
,
343 .remove
= __devexit_p(sh_keysc_remove
),
346 .pm
= &sh_keysc_dev_pm_ops
,
350 static int __init
sh_keysc_init(void)
352 return platform_driver_register(&sh_keysc_device_driver
);
355 static void __exit
sh_keysc_exit(void)
357 platform_driver_unregister(&sh_keysc_device_driver
);
360 module_init(sh_keysc_init
);
361 module_exit(sh_keysc_exit
);
363 MODULE_AUTHOR("Magnus Damm");
364 MODULE_DESCRIPTION("SuperH KEYSC Keypad Driver");
365 MODULE_LICENSE("GPL");