blob: c155adebf96e4d21d3fdeaab81100a6e9468e3d1 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Magnus Damm795e6bf2008-03-04 15:23:45 -08002/*
3 * SuperH KEYSC Keypad Driver
4 *
5 * Copyright (C) 2008 Magnus Damm
6 *
7 * Based on gpio_keys.c, Copyright 2005 Phil Blundell
Magnus Damm795e6bf2008-03-04 15:23:45 -08008 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
Magnus Damm795e6bf2008-03-04 15:23:45 -080012#include <linux/interrupt.h>
13#include <linux/irq.h>
14#include <linux/delay.h>
15#include <linux/platform_device.h>
16#include <linux/input.h>
Magnus Dammfc1d0032009-11-27 07:32:24 +000017#include <linux/input/sh_keysc.h>
Magnus Damm324e5ade2010-02-10 22:13:21 -080018#include <linux/bitmap.h>
Magnus Dammb6d2a3e2011-04-28 09:32:44 -070019#include <linux/pm_runtime.h>
Magnus Damm795e6bf2008-03-04 15:23:45 -080020#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Magnus Damm795e6bf2008-03-04 15:23:45 -080022
Magnus Damm795e6bf2008-03-04 15:23:45 -080023static const struct {
24 unsigned char kymd, keyout, keyin;
25} sh_keysc_mode[] = {
26 [SH_KEYSC_MODE_1] = { 0, 6, 5 },
27 [SH_KEYSC_MODE_2] = { 1, 5, 6 },
28 [SH_KEYSC_MODE_3] = { 2, 4, 7 },
Magnus Damm3bf12762010-01-21 00:02:36 -080029 [SH_KEYSC_MODE_4] = { 3, 6, 6 },
30 [SH_KEYSC_MODE_5] = { 4, 6, 7 },
Magnus Dammcca23d02011-06-18 02:51:52 -070031 [SH_KEYSC_MODE_6] = { 5, 8, 8 },
Magnus Damm795e6bf2008-03-04 15:23:45 -080032};
33
34struct sh_keysc_priv {
35 void __iomem *iomem_base;
Magnus Damm324e5ade2010-02-10 22:13:21 -080036 DECLARE_BITMAP(last_keys, SH_KEYSC_MAXKEYS);
Magnus Damm795e6bf2008-03-04 15:23:45 -080037 struct input_dev *input;
38 struct sh_keysc_info pdata;
39};
40
Magnus Damm2b14a802010-02-10 22:13:21 -080041#define KYCR1 0
42#define KYCR2 1
43#define KYINDR 2
44#define KYOUTDR 3
45
46#define KYCR2_IRQ_LEVEL 0x10
47#define KYCR2_IRQ_DISABLED 0x00
48
49static unsigned long sh_keysc_read(struct sh_keysc_priv *p, int reg_nr)
50{
51 return ioread16(p->iomem_base + (reg_nr << 2));
52}
53
54static void sh_keysc_write(struct sh_keysc_priv *p, int reg_nr,
55 unsigned long value)
56{
57 iowrite16(value, p->iomem_base + (reg_nr << 2));
58}
59
60static void sh_keysc_level_mode(struct sh_keysc_priv *p,
61 unsigned long keys_set)
62{
63 struct sh_keysc_info *pdata = &p->pdata;
64
65 sh_keysc_write(p, KYOUTDR, 0);
66 sh_keysc_write(p, KYCR2, KYCR2_IRQ_LEVEL | (keys_set << 8));
67
68 if (pdata->kycr2_delay)
69 udelay(pdata->kycr2_delay);
70}
71
Magnus Damm324e5ade2010-02-10 22:13:21 -080072static void sh_keysc_map_dbg(struct device *dev, unsigned long *map,
73 const char *str)
74{
75 int k;
76
77 for (k = 0; k < BITS_TO_LONGS(SH_KEYSC_MAXKEYS); k++)
78 dev_dbg(dev, "%s[%d] 0x%lx\n", str, k, map[k]);
79}
80
Magnus Damm795e6bf2008-03-04 15:23:45 -080081static irqreturn_t sh_keysc_isr(int irq, void *dev_id)
82{
83 struct platform_device *pdev = dev_id;
84 struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
85 struct sh_keysc_info *pdata = &priv->pdata;
Magnus Damm324e5ade2010-02-10 22:13:21 -080086 int keyout_nr = sh_keysc_mode[pdata->mode].keyout;
87 int keyin_nr = sh_keysc_mode[pdata->mode].keyin;
88 DECLARE_BITMAP(keys, SH_KEYSC_MAXKEYS);
89 DECLARE_BITMAP(keys0, SH_KEYSC_MAXKEYS);
90 DECLARE_BITMAP(keys1, SH_KEYSC_MAXKEYS);
Magnus Damm795e6bf2008-03-04 15:23:45 -080091 unsigned char keyin_set, tmp;
Magnus Damm324e5ade2010-02-10 22:13:21 -080092 int i, k, n;
Magnus Damm795e6bf2008-03-04 15:23:45 -080093
94 dev_dbg(&pdev->dev, "isr!\n");
95
Magnus Damm324e5ade2010-02-10 22:13:21 -080096 bitmap_fill(keys1, SH_KEYSC_MAXKEYS);
97 bitmap_zero(keys0, SH_KEYSC_MAXKEYS);
Magnus Damm795e6bf2008-03-04 15:23:45 -080098
99 do {
Magnus Damm324e5ade2010-02-10 22:13:21 -0800100 bitmap_zero(keys, SH_KEYSC_MAXKEYS);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800101 keyin_set = 0;
102
Magnus Damm2b14a802010-02-10 22:13:21 -0800103 sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800104
Magnus Damm324e5ade2010-02-10 22:13:21 -0800105 for (i = 0; i < keyout_nr; i++) {
106 n = keyin_nr * i;
107
108 /* drive one KEYOUT pin low, read KEYIN pins */
Magnus Damm8f8be242010-02-10 23:03:22 -0800109 sh_keysc_write(priv, KYOUTDR, 0xffff ^ (3 << (i * 2)));
Magnus Damm795e6bf2008-03-04 15:23:45 -0800110 udelay(pdata->delay);
Magnus Damm2b14a802010-02-10 22:13:21 -0800111 tmp = sh_keysc_read(priv, KYINDR);
112
Magnus Damm324e5ade2010-02-10 22:13:21 -0800113 /* set bit if key press has been detected */
114 for (k = 0; k < keyin_nr; k++) {
115 if (tmp & (1 << k))
116 __set_bit(n + k, keys);
117 }
118
119 /* keep track of which KEYIN bits that have been set */
120 keyin_set |= tmp ^ ((1 << keyin_nr) - 1);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800121 }
122
Magnus Damm2b14a802010-02-10 22:13:21 -0800123 sh_keysc_level_mode(priv, keyin_set);
Kuninori Morimoto1f85d382009-09-15 00:21:34 +0000124
Magnus Damm324e5ade2010-02-10 22:13:21 -0800125 bitmap_complement(keys, keys, SH_KEYSC_MAXKEYS);
126 bitmap_and(keys1, keys1, keys, SH_KEYSC_MAXKEYS);
127 bitmap_or(keys0, keys0, keys, SH_KEYSC_MAXKEYS);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800128
Magnus Damm324e5ade2010-02-10 22:13:21 -0800129 sh_keysc_map_dbg(&pdev->dev, keys, "keys");
Magnus Damm795e6bf2008-03-04 15:23:45 -0800130
Magnus Damm2b14a802010-02-10 22:13:21 -0800131 } while (sh_keysc_read(priv, KYCR2) & 0x01);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800132
Magnus Damm324e5ade2010-02-10 22:13:21 -0800133 sh_keysc_map_dbg(&pdev->dev, priv->last_keys, "last_keys");
134 sh_keysc_map_dbg(&pdev->dev, keys0, "keys0");
135 sh_keysc_map_dbg(&pdev->dev, keys1, "keys1");
Magnus Damm795e6bf2008-03-04 15:23:45 -0800136
137 for (i = 0; i < SH_KEYSC_MAXKEYS; i++) {
138 k = pdata->keycodes[i];
139 if (!k)
140 continue;
141
Magnus Damm324e5ade2010-02-10 22:13:21 -0800142 if (test_bit(i, keys0) == test_bit(i, priv->last_keys))
Magnus Damm795e6bf2008-03-04 15:23:45 -0800143 continue;
144
Magnus Damm324e5ade2010-02-10 22:13:21 -0800145 if (test_bit(i, keys1) || test_bit(i, keys0)) {
Magnus Damm795e6bf2008-03-04 15:23:45 -0800146 input_event(priv->input, EV_KEY, k, 1);
Magnus Damm324e5ade2010-02-10 22:13:21 -0800147 __set_bit(i, priv->last_keys);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800148 }
149
Magnus Damm324e5ade2010-02-10 22:13:21 -0800150 if (!test_bit(i, keys1)) {
Magnus Damm795e6bf2008-03-04 15:23:45 -0800151 input_event(priv->input, EV_KEY, k, 0);
Magnus Damm324e5ade2010-02-10 22:13:21 -0800152 __clear_bit(i, priv->last_keys);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800153 }
154
155 }
156 input_sync(priv->input);
157
158 return IRQ_HANDLED;
159}
160
Bill Pemberton5298cc42012-11-23 21:38:25 -0800161static int sh_keysc_probe(struct platform_device *pdev)
Magnus Damm795e6bf2008-03-04 15:23:45 -0800162{
163 struct sh_keysc_priv *priv;
164 struct sh_keysc_info *pdata;
165 struct resource *res;
166 struct input_dev *input;
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700167 int i;
Magnus Damm795e6bf2008-03-04 15:23:45 -0800168 int irq, error;
169
Jingoo Hanc838cb32013-12-05 19:21:10 -0800170 if (!dev_get_platdata(&pdev->dev)) {
Magnus Damm795e6bf2008-03-04 15:23:45 -0800171 dev_err(&pdev->dev, "no platform data defined\n");
172 error = -EINVAL;
173 goto err0;
174 }
175
176 error = -ENXIO;
177 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
178 if (res == NULL) {
179 dev_err(&pdev->dev, "failed to get I/O memory\n");
180 goto err0;
181 }
182
183 irq = platform_get_irq(pdev, 0);
Stephen Boyd0bec8b72019-08-14 10:46:38 -0700184 if (irq < 0)
Magnus Damm795e6bf2008-03-04 15:23:45 -0800185 goto err0;
Magnus Damm795e6bf2008-03-04 15:23:45 -0800186
187 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
188 if (priv == NULL) {
189 dev_err(&pdev->dev, "failed to allocate driver data\n");
190 error = -ENOMEM;
191 goto err0;
192 }
193
194 platform_set_drvdata(pdev, priv);
Jingoo Hanc838cb32013-12-05 19:21:10 -0800195 memcpy(&priv->pdata, dev_get_platdata(&pdev->dev), sizeof(priv->pdata));
Magnus Damm795e6bf2008-03-04 15:23:45 -0800196 pdata = &priv->pdata;
197
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100198 priv->iomem_base = ioremap(res->start, resource_size(res));
Magnus Damm795e6bf2008-03-04 15:23:45 -0800199 if (priv->iomem_base == NULL) {
200 dev_err(&pdev->dev, "failed to remap I/O memory\n");
201 error = -ENXIO;
Tetsuya Mukawad3aa43a2008-07-19 07:46:53 +0900202 goto err1;
Magnus Damm795e6bf2008-03-04 15:23:45 -0800203 }
204
205 priv->input = input_allocate_device();
206 if (!priv->input) {
207 dev_err(&pdev->dev, "failed to allocate input device\n");
208 error = -ENOMEM;
Magnus Dammb6d2a3e2011-04-28 09:32:44 -0700209 goto err2;
Magnus Damm795e6bf2008-03-04 15:23:45 -0800210 }
211
212 input = priv->input;
213 input->evbit[0] = BIT_MASK(EV_KEY);
214
215 input->name = pdev->name;
216 input->phys = "sh-keysc-keys/input0";
217 input->dev.parent = &pdev->dev;
218
219 input->id.bustype = BUS_HOST;
220 input->id.vendor = 0x0001;
221 input->id.product = 0x0001;
222 input->id.version = 0x0100;
223
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700224 input->keycode = pdata->keycodes;
225 input->keycodesize = sizeof(pdata->keycodes[0]);
226 input->keycodemax = ARRAY_SIZE(pdata->keycodes);
227
Magnus Damm2dfb9a82011-04-28 09:34:32 -0700228 error = request_threaded_irq(irq, NULL, sh_keysc_isr, IRQF_ONESHOT,
229 dev_name(&pdev->dev), pdev);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800230 if (error) {
231 dev_err(&pdev->dev, "failed to request IRQ\n");
Magnus Dammb6d2a3e2011-04-28 09:32:44 -0700232 goto err3;
Magnus Damm795e6bf2008-03-04 15:23:45 -0800233 }
234
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700235 for (i = 0; i < SH_KEYSC_MAXKEYS; i++)
236 __set_bit(pdata->keycodes[i], input->keybit);
237 __clear_bit(KEY_RESERVED, input->keybit);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800238
239 error = input_register_device(input);
240 if (error) {
241 dev_err(&pdev->dev, "failed to register input device\n");
Magnus Dammb6d2a3e2011-04-28 09:32:44 -0700242 goto err4;
Magnus Damm795e6bf2008-03-04 15:23:45 -0800243 }
244
Magnus Dammb6d2a3e2011-04-28 09:32:44 -0700245 pm_runtime_enable(&pdev->dev);
246 pm_runtime_get_sync(&pdev->dev);
Magnus Damm090d9512008-10-31 20:21:23 +0900247
Magnus Damm2b14a802010-02-10 22:13:21 -0800248 sh_keysc_write(priv, KYCR1, (sh_keysc_mode[pdata->mode].kymd << 8) |
249 pdata->scan_timing);
250 sh_keysc_level_mode(priv, 0);
Magnus Damma29b99e2009-03-10 06:24:21 +0000251
252 device_init_wakeup(&pdev->dev, 1);
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700253
Magnus Damm795e6bf2008-03-04 15:23:45 -0800254 return 0;
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700255
Magnus Damm090d9512008-10-31 20:21:23 +0900256 err4:
Magnus Dammb6d2a3e2011-04-28 09:32:44 -0700257 free_irq(irq, pdev);
Magnus Damm090d9512008-10-31 20:21:23 +0900258 err3:
Magnus Dammb6d2a3e2011-04-28 09:32:44 -0700259 input_free_device(input);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800260 err2:
Tetsuya Mukawad3aa43a2008-07-19 07:46:53 +0900261 iounmap(priv->iomem_base);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800262 err1:
Magnus Damm795e6bf2008-03-04 15:23:45 -0800263 kfree(priv);
264 err0:
265 return error;
266}
267
Bill Pembertone2619cf2012-11-23 21:50:47 -0800268static int sh_keysc_remove(struct platform_device *pdev)
Magnus Damm795e6bf2008-03-04 15:23:45 -0800269{
270 struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800271
Magnus Damm2b14a802010-02-10 22:13:21 -0800272 sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800273
274 input_unregister_device(priv->input);
275 free_irq(platform_get_irq(pdev, 0), pdev);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800276 iounmap(priv->iomem_base);
277
Magnus Dammb6d2a3e2011-04-28 09:32:44 -0700278 pm_runtime_put_sync(&pdev->dev);
279 pm_runtime_disable(&pdev->dev);
Magnus Damm090d9512008-10-31 20:21:23 +0900280
Magnus Damm795e6bf2008-03-04 15:23:45 -0800281 kfree(priv);
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700282
Magnus Damm795e6bf2008-03-04 15:23:45 -0800283 return 0;
284}
285
Dmitry Torokhovcca8edf2011-07-04 06:34:48 -0700286#ifdef CONFIG_PM_SLEEP
Magnus Damma29b99e2009-03-10 06:24:21 +0000287static int sh_keysc_suspend(struct device *dev)
288{
Magnus Damm49976922009-03-11 08:04:23 +0000289 struct platform_device *pdev = to_platform_device(dev);
290 struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
Magnus Damm4ba50df62009-04-01 14:39:20 +0000291 int irq = platform_get_irq(pdev, 0);
Magnus Damma29b99e2009-03-10 06:24:21 +0000292 unsigned short value;
Magnus Damm795e6bf2008-03-04 15:23:45 -0800293
Magnus Damm2b14a802010-02-10 22:13:21 -0800294 value = sh_keysc_read(priv, KYCR1);
Magnus Damma29b99e2009-03-10 06:24:21 +0000295
Magnus Damm4ba50df62009-04-01 14:39:20 +0000296 if (device_may_wakeup(dev)) {
Magnus Dammb6d2a3e2011-04-28 09:32:44 -0700297 sh_keysc_write(priv, KYCR1, value | 0x80);
Magnus Damm4ba50df62009-04-01 14:39:20 +0000298 enable_irq_wake(irq);
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700299 } else {
Magnus Dammb6d2a3e2011-04-28 09:32:44 -0700300 sh_keysc_write(priv, KYCR1, value & ~0x80);
301 pm_runtime_put_sync(dev);
Dmitry Torokhov24d01c02009-07-21 01:12:12 -0700302 }
Magnus Damma29b99e2009-03-10 06:24:21 +0000303
Magnus Damma29b99e2009-03-10 06:24:21 +0000304 return 0;
305}
306
Magnus Damm4ba50df62009-04-01 14:39:20 +0000307static int sh_keysc_resume(struct device *dev)
308{
309 struct platform_device *pdev = to_platform_device(dev);
310 int irq = platform_get_irq(pdev, 0);
311
312 if (device_may_wakeup(dev))
313 disable_irq_wake(irq);
Magnus Dammb6d2a3e2011-04-28 09:32:44 -0700314 else
315 pm_runtime_get_sync(dev);
Magnus Damm4ba50df62009-04-01 14:39:20 +0000316
317 return 0;
318}
Dmitry Torokhov9cc7c802011-05-04 20:54:27 -0700319#endif
Magnus Damm4ba50df62009-04-01 14:39:20 +0000320
Dmitry Torokhov9cc7c802011-05-04 20:54:27 -0700321static SIMPLE_DEV_PM_OPS(sh_keysc_dev_pm_ops,
322 sh_keysc_suspend, sh_keysc_resume);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800323
Dmitry Torokhov12d0cef2011-05-02 23:09:19 -0700324static struct platform_driver sh_keysc_device_driver = {
Magnus Damm795e6bf2008-03-04 15:23:45 -0800325 .probe = sh_keysc_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800326 .remove = sh_keysc_remove,
Magnus Damm795e6bf2008-03-04 15:23:45 -0800327 .driver = {
328 .name = "sh_keysc",
Magnus Damma29b99e2009-03-10 06:24:21 +0000329 .pm = &sh_keysc_dev_pm_ops,
Magnus Damm795e6bf2008-03-04 15:23:45 -0800330 }
331};
JJ Ding5146c842011-11-29 11:08:39 -0800332module_platform_driver(sh_keysc_device_driver);
Magnus Damm795e6bf2008-03-04 15:23:45 -0800333
334MODULE_AUTHOR("Magnus Damm");
335MODULE_DESCRIPTION("SuperH KEYSC Keypad Driver");
336MODULE_LICENSE("GPL");