0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/types.h>
0011 #include <linux/delay.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/input.h>
0014 #include <linux/irq.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/jiffies.h>
0017 #include <linux/module.h>
0018 #include <linux/gpio.h>
0019 #include <linux/input/matrix_keypad.h>
0020 #include <linux/slab.h>
0021 #include <linux/of.h>
0022 #include <linux/of_gpio.h>
0023 #include <linux/of_platform.h>
0024
0025 struct matrix_keypad {
0026 const struct matrix_keypad_platform_data *pdata;
0027 struct input_dev *input_dev;
0028 unsigned int row_shift;
0029
0030 DECLARE_BITMAP(disabled_gpios, MATRIX_MAX_ROWS);
0031
0032 uint32_t last_key_state[MATRIX_MAX_COLS];
0033 struct delayed_work work;
0034 spinlock_t lock;
0035 bool scan_pending;
0036 bool stopped;
0037 bool gpio_all_disabled;
0038 };
0039
0040
0041
0042
0043
0044
0045
0046 static void __activate_col(const struct matrix_keypad_platform_data *pdata,
0047 int col, bool on)
0048 {
0049 bool level_on = !pdata->active_low;
0050
0051 if (on) {
0052 gpio_direction_output(pdata->col_gpios[col], level_on);
0053 } else {
0054 gpio_set_value_cansleep(pdata->col_gpios[col], !level_on);
0055 if (!pdata->drive_inactive_cols)
0056 gpio_direction_input(pdata->col_gpios[col]);
0057 }
0058 }
0059
0060 static void activate_col(const struct matrix_keypad_platform_data *pdata,
0061 int col, bool on)
0062 {
0063 __activate_col(pdata, col, on);
0064
0065 if (on && pdata->col_scan_delay_us)
0066 udelay(pdata->col_scan_delay_us);
0067 }
0068
0069 static void activate_all_cols(const struct matrix_keypad_platform_data *pdata,
0070 bool on)
0071 {
0072 int col;
0073
0074 for (col = 0; col < pdata->num_col_gpios; col++)
0075 __activate_col(pdata, col, on);
0076 }
0077
0078 static bool row_asserted(const struct matrix_keypad_platform_data *pdata,
0079 int row)
0080 {
0081 return gpio_get_value_cansleep(pdata->row_gpios[row]) ?
0082 !pdata->active_low : pdata->active_low;
0083 }
0084
0085 static void enable_row_irqs(struct matrix_keypad *keypad)
0086 {
0087 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
0088 int i;
0089
0090 if (pdata->clustered_irq > 0)
0091 enable_irq(pdata->clustered_irq);
0092 else {
0093 for (i = 0; i < pdata->num_row_gpios; i++)
0094 enable_irq(gpio_to_irq(pdata->row_gpios[i]));
0095 }
0096 }
0097
0098 static void disable_row_irqs(struct matrix_keypad *keypad)
0099 {
0100 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
0101 int i;
0102
0103 if (pdata->clustered_irq > 0)
0104 disable_irq_nosync(pdata->clustered_irq);
0105 else {
0106 for (i = 0; i < pdata->num_row_gpios; i++)
0107 disable_irq_nosync(gpio_to_irq(pdata->row_gpios[i]));
0108 }
0109 }
0110
0111
0112
0113
0114 static void matrix_keypad_scan(struct work_struct *work)
0115 {
0116 struct matrix_keypad *keypad =
0117 container_of(work, struct matrix_keypad, work.work);
0118 struct input_dev *input_dev = keypad->input_dev;
0119 const unsigned short *keycodes = input_dev->keycode;
0120 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
0121 uint32_t new_state[MATRIX_MAX_COLS];
0122 int row, col, code;
0123
0124
0125 activate_all_cols(pdata, false);
0126
0127 memset(new_state, 0, sizeof(new_state));
0128
0129
0130 for (col = 0; col < pdata->num_col_gpios; col++) {
0131
0132 activate_col(pdata, col, true);
0133
0134 for (row = 0; row < pdata->num_row_gpios; row++)
0135 new_state[col] |=
0136 row_asserted(pdata, row) ? (1 << row) : 0;
0137
0138 activate_col(pdata, col, false);
0139 }
0140
0141 for (col = 0; col < pdata->num_col_gpios; col++) {
0142 uint32_t bits_changed;
0143
0144 bits_changed = keypad->last_key_state[col] ^ new_state[col];
0145 if (bits_changed == 0)
0146 continue;
0147
0148 for (row = 0; row < pdata->num_row_gpios; row++) {
0149 if ((bits_changed & (1 << row)) == 0)
0150 continue;
0151
0152 code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
0153 input_event(input_dev, EV_MSC, MSC_SCAN, code);
0154 input_report_key(input_dev,
0155 keycodes[code],
0156 new_state[col] & (1 << row));
0157 }
0158 }
0159 input_sync(input_dev);
0160
0161 memcpy(keypad->last_key_state, new_state, sizeof(new_state));
0162
0163 activate_all_cols(pdata, true);
0164
0165
0166 spin_lock_irq(&keypad->lock);
0167 keypad->scan_pending = false;
0168 enable_row_irqs(keypad);
0169 spin_unlock_irq(&keypad->lock);
0170 }
0171
0172 static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
0173 {
0174 struct matrix_keypad *keypad = id;
0175 unsigned long flags;
0176
0177 spin_lock_irqsave(&keypad->lock, flags);
0178
0179
0180
0181
0182
0183
0184 if (unlikely(keypad->scan_pending || keypad->stopped))
0185 goto out;
0186
0187 disable_row_irqs(keypad);
0188 keypad->scan_pending = true;
0189 schedule_delayed_work(&keypad->work,
0190 msecs_to_jiffies(keypad->pdata->debounce_ms));
0191
0192 out:
0193 spin_unlock_irqrestore(&keypad->lock, flags);
0194 return IRQ_HANDLED;
0195 }
0196
0197 static int matrix_keypad_start(struct input_dev *dev)
0198 {
0199 struct matrix_keypad *keypad = input_get_drvdata(dev);
0200
0201 keypad->stopped = false;
0202 mb();
0203
0204
0205
0206
0207
0208 schedule_delayed_work(&keypad->work, 0);
0209
0210 return 0;
0211 }
0212
0213 static void matrix_keypad_stop(struct input_dev *dev)
0214 {
0215 struct matrix_keypad *keypad = input_get_drvdata(dev);
0216
0217 spin_lock_irq(&keypad->lock);
0218 keypad->stopped = true;
0219 spin_unlock_irq(&keypad->lock);
0220
0221 flush_delayed_work(&keypad->work);
0222
0223
0224
0225
0226 disable_row_irqs(keypad);
0227 }
0228
0229 #ifdef CONFIG_PM_SLEEP
0230 static void matrix_keypad_enable_wakeup(struct matrix_keypad *keypad)
0231 {
0232 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
0233 unsigned int gpio;
0234 int i;
0235
0236 if (pdata->clustered_irq > 0) {
0237 if (enable_irq_wake(pdata->clustered_irq) == 0)
0238 keypad->gpio_all_disabled = true;
0239 } else {
0240
0241 for (i = 0; i < pdata->num_row_gpios; i++) {
0242 if (!test_bit(i, keypad->disabled_gpios)) {
0243 gpio = pdata->row_gpios[i];
0244
0245 if (enable_irq_wake(gpio_to_irq(gpio)) == 0)
0246 __set_bit(i, keypad->disabled_gpios);
0247 }
0248 }
0249 }
0250 }
0251
0252 static void matrix_keypad_disable_wakeup(struct matrix_keypad *keypad)
0253 {
0254 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
0255 unsigned int gpio;
0256 int i;
0257
0258 if (pdata->clustered_irq > 0) {
0259 if (keypad->gpio_all_disabled) {
0260 disable_irq_wake(pdata->clustered_irq);
0261 keypad->gpio_all_disabled = false;
0262 }
0263 } else {
0264 for (i = 0; i < pdata->num_row_gpios; i++) {
0265 if (test_and_clear_bit(i, keypad->disabled_gpios)) {
0266 gpio = pdata->row_gpios[i];
0267 disable_irq_wake(gpio_to_irq(gpio));
0268 }
0269 }
0270 }
0271 }
0272
0273 static int matrix_keypad_suspend(struct device *dev)
0274 {
0275 struct platform_device *pdev = to_platform_device(dev);
0276 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
0277
0278 matrix_keypad_stop(keypad->input_dev);
0279
0280 if (device_may_wakeup(&pdev->dev))
0281 matrix_keypad_enable_wakeup(keypad);
0282
0283 return 0;
0284 }
0285
0286 static int matrix_keypad_resume(struct device *dev)
0287 {
0288 struct platform_device *pdev = to_platform_device(dev);
0289 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
0290
0291 if (device_may_wakeup(&pdev->dev))
0292 matrix_keypad_disable_wakeup(keypad);
0293
0294 matrix_keypad_start(keypad->input_dev);
0295
0296 return 0;
0297 }
0298 #endif
0299
0300 static SIMPLE_DEV_PM_OPS(matrix_keypad_pm_ops,
0301 matrix_keypad_suspend, matrix_keypad_resume);
0302
0303 static int matrix_keypad_init_gpio(struct platform_device *pdev,
0304 struct matrix_keypad *keypad)
0305 {
0306 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
0307 int i, err;
0308
0309
0310 for (i = 0; i < pdata->num_col_gpios; i++) {
0311 err = gpio_request(pdata->col_gpios[i], "matrix_kbd_col");
0312 if (err) {
0313 dev_err(&pdev->dev,
0314 "failed to request GPIO%d for COL%d\n",
0315 pdata->col_gpios[i], i);
0316 goto err_free_cols;
0317 }
0318
0319 gpio_direction_output(pdata->col_gpios[i], !pdata->active_low);
0320 }
0321
0322 for (i = 0; i < pdata->num_row_gpios; i++) {
0323 err = gpio_request(pdata->row_gpios[i], "matrix_kbd_row");
0324 if (err) {
0325 dev_err(&pdev->dev,
0326 "failed to request GPIO%d for ROW%d\n",
0327 pdata->row_gpios[i], i);
0328 goto err_free_rows;
0329 }
0330
0331 gpio_direction_input(pdata->row_gpios[i]);
0332 }
0333
0334 if (pdata->clustered_irq > 0) {
0335 err = request_any_context_irq(pdata->clustered_irq,
0336 matrix_keypad_interrupt,
0337 pdata->clustered_irq_flags,
0338 "matrix-keypad", keypad);
0339 if (err < 0) {
0340 dev_err(&pdev->dev,
0341 "Unable to acquire clustered interrupt\n");
0342 goto err_free_rows;
0343 }
0344 } else {
0345 for (i = 0; i < pdata->num_row_gpios; i++) {
0346 err = request_any_context_irq(
0347 gpio_to_irq(pdata->row_gpios[i]),
0348 matrix_keypad_interrupt,
0349 IRQF_TRIGGER_RISING |
0350 IRQF_TRIGGER_FALLING,
0351 "matrix-keypad", keypad);
0352 if (err < 0) {
0353 dev_err(&pdev->dev,
0354 "Unable to acquire interrupt for GPIO line %i\n",
0355 pdata->row_gpios[i]);
0356 goto err_free_irqs;
0357 }
0358 }
0359 }
0360
0361
0362 disable_row_irqs(keypad);
0363 return 0;
0364
0365 err_free_irqs:
0366 while (--i >= 0)
0367 free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
0368 i = pdata->num_row_gpios;
0369 err_free_rows:
0370 while (--i >= 0)
0371 gpio_free(pdata->row_gpios[i]);
0372 i = pdata->num_col_gpios;
0373 err_free_cols:
0374 while (--i >= 0)
0375 gpio_free(pdata->col_gpios[i]);
0376
0377 return err;
0378 }
0379
0380 static void matrix_keypad_free_gpio(struct matrix_keypad *keypad)
0381 {
0382 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
0383 int i;
0384
0385 if (pdata->clustered_irq > 0) {
0386 free_irq(pdata->clustered_irq, keypad);
0387 } else {
0388 for (i = 0; i < pdata->num_row_gpios; i++)
0389 free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
0390 }
0391
0392 for (i = 0; i < pdata->num_row_gpios; i++)
0393 gpio_free(pdata->row_gpios[i]);
0394
0395 for (i = 0; i < pdata->num_col_gpios; i++)
0396 gpio_free(pdata->col_gpios[i]);
0397 }
0398
0399 #ifdef CONFIG_OF
0400 static struct matrix_keypad_platform_data *
0401 matrix_keypad_parse_dt(struct device *dev)
0402 {
0403 struct matrix_keypad_platform_data *pdata;
0404 struct device_node *np = dev->of_node;
0405 unsigned int *gpios;
0406 int ret, i, nrow, ncol;
0407
0408 if (!np) {
0409 dev_err(dev, "device lacks DT data\n");
0410 return ERR_PTR(-ENODEV);
0411 }
0412
0413 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
0414 if (!pdata) {
0415 dev_err(dev, "could not allocate memory for platform data\n");
0416 return ERR_PTR(-ENOMEM);
0417 }
0418
0419 pdata->num_row_gpios = nrow = of_gpio_named_count(np, "row-gpios");
0420 pdata->num_col_gpios = ncol = of_gpio_named_count(np, "col-gpios");
0421 if (nrow <= 0 || ncol <= 0) {
0422 dev_err(dev, "number of keypad rows/columns not specified\n");
0423 return ERR_PTR(-EINVAL);
0424 }
0425
0426 if (of_get_property(np, "linux,no-autorepeat", NULL))
0427 pdata->no_autorepeat = true;
0428
0429 pdata->wakeup = of_property_read_bool(np, "wakeup-source") ||
0430 of_property_read_bool(np, "linux,wakeup");
0431
0432 if (of_get_property(np, "gpio-activelow", NULL))
0433 pdata->active_low = true;
0434
0435 pdata->drive_inactive_cols =
0436 of_property_read_bool(np, "drive-inactive-cols");
0437
0438 of_property_read_u32(np, "debounce-delay-ms", &pdata->debounce_ms);
0439 of_property_read_u32(np, "col-scan-delay-us",
0440 &pdata->col_scan_delay_us);
0441
0442 gpios = devm_kcalloc(dev,
0443 pdata->num_row_gpios + pdata->num_col_gpios,
0444 sizeof(unsigned int),
0445 GFP_KERNEL);
0446 if (!gpios) {
0447 dev_err(dev, "could not allocate memory for gpios\n");
0448 return ERR_PTR(-ENOMEM);
0449 }
0450
0451 for (i = 0; i < nrow; i++) {
0452 ret = of_get_named_gpio(np, "row-gpios", i);
0453 if (ret < 0)
0454 return ERR_PTR(ret);
0455 gpios[i] = ret;
0456 }
0457
0458 for (i = 0; i < ncol; i++) {
0459 ret = of_get_named_gpio(np, "col-gpios", i);
0460 if (ret < 0)
0461 return ERR_PTR(ret);
0462 gpios[nrow + i] = ret;
0463 }
0464
0465 pdata->row_gpios = gpios;
0466 pdata->col_gpios = &gpios[pdata->num_row_gpios];
0467
0468 return pdata;
0469 }
0470 #else
0471 static inline struct matrix_keypad_platform_data *
0472 matrix_keypad_parse_dt(struct device *dev)
0473 {
0474 dev_err(dev, "no platform data defined\n");
0475
0476 return ERR_PTR(-EINVAL);
0477 }
0478 #endif
0479
0480 static int matrix_keypad_probe(struct platform_device *pdev)
0481 {
0482 const struct matrix_keypad_platform_data *pdata;
0483 struct matrix_keypad *keypad;
0484 struct input_dev *input_dev;
0485 int err;
0486
0487 pdata = dev_get_platdata(&pdev->dev);
0488 if (!pdata) {
0489 pdata = matrix_keypad_parse_dt(&pdev->dev);
0490 if (IS_ERR(pdata))
0491 return PTR_ERR(pdata);
0492 } else if (!pdata->keymap_data) {
0493 dev_err(&pdev->dev, "no keymap data defined\n");
0494 return -EINVAL;
0495 }
0496
0497 keypad = kzalloc(sizeof(struct matrix_keypad), GFP_KERNEL);
0498 input_dev = input_allocate_device();
0499 if (!keypad || !input_dev) {
0500 err = -ENOMEM;
0501 goto err_free_mem;
0502 }
0503
0504 keypad->input_dev = input_dev;
0505 keypad->pdata = pdata;
0506 keypad->row_shift = get_count_order(pdata->num_col_gpios);
0507 keypad->stopped = true;
0508 INIT_DELAYED_WORK(&keypad->work, matrix_keypad_scan);
0509 spin_lock_init(&keypad->lock);
0510
0511 input_dev->name = pdev->name;
0512 input_dev->id.bustype = BUS_HOST;
0513 input_dev->dev.parent = &pdev->dev;
0514 input_dev->open = matrix_keypad_start;
0515 input_dev->close = matrix_keypad_stop;
0516
0517 err = matrix_keypad_build_keymap(pdata->keymap_data, NULL,
0518 pdata->num_row_gpios,
0519 pdata->num_col_gpios,
0520 NULL, input_dev);
0521 if (err) {
0522 dev_err(&pdev->dev, "failed to build keymap\n");
0523 goto err_free_mem;
0524 }
0525
0526 if (!pdata->no_autorepeat)
0527 __set_bit(EV_REP, input_dev->evbit);
0528 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
0529 input_set_drvdata(input_dev, keypad);
0530
0531 err = matrix_keypad_init_gpio(pdev, keypad);
0532 if (err)
0533 goto err_free_mem;
0534
0535 err = input_register_device(keypad->input_dev);
0536 if (err)
0537 goto err_free_gpio;
0538
0539 device_init_wakeup(&pdev->dev, pdata->wakeup);
0540 platform_set_drvdata(pdev, keypad);
0541
0542 return 0;
0543
0544 err_free_gpio:
0545 matrix_keypad_free_gpio(keypad);
0546 err_free_mem:
0547 input_free_device(input_dev);
0548 kfree(keypad);
0549 return err;
0550 }
0551
0552 static int matrix_keypad_remove(struct platform_device *pdev)
0553 {
0554 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
0555
0556 matrix_keypad_free_gpio(keypad);
0557 input_unregister_device(keypad->input_dev);
0558 kfree(keypad);
0559
0560 return 0;
0561 }
0562
0563 #ifdef CONFIG_OF
0564 static const struct of_device_id matrix_keypad_dt_match[] = {
0565 { .compatible = "gpio-matrix-keypad" },
0566 { }
0567 };
0568 MODULE_DEVICE_TABLE(of, matrix_keypad_dt_match);
0569 #endif
0570
0571 static struct platform_driver matrix_keypad_driver = {
0572 .probe = matrix_keypad_probe,
0573 .remove = matrix_keypad_remove,
0574 .driver = {
0575 .name = "matrix-keypad",
0576 .pm = &matrix_keypad_pm_ops,
0577 .of_match_table = of_match_ptr(matrix_keypad_dt_match),
0578 },
0579 };
0580 module_platform_driver(matrix_keypad_driver);
0581
0582 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
0583 MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
0584 MODULE_LICENSE("GPL v2");
0585 MODULE_ALIAS("platform:matrix-keypad");