123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- /*
- * GPIO driven matrix keyboard driver
- *
- * Copyright (c) 2008 Marek Vasut <marek.vasut@gmail.com>
- * Copyright (c) 2012, The Linux Foundation. All rights reserved.
- *
- * Based on corgikbd.c
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- */
- #include <linux/types.h>
- #include <linux/delay.h>
- #include <linux/platform_device.h>
- #include <linux/init.h>
- #include <linux/input.h>
- #include <linux/irq.h>
- #include <linux/interrupt.h>
- #include <linux/jiffies.h>
- #include <linux/module.h>
- #include <linux/gpio.h>
- #include <linux/input/matrix_keypad.h>
- #include <linux/slab.h>
- #ifdef CONFIG_SEC_PATEK_PROJECT
- static int check_key_press;
- #endif
- struct matrix_keypad {
- const struct matrix_keypad_platform_data *pdata;
- struct input_dev *input_dev;
- unsigned short *keycodes;
- unsigned int row_shift;
- DECLARE_BITMAP(disabled_gpios, MATRIX_MAX_ROWS);
- uint32_t last_key_state[MATRIX_MAX_COLS];
- struct delayed_work work;
- struct mutex lock;
- bool scan_pending;
- bool stopped;
- bool gpio_all_disabled;
- };
- /*
- * NOTE: normally the GPIO has to be put into HiZ when de-activated to cause
- * minmal side effect when scanning other columns, here it is configured to
- * be input, and it should work on most platforms.
- */
- static void __activate_col(const struct matrix_keypad_platform_data *pdata,
- int col, bool on)
- {
- bool level_on = !pdata->active_low;
- if (on) {
- gpio_direction_output(pdata->col_gpios[col], level_on);
- } else {
- gpio_set_value_cansleep(pdata->col_gpios[col], !level_on);
- gpio_direction_input(pdata->col_gpios[col]);
- }
- }
- static void activate_col(const struct matrix_keypad_platform_data *pdata,
- int col, bool on)
- {
- __activate_col(pdata, col, on);
- if (on && pdata->col_scan_delay_us)
- udelay(pdata->col_scan_delay_us);
- }
- static void activate_all_cols(const struct matrix_keypad_platform_data *pdata,
- bool on)
- {
- int col;
- for (col = 0; col < pdata->num_col_gpios; col++)
- __activate_col(pdata, col, on);
- }
- static bool row_asserted(const struct matrix_keypad_platform_data *pdata,
- int row)
- {
- return gpio_get_value_cansleep(pdata->row_gpios[row]) ?
- !pdata->active_low : pdata->active_low;
- }
- static void enable_row_irqs(struct matrix_keypad *keypad)
- {
- const struct matrix_keypad_platform_data *pdata = keypad->pdata;
- int i;
- if (pdata->clustered_irq > 0)
- enable_irq(pdata->clustered_irq);
- else {
- for (i = 0; i < pdata->num_row_gpios; i++)
- enable_irq(gpio_to_irq(pdata->row_gpios[i]));
- }
- }
- static void disable_row_irqs(struct matrix_keypad *keypad)
- {
- const struct matrix_keypad_platform_data *pdata = keypad->pdata;
- int i;
- if (pdata->clustered_irq > 0)
- disable_irq_nosync(pdata->clustered_irq);
- else {
- for (i = 0; i < pdata->num_row_gpios; i++)
- disable_irq_nosync(gpio_to_irq(pdata->row_gpios[i]));
- }
- }
- /*
- * This gets the keys from keyboard and reports it to input subsystem
- */
- static void matrix_keypad_scan(struct work_struct *work)
- {
- struct matrix_keypad *keypad =
- container_of(work, struct matrix_keypad, work.work);
- struct input_dev *input_dev = keypad->input_dev;
- const struct matrix_keypad_platform_data *pdata = keypad->pdata;
- uint32_t new_state[MATRIX_MAX_COLS];
- int row, col, code;
- /* de-activate all columns for scanning */
- activate_all_cols(pdata, false);
- memset(new_state, 0, sizeof(new_state));
- /* assert each column and read the row status out */
- for (col = 0; col < pdata->num_col_gpios; col++) {
- activate_col(pdata, col, true);
- for (row = 0; row < pdata->num_row_gpios; row++)
- new_state[col] |=
- row_asserted(pdata, row) ? (1 << row) : 0;
- activate_col(pdata, col, false);
- }
- for (col = 0; col < pdata->num_col_gpios; col++) {
- uint32_t bits_changed;
- bits_changed = keypad->last_key_state[col] ^ new_state[col];
- if (bits_changed == 0)
- continue;
- for (row = 0; row < pdata->num_row_gpios; row++) {
- if ((bits_changed & (1 << row)) == 0)
- continue;
- code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
- input_event(input_dev, EV_MSC, MSC_SCAN, code);
- #ifdef CONFIG_SEC_PATEK_PROJECT
- pr_info("%s: [key] [%d:%d] %x, %s\n",__func__,row,col,
- (new_state[col] & ( 1 << row )),
- !(!(new_state[col] & (1 << row))) ? "pressed" : "released");
- if(!(!(new_state[col] & (1 << row)))){
- check_key_press++;
- }else{
- check_key_press--;
- }
- #endif
- input_report_key(input_dev,
- keypad->keycodes[code],
- new_state[col] & (1 << row));
- }
- }
- input_sync(input_dev);
- memcpy(keypad->last_key_state, new_state, sizeof(new_state));
- activate_all_cols(pdata, true);
- mutex_lock(&keypad->lock);
- keypad->scan_pending = false;
- enable_row_irqs(keypad);
- mutex_unlock(&keypad->lock);
- }
- #ifdef CONFIG_SEC_PATEK_PROJECT
- int check_short_key(void)
- {
- int ret;
- ret = !(!check_key_press);
- return ret;
- }
- EXPORT_SYMBOL(check_short_key);
- #endif
- static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
- {
- struct matrix_keypad *keypad = id;
- mutex_lock(&keypad->lock);
- /*
- * See if another IRQ beaten us to it and scheduled the
- * scan already. In that case we should not try to
- * disable IRQs again.
- */
- if (unlikely(keypad->scan_pending || keypad->stopped))
- goto out;
- disable_row_irqs(keypad);
- keypad->scan_pending = true;
- schedule_delayed_work(&keypad->work,
- msecs_to_jiffies(keypad->pdata->debounce_ms));
- out:
- mutex_unlock(&keypad->lock);
- return IRQ_HANDLED;
- }
- static int matrix_keypad_start(struct input_dev *dev)
- {
- struct matrix_keypad *keypad = input_get_drvdata(dev);
- keypad->stopped = false;
- mb();
- /*
- * Schedule an immediate key scan to capture current key state;
- * columns will be activated and IRQs be enabled after the scan.
- */
- schedule_delayed_work(&keypad->work, 0);
- return 0;
- }
- static void matrix_keypad_stop(struct input_dev *dev)
- {
- struct matrix_keypad *keypad = input_get_drvdata(dev);
- keypad->stopped = true;
- mb();
- flush_work(&keypad->work.work);
- /*
- * matrix_keypad_scan() will leave IRQs enabled;
- * we should disable them now.
- */
- disable_row_irqs(keypad);
- }
- #ifdef CONFIG_PM
- static void matrix_keypad_enable_wakeup(struct matrix_keypad *keypad)
- {
- const struct matrix_keypad_platform_data *pdata = keypad->pdata;
- unsigned int gpio;
- int i;
- if (pdata->clustered_irq > 0) {
- if (enable_irq_wake(pdata->clustered_irq) == 0)
- keypad->gpio_all_disabled = true;
- } else {
- for (i = 0; i < pdata->num_row_gpios; i++) {
- if (!test_bit(i, keypad->disabled_gpios)) {
- gpio = pdata->row_gpios[i];
- if (enable_irq_wake(gpio_to_irq(gpio)) == 0)
- __set_bit(i, keypad->disabled_gpios);
- }
- }
- }
- }
- static void matrix_keypad_disable_wakeup(struct matrix_keypad *keypad)
- {
- const struct matrix_keypad_platform_data *pdata = keypad->pdata;
- unsigned int gpio;
- int i;
- if (pdata->clustered_irq > 0) {
- if (keypad->gpio_all_disabled) {
- disable_irq_wake(pdata->clustered_irq);
- keypad->gpio_all_disabled = false;
- }
- } else {
- for (i = 0; i < pdata->num_row_gpios; i++) {
- if (test_and_clear_bit(i, keypad->disabled_gpios)) {
- gpio = pdata->row_gpios[i];
- disable_irq_wake(gpio_to_irq(gpio));
- }
- }
- }
- }
- static int matrix_keypad_suspend(struct device *dev)
- {
- struct platform_device *pdev = to_platform_device(dev);
- struct matrix_keypad *keypad = platform_get_drvdata(pdev);
- matrix_keypad_stop(keypad->input_dev);
- if (device_may_wakeup(&pdev->dev))
- matrix_keypad_enable_wakeup(keypad);
- return 0;
- }
- static int matrix_keypad_resume(struct device *dev)
- {
- struct platform_device *pdev = to_platform_device(dev);
- struct matrix_keypad *keypad = platform_get_drvdata(pdev);
- if (device_may_wakeup(&pdev->dev))
- matrix_keypad_disable_wakeup(keypad);
- matrix_keypad_start(keypad->input_dev);
- return 0;
- }
- static const SIMPLE_DEV_PM_OPS(matrix_keypad_pm_ops,
- matrix_keypad_suspend, matrix_keypad_resume);
- #endif
- extern int system_rev;
- static int __devinit init_matrix_gpio(struct platform_device *pdev,
- struct matrix_keypad *keypad)
- {
- const struct matrix_keypad_platform_data *pdata = keypad->pdata;
- int i, err = -EINVAL;
- /* initialized strobe lines as outputs, activated */
- for (i = 0; i < pdata->num_col_gpios; i++) {
- err = gpio_request(pdata->col_gpios[i], "matrix_kbd_col");
- if (err) {
- dev_err(&pdev->dev,
- "failed to request GPIO%d for COL%d\n",
- pdata->col_gpios[i], i);
- goto err_free_cols;
- }
- #ifdef CONFIG_SEC_PATEK_PROJECT
- gpio_tlmm_config(GPIO_CFG((pdata->col_gpios[i]), 0,
- GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
- gpio_set_value((pdata->col_gpios[i]), 0);
- #else
- gpio_direction_output(pdata->col_gpios[i], !pdata->active_low);
- #endif
- }
- #ifdef CONFIG_SEC_PATEK_PROJECT
- if (system_rev <= 0x01){
- pdata->row_gpios[3] = 28;
- pr_info("[keypad] KBC(3) : gpio 77 -> 28 (board_rev = %x)\n", system_rev);
- }
- #endif
- for (i = 0; i < pdata->num_row_gpios; i++) {
- err = gpio_request(pdata->row_gpios[i], "matrix_kbd_row");
- if (err) {
- dev_err(&pdev->dev,
- "failed to request GPIO%d for ROW%d\n",
- pdata->row_gpios[i], i);
- goto err_free_rows;
- }
- #ifdef CONFIG_SEC_PATEK_PROJECT
- gpio_tlmm_config(GPIO_CFG((pdata->row_gpios[i]), 0,
- GPIO_CFG_INPUT, GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
- #else
- gpio_direction_input(pdata->row_gpios[i]);
- #endif
- }
- if (pdata->clustered_irq > 0) {
- err = request_irq(pdata->clustered_irq,
- matrix_keypad_interrupt,
- pdata->clustered_irq_flags,
- "matrix-keypad", keypad);
- if (err < 0) {
- dev_err(&pdev->dev,
- "Unable to acquire clustered interrupt\n");
- goto err_free_rows;
- }
- } else {
- for (i = 0; i < pdata->num_row_gpios; i++) {
- err = request_threaded_irq(
- gpio_to_irq(pdata->row_gpios[i]),
- NULL,
- matrix_keypad_interrupt,
- IRQF_DISABLED | IRQF_ONESHOT |
- IRQF_TRIGGER_RISING |
- IRQF_TRIGGER_FALLING,
- "matrix-keypad", keypad);
- if (err < 0) {
- dev_err(&pdev->dev,
- "Unable to acquire interrupt "
- "for GPIO line %i\n",
- pdata->row_gpios[i]);
- goto err_free_irqs;
- }
- }
- }
- /* initialized as disabled - enabled by input->open */
- disable_row_irqs(keypad);
- return 0;
- err_free_irqs:
- while (--i >= 0)
- free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
- i = pdata->num_row_gpios;
- err_free_rows:
- while (--i >= 0)
- gpio_free(pdata->row_gpios[i]);
- i = pdata->num_col_gpios;
- err_free_cols:
- while (--i >= 0)
- gpio_free(pdata->col_gpios[i]);
- return err;
- }
- static int __devinit matrix_keypad_probe(struct platform_device *pdev)
- {
- const struct matrix_keypad_platform_data *pdata;
- const struct matrix_keymap_data *keymap_data;
- struct matrix_keypad *keypad;
- struct input_dev *input_dev;
- unsigned short *keycodes;
- unsigned int row_shift;
- int err;
- pdata = pdev->dev.platform_data;
- if (!pdata) {
- dev_err(&pdev->dev, "no platform data defined\n");
- return -EINVAL;
- }
- keymap_data = pdata->keymap_data;
- if (!keymap_data) {
- dev_err(&pdev->dev, "no keymap data defined\n");
- return -EINVAL;
- }
- row_shift = get_count_order(pdata->num_col_gpios);
- keypad = kzalloc(sizeof(struct matrix_keypad), GFP_KERNEL);
- keycodes = kzalloc((pdata->num_row_gpios << row_shift) *
- sizeof(*keycodes),
- GFP_KERNEL);
- input_dev = input_allocate_device();
- if (!keypad || !keycodes || !input_dev) {
- err = -ENOMEM;
- goto err_free_mem;
- }
- keypad->input_dev = input_dev;
- keypad->pdata = pdata;
- keypad->keycodes = keycodes;
- keypad->row_shift = row_shift;
- keypad->stopped = true;
- INIT_DELAYED_WORK(&keypad->work, matrix_keypad_scan);
- mutex_init(&keypad->lock);
- input_dev->name = pdev->name;
- input_dev->id.bustype = BUS_HOST;
- input_dev->dev.parent = &pdev->dev;
- input_dev->evbit[0] = BIT_MASK(EV_KEY);
- if (!pdata->no_autorepeat)
- input_dev->evbit[0] |= BIT_MASK(EV_REP);
- input_dev->open = matrix_keypad_start;
- input_dev->close = matrix_keypad_stop;
- input_dev->keycode = keycodes;
- input_dev->keycodesize = sizeof(*keycodes);
- input_dev->keycodemax = pdata->num_row_gpios << row_shift;
- matrix_keypad_build_keymap(keymap_data, row_shift,
- input_dev->keycode, input_dev->keybit);
- input_set_capability(input_dev, EV_MSC, MSC_SCAN);
- input_set_drvdata(input_dev, keypad);
- err = init_matrix_gpio(pdev, keypad);
- if (err)
- goto err_free_mem;
- err = input_register_device(keypad->input_dev);
- if (err)
- goto err_free_mem;
- device_init_wakeup(&pdev->dev, pdata->wakeup);
- platform_set_drvdata(pdev, keypad);
- return 0;
- err_free_mem:
- input_free_device(input_dev);
- kfree(keycodes);
- kfree(keypad);
- return err;
- }
- static int __devexit matrix_keypad_remove(struct platform_device *pdev)
- {
- struct matrix_keypad *keypad = platform_get_drvdata(pdev);
- const struct matrix_keypad_platform_data *pdata = keypad->pdata;
- int i;
- device_init_wakeup(&pdev->dev, 0);
- if (pdata->clustered_irq > 0) {
- free_irq(pdata->clustered_irq, keypad);
- } else {
- for (i = 0; i < pdata->num_row_gpios; i++)
- free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
- }
- for (i = 0; i < pdata->num_row_gpios; i++)
- gpio_free(pdata->row_gpios[i]);
- for (i = 0; i < pdata->num_col_gpios; i++)
- gpio_free(pdata->col_gpios[i]);
- mutex_destroy(&keypad->lock);
- input_unregister_device(keypad->input_dev);
- platform_set_drvdata(pdev, NULL);
- kfree(keypad->keycodes);
- kfree(keypad);
- return 0;
- }
- static struct platform_driver matrix_keypad_driver = {
- .probe = matrix_keypad_probe,
- .remove = __devexit_p(matrix_keypad_remove),
- .driver = {
- #ifdef CONFIG_SEC_PATEK_PROJECT
- .name = "patek_3x4_keypad",
- #else
- .name = "matrix-keypad",
- #endif
- .owner = THIS_MODULE,
- #ifdef CONFIG_PM
- .pm = &matrix_keypad_pm_ops,
- #endif
- },
- };
- module_platform_driver(matrix_keypad_driver);
- MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
- MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
- MODULE_LICENSE("GPL v2");
- MODULE_ALIAS("platform:matrix-keypad");
|