--- /dev/null
+#include <mach/key.h>\r
+#include <mach/gpio.h>\r
+\r
+#define EV_ENCALL KEY_F4\r
+#define EV_MENU KEY_F1\r
+\r
+#define PRESS_LEV_LOW 1\r
+#define PRESS_LEV_HIGH 0\r
+\r
+static struct rk29_keys_button key_button[] = {\r
+#ifdef CONFIG_MACH_RK29SDK\r
+ [0] = {\r
+ .desc = "vol+",\r
+ .code = KEY_VOLUMEDOWN,\r
+ .gpio = RK29_PIN0_PB0,\r
+ .active_low = PRESS_LEV_LOW,\r
+ },\r
+ [1] = {\r
+ .desc = "vol-",\r
+ .code = KEY_VOLUMEUP,\r
+ .gpio = RK29_PIN0_PB1,\r
+ .active_low = PRESS_LEV_LOW,\r
+ },\r
+ [2] = {\r
+ .desc = "menu",\r
+ .code = EV_MENU,\r
+ .gpio = RK29_PIN0_PB2,\r
+ .active_low = PRESS_LEV_LOW,\r
+ },\r
+ [3] = {\r
+ .desc = "home",\r
+ .code = KEY_HOME,\r
+ .code_long_press = KEY_F4,\r
+ .gpio = RK29_PIN0_PB3,\r
+ .active_low = PRESS_LEV_LOW,\r
+ },\r
+ [4] = {\r
+ .desc = "back",\r
+ .code = KEY_BACK,\r
+ .gpio = RK29_PIN0_PB4,\r
+ .active_low = PRESS_LEV_LOW,\r
+ },\r
+ [5] = {\r
+ .desc = "esc",\r
+ .code = KEY_ESC,\r
+ .gpio = RK29_PIN0_PB5,\r
+ .active_low = PRESS_LEV_LOW,\r
+ },\r
+#if 0\r
+ [6] = {\r
+ .desc = "vol+",\r
+ .code = KEY_VOLUMEDOWN,\r
+ .adc_value = 95,\r
+ .active_low = PRESS_LEV_LOW,\r
+ },\r
+ [7] = {\r
+ .desc = "vol-",\r
+ .code = KEY_VOLUMEUP,\r
+ .adc_value = 249,\r
+ .active_low = PRESS_LEV_LOW,\r
+ },\r
+ [8] = {\r
+ .desc = "menu",\r
+ .code = EV_MENU,\r
+ .adc_value = 406,\r
+ .active_low = PRESS_LEV_LOW,\r
+ },\r
+ [9] = {\r
+ .desc = "home",\r
+ .code = KEY_HOME,\r
+ .code_long_press = KEY_F4,\r
+ .adc_value = 561,\r
+ .active_low = PRESS_LEV_LOW,\r
+ },\r
+ [10] = {\r
+ .desc = "back",\r
+ .code = KEY_BACK,\r
+ .adc_value = 726,\r
+ .active_low = PRESS_LEV_LOW,\r
+ },\r
+ [11] = {\r
+ .desc = "esc",\r
+ .code = KEY_ESC,\r
+ .adc_value = 899,\r
+ .active_low = PRESS_LEV_LOW,\r
+ },\r
+#endif\r
+#endif\r
+};\r
+struct rk29_keys_platform_data rk29_keys_pdata = {\r
+ .buttons = key_button,\r
+ .nbuttons = ARRAY_SIZE(key_button),\r
+ .chn = -1, //chn: 0-7, if do not use ADC,set 'chn' -1\r
+};\r
+\r
--- /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/workqueue.h>
+#include <linux/adc.h>
+
+#include <asm/gpio.h>
+#include <mach/key.h>
+
+#define EMPTY_ADVALUE 950
+#define DRIFT_ADVALUE 70
+#define INVALID_ADVALUE 10
+
+
+#if 1
+#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 work_struct work;
+};
+
+struct rk29_keys_drvdata {
+ int nbuttons;
+ struct input_dev *input;
+ struct adc_client *client;
+ struct timer_list timer;
+ struct rk29_button_data data[0];
+};
+
+static void keys_do_something(struct work_struct *work)
+{
+ struct rk29_button_data *bdata =
+ container_of(work, struct rk29_button_data, work);
+
+ key_dbg(bdata, "do what?\n");
+ return;
+}
+
+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)
+ 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, "key[%s]: report ev[%d] state[0]\n", 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, "key[%s]: report ev[%d] state[1]\n", 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, "key[%s]: report ev[%d] state[1], report ev[%d] state[0]\n",
+ 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, "key[%s]: report ev[%d] state[0]\n", 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(button->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, "key[%s]: report ev[%d] state[%d]\n", 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));
+ if(button->wakeup)
+ schedule_work(&bdata->work);
+ bdata->long_press_count = 0;
+ mod_timer(&bdata->timer,
+ jiffies + msecs_to_jiffies(DEFAULT_DEBOUNCE_INTERVAL));
+ return IRQ_HANDLED;
+}
+static void callback(struct adc_client *client, void *client_param, int result)
+{
+ struct rk29_keys_drvdata *ddata = (struct rk29_keys_drvdata *)client_param;
+ int i;
+ for (i = 0; i < ddata->nbuttons; i++) {
+ struct rk29_button_data *bdata = &ddata->data[i];
+ struct rk29_keys_button *button = bdata->button;
+ if(!button->adc_value)
+ continue;
+ if(result < button->adc_value + DRIFT_ADVALUE &&
+ result > button->adc_value - DRIFT_ADVALUE)
+ button->adc_state = 1;
+ else
+ button->adc_state = 0;
+
+ }
+ return;
+}
+static void adc_timer(unsigned long _data)
+{
+ struct rk29_keys_drvdata *ddata = (struct rk29_keys_drvdata *)_data;
+
+ adc_async_read(ddata->client);
+ mod_timer(&ddata->timer, jiffies + msecs_to_jiffies(ADC_SAMPLE_TIME));
+}
+
+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;
+ if(pdata->chn >= 0) {
+ ddata->client = adc_register(pdata->chn, callback, (void *)ddata);
+ if(!ddata->client) {
+ error = -EINVAL;
+ goto fail1;
+ }
+ setup_timer(&ddata->timer,
+ adc_timer, (unsigned long)ddata);
+ mod_timer(&ddata->timer, jiffies + msecs_to_jiffies(100));
+ }
+ 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);
+ INIT_WORK(&bdata->work, keys_do_something);
+ if(button->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);
+ }
+
+ 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);
+
+ return 0;
+
+ fail2:
+ while (--i >= 0) {
+ free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
+ del_timer_sync(&ddata->data[i].timer);
+ cancel_work_sync(&ddata->data[i].work);
+ gpio_free(pdata->buttons[i].gpio);
+ }
+ if(pdata->chn >= 0 && ddata->client);
+ adc_unregister(ddata->client);
+ fail1:
+ 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;
+
+ 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);
+ cancel_work_sync(&ddata->data[i].work);
+ gpio_free(pdata->buttons[i].gpio);
+ }
+ if(pdata->chn >= 0 && ddata->client);
+ adc_unregister(ddata->client);
+ input_unregister_device(input);
+
+ return 0;
+}
+
+
+#ifdef CONFIG_PM
+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];
+ if (button->wakeup) {
+ int irq = gpio_to_irq(button->gpio);
+ enable_irq_wake(irq);
+ }
+ }
+ }
+
+ 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);
+ }
+ }
+ }
+
+ return 0;
+}
+
+static const struct dev_pm_ops keys_pm_ops = {
+ .suspend = keys_suspend,
+ .resume = keys_resume,
+};
+#endif
+
+static struct platform_driver keys_device_driver = {
+ .probe = keys_probe,
+ .remove = __devexit_p(keys_remove),
+ .driver = {
+ .name = "gpio-keys",
+ .owner = THIS_MODULE,
+#ifdef CONFIG_PM
+ .pm = &keys_pm_ops,
+#endif
+ }
+};
+
+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");