Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gpio_keys_polled.c
Go to the documentation of this file.
1 /*
2  * Driver for buttons on GPIO lines not capable of generating interrupts
3  *
4  * Copyright (C) 2007-2010 Gabor Juhos <[email protected]>
5  * Copyright (C) 2010 Nuno Goncalves <[email protected]>
6  *
7  * This file was based on: /drivers/input/misc/cobalt_btns.c
8  * Copyright (C) 2007 Yoichi Yuasa <[email protected]>
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>
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/platform_device.h>
26 #include <linux/gpio.h>
27 #include <linux/gpio_keys.h>
28 #include <linux/of_platform.h>
29 #include <linux/of_gpio.h>
30 
31 #define DRV_NAME "gpio-keys-polled"
32 
35  int count;
36  int threshold;
37  int can_sleep;
38 };
39 
41  struct input_polled_dev *poll_dev;
42  struct device *dev;
45 };
46 
47 static void gpio_keys_polled_check_state(struct input_dev *input,
48  struct gpio_keys_button *button,
49  struct gpio_keys_button_data *bdata)
50 {
51  int state;
52 
53  if (bdata->can_sleep)
54  state = !!gpio_get_value_cansleep(button->gpio);
55  else
56  state = !!gpio_get_value(button->gpio);
57 
58  if (state != bdata->last_state) {
59  unsigned int type = button->type ?: EV_KEY;
60 
61  input_event(input, type, button->code,
62  !!(state ^ button->active_low));
63  input_sync(input);
64  bdata->count = 0;
65  bdata->last_state = state;
66  }
67 }
68 
69 static void gpio_keys_polled_poll(struct input_polled_dev *dev)
70 {
71  struct gpio_keys_polled_dev *bdev = dev->private;
72  const struct gpio_keys_platform_data *pdata = bdev->pdata;
73  struct input_dev *input = dev->input;
74  int i;
75 
76  for (i = 0; i < pdata->nbuttons; i++) {
77  struct gpio_keys_button_data *bdata = &bdev->data[i];
78 
79  if (bdata->count < bdata->threshold)
80  bdata->count++;
81  else
82  gpio_keys_polled_check_state(input, &pdata->buttons[i],
83  bdata);
84  }
85 }
86 
87 static void gpio_keys_polled_open(struct input_polled_dev *dev)
88 {
89  struct gpio_keys_polled_dev *bdev = dev->private;
90  const struct gpio_keys_platform_data *pdata = bdev->pdata;
91 
92  if (pdata->enable)
93  pdata->enable(bdev->dev);
94 }
95 
96 static void gpio_keys_polled_close(struct input_polled_dev *dev)
97 {
98  struct gpio_keys_polled_dev *bdev = dev->private;
99  const struct gpio_keys_platform_data *pdata = bdev->pdata;
100 
101  if (pdata->disable)
102  pdata->disable(bdev->dev);
103 }
104 
105 #ifdef CONFIG_OF
106 static struct gpio_keys_platform_data * __devinit
107 gpio_keys_polled_get_devtree_pdata(struct device *dev)
108 {
109  struct device_node *node, *pp;
111  struct gpio_keys_button *button;
112  int error;
113  int nbuttons;
114  int i;
115 
116  node = dev->of_node;
117  if (!node)
118  return NULL;
119 
120  nbuttons = of_get_child_count(node);
121  if (nbuttons == 0)
122  return NULL;
123 
124  pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button),
125  GFP_KERNEL);
126  if (!pdata) {
127  error = -ENOMEM;
128  goto err_out;
129  }
130 
131  pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
132  pdata->nbuttons = nbuttons;
133 
134  pdata->rep = !!of_get_property(node, "autorepeat", NULL);
135  of_property_read_u32(node, "poll-interval", &pdata->poll_interval);
136 
137  i = 0;
138  for_each_child_of_node(node, pp) {
139  enum of_gpio_flags flags;
140 
141  if (!of_find_property(pp, "gpios", NULL)) {
142  pdata->nbuttons--;
143  dev_warn(dev, "Found button without gpios\n");
144  continue;
145  }
146 
147  button = &pdata->buttons[i++];
148 
149  button->gpio = of_get_gpio_flags(pp, 0, &flags);
150  button->active_low = flags & OF_GPIO_ACTIVE_LOW;
151 
152  if (of_property_read_u32(pp, "linux,code", &button->code)) {
153  dev_err(dev, "Button without keycode: 0x%x\n",
154  button->gpio);
155  error = -EINVAL;
156  goto err_free_pdata;
157  }
158 
159  button->desc = of_get_property(pp, "label", NULL);
160 
161  if (of_property_read_u32(pp, "linux,input-type", &button->type))
162  button->type = EV_KEY;
163 
164  button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
165 
166  if (of_property_read_u32(pp, "debounce-interval",
167  &button->debounce_interval))
168  button->debounce_interval = 5;
169  }
170 
171  if (pdata->nbuttons == 0) {
172  error = -EINVAL;
173  goto err_free_pdata;
174  }
175 
176  return pdata;
177 
178 err_free_pdata:
179  kfree(pdata);
180 err_out:
181  return ERR_PTR(error);
182 }
183 
184 static struct of_device_id gpio_keys_polled_of_match[] = {
185  { .compatible = "gpio-keys-polled", },
186  { },
187 };
188 MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
189 
190 #else
191 
192 static inline struct gpio_keys_platform_data *
193 gpio_keys_polled_get_devtree_pdata(struct device *dev)
194 {
195  return NULL;
196 }
197 #endif
198 
199 static int __devinit gpio_keys_polled_probe(struct platform_device *pdev)
200 {
201  struct device *dev = &pdev->dev;
202  const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
203  struct gpio_keys_polled_dev *bdev;
204  struct input_polled_dev *poll_dev;
205  struct input_dev *input;
206  int error;
207  int i;
208 
209  if (!pdata) {
210  pdata = gpio_keys_polled_get_devtree_pdata(dev);
211  if (IS_ERR(pdata))
212  return PTR_ERR(pdata);
213  if (!pdata) {
214  dev_err(dev, "missing platform data\n");
215  return -EINVAL;
216  }
217  }
218 
219  if (!pdata->poll_interval) {
220  dev_err(dev, "missing poll_interval value\n");
221  error = -EINVAL;
222  goto err_free_pdata;
223  }
224 
225  bdev = kzalloc(sizeof(struct gpio_keys_polled_dev) +
226  pdata->nbuttons * sizeof(struct gpio_keys_button_data),
227  GFP_KERNEL);
228  if (!bdev) {
229  dev_err(dev, "no memory for private data\n");
230  error = -ENOMEM;
231  goto err_free_pdata;
232  }
233 
234  poll_dev = input_allocate_polled_device();
235  if (!poll_dev) {
236  dev_err(dev, "no memory for polled device\n");
237  error = -ENOMEM;
238  goto err_free_bdev;
239  }
240 
241  poll_dev->private = bdev;
242  poll_dev->poll = gpio_keys_polled_poll;
243  poll_dev->poll_interval = pdata->poll_interval;
244  poll_dev->open = gpio_keys_polled_open;
245  poll_dev->close = gpio_keys_polled_close;
246 
247  input = poll_dev->input;
248 
249  input->evbit[0] = BIT(EV_KEY);
250  input->name = pdev->name;
251  input->phys = DRV_NAME"/input0";
252  input->dev.parent = &pdev->dev;
253 
254  input->id.bustype = BUS_HOST;
255  input->id.vendor = 0x0001;
256  input->id.product = 0x0001;
257  input->id.version = 0x0100;
258 
259  for (i = 0; i < pdata->nbuttons; i++) {
260  struct gpio_keys_button *button = &pdata->buttons[i];
261  struct gpio_keys_button_data *bdata = &bdev->data[i];
262  unsigned int gpio = button->gpio;
263  unsigned int type = button->type ?: EV_KEY;
264 
265  if (button->wakeup) {
266  dev_err(dev, DRV_NAME " does not support wakeup\n");
267  error = -EINVAL;
268  goto err_free_gpio;
269  }
270 
271  error = gpio_request(gpio,
272  button->desc ? button->desc : DRV_NAME);
273  if (error) {
274  dev_err(dev, "unable to claim gpio %u, err=%d\n",
275  gpio, error);
276  goto err_free_gpio;
277  }
278 
279  error = gpio_direction_input(gpio);
280  if (error) {
281  dev_err(dev,
282  "unable to set direction on gpio %u, err=%d\n",
283  gpio, error);
284  goto err_free_gpio;
285  }
286 
287  bdata->can_sleep = gpio_cansleep(gpio);
288  bdata->last_state = -1;
289  bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
290  pdata->poll_interval);
291 
292  input_set_capability(input, type, button->code);
293  }
294 
295  bdev->poll_dev = poll_dev;
296  bdev->dev = dev;
297  bdev->pdata = pdata;
298  platform_set_drvdata(pdev, bdev);
299 
300  error = input_register_polled_device(poll_dev);
301  if (error) {
302  dev_err(dev, "unable to register polled device, err=%d\n",
303  error);
304  goto err_free_gpio;
305  }
306 
307  /* report initial state of the buttons */
308  for (i = 0; i < pdata->nbuttons; i++)
309  gpio_keys_polled_check_state(input, &pdata->buttons[i],
310  &bdev->data[i]);
311 
312  return 0;
313 
314 err_free_gpio:
315  while (--i >= 0)
316  gpio_free(pdata->buttons[i].gpio);
317 
318  input_free_polled_device(poll_dev);
319 
320 err_free_bdev:
321  kfree(bdev);
322  platform_set_drvdata(pdev, NULL);
323 
324 err_free_pdata:
325  /* If we have no platform_data, we allocated pdata dynamically. */
326  if (!dev_get_platdata(&pdev->dev))
327  kfree(pdata);
328 
329  return error;
330 }
331 
332 static int __devexit gpio_keys_polled_remove(struct platform_device *pdev)
333 {
334  struct gpio_keys_polled_dev *bdev = platform_get_drvdata(pdev);
335  const struct gpio_keys_platform_data *pdata = bdev->pdata;
336  int i;
337 
339 
340  for (i = 0; i < pdata->nbuttons; i++)
341  gpio_free(pdata->buttons[i].gpio);
342 
344 
345  /*
346  * If we had no platform_data, we allocated pdata dynamically and
347  * must free it here.
348  */
349  if (!dev_get_platdata(&pdev->dev))
350  kfree(pdata);
351 
352  kfree(bdev);
353  platform_set_drvdata(pdev, NULL);
354 
355  return 0;
356 }
357 
358 static struct platform_driver gpio_keys_polled_driver = {
359  .probe = gpio_keys_polled_probe,
360  .remove = __devexit_p(gpio_keys_polled_remove),
361  .driver = {
362  .name = DRV_NAME,
363  .owner = THIS_MODULE,
364  .of_match_table = of_match_ptr(gpio_keys_polled_of_match),
365  },
366 };
367 module_platform_driver(gpio_keys_polled_driver);
368 
369 MODULE_LICENSE("GPL v2");
370 MODULE_AUTHOR("Gabor Juhos <[email protected]>");
371 MODULE_DESCRIPTION("Polled GPIO Buttons driver");
372 MODULE_ALIAS("platform:" DRV_NAME);