2 * Driver for buttons on GPIO lines not capable of generating interrupts
4 * Copyright (C) 2007-2010 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2010 Nuno Goncalves <nunojpg@gmail.com>
7 * This file was based on: /drivers/input/misc/cobalt_btns.c
8 * Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
10 * also was based on: /drivers/input/keyboard/gpio_keys.c
11 * Copyright 2005 Phil Blundell
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/input.h>
23 #include <linux/input-polldev.h>
24 #include <linux/ioport.h>
25 #include <linux/gpio.h>
26 #include <linux/gpio_keys.h>
27 #include <linux/platform_device.h>
28 #include <mach/hardware.h>
30 #define DRV_NAME "n2100-copy-button"
32 struct gpio_keys_polled_device {
33 struct input_polled_dev *poll_dev;
41 static struct gpio_keys_polled_device bdev;
43 static void gpio_keys_polled_check_state(struct input_dev *input,
44 struct gpio_keys_polled_device *bdata)
47 state = !!gpio_get_value(bdata->gpio);
49 if (state != bdata->last_state) {
50 input_event(input, EV_KEY, KEY_COPY,
54 bdata->last_state = state;
58 static void gpio_keys_polled_poll(struct input_polled_dev *dev)
60 struct gpio_keys_polled_device *bdev = dev->private;
61 struct input_dev *input = dev->input;
63 gpio_keys_polled_check_state(input, bdev);
66 static int __devinit gpio_keys_polled_init(void)
68 struct input_polled_dev *poll_dev;
69 struct input_dev *input;
71 unsigned int gpio = N2100_COPY_BUTTON;
73 poll_dev = input_allocate_polled_device();
75 pr_err("no memory for polled device\n");
80 poll_dev->private = &bdev;
81 poll_dev->poll = gpio_keys_polled_poll;
82 poll_dev->poll_interval = 100;
84 input = poll_dev->input;
86 input->evbit[0] = BIT(EV_KEY);
87 input->name = "copy-button";
88 input->phys = DRV_NAME"/input0";
89 // input->dev.parent = &pdev->dev;
91 input->id.bustype = BUS_HOST;
92 input->id.vendor = 0x0001;
93 input->id.product = 0x0001;
94 input->id.version = 0x0100;
96 error = gpio_request(gpio,
99 pr_err("unable to claim gpio %u, err=%d\n",
104 error = gpio_direction_input(gpio);
106 pr_err("unable to set direction on gpio %u, err=%d\n",
111 bdev.last_state = -1;
114 input_set_capability(input, EV_KEY, KEY_COPY);
116 bdev.poll_dev = poll_dev;
118 error = input_register_polled_device(poll_dev);
120 pr_err("unable to register polled device, err=%d\n",
125 /* report initial state of the buttons */
126 gpio_keys_polled_check_state(input, &bdev);
131 gpio_free(N2100_COPY_BUTTON);
132 input_free_polled_device(poll_dev);
138 static void __devexit gpio_keys_polled_exit(void)
140 input_unregister_polled_device(bdev.poll_dev);
141 gpio_free(N2100_COPY_BUTTON);
142 input_free_polled_device(bdev.poll_dev);
145 module_init(gpio_keys_polled_init);
146 module_exit(gpio_keys_polled_exit);
148 MODULE_LICENSE("GPL v2");
149 MODULE_AUTHOR("Roman Bazalevskiy <rvb@rvb.name>, Gabor Juhos <juhosg@openwrt.org>");
150 MODULE_DESCRIPTION("Polled GPIO Copy Buttons driver for Thecus N2100");
151 MODULE_ALIAS("platform:" DRV_NAME);