Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / input / keyboard / sh_keysc.c
blobc155adebf96e4d21d3fdeaab81100a6e9468e3d1
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * SuperH KEYSC Keypad Driver
5 * Copyright (C) 2008 Magnus Damm
7 * Based on gpio_keys.c, Copyright 2005 Phil Blundell
8 */
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #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>
17 #include <linux/input/sh_keysc.h>
18 #include <linux/bitmap.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/io.h>
21 #include <linux/slab.h>
23 static 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 },
29 [SH_KEYSC_MODE_4] = { 3, 6, 6 },
30 [SH_KEYSC_MODE_5] = { 4, 6, 7 },
31 [SH_KEYSC_MODE_6] = { 5, 8, 8 },
34 struct sh_keysc_priv {
35 void __iomem *iomem_base;
36 DECLARE_BITMAP(last_keys, SH_KEYSC_MAXKEYS);
37 struct input_dev *input;
38 struct sh_keysc_info pdata;
41 #define KYCR1 0
42 #define KYCR2 1
43 #define KYINDR 2
44 #define KYOUTDR 3
46 #define KYCR2_IRQ_LEVEL 0x10
47 #define KYCR2_IRQ_DISABLED 0x00
49 static unsigned long sh_keysc_read(struct sh_keysc_priv *p, int reg_nr)
51 return ioread16(p->iomem_base + (reg_nr << 2));
54 static void sh_keysc_write(struct sh_keysc_priv *p, int reg_nr,
55 unsigned long value)
57 iowrite16(value, p->iomem_base + (reg_nr << 2));
60 static void sh_keysc_level_mode(struct sh_keysc_priv *p,
61 unsigned long keys_set)
63 struct sh_keysc_info *pdata = &p->pdata;
65 sh_keysc_write(p, KYOUTDR, 0);
66 sh_keysc_write(p, KYCR2, KYCR2_IRQ_LEVEL | (keys_set << 8));
68 if (pdata->kycr2_delay)
69 udelay(pdata->kycr2_delay);
72 static void sh_keysc_map_dbg(struct device *dev, unsigned long *map,
73 const char *str)
75 int k;
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]);
81 static irqreturn_t sh_keysc_isr(int irq, void *dev_id)
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;
86 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);
91 unsigned char keyin_set, tmp;
92 int i, k, n;
94 dev_dbg(&pdev->dev, "isr!\n");
96 bitmap_fill(keys1, SH_KEYSC_MAXKEYS);
97 bitmap_zero(keys0, SH_KEYSC_MAXKEYS);
99 do {
100 bitmap_zero(keys, SH_KEYSC_MAXKEYS);
101 keyin_set = 0;
103 sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED);
105 for (i = 0; i < keyout_nr; i++) {
106 n = keyin_nr * i;
108 /* drive one KEYOUT pin low, read KEYIN pins */
109 sh_keysc_write(priv, KYOUTDR, 0xffff ^ (3 << (i * 2)));
110 udelay(pdata->delay);
111 tmp = sh_keysc_read(priv, KYINDR);
113 /* 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);
119 /* keep track of which KEYIN bits that have been set */
120 keyin_set |= tmp ^ ((1 << keyin_nr) - 1);
123 sh_keysc_level_mode(priv, keyin_set);
125 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);
129 sh_keysc_map_dbg(&pdev->dev, keys, "keys");
131 } while (sh_keysc_read(priv, KYCR2) & 0x01);
133 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");
137 for (i = 0; i < SH_KEYSC_MAXKEYS; i++) {
138 k = pdata->keycodes[i];
139 if (!k)
140 continue;
142 if (test_bit(i, keys0) == test_bit(i, priv->last_keys))
143 continue;
145 if (test_bit(i, keys1) || test_bit(i, keys0)) {
146 input_event(priv->input, EV_KEY, k, 1);
147 __set_bit(i, priv->last_keys);
150 if (!test_bit(i, keys1)) {
151 input_event(priv->input, EV_KEY, k, 0);
152 __clear_bit(i, priv->last_keys);
156 input_sync(priv->input);
158 return IRQ_HANDLED;
161 static int sh_keysc_probe(struct platform_device *pdev)
163 struct sh_keysc_priv *priv;
164 struct sh_keysc_info *pdata;
165 struct resource *res;
166 struct input_dev *input;
167 int i;
168 int irq, error;
170 if (!dev_get_platdata(&pdev->dev)) {
171 dev_err(&pdev->dev, "no platform data defined\n");
172 error = -EINVAL;
173 goto err0;
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;
183 irq = platform_get_irq(pdev, 0);
184 if (irq < 0)
185 goto err0;
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;
194 platform_set_drvdata(pdev, priv);
195 memcpy(&priv->pdata, dev_get_platdata(&pdev->dev), sizeof(priv->pdata));
196 pdata = &priv->pdata;
198 priv->iomem_base = ioremap(res->start, resource_size(res));
199 if (priv->iomem_base == NULL) {
200 dev_err(&pdev->dev, "failed to remap I/O memory\n");
201 error = -ENXIO;
202 goto err1;
205 priv->input = input_allocate_device();
206 if (!priv->input) {
207 dev_err(&pdev->dev, "failed to allocate input device\n");
208 error = -ENOMEM;
209 goto err2;
212 input = priv->input;
213 input->evbit[0] = BIT_MASK(EV_KEY);
215 input->name = pdev->name;
216 input->phys = "sh-keysc-keys/input0";
217 input->dev.parent = &pdev->dev;
219 input->id.bustype = BUS_HOST;
220 input->id.vendor = 0x0001;
221 input->id.product = 0x0001;
222 input->id.version = 0x0100;
224 input->keycode = pdata->keycodes;
225 input->keycodesize = sizeof(pdata->keycodes[0]);
226 input->keycodemax = ARRAY_SIZE(pdata->keycodes);
228 error = request_threaded_irq(irq, NULL, sh_keysc_isr, IRQF_ONESHOT,
229 dev_name(&pdev->dev), pdev);
230 if (error) {
231 dev_err(&pdev->dev, "failed to request IRQ\n");
232 goto err3;
235 for (i = 0; i < SH_KEYSC_MAXKEYS; i++)
236 __set_bit(pdata->keycodes[i], input->keybit);
237 __clear_bit(KEY_RESERVED, input->keybit);
239 error = input_register_device(input);
240 if (error) {
241 dev_err(&pdev->dev, "failed to register input device\n");
242 goto err4;
245 pm_runtime_enable(&pdev->dev);
246 pm_runtime_get_sync(&pdev->dev);
248 sh_keysc_write(priv, KYCR1, (sh_keysc_mode[pdata->mode].kymd << 8) |
249 pdata->scan_timing);
250 sh_keysc_level_mode(priv, 0);
252 device_init_wakeup(&pdev->dev, 1);
254 return 0;
256 err4:
257 free_irq(irq, pdev);
258 err3:
259 input_free_device(input);
260 err2:
261 iounmap(priv->iomem_base);
262 err1:
263 kfree(priv);
264 err0:
265 return error;
268 static int sh_keysc_remove(struct platform_device *pdev)
270 struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
272 sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED);
274 input_unregister_device(priv->input);
275 free_irq(platform_get_irq(pdev, 0), pdev);
276 iounmap(priv->iomem_base);
278 pm_runtime_put_sync(&pdev->dev);
279 pm_runtime_disable(&pdev->dev);
281 kfree(priv);
283 return 0;
286 #ifdef CONFIG_PM_SLEEP
287 static int sh_keysc_suspend(struct device *dev)
289 struct platform_device *pdev = to_platform_device(dev);
290 struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
291 int irq = platform_get_irq(pdev, 0);
292 unsigned short value;
294 value = sh_keysc_read(priv, KYCR1);
296 if (device_may_wakeup(dev)) {
297 sh_keysc_write(priv, KYCR1, value | 0x80);
298 enable_irq_wake(irq);
299 } else {
300 sh_keysc_write(priv, KYCR1, value & ~0x80);
301 pm_runtime_put_sync(dev);
304 return 0;
307 static int sh_keysc_resume(struct device *dev)
309 struct platform_device *pdev = to_platform_device(dev);
310 int irq = platform_get_irq(pdev, 0);
312 if (device_may_wakeup(dev))
313 disable_irq_wake(irq);
314 else
315 pm_runtime_get_sync(dev);
317 return 0;
319 #endif
321 static SIMPLE_DEV_PM_OPS(sh_keysc_dev_pm_ops,
322 sh_keysc_suspend, sh_keysc_resume);
324 static struct platform_driver sh_keysc_device_driver = {
325 .probe = sh_keysc_probe,
326 .remove = sh_keysc_remove,
327 .driver = {
328 .name = "sh_keysc",
329 .pm = &sh_keysc_dev_pm_ops,
332 module_platform_driver(sh_keysc_device_driver);
334 MODULE_AUTHOR("Magnus Damm");
335 MODULE_DESCRIPTION("SuperH KEYSC Keypad Driver");
336 MODULE_LICENSE("GPL");