ARM: mach-shmobile: g3evm: Add IrDA support
[zen-stable.git] / drivers / input / keyboard / sh_keysc.c
blobd7dafd9425b69b0682035822185900b8e8ac3d72
1 /*
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>
24 #include <linux/io.h>
25 #include <linux/slab.h>
27 static const struct {
28 unsigned char kymd, keyout, keyin;
29 } sh_keysc_mode[] = {
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;
40 struct clk *clk;
41 DECLARE_BITMAP(last_keys, SH_KEYSC_MAXKEYS);
42 struct input_dev *input;
43 struct sh_keysc_info pdata;
46 #define KYCR1 0
47 #define KYCR2 1
48 #define KYINDR 2
49 #define KYOUTDR 3
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,
60 unsigned long value)
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,
78 const char *str)
80 int k;
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;
97 int i, k, n;
99 dev_dbg(&pdev->dev, "isr!\n");
101 bitmap_fill(keys1, SH_KEYSC_MAXKEYS);
102 bitmap_zero(keys0, SH_KEYSC_MAXKEYS);
104 do {
105 bitmap_zero(keys, SH_KEYSC_MAXKEYS);
106 keyin_set = 0;
108 sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED);
110 for (i = 0; i < keyout_nr; i++) {
111 n = keyin_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++) {
120 if (tmp & (1 << 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];
144 if (!k)
145 continue;
147 if (test_bit(i, keys0) == test_bit(i, priv->last_keys))
148 continue;
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);
163 return IRQ_HANDLED;
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;
172 char clk_name[8];
173 int i;
174 int irq, error;
176 if (!pdev->dev.platform_data) {
177 dev_err(&pdev->dev, "no platform data defined\n");
178 error = -EINVAL;
179 goto err0;
182 error = -ENXIO;
183 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
184 if (res == NULL) {
185 dev_err(&pdev->dev, "failed to get I/O memory\n");
186 goto err0;
189 irq = platform_get_irq(pdev, 0);
190 if (irq < 0) {
191 dev_err(&pdev->dev, "failed to get irq\n");
192 goto err0;
195 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
196 if (priv == NULL) {
197 dev_err(&pdev->dev, "failed to allocate driver data\n");
198 error = -ENOMEM;
199 goto err0;
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");
209 error = -ENXIO;
210 goto err1;
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);
218 goto err2;
221 priv->input = input_allocate_device();
222 if (!priv->input) {
223 dev_err(&pdev->dev, "failed to allocate input device\n");
224 error = -ENOMEM;
225 goto err3;
228 input = priv->input;
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);
245 if (error) {
246 dev_err(&pdev->dev, "failed to request IRQ\n");
247 goto err4;
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);
255 if (error) {
256 dev_err(&pdev->dev, "failed to register input device\n");
257 goto err5;
260 clk_enable(priv->clk);
262 sh_keysc_write(priv, KYCR1, (sh_keysc_mode[pdata->mode].kymd << 8) |
263 pdata->scan_timing);
264 sh_keysc_level_mode(priv, 0);
266 device_init_wakeup(&pdev->dev, 1);
268 return 0;
270 err5:
271 free_irq(irq, pdev);
272 err4:
273 input_free_device(input);
274 err3:
275 clk_put(priv->clk);
276 err2:
277 iounmap(priv->iomem_base);
278 err1:
279 platform_set_drvdata(pdev, NULL);
280 kfree(priv);
281 err0:
282 return error;
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);
296 clk_put(priv->clk);
298 platform_set_drvdata(pdev, NULL);
299 kfree(priv);
301 return 0;
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)) {
314 value |= 0x80;
315 enable_irq_wake(irq);
316 } else {
317 value &= ~0x80;
320 sh_keysc_write(priv, KYCR1, value);
322 return 0;
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);
333 return 0;
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),
344 .driver = {
345 .name = "sh_keysc",
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");