1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * SuperH KEYSC Keypad Driver 4 * 5 * Copyright (C) 2008 Magnus Damm 6 * 7 * Based on gpio_keys.c, Copyright 2005 Phil Blundell 8 */ 9 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> 22 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 }, 32 }; 33 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; 39 }; 40 41 #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 49 static 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 54 static 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 60 static 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 72 static 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 81 static 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; 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; 93 94 dev_dbg(&pdev->dev, "isr!\n"); 95 96 bitmap_fill(keys1, SH_KEYSC_MAXKEYS); 97 bitmap_zero(keys0, SH_KEYSC_MAXKEYS); 98 99 do { 100 bitmap_zero(keys, SH_KEYSC_MAXKEYS); 101 keyin_set = 0; 102 103 sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED); 104 105 for (i = 0; i < keyout_nr; i++) { 106 n = keyin_nr * i; 107 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); 112 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); 117 } 118 119 /* keep track of which KEYIN bits that have been set */ 120 keyin_set |= tmp ^ ((1 << keyin_nr) - 1); 121 } 122 123 sh_keysc_level_mode(priv, keyin_set); 124 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); 128 129 sh_keysc_map_dbg(&pdev->dev, keys, "keys"); 130 131 } while (sh_keysc_read(priv, KYCR2) & 0x01); 132 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"); 136 137 for (i = 0; i < SH_KEYSC_MAXKEYS; i++) { 138 k = pdata->keycodes[i]; 139 if (!k) 140 continue; 141 142 if (test_bit(i, keys0) == test_bit(i, priv->last_keys)) 143 continue; 144 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); 148 } 149 150 if (!test_bit(i, keys1)) { 151 input_event(priv->input, EV_KEY, k, 0); 152 __clear_bit(i, priv->last_keys); 153 } 154 155 } 156 input_sync(priv->input); 157 158 return IRQ_HANDLED; 159 } 160 161 static int sh_keysc_probe(struct platform_device *pdev) 162 { 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; 169 170 if (!dev_get_platdata(&pdev->dev)) { 171 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); 184 if (irq < 0) { 185 dev_err(&pdev->dev, "failed to get irq\n"); 186 goto err0; 187 } 188 189 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 190 if (priv == NULL) { 191 dev_err(&pdev->dev, "failed to allocate driver data\n"); 192 error = -ENOMEM; 193 goto err0; 194 } 195 196 platform_set_drvdata(pdev, priv); 197 memcpy(&priv->pdata, dev_get_platdata(&pdev->dev), sizeof(priv->pdata)); 198 pdata = &priv->pdata; 199 200 priv->iomem_base = ioremap_nocache(res->start, resource_size(res)); 201 if (priv->iomem_base == NULL) { 202 dev_err(&pdev->dev, "failed to remap I/O memory\n"); 203 error = -ENXIO; 204 goto err1; 205 } 206 207 priv->input = input_allocate_device(); 208 if (!priv->input) { 209 dev_err(&pdev->dev, "failed to allocate input device\n"); 210 error = -ENOMEM; 211 goto err2; 212 } 213 214 input = priv->input; 215 input->evbit[0] = BIT_MASK(EV_KEY); 216 217 input->name = pdev->name; 218 input->phys = "sh-keysc-keys/input0"; 219 input->dev.parent = &pdev->dev; 220 221 input->id.bustype = BUS_HOST; 222 input->id.vendor = 0x0001; 223 input->id.product = 0x0001; 224 input->id.version = 0x0100; 225 226 input->keycode = pdata->keycodes; 227 input->keycodesize = sizeof(pdata->keycodes[0]); 228 input->keycodemax = ARRAY_SIZE(pdata->keycodes); 229 230 error = request_threaded_irq(irq, NULL, sh_keysc_isr, IRQF_ONESHOT, 231 dev_name(&pdev->dev), pdev); 232 if (error) { 233 dev_err(&pdev->dev, "failed to request IRQ\n"); 234 goto err3; 235 } 236 237 for (i = 0; i < SH_KEYSC_MAXKEYS; i++) 238 __set_bit(pdata->keycodes[i], input->keybit); 239 __clear_bit(KEY_RESERVED, input->keybit); 240 241 error = input_register_device(input); 242 if (error) { 243 dev_err(&pdev->dev, "failed to register input device\n"); 244 goto err4; 245 } 246 247 pm_runtime_enable(&pdev->dev); 248 pm_runtime_get_sync(&pdev->dev); 249 250 sh_keysc_write(priv, KYCR1, (sh_keysc_mode[pdata->mode].kymd << 8) | 251 pdata->scan_timing); 252 sh_keysc_level_mode(priv, 0); 253 254 device_init_wakeup(&pdev->dev, 1); 255 256 return 0; 257 258 err4: 259 free_irq(irq, pdev); 260 err3: 261 input_free_device(input); 262 err2: 263 iounmap(priv->iomem_base); 264 err1: 265 kfree(priv); 266 err0: 267 return error; 268 } 269 270 static int sh_keysc_remove(struct platform_device *pdev) 271 { 272 struct sh_keysc_priv *priv = platform_get_drvdata(pdev); 273 274 sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED); 275 276 input_unregister_device(priv->input); 277 free_irq(platform_get_irq(pdev, 0), pdev); 278 iounmap(priv->iomem_base); 279 280 pm_runtime_put_sync(&pdev->dev); 281 pm_runtime_disable(&pdev->dev); 282 283 kfree(priv); 284 285 return 0; 286 } 287 288 #ifdef CONFIG_PM_SLEEP 289 static int sh_keysc_suspend(struct device *dev) 290 { 291 struct platform_device *pdev = to_platform_device(dev); 292 struct sh_keysc_priv *priv = platform_get_drvdata(pdev); 293 int irq = platform_get_irq(pdev, 0); 294 unsigned short value; 295 296 value = sh_keysc_read(priv, KYCR1); 297 298 if (device_may_wakeup(dev)) { 299 sh_keysc_write(priv, KYCR1, value | 0x80); 300 enable_irq_wake(irq); 301 } else { 302 sh_keysc_write(priv, KYCR1, value & ~0x80); 303 pm_runtime_put_sync(dev); 304 } 305 306 return 0; 307 } 308 309 static int sh_keysc_resume(struct device *dev) 310 { 311 struct platform_device *pdev = to_platform_device(dev); 312 int irq = platform_get_irq(pdev, 0); 313 314 if (device_may_wakeup(dev)) 315 disable_irq_wake(irq); 316 else 317 pm_runtime_get_sync(dev); 318 319 return 0; 320 } 321 #endif 322 323 static SIMPLE_DEV_PM_OPS(sh_keysc_dev_pm_ops, 324 sh_keysc_suspend, sh_keysc_resume); 325 326 static struct platform_driver sh_keysc_device_driver = { 327 .probe = sh_keysc_probe, 328 .remove = sh_keysc_remove, 329 .driver = { 330 .name = "sh_keysc", 331 .pm = &sh_keysc_dev_pm_ops, 332 } 333 }; 334 module_platform_driver(sh_keysc_device_driver); 335 336 MODULE_AUTHOR("Magnus Damm"); 337 MODULE_DESCRIPTION("SuperH KEYSC Keypad Driver"); 338 MODULE_LICENSE("GPL"); 339