12 #include <linux/module.h>
18 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/sysctl.h>
25 #include <linux/input.h>
93 static inline int get_n_events_by_type(
int type)
160 char *
buf,
unsigned int type,
163 int n_events = get_n_events_by_type(type);
172 for (i = 0; i < ddata->
pdata->nbuttons; i++) {
175 if (bdata->
button->type != type)
178 if (only_disabled && !bdata->
disabled)
204 const char *buf,
unsigned int type)
206 int n_events = get_n_events_by_type(type);
220 for (i = 0; i < ddata->
pdata->nbuttons; i++) {
223 if (bdata->
button->type != type)
227 !bdata->
button->can_disable) {
235 for (i = 0; i < ddata->
pdata->nbuttons; i++) {
238 if (bdata->
button->type != type)
242 gpio_keys_disable_button(bdata);
244 gpio_keys_enable_button(bdata);
254 #define ATTR_SHOW_FN(name, type, only_disabled) \
255 static ssize_t gpio_keys_show_##name(struct device *dev, \
256 struct device_attribute *attr, \
259 struct platform_device *pdev = to_platform_device(dev); \
260 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
262 return gpio_keys_attr_show_helper(ddata, buf, \
263 type, only_disabled); \
280 #define ATTR_STORE_FN(name, type) \
281 static ssize_t gpio_keys_store_##name(struct device *dev, \
282 struct device_attribute *attr, \
286 struct platform_device *pdev = to_platform_device(dev); \
287 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
290 error = gpio_keys_attr_store_helper(ddata, buf, type); \
307 gpio_keys_show_disabled_keys,
308 gpio_keys_store_disabled_keys);
310 gpio_keys_show_disabled_switches,
311 gpio_keys_store_disabled_switches);
313 static struct attribute *gpio_keys_attrs[] = {
315 &dev_attr_switches.attr,
316 &dev_attr_disabled_keys.attr,
317 &dev_attr_disabled_switches.attr,
322 .attrs = gpio_keys_attrs,
346 gpio_keys_gpio_report_event(bdata);
348 if (bdata->
button->wakeup)
352 static void gpio_keys_gpio_timer(
unsigned long _data)
365 if (bdata->
button->wakeup)
376 static void gpio_keys_irq_timer(
unsigned long _data)
379 struct input_dev *input = bdata->
input;
388 spin_unlock_irqrestore(&bdata->
lock, flags);
391 static irqreturn_t gpio_keys_irq_isr(
int irq,
void *dev_id)
395 struct input_dev *input = bdata->
input;
403 if (bdata->
button->wakeup)
422 spin_unlock_irqrestore(&bdata->
lock, flags);
427 struct input_dev *input,
431 const char *
desc = button->
desc ? button->
desc :
"gpio_keys";
434 unsigned long irqflags;
441 if (gpio_is_valid(button->
gpio)) {
445 dev_err(dev,
"Failed to request GPIO %d, error %d\n",
446 button->
gpio, error);
453 "Failed to configure direction for GPIO %d, error %d\n",
454 button->
gpio, error);
471 "Unable to get irq number for GPIO %d, error %d\n",
472 button->
gpio, error);
479 gpio_keys_gpio_timer, (
unsigned long)bdata);
481 isr = gpio_keys_gpio_isr;
486 dev_err(dev,
"No IRQ specified\n");
492 dev_err(dev,
"Only EV_KEY allowed for IRQ buttons.\n");
498 gpio_keys_irq_timer, (
unsigned long)bdata);
500 isr = gpio_keys_irq_isr;
504 input_set_capability(input, button->
type ?:
EV_KEY, button->
code);
515 dev_err(dev,
"Unable to claim irq %d; error %d\n",
523 if (gpio_is_valid(button->
gpio))
529 static int gpio_keys_open(
struct input_dev *input)
534 return pdata->
enable ? pdata->
enable(input->dev.parent) : 0;
537 static void gpio_keys_close(
struct input_dev *input)
543 pdata->
disable(input->dev.parent);
555 gpio_keys_get_devtree_pdata(
struct device *dev)
570 nbuttons = of_get_child_count(node);
576 pdata = kzalloc(
sizeof(*pdata) + nbuttons * (
sizeof *button),
594 dev_warn(dev,
"Found button without gpios\n");
600 button->
gpio = of_get_gpio_flags(pp, 0, &flags);
603 if (of_property_read_u32(pp,
"linux,code", &button->
code)) {
604 dev_err(dev,
"Button without keycode: 0x%x\n",
612 if (of_property_read_u32(pp,
"linux,input-type", &button->
type))
617 if (of_property_read_u32(pp,
"debounce-interval",
632 return ERR_PTR(error);
644 gpio_keys_get_devtree_pdata(
struct device *dev)
657 if (gpio_is_valid(bdata->
button->gpio))
666 struct input_dev *
input;
671 pdata = gpio_keys_get_devtree_pdata(dev);
673 return PTR_ERR(pdata);
679 input = input_allocate_device();
680 if (!ddata || !input) {
681 dev_err(dev,
"failed to allocate state\n");
690 platform_set_drvdata(pdev, ddata);
691 input_set_drvdata(input, ddata);
693 input->name = pdata->
name ? : pdev->
name;
694 input->phys =
"gpio-keys/input0";
695 input->dev.parent = &pdev->
dev;
696 input->open = gpio_keys_open;
697 input->close = gpio_keys_close;
700 input->id.vendor = 0x0001;
701 input->id.product = 0x0001;
702 input->id.version = 0x0100;
708 for (i = 0; i < pdata->
nbuttons; i++) {
712 error = gpio_keys_setup_key(pdev, input, bdata, button);
722 dev_err(dev,
"Unable to export keys/switches, error: %d\n",
727 error = input_register_device(input);
729 dev_err(dev,
"Unable to register input device, error: %d\n",
735 for (i = 0; i < pdata->
nbuttons; i++) {
737 if (gpio_is_valid(bdata->
button->gpio))
738 gpio_keys_gpio_report_event(bdata);
750 gpio_remove_key(&ddata->
data[i]);
752 platform_set_drvdata(pdev,
NULL);
754 input_free_device(input);
757 if (!dev_get_platdata(&pdev->
dev))
766 struct input_dev *input = ddata->
input;
773 for (i = 0; i < ddata->
pdata->nbuttons; i++)
774 gpio_remove_key(&ddata->
data[i]);
776 input_unregister_device(input);
779 if (!dev_get_platdata(&pdev->
dev))
787 #ifdef CONFIG_PM_SLEEP
788 static int gpio_keys_suspend(
struct device *dev)
793 if (device_may_wakeup(dev)) {
794 for (i = 0; i < ddata->
pdata->nbuttons; i++) {
796 if (bdata->
button->wakeup)
797 enable_irq_wake(bdata->
irq);
804 static int gpio_keys_resume(
struct device *dev)
809 for (i = 0; i < ddata->
pdata->nbuttons; i++) {
811 if (bdata->
button->wakeup && device_may_wakeup(dev))
812 disable_irq_wake(bdata->
irq);
814 if (gpio_is_valid(bdata->
button->gpio))
815 gpio_keys_gpio_report_event(bdata);
817 input_sync(ddata->
input);
826 .probe = gpio_keys_probe,
831 .pm = &gpio_keys_pm_ops,
836 static int __init gpio_keys_init(
void)
841 static void __exit gpio_keys_exit(
void)