--- /dev/null
+/*
+ * Driver for keys on GPIO lines capable of generating interrupts.
+ *
+ * Copyright 2005 Phil Blundell
+ *
+ * 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/module.h>
+
+#include <linux/init.h>
+#include <linux/fs.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/sched.h>
+#include <linux/pm.h>
+#include <linux/sysctl.h>
+#include <linux/proc_fs.h>
+#include <linux/delay.h>
+#include <linux/platform_device.h>
+#include <linux/input.h>
+#include <linux/adc.h>
+#include <linux/earlysuspend.h>
+#include <asm/gpio.h>
+#include <mach/key.h>
+#include <mach/board.h>
+
+
+#define EMPTY_ADVALUE 950
+#define DRIFT_ADVALUE 70
+#define INVALID_ADVALUE 10
+
+
+#if 0
+#define key_dbg(bdata, format, arg...) \
+ dev_printk(KERN_INFO , &bdata->input->dev , format , ## arg)
+#else
+#define key_dbg(bdata, format, arg...)
+#endif
+
+struct rk29_button_data {
+ int state;
+ int long_press_count;
+ struct rk29_keys_button *button;
+ struct input_dev *input;
+ struct timer_list timer;
+};
+
+struct rk29_keys_drvdata {
+ int nbuttons;
+ int result;
+ struct input_dev *input;
+ struct adc_client *client;
+ struct timer_list timer;
+ struct rk29_button_data data[0];
+};
+
+static struct input_dev *input_dev;
+static struct early_suspend newton_key_power;
+static int suspend = 0;
+void rk29_send_power_key(int state)
+{
+ if (!input_dev)
+ return;
+ if(state)
+ {
+ input_report_key(input_dev, KEY_POWER, 1);
+ input_sync(input_dev);
+ }
+ else
+ {
+ input_report_key(input_dev, KEY_POWER, 0);
+ input_sync(input_dev);
+ }
+}
+
+void rk28_send_wakeup_key(void)
+{
+ if (!input_dev)
+ return;
+
+ input_report_key(input_dev, KEY_WAKEUP, 1);
+ input_sync(input_dev);
+ input_report_key(input_dev, KEY_WAKEUP, 0);
+ input_sync(input_dev);
+}
+
+static void keys_long_press_timer(unsigned long _data)
+{
+ int state;
+ struct rk29_button_data *bdata = (struct rk29_button_data *)_data;
+ struct rk29_keys_button *button = bdata->button;
+ struct input_dev *input = bdata->input;
+ unsigned int type = EV_KEY;
+ if(button->gpio != INVALID_GPIO )
+ state = !!((gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low);
+ else
+ state = !!button->adc_state;
+ if(state) {
+ if(bdata->long_press_count != 0) {
+ if(bdata->long_press_count % (LONG_PRESS_COUNT+ONE_SEC_COUNT) == 0){
+ key_dbg(bdata, "%skey[%s]: report ev[%d] state[0]\n",
+ (button->gpio == INVALID_GPIO)?"ad":"io", button->desc, button->code_long_press);
+ input_event(input, type, button->code_long_press, 0);
+ input_sync(input);
+ }
+ else if(bdata->long_press_count%LONG_PRESS_COUNT == 0) {
+ key_dbg(bdata, "%skey[%s]: report ev[%d] state[1]\n",
+ (button->gpio == INVALID_GPIO)?"ad":"io", button->desc, button->code_long_press);
+ input_event(input, type, button->code_long_press, 1);
+ input_sync(input);
+ }
+ }
+ bdata->long_press_count++;
+ mod_timer(&bdata->timer,
+ jiffies + msecs_to_jiffies(DEFAULT_DEBOUNCE_INTERVAL));
+ }
+ else {
+ if(bdata->long_press_count <= LONG_PRESS_COUNT) {
+ bdata->long_press_count = 0;
+ key_dbg(bdata, "%skey[%s]: report ev[%d] state[1], report ev[%d] state[0]\n",
+ (button->gpio == INVALID_GPIO)?"ad":"io", button->desc, button->code, button->code);
+ input_event(input, type, button->code, 1);
+ input_sync(input);
+ input_event(input, type, button->code, 0);
+ input_sync(input);
+ }
+ else if(bdata->state != state) {
+ key_dbg(bdata, "%skey[%s]: report ev[%d] state[0]\n",
+ (button->gpio == INVALID_GPIO)?"ad":"io", button->desc, button->code_long_press);
+ input_event(input, type, button->code_long_press, 0);
+ input_sync(input);
+ }
+ }
+ bdata->state = state;
+}
+static void keys_timer(unsigned long _data)
+{
+ int state;
+ struct rk29_button_data *bdata = (struct rk29_button_data *)_data;
+ struct rk29_keys_button *button = bdata->button;
+ struct input_dev *input = bdata->input;
+ unsigned int type = EV_KEY;
+
+ if((suspend)&&(!button->wakeup))
+ return;
+ if(button->gpio != INVALID_GPIO)
+ state = !!((gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low);
+ else
+ state = !!button->adc_state;
+ if(bdata->state != state) {
+ bdata->state = state;
+ key_dbg(bdata, "%skey[%s]: report ev[%d] state[%d]\n",
+ (button->gpio == INVALID_GPIO)?"ad":"io", button->desc, button->code, bdata->state);
+ input_event(input, type, button->code, bdata->state);
+ input_sync(input);
+ }
+ if(state)
+ mod_timer(&bdata->timer,
+ jiffies + msecs_to_jiffies(DEFAULT_DEBOUNCE_INTERVAL));
+}
+
+static irqreturn_t keys_isr(int irq, void *dev_id)
+{
+ struct rk29_button_data *bdata = dev_id;
+ struct rk29_keys_button *button = bdata->button;
+ BUG_ON(irq != gpio_to_irq(button->gpio));
+ bdata->long_press_count = 0;
+ mod_timer(&bdata->timer,
+ jiffies + msecs_to_jiffies(DEFAULT_DEBOUNCE_INTERVAL));
+ return IRQ_HANDLED;
+}
+
+static int keys_suspend(struct device *dev)
+{
+ struct platform_device *pdev = to_platform_device(dev);
+ struct rk29_keys_platform_data *pdata = pdev->dev.platform_data;
+ int i;
+
+ if (device_may_wakeup(&pdev->dev)) {
+ for (i = 0; i < pdata->nbuttons; i++) {
+ struct rk29_keys_button *button = &pdata->buttons[i];
+ int irq = gpio_to_irq(button->gpio);
+ enable_irq_wake(irq);
+ }
+ }
+ suspend = 1;
+ return 0;
+}
+
+static int keys_resume(struct device *dev)
+{
+ struct platform_device *pdev = to_platform_device(dev);
+ struct rk29_keys_platform_data *pdata = pdev->dev.platform_data;
+ int i;
+
+ if (device_may_wakeup(&pdev->dev)) {
+ for (i = 0; i < pdata->nbuttons; i++) {
+ struct rk29_keys_button *button = &pdata->buttons[i];
+ if (button->wakeup) {
+ int irq = gpio_to_irq(button->gpio);
+ disable_irq_wake(irq);
+ }
+ }
+ }
+ suspend = 0;
+ return 0;
+}
+
+static void newton_key_early_suspend(struct early_suspend *h)
+{
+ dev_info(&input_dev->dev, "newton_key_early_suspend!\n");
+ keys_suspend(&input_dev->dev);
+}
+
+static void newton_key_last_resume(struct early_suspend *h)
+{
+ dev_info(&input_dev->dev, "newton_key_last_resume!\n");
+ keys_resume(&input_dev->dev);
+}
+
+static int __devinit keys_probe(struct platform_device *pdev)
+{
+ struct rk29_keys_platform_data *pdata = pdev->dev.platform_data;
+ struct rk29_keys_drvdata *ddata;
+ struct input_dev *input;
+ int i, error = 0;
+ int wakeup = 0;
+
+ if(!pdata)
+ return -EINVAL;
+
+ ddata = kzalloc(sizeof(struct rk29_keys_drvdata) +
+ pdata->nbuttons * sizeof(struct rk29_button_data),
+ GFP_KERNEL);
+ input = input_allocate_device();
+ if (!ddata || !input) {
+ error = -ENOMEM;
+ goto fail0;
+ }
+
+ platform_set_drvdata(pdev, ddata);
+
+ input->name = pdev->name;
+ input->phys = "gpio-keys/input0";
+ input->dev.parent = &pdev->dev;
+
+ input->id.bustype = BUS_HOST;
+ input->id.vendor = 0x0001;
+ input->id.product = 0x0001;
+ input->id.version = 0x0100;
+
+ /* Enable auto repeat feature of Linux input subsystem */
+ if (pdata->rep)
+ __set_bit(EV_REP, input->evbit);
+ ddata->nbuttons = pdata->nbuttons;
+ ddata->input = input;
+
+ for (i = 0; i < pdata->nbuttons; i++) {
+ struct rk29_keys_button *button = &pdata->buttons[i];
+ struct rk29_button_data *bdata = &ddata->data[i];
+ int irq;
+ unsigned int type = EV_KEY;
+
+ bdata->input = input;
+ bdata->button = button;
+ if(button->code_long_press)
+ setup_timer(&bdata->timer,
+ keys_long_press_timer, (unsigned long)bdata);
+ else if(button->code)
+ setup_timer(&bdata->timer,
+ keys_timer, (unsigned long)bdata);
+ if(button->gpio != INVALID_GPIO) {
+ error = gpio_request(button->gpio, button->desc ?: "keys");
+ if (error < 0) {
+ pr_err("gpio-keys: failed to request GPIO %d,"
+ " error %d\n", button->gpio, error);
+ goto fail2;
+ }
+
+ error = gpio_direction_input(button->gpio);
+ if (error < 0) {
+ pr_err("gpio-keys: failed to configure input"
+ " direction for GPIO %d, error %d\n",
+ button->gpio, error);
+ gpio_free(button->gpio);
+ goto fail2;
+ }
+ irq = gpio_to_irq(button->gpio);
+ if (irq < 0) {
+ error = irq;
+ pr_err("gpio-keys: Unable to get irq number"
+ " for GPIO %d, error %d\n",
+ button->gpio, error);
+ gpio_free(button->gpio);
+ goto fail2;
+ }
+
+ error = request_irq(irq, keys_isr,
+ (button->active_low)?IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING,
+ button->desc ? button->desc : "keys",
+ bdata);
+ if (error) {
+ pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
+ irq, error);
+ gpio_free(button->gpio);
+ goto fail2;
+ }
+ }
+ if (button->wakeup)
+ wakeup = 1;
+
+ input_set_capability(input, type, button->code);
+ }
+
+ input_set_capability(input, EV_KEY, KEY_WAKEUP);
+
+ error = input_register_device(input);
+ if (error) {
+ pr_err("gpio-keys: Unable to register input device, "
+ "error: %d\n", error);
+ goto fail2;
+ }
+
+ device_init_wakeup(&pdev->dev, wakeup);
+
+ input_dev = input;
+
+ newton_key_power.suspend = newton_key_early_suspend;
+ newton_key_power.resume = newton_key_last_resume;
+ newton_key_power.level = 0x2;
+ register_early_suspend(&newton_key_power);
+ return error;
+
+ fail2:
+ while (--i >= 0) {
+ free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
+ del_timer_sync(&ddata->data[i].timer);
+ gpio_free(pdata->buttons[i].gpio);
+ }
+ platform_set_drvdata(pdev, NULL);
+ fail0:
+ input_free_device(input);
+ kfree(ddata);
+
+ return error;
+}
+
+static int __devexit keys_remove(struct platform_device *pdev)
+{
+ struct rk29_keys_platform_data *pdata = pdev->dev.platform_data;
+ struct rk29_keys_drvdata *ddata = platform_get_drvdata(pdev);
+ struct input_dev *input = ddata->input;
+ int i;
+
+ input_dev = NULL;
+ device_init_wakeup(&pdev->dev, 0);
+
+ for (i = 0; i < pdata->nbuttons; i++) {
+ int irq = gpio_to_irq(pdata->buttons[i].gpio);
+ free_irq(irq, &ddata->data[i]);
+ del_timer_sync(&ddata->data[i].timer);
+ gpio_free(pdata->buttons[i].gpio);
+ }
+ if(pdata->chn >= 0 && ddata->client);
+ adc_unregister(ddata->client);
+ input_unregister_device(input);
+
+ return 0;
+}
+
+
+
+static struct platform_driver keys_device_driver = {
+ .probe = keys_probe,
+ .remove = __devexit_p(keys_remove),
+ .driver = {
+ .name = "rk29-keypad",
+ .owner = THIS_MODULE,
+ }
+};
+
+static int __init keys_init(void)
+{
+ return platform_driver_register(&keys_device_driver);
+}
+
+static void __exit keys_exit(void)
+{
+ platform_driver_unregister(&keys_device_driver);
+}
+
+module_init(keys_init);
+module_exit(keys_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
+MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
+MODULE_ALIAS("platform:gpio-keys");