1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * OMAP4 Keypad Driver 4 * 5 * Copyright (C) 2010 Texas Instruments 6 * 7 * Author: Abraham Arce <x0066660@ti.com> 8 * Initial Code: Syed Rafiuddin <rafiuddin.syed@ti.com> 9 */ 10 11 #include <linux/module.h> 12 #include <linux/interrupt.h> 13 #include <linux/platform_device.h> 14 #include <linux/clk.h> 15 #include <linux/errno.h> 16 #include <linux/io.h> 17 #include <linux/of.h> 18 #include <linux/input.h> 19 #include <linux/input/matrix_keypad.h> 20 #include <linux/slab.h> 21 #include <linux/pm_runtime.h> 22 #include <linux/pm_wakeirq.h> 23 24 /* OMAP4 registers */ 25 #define OMAP4_KBD_REVISION 0x00 26 #define OMAP4_KBD_SYSCONFIG 0x10 27 #define OMAP4_KBD_SYSSTATUS 0x14 28 #define OMAP4_KBD_IRQSTATUS 0x18 29 #define OMAP4_KBD_IRQENABLE 0x1C 30 #define OMAP4_KBD_WAKEUPENABLE 0x20 31 #define OMAP4_KBD_PENDING 0x24 32 #define OMAP4_KBD_CTRL 0x28 33 #define OMAP4_KBD_DEBOUNCINGTIME 0x2C 34 #define OMAP4_KBD_LONGKEYTIME 0x30 35 #define OMAP4_KBD_TIMEOUT 0x34 36 #define OMAP4_KBD_STATEMACHINE 0x38 37 #define OMAP4_KBD_ROWINPUTS 0x3C 38 #define OMAP4_KBD_COLUMNOUTPUTS 0x40 39 #define OMAP4_KBD_FULLCODE31_0 0x44 40 #define OMAP4_KBD_FULLCODE63_32 0x48 41 42 /* OMAP4 bit definitions */ 43 #define OMAP4_DEF_IRQENABLE_EVENTEN BIT(0) 44 #define OMAP4_DEF_IRQENABLE_LONGKEY BIT(1) 45 #define OMAP4_DEF_WUP_EVENT_ENA BIT(0) 46 #define OMAP4_DEF_WUP_LONG_KEY_ENA BIT(1) 47 #define OMAP4_DEF_CTRL_NOSOFTMODE BIT(1) 48 #define OMAP4_DEF_CTRL_PTV_SHIFT 2 49 50 /* OMAP4 values */ 51 #define OMAP4_VAL_IRQDISABLE 0x0 52 53 /* 54 * Errata i689: If a key is released for a time shorter than debounce time, 55 * the keyboard will idle and never detect the key release. The workaround 56 * is to use at least a 12ms debounce time. See omap5432 TRM chapter 57 * "26.4.6.2 Keyboard Controller Timer" for more information. 58 */ 59 #define OMAP4_KEYPAD_PTV_DIV_128 0x6 60 #define OMAP4_KEYPAD_DEBOUNCINGTIME_MS(dbms, ptv) \ 61 ((((dbms) * 1000) / ((1 << ((ptv) + 1)) * (1000000 / 32768))) - 1) 62 #define OMAP4_VAL_DEBOUNCINGTIME_16MS \ 63 OMAP4_KEYPAD_DEBOUNCINGTIME_MS(16, OMAP4_KEYPAD_PTV_DIV_128) 64 #define OMAP4_KEYPAD_AUTOIDLE_MS 50 /* Approximate measured time */ 65 #define OMAP4_KEYPAD_IDLE_CHECK_MS (OMAP4_KEYPAD_AUTOIDLE_MS / 2) 66 67 enum { 68 KBD_REVISION_OMAP4 = 0, 69 KBD_REVISION_OMAP5, 70 }; 71 72 struct omap4_keypad { 73 struct input_dev *input; 74 75 void __iomem *base; 76 unsigned int irq; 77 struct mutex lock; /* for key scan */ 78 79 unsigned int rows; 80 unsigned int cols; 81 u32 reg_offset; 82 u32 irqreg_offset; 83 unsigned int row_shift; 84 bool no_autorepeat; 85 u64 keys; 86 unsigned short *keymap; 87 struct clk *fck; 88 }; 89 90 static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset) 91 { 92 return __raw_readl(keypad_data->base + 93 keypad_data->reg_offset + offset); 94 } 95 96 static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value) 97 { 98 __raw_writel(value, 99 keypad_data->base + keypad_data->reg_offset + offset); 100 } 101 102 static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset) 103 { 104 return __raw_readl(keypad_data->base + 105 keypad_data->irqreg_offset + offset); 106 } 107 108 static void kbd_write_irqreg(struct omap4_keypad *keypad_data, 109 u32 offset, u32 value) 110 { 111 __raw_writel(value, 112 keypad_data->base + keypad_data->irqreg_offset + offset); 113 } 114 115 static int omap4_keypad_report_keys(struct omap4_keypad *keypad_data, 116 u64 keys, bool down) 117 { 118 struct input_dev *input_dev = keypad_data->input; 119 unsigned int col, row, code; 120 DECLARE_BITMAP(mask, 64); 121 unsigned long bit; 122 int events = 0; 123 124 bitmap_from_u64(mask, keys); 125 126 for_each_set_bit(bit, mask, keypad_data->rows * BITS_PER_BYTE) { 127 row = bit / BITS_PER_BYTE; 128 col = bit % BITS_PER_BYTE; 129 code = MATRIX_SCAN_CODE(row, col, keypad_data->row_shift); 130 131 input_event(input_dev, EV_MSC, MSC_SCAN, code); 132 input_report_key(input_dev, keypad_data->keymap[code], down); 133 134 events++; 135 } 136 137 if (events) 138 input_sync(input_dev); 139 140 return events; 141 } 142 143 static void omap4_keypad_scan_keys(struct omap4_keypad *keypad_data, u64 keys) 144 { 145 u64 changed; 146 147 guard(mutex)(&keypad_data->lock); 148 149 changed = keys ^ keypad_data->keys; 150 151 /* 152 * Report key up events separately and first. This matters in case we 153 * lost key-up interrupt and just now catching up. 154 */ 155 omap4_keypad_report_keys(keypad_data, changed & ~keys, false); 156 157 /* Report key down events */ 158 omap4_keypad_report_keys(keypad_data, changed & keys, true); 159 160 keypad_data->keys = keys; 161 } 162 163 /* Interrupt handlers */ 164 static irqreturn_t omap4_keypad_irq_handler(int irq, void *dev_id) 165 { 166 struct omap4_keypad *keypad_data = dev_id; 167 168 if (kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)) 169 return IRQ_WAKE_THREAD; 170 171 return IRQ_NONE; 172 } 173 174 static irqreturn_t omap4_keypad_irq_thread_fn(int irq, void *dev_id) 175 { 176 struct omap4_keypad *keypad_data = dev_id; 177 struct device *dev = keypad_data->input->dev.parent; 178 u32 low, high; 179 int error; 180 u64 keys; 181 182 error = pm_runtime_resume_and_get(dev); 183 if (error) 184 return IRQ_NONE; 185 186 low = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE31_0); 187 high = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE63_32); 188 keys = low | (u64)high << 32; 189 190 omap4_keypad_scan_keys(keypad_data, keys); 191 192 /* clear pending interrupts */ 193 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS, 194 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)); 195 196 pm_runtime_mark_last_busy(dev); 197 pm_runtime_put_autosuspend(dev); 198 199 return IRQ_HANDLED; 200 } 201 202 static int omap4_keypad_open(struct input_dev *input) 203 { 204 struct omap4_keypad *keypad_data = input_get_drvdata(input); 205 struct device *dev = input->dev.parent; 206 int error; 207 208 error = pm_runtime_resume_and_get(dev); 209 if (error) 210 return error; 211 212 error = clk_prepare_enable(keypad_data->fck); 213 if (error) 214 goto out; 215 216 disable_irq(keypad_data->irq); 217 218 kbd_writel(keypad_data, OMAP4_KBD_CTRL, 219 OMAP4_DEF_CTRL_NOSOFTMODE | 220 (OMAP4_KEYPAD_PTV_DIV_128 << OMAP4_DEF_CTRL_PTV_SHIFT)); 221 kbd_writel(keypad_data, OMAP4_KBD_DEBOUNCINGTIME, 222 OMAP4_VAL_DEBOUNCINGTIME_16MS); 223 /* clear pending interrupts */ 224 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS, 225 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)); 226 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE, 227 OMAP4_DEF_IRQENABLE_EVENTEN); 228 kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE, 229 OMAP4_DEF_WUP_EVENT_ENA); 230 231 enable_irq(keypad_data->irq); 232 233 out: 234 pm_runtime_mark_last_busy(dev); 235 pm_runtime_put_autosuspend(dev); 236 237 return error; 238 } 239 240 static void omap4_keypad_stop(struct omap4_keypad *keypad_data) 241 { 242 /* Disable interrupts and wake-up events */ 243 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE, 244 OMAP4_VAL_IRQDISABLE); 245 kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE, 0); 246 247 /* clear pending interrupts */ 248 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS, 249 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)); 250 } 251 252 static void omap4_keypad_close(struct input_dev *input) 253 { 254 struct omap4_keypad *keypad_data = input_get_drvdata(input); 255 struct device *dev = input->dev.parent; 256 int error; 257 258 error = pm_runtime_resume_and_get(dev); 259 if (error) 260 dev_err(dev, "%s: pm_runtime_resume_and_get() failed: %d\n", 261 __func__, error); 262 263 disable_irq(keypad_data->irq); 264 omap4_keypad_stop(keypad_data); 265 enable_irq(keypad_data->irq); 266 clk_disable_unprepare(keypad_data->fck); 267 268 pm_runtime_mark_last_busy(dev); 269 pm_runtime_put_autosuspend(dev); 270 } 271 272 static int omap4_keypad_parse_dt(struct device *dev, 273 struct omap4_keypad *keypad_data) 274 { 275 struct device_node *np = dev->of_node; 276 int err; 277 278 err = matrix_keypad_parse_properties(dev, &keypad_data->rows, 279 &keypad_data->cols); 280 if (err) 281 return err; 282 283 keypad_data->no_autorepeat = of_property_read_bool(np, "linux,input-no-autorepeat"); 284 285 return 0; 286 } 287 288 static int omap4_keypad_check_revision(struct device *dev, 289 struct omap4_keypad *keypad_data) 290 { 291 unsigned int rev; 292 293 rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION); 294 rev &= 0x03 << 30; 295 rev >>= 30; 296 switch (rev) { 297 case KBD_REVISION_OMAP4: 298 keypad_data->reg_offset = 0x00; 299 keypad_data->irqreg_offset = 0x00; 300 break; 301 case KBD_REVISION_OMAP5: 302 keypad_data->reg_offset = 0x10; 303 keypad_data->irqreg_offset = 0x0c; 304 break; 305 default: 306 dev_err(dev, "Keypad reports unsupported revision %d", rev); 307 return -EINVAL; 308 } 309 310 return 0; 311 } 312 313 /* 314 * Errata ID i689 "1.32 Keyboard Key Up Event Can Be Missed". 315 * Interrupt may not happen for key-up events. We must clear stuck 316 * key-up events after the keyboard hardware has auto-idled. 317 */ 318 static int omap4_keypad_runtime_suspend(struct device *dev) 319 { 320 struct platform_device *pdev = to_platform_device(dev); 321 struct omap4_keypad *keypad_data = platform_get_drvdata(pdev); 322 u32 active; 323 324 active = kbd_readl(keypad_data, OMAP4_KBD_STATEMACHINE); 325 if (active) { 326 pm_runtime_mark_last_busy(dev); 327 return -EBUSY; 328 } 329 330 omap4_keypad_scan_keys(keypad_data, 0); 331 332 return 0; 333 } 334 335 static const struct dev_pm_ops omap4_keypad_pm_ops = { 336 RUNTIME_PM_OPS(omap4_keypad_runtime_suspend, NULL, NULL) 337 }; 338 339 static void omap4_disable_pm(void *d) 340 { 341 pm_runtime_dont_use_autosuspend(d); 342 pm_runtime_disable(d); 343 } 344 345 static int omap4_keypad_probe(struct platform_device *pdev) 346 { 347 struct device *dev = &pdev->dev; 348 struct omap4_keypad *keypad_data; 349 struct input_dev *input_dev; 350 unsigned int max_keys; 351 int irq; 352 int error; 353 354 irq = platform_get_irq(pdev, 0); 355 if (irq < 0) 356 return irq; 357 358 keypad_data = devm_kzalloc(dev, sizeof(*keypad_data), GFP_KERNEL); 359 if (!keypad_data) { 360 dev_err(dev, "keypad_data memory allocation failed\n"); 361 return -ENOMEM; 362 } 363 364 keypad_data->irq = irq; 365 keypad_data->fck = devm_clk_get(&pdev->dev, "fck"); 366 if (IS_ERR(keypad_data->fck)) 367 return dev_err_probe(&pdev->dev, PTR_ERR(keypad_data->fck), 368 "unable to get fck"); 369 370 mutex_init(&keypad_data->lock); 371 platform_set_drvdata(pdev, keypad_data); 372 373 error = omap4_keypad_parse_dt(dev, keypad_data); 374 if (error) 375 return error; 376 377 keypad_data->base = devm_platform_ioremap_resource(pdev, 0); 378 if (IS_ERR(keypad_data->base)) 379 return PTR_ERR(keypad_data->base); 380 381 pm_runtime_use_autosuspend(dev); 382 pm_runtime_set_autosuspend_delay(dev, OMAP4_KEYPAD_IDLE_CHECK_MS); 383 pm_runtime_enable(dev); 384 385 error = devm_add_action_or_reset(dev, omap4_disable_pm, dev); 386 if (error) { 387 dev_err(dev, "unable to register cleanup action\n"); 388 return error; 389 } 390 391 /* 392 * Enable clocks for the keypad module so that we can read 393 * revision register. 394 */ 395 error = pm_runtime_resume_and_get(dev); 396 if (error) { 397 dev_err(dev, "pm_runtime_resume_and_get() failed\n"); 398 return error; 399 } 400 401 error = omap4_keypad_check_revision(dev, keypad_data); 402 if (!error) { 403 /* Ensure device does not raise interrupts */ 404 omap4_keypad_stop(keypad_data); 405 } 406 407 pm_runtime_mark_last_busy(dev); 408 pm_runtime_put_autosuspend(dev); 409 if (error) 410 return error; 411 412 /* input device allocation */ 413 keypad_data->input = input_dev = devm_input_allocate_device(dev); 414 if (!input_dev) 415 return -ENOMEM; 416 417 input_dev->name = pdev->name; 418 input_dev->id.bustype = BUS_HOST; 419 input_dev->id.vendor = 0x0001; 420 input_dev->id.product = 0x0001; 421 input_dev->id.version = 0x0001; 422 423 input_dev->open = omap4_keypad_open; 424 input_dev->close = omap4_keypad_close; 425 426 input_set_capability(input_dev, EV_MSC, MSC_SCAN); 427 if (!keypad_data->no_autorepeat) 428 __set_bit(EV_REP, input_dev->evbit); 429 430 input_set_drvdata(input_dev, keypad_data); 431 432 keypad_data->row_shift = get_count_order(keypad_data->cols); 433 max_keys = keypad_data->rows << keypad_data->row_shift; 434 keypad_data->keymap = devm_kcalloc(dev, 435 max_keys, 436 sizeof(keypad_data->keymap[0]), 437 GFP_KERNEL); 438 if (!keypad_data->keymap) { 439 dev_err(dev, "Not enough memory for keymap\n"); 440 return -ENOMEM; 441 } 442 443 error = matrix_keypad_build_keymap(NULL, NULL, 444 keypad_data->rows, keypad_data->cols, 445 keypad_data->keymap, input_dev); 446 if (error) { 447 dev_err(dev, "failed to build keymap\n"); 448 return error; 449 } 450 451 error = devm_request_threaded_irq(dev, keypad_data->irq, 452 omap4_keypad_irq_handler, 453 omap4_keypad_irq_thread_fn, 454 IRQF_ONESHOT, 455 "omap4-keypad", keypad_data); 456 if (error) { 457 dev_err(dev, "failed to register interrupt\n"); 458 return error; 459 } 460 461 error = input_register_device(keypad_data->input); 462 if (error) { 463 dev_err(dev, "failed to register input device\n"); 464 return error; 465 } 466 467 device_init_wakeup(dev, true); 468 error = dev_pm_set_wake_irq(dev, keypad_data->irq); 469 if (error) 470 dev_warn(dev, "failed to set up wakeup irq: %d\n", error); 471 472 return 0; 473 } 474 475 static void omap4_keypad_remove(struct platform_device *pdev) 476 { 477 dev_pm_clear_wake_irq(&pdev->dev); 478 } 479 480 static const struct of_device_id omap_keypad_dt_match[] = { 481 { .compatible = "ti,omap4-keypad" }, 482 {}, 483 }; 484 MODULE_DEVICE_TABLE(of, omap_keypad_dt_match); 485 486 static struct platform_driver omap4_keypad_driver = { 487 .probe = omap4_keypad_probe, 488 .remove = omap4_keypad_remove, 489 .driver = { 490 .name = "omap4-keypad", 491 .of_match_table = omap_keypad_dt_match, 492 .pm = pm_ptr(&omap4_keypad_pm_ops), 493 }, 494 }; 495 module_platform_driver(omap4_keypad_driver); 496 497 MODULE_AUTHOR("Texas Instruments"); 498 MODULE_DESCRIPTION("OMAP4 Keypad Driver"); 499 MODULE_LICENSE("GPL"); 500 MODULE_ALIAS("platform:omap4-keypad"); 501