Input: keyboard - drop calls to platform_set_drvdata and i2c_set_clientdata
[linux-2.6-block.git] / drivers / input / keyboard / gpio_keys_polled.c
CommitLineData
0e7d0c86
GJ
1/*
2 * Driver for buttons on GPIO lines not capable of generating interrupts
3 *
4 * Copyright (C) 2007-2010 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2010 Nuno Goncalves <nunojpg@gmail.com>
6 *
7 * This file was based on: /drivers/input/misc/cobalt_btns.c
8 * Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
9 *
10 * also was based on: /drivers/input/keyboard/gpio_keys.c
11 * Copyright 2005 Phil Blundell
12 *
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.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
0e7d0c86
GJ
20#include <linux/slab.h>
21#include <linux/input.h>
22#include <linux/input-polldev.h>
23#include <linux/ioport.h>
24#include <linux/platform_device.h>
25#include <linux/gpio.h>
633a21d8 26#include <linux/gpio/consumer.h>
0e7d0c86 27#include <linux/gpio_keys.h>
b26d4e22 28#include <linux/property.h>
0e7d0c86
GJ
29
30#define DRV_NAME "gpio-keys-polled"
31
32struct gpio_keys_button_data {
0f78ba96 33 struct gpio_desc *gpiod;
0e7d0c86
GJ
34 int last_state;
35 int count;
36 int threshold;
0e7d0c86
GJ
37};
38
39struct gpio_keys_polled_dev {
40 struct input_polled_dev *poll_dev;
41 struct device *dev;
2976f247 42 const struct gpio_keys_platform_data *pdata;
6f29d3b2
HG
43 unsigned long rel_axis_seen[BITS_TO_LONGS(REL_CNT)];
44 unsigned long abs_axis_seen[BITS_TO_LONGS(ABS_CNT)];
0e7d0c86
GJ
45 struct gpio_keys_button_data data[0];
46};
47
6f29d3b2 48static void gpio_keys_button_event(struct input_polled_dev *dev,
0f78ba96 49 const struct gpio_keys_button *button,
6f29d3b2
HG
50 int state)
51{
52 struct gpio_keys_polled_dev *bdev = dev->private;
53 struct input_dev *input = dev->input;
54 unsigned int type = button->type ?: EV_KEY;
55
56 if (type == EV_REL) {
57 if (state) {
58 input_event(input, type, button->code, button->value);
59 __set_bit(button->code, bdev->rel_axis_seen);
60 }
61 } else if (type == EV_ABS) {
62 if (state) {
63 input_event(input, type, button->code, button->value);
64 __set_bit(button->code, bdev->abs_axis_seen);
65 }
66 } else {
67 input_event(input, type, button->code, state);
68 input_sync(input);
69 }
70}
71
72static void gpio_keys_polled_check_state(struct input_polled_dev *dev,
0f78ba96 73 const struct gpio_keys_button *button,
0e7d0c86
GJ
74 struct gpio_keys_button_data *bdata)
75{
76 int state;
77
ea6aabf8
DT
78 state = gpiod_get_value_cansleep(bdata->gpiod);
79 if (state < 0) {
80 dev_err(dev->input->dev.parent,
81 "failed to get gpio state: %d\n", state);
82 } else {
83 gpio_keys_button_event(dev, button, state);
0e7d0c86 84
ea6aabf8
DT
85 if (state != bdata->last_state) {
86 bdata->count = 0;
87 bdata->last_state = state;
88 }
0e7d0c86
GJ
89 }
90}
91
92static void gpio_keys_polled_poll(struct input_polled_dev *dev)
93{
94 struct gpio_keys_polled_dev *bdev = dev->private;
2976f247 95 const struct gpio_keys_platform_data *pdata = bdev->pdata;
0e7d0c86
GJ
96 struct input_dev *input = dev->input;
97 int i;
98
6f29d3b2
HG
99 memset(bdev->rel_axis_seen, 0, sizeof(bdev->rel_axis_seen));
100 memset(bdev->abs_axis_seen, 0, sizeof(bdev->abs_axis_seen));
101
2976f247 102 for (i = 0; i < pdata->nbuttons; i++) {
0e7d0c86
GJ
103 struct gpio_keys_button_data *bdata = &bdev->data[i];
104
6f29d3b2 105 if (bdata->count < bdata->threshold) {
0e7d0c86 106 bdata->count++;
6f29d3b2
HG
107 gpio_keys_button_event(dev, &pdata->buttons[i],
108 bdata->last_state);
109 } else {
110 gpio_keys_polled_check_state(dev, &pdata->buttons[i],
0e7d0c86 111 bdata);
6f29d3b2
HG
112 }
113 }
114
115 for_each_set_bit(i, input->relbit, REL_CNT) {
116 if (!test_bit(i, bdev->rel_axis_seen))
117 input_event(input, EV_REL, i, 0);
118 }
119
120 for_each_set_bit(i, input->absbit, ABS_CNT) {
121 if (!test_bit(i, bdev->abs_axis_seen))
122 input_event(input, EV_ABS, i, 0);
0e7d0c86 123 }
6f29d3b2
HG
124
125 input_sync(input);
0e7d0c86
GJ
126}
127
128static void gpio_keys_polled_open(struct input_polled_dev *dev)
129{
130 struct gpio_keys_polled_dev *bdev = dev->private;
2976f247 131 const struct gpio_keys_platform_data *pdata = bdev->pdata;
0e7d0c86
GJ
132
133 if (pdata->enable)
134 pdata->enable(bdev->dev);
135}
136
137static void gpio_keys_polled_close(struct input_polled_dev *dev)
138{
139 struct gpio_keys_polled_dev *bdev = dev->private;
2976f247 140 const struct gpio_keys_platform_data *pdata = bdev->pdata;
0e7d0c86
GJ
141
142 if (pdata->disable)
143 pdata->disable(bdev->dev);
144}
145
0f78ba96
DT
146static struct gpio_keys_platform_data *
147gpio_keys_polled_get_devtree_pdata(struct device *dev)
a2f25245 148{
a2f25245
APS
149 struct gpio_keys_platform_data *pdata;
150 struct gpio_keys_button *button;
b26d4e22 151 struct fwnode_handle *child;
a2f25245 152 int nbuttons;
a2f25245 153
b26d4e22 154 nbuttons = device_get_child_node_count(dev);
a2f25245 155 if (nbuttons == 0)
0f78ba96 156 return ERR_PTR(-EINVAL);
a2f25245 157
68252638
AS
158 pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * sizeof(*button),
159 GFP_KERNEL);
160 if (!pdata)
161 return ERR_PTR(-ENOMEM);
a2f25245 162
0f78ba96
DT
163 button = (struct gpio_keys_button *)(pdata + 1);
164
165 pdata->buttons = button;
166 pdata->nbuttons = nbuttons;
a2f25245 167
b26d4e22
AL
168 pdata->rep = device_property_present(dev, "autorepeat");
169 device_property_read_u32(dev, "poll-interval", &pdata->poll_interval);
a2f25245 170
b26d4e22 171 device_for_each_child_node(dev, child) {
0f78ba96
DT
172 if (fwnode_property_read_u32(child, "linux,code",
173 &button->code)) {
174 dev_err(dev, "button without keycode\n");
b26d4e22 175 fwnode_handle_put(child);
68252638 176 return ERR_PTR(-EINVAL);
a2f25245
APS
177 }
178
b26d4e22 179 fwnode_property_read_string(child, "label", &button->desc);
a2f25245 180
b26d4e22
AL
181 if (fwnode_property_read_u32(child, "linux,input-type",
182 &button->type))
a2f25245
APS
183 button->type = EV_KEY;
184
6f29d3b2
HG
185 if (fwnode_property_read_u32(child, "linux,input-value",
186 (u32 *)&button->value))
187 button->value = 1;
188
99b4ffbd
DT
189 button->wakeup =
190 fwnode_property_read_bool(child, "wakeup-source") ||
191 /* legacy name */
192 fwnode_property_read_bool(child, "gpio-key,wakeup");
a2f25245 193
b26d4e22
AL
194 if (fwnode_property_read_u32(child, "debounce-interval",
195 &button->debounce_interval))
a2f25245 196 button->debounce_interval = 5;
a2f25245 197
0f78ba96
DT
198 button++;
199 }
a2f25245
APS
200
201 return pdata;
a2f25245
APS
202}
203
6f29d3b2
HG
204static void gpio_keys_polled_set_abs_params(struct input_dev *input,
205 const struct gpio_keys_platform_data *pdata, unsigned int code)
206{
207 int i, min = 0, max = 0;
208
209 for (i = 0; i < pdata->nbuttons; i++) {
0f78ba96 210 const struct gpio_keys_button *button = &pdata->buttons[i];
6f29d3b2
HG
211
212 if (button->type != EV_ABS || button->code != code)
213 continue;
214
215 if (button->value < min)
216 min = button->value;
217 if (button->value > max)
218 max = button->value;
219 }
0f78ba96 220
6f29d3b2
HG
221 input_set_abs_params(input, code, min, max, 0, 0);
222}
223
90c98ef5 224static const struct of_device_id gpio_keys_polled_of_match[] = {
a2f25245
APS
225 { .compatible = "gpio-keys-polled", },
226 { },
227};
228MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
229
5298cc4c 230static int gpio_keys_polled_probe(struct platform_device *pdev)
0e7d0c86 231{
0e7d0c86 232 struct device *dev = &pdev->dev;
0f78ba96 233 struct fwnode_handle *child = NULL;
2976f247 234 const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
0e7d0c86
GJ
235 struct gpio_keys_polled_dev *bdev;
236 struct input_polled_dev *poll_dev;
237 struct input_dev *input;
68252638 238 size_t size;
0e7d0c86
GJ
239 int error;
240 int i;
241
a2f25245
APS
242 if (!pdata) {
243 pdata = gpio_keys_polled_get_devtree_pdata(dev);
244 if (IS_ERR(pdata))
245 return PTR_ERR(pdata);
a2f25245
APS
246 }
247
248 if (!pdata->poll_interval) {
249 dev_err(dev, "missing poll_interval value\n");
68252638 250 return -EINVAL;
a2f25245 251 }
0e7d0c86 252
68252638
AS
253 size = sizeof(struct gpio_keys_polled_dev) +
254 pdata->nbuttons * sizeof(struct gpio_keys_button_data);
255 bdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
0e7d0c86
GJ
256 if (!bdev) {
257 dev_err(dev, "no memory for private data\n");
68252638 258 return -ENOMEM;
0e7d0c86
GJ
259 }
260
68252638 261 poll_dev = devm_input_allocate_polled_device(&pdev->dev);
0e7d0c86
GJ
262 if (!poll_dev) {
263 dev_err(dev, "no memory for polled device\n");
68252638 264 return -ENOMEM;
0e7d0c86
GJ
265 }
266
267 poll_dev->private = bdev;
268 poll_dev->poll = gpio_keys_polled_poll;
269 poll_dev->poll_interval = pdata->poll_interval;
270 poll_dev->open = gpio_keys_polled_open;
271 poll_dev->close = gpio_keys_polled_close;
272
273 input = poll_dev->input;
274
0e7d0c86
GJ
275 input->name = pdev->name;
276 input->phys = DRV_NAME"/input0";
0e7d0c86
GJ
277
278 input->id.bustype = BUS_HOST;
279 input->id.vendor = 0x0001;
280 input->id.product = 0x0001;
281 input->id.version = 0x0100;
282
1a22e16f
AS
283 __set_bit(EV_KEY, input->evbit);
284 if (pdata->rep)
285 __set_bit(EV_REP, input->evbit);
286
0e7d0c86 287 for (i = 0; i < pdata->nbuttons; i++) {
0f78ba96 288 const struct gpio_keys_button *button = &pdata->buttons[i];
0e7d0c86 289 struct gpio_keys_button_data *bdata = &bdev->data[i];
0e7d0c86
GJ
290 unsigned int type = button->type ?: EV_KEY;
291
292 if (button->wakeup) {
293 dev_err(dev, DRV_NAME " does not support wakeup\n");
0f78ba96 294 fwnode_handle_put(child);
68252638 295 return -EINVAL;
0e7d0c86
GJ
296 }
297
0f78ba96
DT
298 if (!dev_get_platdata(dev)) {
299 /* No legacy static platform data */
300 child = device_get_next_child_node(dev, child);
301 if (!child) {
302 dev_err(dev, "missing child device node\n");
303 return -EINVAL;
304 }
305
306 bdata->gpiod = devm_get_gpiod_from_child(dev, NULL,
307 child);
308 if (IS_ERR(bdata->gpiod)) {
309 error = PTR_ERR(bdata->gpiod);
310 if (error != -EPROBE_DEFER)
311 dev_err(dev,
312 "failed to get gpio: %d\n",
313 error);
314 fwnode_handle_put(child);
315 return error;
316 }
dadbb0aa
SH
317
318 error = gpiod_direction_input(bdata->gpiod);
319 if (error) {
320 dev_err(dev, "Failed to configure GPIO %d as input: %d\n",
321 desc_to_gpio(bdata->gpiod), error);
322 fwnode_handle_put(child);
323 return error;
324 }
0f78ba96
DT
325 } else if (gpio_is_valid(button->gpio)) {
326 /*
327 * Legacy GPIO number so request the GPIO here and
328 * convert it to descriptor.
329 */
1ae5ddb6 330 unsigned flags = GPIOF_IN;
633a21d8
AL
331
332 if (button->active_low)
333 flags |= GPIOF_ACTIVE_LOW;
334
335 error = devm_gpio_request_one(&pdev->dev, button->gpio,
336 flags, button->desc ? : DRV_NAME);
337 if (error) {
0f78ba96
DT
338 dev_err(dev,
339 "unable to claim gpio %u, err=%d\n",
633a21d8
AL
340 button->gpio, error);
341 return error;
342 }
343
0f78ba96
DT
344 bdata->gpiod = gpio_to_desc(button->gpio);
345 if (!bdata->gpiod) {
346 dev_err(dev,
347 "unable to convert gpio %u to descriptor\n",
348 button->gpio);
349 return -EINVAL;
350 }
0e7d0c86 351 }
0e7d0c86 352
0e7d0c86
GJ
353 bdata->last_state = -1;
354 bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
355 pdata->poll_interval);
356
357 input_set_capability(input, type, button->code);
6f29d3b2
HG
358 if (type == EV_ABS)
359 gpio_keys_polled_set_abs_params(input, pdata,
360 button->code);
0e7d0c86
GJ
361 }
362
0f78ba96
DT
363 fwnode_handle_put(child);
364
0e7d0c86
GJ
365 bdev->poll_dev = poll_dev;
366 bdev->dev = dev;
367 bdev->pdata = pdata;
0e7d0c86
GJ
368
369 error = input_register_polled_device(poll_dev);
370 if (error) {
371 dev_err(dev, "unable to register polled device, err=%d\n",
372 error);
68252638 373 return error;
0e7d0c86
GJ
374 }
375
376 /* report initial state of the buttons */
377 for (i = 0; i < pdata->nbuttons; i++)
6f29d3b2 378 gpio_keys_polled_check_state(poll_dev, &pdata->buttons[i],
a2f25245 379 &bdev->data[i]);
0e7d0c86 380
6f29d3b2
HG
381 input_sync(input);
382
0e7d0c86 383 return 0;
0e7d0c86
GJ
384}
385
386static struct platform_driver gpio_keys_polled_driver = {
387 .probe = gpio_keys_polled_probe,
0e7d0c86
GJ
388 .driver = {
389 .name = DRV_NAME,
b26d4e22 390 .of_match_table = gpio_keys_polled_of_match,
0e7d0c86
GJ
391 },
392};
5146c84f 393module_platform_driver(gpio_keys_polled_driver);
0e7d0c86
GJ
394
395MODULE_LICENSE("GPL v2");
396MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
397MODULE_DESCRIPTION("Polled GPIO Buttons driver");
398MODULE_ALIAS("platform:" DRV_NAME);