blob: 8e9380bfed4097fcdc283cfc7c3532ed0e911318 [file] [log] [blame]
Magnus Damm795e6bf2008-03-04 15:23:45 -08001/*
2 * SuperH KEYSC Keypad Driver
3 *
4 * Copyright (C) 2008 Magnus Damm
5 *
6 * Based on gpio_keys.c, Copyright 2005 Phil Blundell
7 *
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.
11 */
12
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>
Magnus Dammfc1d0032009-11-27 07:32:24 +000021#include <linux/input/sh_keysc.h>
Magnus Damm090d9512008-10-31 20:21:23 +090022#include <linux/clk.h>
Magnus Damm795e6bf2008-03-04 15:23:45 -080023#include <linux/io.h>
Magnus Damm795e6bf2008-03-04 15:23:45 -080024
25#define KYCR1_OFFS 0x00
26#define KYCR2_OFFS 0x04
27#define KYINDR_OFFS 0x08
28#define KYOUTDR_OFFS 0x0c
29
30#define KYCR2_IRQ_LEVEL 0x10
31#define KYCR2_IRQ_DISABLED 0x00
32
33static const struct {
34 unsigned char kymd, keyout, keyin;
35} sh_keysc_mode[] = {
36 [SH_KEYSC_MODE_1] = { 0, 6, 5 },
37 [SH_KEYSC_MODE_2] = { 1, 5, 6 },
38 [SH_KEYSC_MODE_3] = { 2, 4, 7 },
39};
40
41struct sh_keysc_priv {
42 void __iomem *iomem_base;
Magnus Damm090d9512008-10-31 20:21:23 +090043 struct clk *clk;
Magnus Damm795e6bf2008-03-04 15:23:45 -080044 unsigned long last_keys;
45 struct input_dev *input;
46 struct sh_keysc_info pdata;
47};
48
49static irqreturn_t sh_keysc_isr(int irq, void *dev_id)
50{
51 struct platform_device *pdev = dev_id;
52 struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
53 struct sh_keysc_info *pdata = &priv->pdata;
54 unsigned long keys, keys1, keys0, mask;
55 unsigned char keyin_set, tmp;
56 int i, k;
57
58 dev_dbg(&pdev->dev, "isr!\n");
59
60 keys1 = ~0;
61 keys0 = 0;
62
63 do {
64 keys = 0;
65 keyin_set = 0;
66
67 iowrite16(KYCR2_IRQ_DISABLED, priv->iomem_base + KYCR2_OFFS);
68
69 for (i = 0; i < sh_keysc_mode[pdata->mode].keyout; i++) {
70 iowrite16(0xfff ^ (3 << (i * 2)),
71 priv->iomem_base + KYOUTDR_OFFS);
72 udelay(pdata->delay);
73 tmp = ioread16(priv->iomem_base + KYINDR_OFFS);
74 keys |= tmp << (sh_keysc_mode[pdata->mode].keyin * i);
75 tmp ^= (1 << sh_keysc_mode[pdata->mode].keyin) - 1;
76 keyin_set |= tmp;
77 }
78
79 iowrite16(0, priv->iomem_base + KYOUTDR_OFFS);
80 iowrite16(KYCR2_IRQ_LEVEL | (keyin_set << 8),
81 priv->iomem_base + KYCR2_OFFS);
82
Kuninori Morimoto1f85d382009-09-15 00:21:34 +000083 if (pdata->kycr2_delay)
84 udelay(pdata->kycr2_delay);
85
Magnus Damm795e6bf2008-03-04 15:23:45 -080086 keys ^= ~0;
87 keys &= (1 << (sh_keysc_mode[pdata->mode].keyin *
88 sh_keysc_mode[pdata->mode].keyout)) - 1;
89 keys1 &= keys;
90 keys0 |= keys;
91
92 dev_dbg(&pdev->dev, "keys 0x%08lx\n", keys);
93
94 } while (ioread16(priv->iomem_base + KYCR2_OFFS) & 0x01);
95
96 dev_dbg(&pdev->dev, "last_keys 0x%08lx keys0 0x%08lx keys1 0x%08lx\n",
97 priv->last_keys, keys0, keys1);
98
99 for (i = 0; i < SH_KEYSC_MAXKEYS; i++) {
100 k = pdata->keycodes[i];
101 if (!k)
102 continue;
103
104 mask = 1 << i;
105
106 if (!((priv->last_keys ^ keys0) & mask))
107 continue;
108
109 if ((keys1 | keys0) & mask) {
110 input_event(priv->input, EV_KEY, k, 1);
111 priv->last_keys |= mask;
112 }
113
114 if (!(keys1 & mask)) {
115 input_event(priv->input, EV_KEY, k, 0);
116 priv->last_keys &= ~mask;
117 }
118
119 }
120 input_sync(priv->input);
121
122 return IRQ_HANDLED;
123}
124
125#define res_size(res) ((res)->end - (res)->start + 1)
126
127static int __devinit sh_keysc_probe(struct platform_device *pdev)
128{
129 struct sh_keysc_priv *priv;
130 struct sh_keysc_info *pdata;
131 struct resource *res;
132 struct input_dev *input;
Magnus Damm090d9512008-10-31 20:21:23 +0900133 char clk_name[8];
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700134 int i;
Magnus Damm795e6bf2008-03-04 15:23:45 -0800135 int irq, error;
136
137 if (!pdev->dev.platform_data) {
138 dev_err(&pdev->dev, "no platform data defined\n");
139 error = -EINVAL;
140 goto err0;
141 }
142
143 error = -ENXIO;
144 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
145 if (res == NULL) {
146 dev_err(&pdev->dev, "failed to get I/O memory\n");
147 goto err0;
148 }
149
150 irq = platform_get_irq(pdev, 0);
151 if (irq < 0) {
152 dev_err(&pdev->dev, "failed to get irq\n");
153 goto err0;
154 }
155
156 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
157 if (priv == NULL) {
158 dev_err(&pdev->dev, "failed to allocate driver data\n");
159 error = -ENOMEM;
160 goto err0;
161 }
162
163 platform_set_drvdata(pdev, priv);
164 memcpy(&priv->pdata, pdev->dev.platform_data, sizeof(priv->pdata));
165 pdata = &priv->pdata;
166
Magnus Damm795e6bf2008-03-04 15:23:45 -0800167 priv->iomem_base = ioremap_nocache(res->start, res_size(res));
168 if (priv->iomem_base == NULL) {
169 dev_err(&pdev->dev, "failed to remap I/O memory\n");
170 error = -ENXIO;
Tetsuya Mukawad3aa43a2008-07-19 07:46:53 +0900171 goto err1;
Magnus Damm795e6bf2008-03-04 15:23:45 -0800172 }
173
Magnus Damm090d9512008-10-31 20:21:23 +0900174 snprintf(clk_name, sizeof(clk_name), "keysc%d", pdev->id);
175 priv->clk = clk_get(&pdev->dev, clk_name);
176 if (IS_ERR(priv->clk)) {
177 dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
178 error = PTR_ERR(priv->clk);
179 goto err2;
180 }
181
Magnus Damm795e6bf2008-03-04 15:23:45 -0800182 priv->input = input_allocate_device();
183 if (!priv->input) {
184 dev_err(&pdev->dev, "failed to allocate input device\n");
185 error = -ENOMEM;
Magnus Damm090d9512008-10-31 20:21:23 +0900186 goto err3;
Magnus Damm795e6bf2008-03-04 15:23:45 -0800187 }
188
189 input = priv->input;
190 input->evbit[0] = BIT_MASK(EV_KEY);
191
192 input->name = pdev->name;
193 input->phys = "sh-keysc-keys/input0";
194 input->dev.parent = &pdev->dev;
195
196 input->id.bustype = BUS_HOST;
197 input->id.vendor = 0x0001;
198 input->id.product = 0x0001;
199 input->id.version = 0x0100;
200
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700201 input->keycode = pdata->keycodes;
202 input->keycodesize = sizeof(pdata->keycodes[0]);
203 input->keycodemax = ARRAY_SIZE(pdata->keycodes);
204
Magnus Damm795e6bf2008-03-04 15:23:45 -0800205 error = request_irq(irq, sh_keysc_isr, 0, pdev->name, pdev);
206 if (error) {
207 dev_err(&pdev->dev, "failed to request IRQ\n");
Magnus Damm090d9512008-10-31 20:21:23 +0900208 goto err4;
Magnus Damm795e6bf2008-03-04 15:23:45 -0800209 }
210
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700211 for (i = 0; i < SH_KEYSC_MAXKEYS; i++)
212 __set_bit(pdata->keycodes[i], input->keybit);
213 __clear_bit(KEY_RESERVED, input->keybit);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800214
215 error = input_register_device(input);
216 if (error) {
217 dev_err(&pdev->dev, "failed to register input device\n");
Magnus Damm090d9512008-10-31 20:21:23 +0900218 goto err5;
Magnus Damm795e6bf2008-03-04 15:23:45 -0800219 }
220
Magnus Damm090d9512008-10-31 20:21:23 +0900221 clk_enable(priv->clk);
222
Magnus Damm795e6bf2008-03-04 15:23:45 -0800223 iowrite16((sh_keysc_mode[pdata->mode].kymd << 8) |
224 pdata->scan_timing, priv->iomem_base + KYCR1_OFFS);
225 iowrite16(0, priv->iomem_base + KYOUTDR_OFFS);
226 iowrite16(KYCR2_IRQ_LEVEL, priv->iomem_base + KYCR2_OFFS);
Magnus Damma29b99e2009-03-10 06:24:21 +0000227
228 device_init_wakeup(&pdev->dev, 1);
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700229
Magnus Damm795e6bf2008-03-04 15:23:45 -0800230 return 0;
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700231
Magnus Damm090d9512008-10-31 20:21:23 +0900232 err5:
Tetsuya Mukawad3aa43a2008-07-19 07:46:53 +0900233 free_irq(irq, pdev);
Magnus Damm090d9512008-10-31 20:21:23 +0900234 err4:
Tetsuya Mukawad3aa43a2008-07-19 07:46:53 +0900235 input_free_device(input);
Magnus Damm090d9512008-10-31 20:21:23 +0900236 err3:
237 clk_put(priv->clk);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800238 err2:
Tetsuya Mukawad3aa43a2008-07-19 07:46:53 +0900239 iounmap(priv->iomem_base);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800240 err1:
241 platform_set_drvdata(pdev, NULL);
242 kfree(priv);
243 err0:
244 return error;
245}
246
247static int __devexit sh_keysc_remove(struct platform_device *pdev)
248{
249 struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800250
251 iowrite16(KYCR2_IRQ_DISABLED, priv->iomem_base + KYCR2_OFFS);
252
253 input_unregister_device(priv->input);
254 free_irq(platform_get_irq(pdev, 0), pdev);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800255 iounmap(priv->iomem_base);
256
Magnus Damm090d9512008-10-31 20:21:23 +0900257 clk_disable(priv->clk);
258 clk_put(priv->clk);
259
Magnus Damm795e6bf2008-03-04 15:23:45 -0800260 platform_set_drvdata(pdev, NULL);
261 kfree(priv);
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700262
Magnus Damm795e6bf2008-03-04 15:23:45 -0800263 return 0;
264}
265
Magnus Damma29b99e2009-03-10 06:24:21 +0000266static int sh_keysc_suspend(struct device *dev)
267{
Magnus Damm49976922009-03-11 08:04:23 +0000268 struct platform_device *pdev = to_platform_device(dev);
269 struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm4ba50df2009-04-01 14:39:20 +0000270 int irq = platform_get_irq(pdev, 0);
Magnus Damma29b99e2009-03-10 06:24:21 +0000271 unsigned short value;
Magnus Damm795e6bf2008-03-04 15:23:45 -0800272
Magnus Damma29b99e2009-03-10 06:24:21 +0000273 value = ioread16(priv->iomem_base + KYCR1_OFFS);
274
Magnus Damm4ba50df2009-04-01 14:39:20 +0000275 if (device_may_wakeup(dev)) {
Magnus Damma29b99e2009-03-10 06:24:21 +0000276 value |= 0x80;
Magnus Damm4ba50df2009-04-01 14:39:20 +0000277 enable_irq_wake(irq);
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700278 } else {
Magnus Damma29b99e2009-03-10 06:24:21 +0000279 value &= ~0x80;
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700280 }
Magnus Damma29b99e2009-03-10 06:24:21 +0000281
282 iowrite16(value, priv->iomem_base + KYCR1_OFFS);
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700283
Magnus Damma29b99e2009-03-10 06:24:21 +0000284 return 0;
285}
286
Magnus Damm4ba50df2009-04-01 14:39:20 +0000287static int sh_keysc_resume(struct device *dev)
288{
289 struct platform_device *pdev = to_platform_device(dev);
290 int irq = platform_get_irq(pdev, 0);
291
292 if (device_may_wakeup(dev))
293 disable_irq_wake(irq);
294
295 return 0;
296}
297
Alexey Dobriyan47145212009-12-14 18:00:08 -0800298static const struct dev_pm_ops sh_keysc_dev_pm_ops = {
Magnus Damma29b99e2009-03-10 06:24:21 +0000299 .suspend = sh_keysc_suspend,
Magnus Damm4ba50df2009-04-01 14:39:20 +0000300 .resume = sh_keysc_resume,
Magnus Damma29b99e2009-03-10 06:24:21 +0000301};
Magnus Damm795e6bf2008-03-04 15:23:45 -0800302
303struct platform_driver sh_keysc_device_driver = {
304 .probe = sh_keysc_probe,
305 .remove = __devexit_p(sh_keysc_remove),
Magnus Damm795e6bf2008-03-04 15:23:45 -0800306 .driver = {
307 .name = "sh_keysc",
Magnus Damma29b99e2009-03-10 06:24:21 +0000308 .pm = &sh_keysc_dev_pm_ops,
Magnus Damm795e6bf2008-03-04 15:23:45 -0800309 }
310};
311
312static int __init sh_keysc_init(void)
313{
314 return platform_driver_register(&sh_keysc_device_driver);
315}
316
317static void __exit sh_keysc_exit(void)
318{
319 platform_driver_unregister(&sh_keysc_device_driver);
320}
321
322module_init(sh_keysc_init);
323module_exit(sh_keysc_exit);
324
325MODULE_AUTHOR("Magnus Damm");
326MODULE_DESCRIPTION("SuperH KEYSC Keypad Driver");
327MODULE_LICENSE("GPL");