summaryrefslogtreecommitdiffstats
path: root/drivers/input/gpio_keys.c
blob: 5b03fd76cb36dcdf361c3264b35cdb7b18b990aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright (C) 2011 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
 *
 * Under GPLv2
 */

#include <common.h>
#include <errno.h>
#include <init.h>
#include <clock.h>
#include <gpio_keys.h>
#include <poller.h>
#include <gpio.h>
#include <of_gpio.h>
#include <input/keyboard.h>

struct gpio_key {
	int code;

	int gpio;
	int active_low;

	int previous_state;

	int debounce_interval;
	u64 debounce_start;
};

struct gpio_keys {
	struct gpio_key *buttons;
	int nbuttons;

	/* optional */
	int fifo_size;

	struct kfifo *recv_fifo;
	struct poller_struct poller;
	struct console_device cdev;

	int use_keycodes;
};

static inline struct gpio_keys *
poller_to_gk_pdata(struct poller_struct *poller)
{
	return container_of(poller, struct gpio_keys, poller);
}

static inline struct gpio_keys *
cdev_to_gk_pdata(struct console_device *cdev)
{
	return container_of(cdev, struct gpio_keys, cdev);
}

static void gpio_key_poller(struct poller_struct *poller)
{
	struct gpio_keys *gk = poller_to_gk_pdata(poller);
	struct gpio_key *gb;
	int i, val;

	for (i = 0; i < gk->nbuttons; i++) {

		gb = &gk->buttons[i];
		val = gpio_get_value(gb->gpio);

		if (!is_timeout(gb->debounce_start, gb->debounce_interval * MSECOND))
			continue;

		if (val != gb->previous_state) {
			gb->debounce_start = get_time_ns();
			if (val != gb->active_low) {
				kfifo_put(gk->recv_fifo, (u_char*)&gb->code, sizeof(int));
				debug("pressed gpio(%d) as %d\n", gb->gpio, gb->code);
			}
			gb->previous_state = val;
		}
	}
}

static int gpio_keys_tstc(struct console_device *cdev)
{
	struct gpio_keys *gk = cdev_to_gk_pdata(cdev);

	return (kfifo_len(gk->recv_fifo) == 0) ? 0 : 1;
}

static int gpio_keys_getc(struct console_device *cdev)
{
	int code = 0;
	struct gpio_keys *gk = cdev_to_gk_pdata(cdev);

	kfifo_get(gk->recv_fifo, (u_char*)&code, sizeof(int));

	if (IS_ENABLED(CONFIG_OFDEVICE) && gk->use_keycodes)
		return keycode_bb_keys[code];
	else
		return code;
}

static int gpio_keys_probe_pdata(struct gpio_keys *gk, struct device_d *dev)
{
	struct gpio_keys_platform_data *pdata;
	int i;

	pdata = dev->platform_data;

	if (!pdata) {
		/* small (so we copy it) but critical! */
		dev_err(dev, "missing platform_data\n");
		return -ENODEV;
	}

	if (pdata->fifo_size)
		gk->fifo_size = pdata->fifo_size;

	gk->buttons = xzalloc(pdata->nbuttons * sizeof(*gk->buttons));
	gk->nbuttons = pdata->nbuttons;

	for (i = 0; i < gk->nbuttons; i++) {
		gk->buttons[i].gpio = pdata->buttons[i].gpio;
		gk->buttons[i].code = pdata->buttons[i].code;
		gk->buttons[i].active_low = pdata->buttons[i].active_low;
		gk->buttons[i].debounce_interval = 20;
	}

	return 0;
}

static int gpio_keys_probe_dt(struct gpio_keys *gk, struct device_d *dev)
{
	struct device_node *npkey, *np = dev->device_node;
	int i = 0, ret;

	if (!IS_ENABLED(CONFIG_OFDEVICE) || !IS_ENABLED(CONFIG_OF_GPIO))
		return -ENODEV;

	gk->nbuttons = of_get_child_count(np);
	gk->buttons = xzalloc(gk->nbuttons * sizeof(*gk->buttons));

	for_each_child_of_node(np, npkey) {
		enum of_gpio_flags gpioflags;
		uint32_t keycode;

		gk->buttons[i].gpio = of_get_named_gpio_flags(npkey, "gpios", 0, &gpioflags);
		if (gk->buttons[i].gpio < 0)
			return gk->buttons[i].gpio;

		if (gpioflags & OF_GPIO_ACTIVE_LOW)
			gk->buttons[i].active_low = 1;

		ret = of_property_read_u32(npkey, "linux,code", &keycode);
		if (ret)
			return ret;

		gk->buttons[i].debounce_interval = 20;

		of_property_read_u32(npkey, "debounce-interval",
				     &gk->buttons[i].debounce_interval);

		gk->buttons[i].code = keycode;

		i++;
	}

	gk->use_keycodes = 1;

	return 0;
}

static int __init gpio_keys_probe(struct device_d *dev)
{
	int ret, i, gpio;
	struct console_device *cdev;
	struct gpio_keys *gk;

	gk = xzalloc(sizeof(*gk));
	gk->fifo_size = 50;

	if (dev->device_node)
		ret = gpio_keys_probe_dt(gk, dev);
	else
		ret = gpio_keys_probe_pdata(gk, dev);

	if (ret)
		return ret;

	gk->recv_fifo = kfifo_alloc(gk->fifo_size);

	for (i = 0; i < gk->nbuttons; i++) {
		gpio = gk->buttons[i].gpio;
		ret = gpio_request(gpio, "gpio_keys");
		if (ret) {
			pr_err("gpio_keys: (%d) can not be requested\n", gpio);
			return ret;
		}
		gpio_direction_input(gpio);
		gk->buttons[i].previous_state = gk->buttons[i].active_low;
	}

	gk->poller.func = gpio_key_poller;

	cdev = &gk->cdev;
	dev->type_data = cdev;
	cdev->dev = dev;
	cdev->tstc = gpio_keys_tstc;
	cdev->getc = gpio_keys_getc;

	console_register(&gk->cdev);

	return poller_register(&gk->poller);
}

static struct of_device_id key_gpio_of_ids[] = {
	{ .compatible = "gpio-keys", },
	{ }
};

static struct driver_d gpio_keys_driver = {
	.name	= "gpio_keys",
	.probe	= gpio_keys_probe,
	.of_compatible = DRV_OF_COMPAT(key_gpio_of_ids),
};
device_platform_driver(gpio_keys_driver);