0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/module.h>
0010
0011 #include <linux/hrtimer.h>
0012 #include <linux/init.h>
0013 #include <linux/fs.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/irq.h>
0016 #include <linux/sched.h>
0017 #include <linux/pm.h>
0018 #include <linux/slab.h>
0019 #include <linux/sysctl.h>
0020 #include <linux/proc_fs.h>
0021 #include <linux/delay.h>
0022 #include <linux/platform_device.h>
0023 #include <linux/input.h>
0024 #include <linux/gpio_keys.h>
0025 #include <linux/workqueue.h>
0026 #include <linux/gpio.h>
0027 #include <linux/gpio/consumer.h>
0028 #include <linux/of.h>
0029 #include <linux/of_irq.h>
0030 #include <linux/spinlock.h>
0031 #include <dt-bindings/input/gpio-keys.h>
0032
0033 struct gpio_button_data {
0034 const struct gpio_keys_button *button;
0035 struct input_dev *input;
0036 struct gpio_desc *gpiod;
0037
0038 unsigned short *code;
0039
0040 struct hrtimer release_timer;
0041 unsigned int release_delay;
0042
0043 struct delayed_work work;
0044 struct hrtimer debounce_timer;
0045 unsigned int software_debounce;
0046
0047 unsigned int irq;
0048 unsigned int wakeup_trigger_type;
0049 spinlock_t lock;
0050 bool disabled;
0051 bool key_pressed;
0052 bool suspended;
0053 bool debounce_use_hrtimer;
0054 };
0055
0056 struct gpio_keys_drvdata {
0057 const struct gpio_keys_platform_data *pdata;
0058 struct input_dev *input;
0059 struct mutex disable_lock;
0060 unsigned short *keymap;
0061 struct gpio_button_data data[];
0062 };
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105 static int get_n_events_by_type(int type)
0106 {
0107 BUG_ON(type != EV_SW && type != EV_KEY);
0108
0109 return (type == EV_KEY) ? KEY_CNT : SW_CNT;
0110 }
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120 static const unsigned long *get_bm_events_by_type(struct input_dev *dev,
0121 int type)
0122 {
0123 BUG_ON(type != EV_SW && type != EV_KEY);
0124
0125 return (type == EV_KEY) ? dev->keybit : dev->swbit;
0126 }
0127
0128 static void gpio_keys_quiesce_key(void *data)
0129 {
0130 struct gpio_button_data *bdata = data;
0131
0132 if (!bdata->gpiod)
0133 hrtimer_cancel(&bdata->release_timer);
0134 else if (bdata->debounce_use_hrtimer)
0135 hrtimer_cancel(&bdata->debounce_timer);
0136 else
0137 cancel_delayed_work_sync(&bdata->work);
0138 }
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153 static void gpio_keys_disable_button(struct gpio_button_data *bdata)
0154 {
0155 if (!bdata->disabled) {
0156
0157
0158
0159 disable_irq(bdata->irq);
0160 gpio_keys_quiesce_key(bdata);
0161 bdata->disabled = true;
0162 }
0163 }
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175 static void gpio_keys_enable_button(struct gpio_button_data *bdata)
0176 {
0177 if (bdata->disabled) {
0178 enable_irq(bdata->irq);
0179 bdata->disabled = false;
0180 }
0181 }
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197 static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
0198 char *buf, unsigned int type,
0199 bool only_disabled)
0200 {
0201 int n_events = get_n_events_by_type(type);
0202 unsigned long *bits;
0203 ssize_t ret;
0204 int i;
0205
0206 bits = bitmap_zalloc(n_events, GFP_KERNEL);
0207 if (!bits)
0208 return -ENOMEM;
0209
0210 for (i = 0; i < ddata->pdata->nbuttons; i++) {
0211 struct gpio_button_data *bdata = &ddata->data[i];
0212
0213 if (bdata->button->type != type)
0214 continue;
0215
0216 if (only_disabled && !bdata->disabled)
0217 continue;
0218
0219 __set_bit(*bdata->code, bits);
0220 }
0221
0222 ret = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", n_events, bits);
0223 buf[ret++] = '\n';
0224 buf[ret] = '\0';
0225
0226 bitmap_free(bits);
0227
0228 return ret;
0229 }
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241 static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
0242 const char *buf, unsigned int type)
0243 {
0244 int n_events = get_n_events_by_type(type);
0245 const unsigned long *bitmap = get_bm_events_by_type(ddata->input, type);
0246 unsigned long *bits;
0247 ssize_t error;
0248 int i;
0249
0250 bits = bitmap_alloc(n_events, GFP_KERNEL);
0251 if (!bits)
0252 return -ENOMEM;
0253
0254 error = bitmap_parselist(buf, bits, n_events);
0255 if (error)
0256 goto out;
0257
0258
0259 if (!bitmap_subset(bits, bitmap, n_events)) {
0260 error = -EINVAL;
0261 goto out;
0262 }
0263
0264 for (i = 0; i < ddata->pdata->nbuttons; i++) {
0265 struct gpio_button_data *bdata = &ddata->data[i];
0266
0267 if (bdata->button->type != type)
0268 continue;
0269
0270 if (test_bit(*bdata->code, bits) &&
0271 !bdata->button->can_disable) {
0272 error = -EINVAL;
0273 goto out;
0274 }
0275 }
0276
0277 mutex_lock(&ddata->disable_lock);
0278
0279 for (i = 0; i < ddata->pdata->nbuttons; i++) {
0280 struct gpio_button_data *bdata = &ddata->data[i];
0281
0282 if (bdata->button->type != type)
0283 continue;
0284
0285 if (test_bit(*bdata->code, bits))
0286 gpio_keys_disable_button(bdata);
0287 else
0288 gpio_keys_enable_button(bdata);
0289 }
0290
0291 mutex_unlock(&ddata->disable_lock);
0292
0293 out:
0294 bitmap_free(bits);
0295 return error;
0296 }
0297
0298 #define ATTR_SHOW_FN(name, type, only_disabled) \
0299 static ssize_t gpio_keys_show_##name(struct device *dev, \
0300 struct device_attribute *attr, \
0301 char *buf) \
0302 { \
0303 struct platform_device *pdev = to_platform_device(dev); \
0304 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
0305 \
0306 return gpio_keys_attr_show_helper(ddata, buf, \
0307 type, only_disabled); \
0308 }
0309
0310 ATTR_SHOW_FN(keys, EV_KEY, false);
0311 ATTR_SHOW_FN(switches, EV_SW, false);
0312 ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
0313 ATTR_SHOW_FN(disabled_switches, EV_SW, true);
0314
0315
0316
0317
0318
0319
0320
0321 static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
0322 static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
0323
0324 #define ATTR_STORE_FN(name, type) \
0325 static ssize_t gpio_keys_store_##name(struct device *dev, \
0326 struct device_attribute *attr, \
0327 const char *buf, \
0328 size_t count) \
0329 { \
0330 struct platform_device *pdev = to_platform_device(dev); \
0331 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
0332 ssize_t error; \
0333 \
0334 error = gpio_keys_attr_store_helper(ddata, buf, type); \
0335 if (error) \
0336 return error; \
0337 \
0338 return count; \
0339 }
0340
0341 ATTR_STORE_FN(disabled_keys, EV_KEY);
0342 ATTR_STORE_FN(disabled_switches, EV_SW);
0343
0344
0345
0346
0347
0348
0349
0350 static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
0351 gpio_keys_show_disabled_keys,
0352 gpio_keys_store_disabled_keys);
0353 static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
0354 gpio_keys_show_disabled_switches,
0355 gpio_keys_store_disabled_switches);
0356
0357 static struct attribute *gpio_keys_attrs[] = {
0358 &dev_attr_keys.attr,
0359 &dev_attr_switches.attr,
0360 &dev_attr_disabled_keys.attr,
0361 &dev_attr_disabled_switches.attr,
0362 NULL,
0363 };
0364 ATTRIBUTE_GROUPS(gpio_keys);
0365
0366 static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
0367 {
0368 const struct gpio_keys_button *button = bdata->button;
0369 struct input_dev *input = bdata->input;
0370 unsigned int type = button->type ?: EV_KEY;
0371 int state;
0372
0373 state = bdata->debounce_use_hrtimer ?
0374 gpiod_get_value(bdata->gpiod) :
0375 gpiod_get_value_cansleep(bdata->gpiod);
0376 if (state < 0) {
0377 dev_err(input->dev.parent,
0378 "failed to get gpio state: %d\n", state);
0379 return;
0380 }
0381
0382 if (type == EV_ABS) {
0383 if (state)
0384 input_event(input, type, button->code, button->value);
0385 } else {
0386 input_event(input, type, *bdata->code, state);
0387 }
0388 }
0389
0390 static void gpio_keys_debounce_event(struct gpio_button_data *bdata)
0391 {
0392 gpio_keys_gpio_report_event(bdata);
0393 input_sync(bdata->input);
0394
0395 if (bdata->button->wakeup)
0396 pm_relax(bdata->input->dev.parent);
0397 }
0398
0399 static void gpio_keys_gpio_work_func(struct work_struct *work)
0400 {
0401 struct gpio_button_data *bdata =
0402 container_of(work, struct gpio_button_data, work.work);
0403
0404 gpio_keys_debounce_event(bdata);
0405 }
0406
0407 static enum hrtimer_restart gpio_keys_debounce_timer(struct hrtimer *t)
0408 {
0409 struct gpio_button_data *bdata =
0410 container_of(t, struct gpio_button_data, debounce_timer);
0411
0412 gpio_keys_debounce_event(bdata);
0413
0414 return HRTIMER_NORESTART;
0415 }
0416
0417 static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
0418 {
0419 struct gpio_button_data *bdata = dev_id;
0420
0421 BUG_ON(irq != bdata->irq);
0422
0423 if (bdata->button->wakeup) {
0424 const struct gpio_keys_button *button = bdata->button;
0425
0426 pm_stay_awake(bdata->input->dev.parent);
0427 if (bdata->suspended &&
0428 (button->type == 0 || button->type == EV_KEY)) {
0429
0430
0431
0432
0433
0434 input_report_key(bdata->input, button->code, 1);
0435 }
0436 }
0437
0438 if (bdata->debounce_use_hrtimer) {
0439 hrtimer_start(&bdata->debounce_timer,
0440 ms_to_ktime(bdata->software_debounce),
0441 HRTIMER_MODE_REL);
0442 } else {
0443 mod_delayed_work(system_wq,
0444 &bdata->work,
0445 msecs_to_jiffies(bdata->software_debounce));
0446 }
0447
0448 return IRQ_HANDLED;
0449 }
0450
0451 static enum hrtimer_restart gpio_keys_irq_timer(struct hrtimer *t)
0452 {
0453 struct gpio_button_data *bdata = container_of(t,
0454 struct gpio_button_data,
0455 release_timer);
0456 struct input_dev *input = bdata->input;
0457
0458 if (bdata->key_pressed) {
0459 input_event(input, EV_KEY, *bdata->code, 0);
0460 input_sync(input);
0461 bdata->key_pressed = false;
0462 }
0463
0464 return HRTIMER_NORESTART;
0465 }
0466
0467 static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
0468 {
0469 struct gpio_button_data *bdata = dev_id;
0470 struct input_dev *input = bdata->input;
0471 unsigned long flags;
0472
0473 BUG_ON(irq != bdata->irq);
0474
0475 spin_lock_irqsave(&bdata->lock, flags);
0476
0477 if (!bdata->key_pressed) {
0478 if (bdata->button->wakeup)
0479 pm_wakeup_event(bdata->input->dev.parent, 0);
0480
0481 input_event(input, EV_KEY, *bdata->code, 1);
0482 input_sync(input);
0483
0484 if (!bdata->release_delay) {
0485 input_event(input, EV_KEY, *bdata->code, 0);
0486 input_sync(input);
0487 goto out;
0488 }
0489
0490 bdata->key_pressed = true;
0491 }
0492
0493 if (bdata->release_delay)
0494 hrtimer_start(&bdata->release_timer,
0495 ms_to_ktime(bdata->release_delay),
0496 HRTIMER_MODE_REL_HARD);
0497 out:
0498 spin_unlock_irqrestore(&bdata->lock, flags);
0499 return IRQ_HANDLED;
0500 }
0501
0502 static int gpio_keys_setup_key(struct platform_device *pdev,
0503 struct input_dev *input,
0504 struct gpio_keys_drvdata *ddata,
0505 const struct gpio_keys_button *button,
0506 int idx,
0507 struct fwnode_handle *child)
0508 {
0509 const char *desc = button->desc ? button->desc : "gpio_keys";
0510 struct device *dev = &pdev->dev;
0511 struct gpio_button_data *bdata = &ddata->data[idx];
0512 irq_handler_t isr;
0513 unsigned long irqflags;
0514 int irq;
0515 int error;
0516
0517 bdata->input = input;
0518 bdata->button = button;
0519 spin_lock_init(&bdata->lock);
0520
0521 if (child) {
0522 bdata->gpiod = devm_fwnode_gpiod_get(dev, child,
0523 NULL, GPIOD_IN, desc);
0524 if (IS_ERR(bdata->gpiod)) {
0525 error = PTR_ERR(bdata->gpiod);
0526 if (error == -ENOENT) {
0527
0528
0529
0530
0531 bdata->gpiod = NULL;
0532 } else {
0533 if (error != -EPROBE_DEFER)
0534 dev_err(dev, "failed to get gpio: %d\n",
0535 error);
0536 return error;
0537 }
0538 }
0539 } else if (gpio_is_valid(button->gpio)) {
0540
0541
0542
0543
0544 unsigned flags = GPIOF_IN;
0545
0546 if (button->active_low)
0547 flags |= GPIOF_ACTIVE_LOW;
0548
0549 error = devm_gpio_request_one(dev, button->gpio, flags, desc);
0550 if (error < 0) {
0551 dev_err(dev, "Failed to request GPIO %d, error %d\n",
0552 button->gpio, error);
0553 return error;
0554 }
0555
0556 bdata->gpiod = gpio_to_desc(button->gpio);
0557 if (!bdata->gpiod)
0558 return -EINVAL;
0559 }
0560
0561 if (bdata->gpiod) {
0562 bool active_low = gpiod_is_active_low(bdata->gpiod);
0563
0564 if (button->debounce_interval) {
0565 error = gpiod_set_debounce(bdata->gpiod,
0566 button->debounce_interval * 1000);
0567
0568 if (error < 0)
0569 bdata->software_debounce =
0570 button->debounce_interval;
0571
0572
0573
0574
0575
0576
0577 bdata->debounce_use_hrtimer =
0578 !gpiod_cansleep(bdata->gpiod);
0579 }
0580
0581 if (button->irq) {
0582 bdata->irq = button->irq;
0583 } else {
0584 irq = gpiod_to_irq(bdata->gpiod);
0585 if (irq < 0) {
0586 error = irq;
0587 dev_err(dev,
0588 "Unable to get irq number for GPIO %d, error %d\n",
0589 button->gpio, error);
0590 return error;
0591 }
0592 bdata->irq = irq;
0593 }
0594
0595 INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func);
0596
0597 hrtimer_init(&bdata->debounce_timer,
0598 CLOCK_REALTIME, HRTIMER_MODE_REL);
0599 bdata->debounce_timer.function = gpio_keys_debounce_timer;
0600
0601 isr = gpio_keys_gpio_isr;
0602 irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
0603
0604 switch (button->wakeup_event_action) {
0605 case EV_ACT_ASSERTED:
0606 bdata->wakeup_trigger_type = active_low ?
0607 IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
0608 break;
0609 case EV_ACT_DEASSERTED:
0610 bdata->wakeup_trigger_type = active_low ?
0611 IRQ_TYPE_EDGE_RISING : IRQ_TYPE_EDGE_FALLING;
0612 break;
0613 case EV_ACT_ANY:
0614 default:
0615
0616
0617
0618
0619 break;
0620 }
0621 } else {
0622 if (!button->irq) {
0623 dev_err(dev, "Found button without gpio or irq\n");
0624 return -EINVAL;
0625 }
0626
0627 bdata->irq = button->irq;
0628
0629 if (button->type && button->type != EV_KEY) {
0630 dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n");
0631 return -EINVAL;
0632 }
0633
0634 bdata->release_delay = button->debounce_interval;
0635 hrtimer_init(&bdata->release_timer,
0636 CLOCK_REALTIME, HRTIMER_MODE_REL_HARD);
0637 bdata->release_timer.function = gpio_keys_irq_timer;
0638
0639 isr = gpio_keys_irq_isr;
0640 irqflags = 0;
0641
0642
0643
0644
0645
0646 }
0647
0648 bdata->code = &ddata->keymap[idx];
0649 *bdata->code = button->code;
0650 input_set_capability(input, button->type ?: EV_KEY, *bdata->code);
0651
0652
0653
0654
0655
0656 error = devm_add_action(dev, gpio_keys_quiesce_key, bdata);
0657 if (error) {
0658 dev_err(dev, "failed to register quiesce action, error: %d\n",
0659 error);
0660 return error;
0661 }
0662
0663
0664
0665
0666
0667 if (!button->can_disable)
0668 irqflags |= IRQF_SHARED;
0669
0670 error = devm_request_any_context_irq(dev, bdata->irq, isr, irqflags,
0671 desc, bdata);
0672 if (error < 0) {
0673 dev_err(dev, "Unable to claim irq %d; error %d\n",
0674 bdata->irq, error);
0675 return error;
0676 }
0677
0678 return 0;
0679 }
0680
0681 static void gpio_keys_report_state(struct gpio_keys_drvdata *ddata)
0682 {
0683 struct input_dev *input = ddata->input;
0684 int i;
0685
0686 for (i = 0; i < ddata->pdata->nbuttons; i++) {
0687 struct gpio_button_data *bdata = &ddata->data[i];
0688 if (bdata->gpiod)
0689 gpio_keys_gpio_report_event(bdata);
0690 }
0691 input_sync(input);
0692 }
0693
0694 static int gpio_keys_open(struct input_dev *input)
0695 {
0696 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
0697 const struct gpio_keys_platform_data *pdata = ddata->pdata;
0698 int error;
0699
0700 if (pdata->enable) {
0701 error = pdata->enable(input->dev.parent);
0702 if (error)
0703 return error;
0704 }
0705
0706
0707 gpio_keys_report_state(ddata);
0708
0709 return 0;
0710 }
0711
0712 static void gpio_keys_close(struct input_dev *input)
0713 {
0714 struct gpio_keys_drvdata *ddata = input_get_drvdata(input);
0715 const struct gpio_keys_platform_data *pdata = ddata->pdata;
0716
0717 if (pdata->disable)
0718 pdata->disable(input->dev.parent);
0719 }
0720
0721
0722
0723
0724
0725
0726
0727
0728 static struct gpio_keys_platform_data *
0729 gpio_keys_get_devtree_pdata(struct device *dev)
0730 {
0731 struct gpio_keys_platform_data *pdata;
0732 struct gpio_keys_button *button;
0733 struct fwnode_handle *child;
0734 int nbuttons;
0735
0736 nbuttons = device_get_child_node_count(dev);
0737 if (nbuttons == 0)
0738 return ERR_PTR(-ENODEV);
0739
0740 pdata = devm_kzalloc(dev,
0741 sizeof(*pdata) + nbuttons * sizeof(*button),
0742 GFP_KERNEL);
0743 if (!pdata)
0744 return ERR_PTR(-ENOMEM);
0745
0746 button = (struct gpio_keys_button *)(pdata + 1);
0747
0748 pdata->buttons = button;
0749 pdata->nbuttons = nbuttons;
0750
0751 pdata->rep = device_property_read_bool(dev, "autorepeat");
0752
0753 device_property_read_string(dev, "label", &pdata->name);
0754
0755 device_for_each_child_node(dev, child) {
0756 if (is_of_node(child))
0757 button->irq =
0758 irq_of_parse_and_map(to_of_node(child), 0);
0759
0760 if (fwnode_property_read_u32(child, "linux,code",
0761 &button->code)) {
0762 dev_err(dev, "Button without keycode\n");
0763 fwnode_handle_put(child);
0764 return ERR_PTR(-EINVAL);
0765 }
0766
0767 fwnode_property_read_string(child, "label", &button->desc);
0768
0769 if (fwnode_property_read_u32(child, "linux,input-type",
0770 &button->type))
0771 button->type = EV_KEY;
0772
0773 button->wakeup =
0774 fwnode_property_read_bool(child, "wakeup-source") ||
0775
0776 fwnode_property_read_bool(child, "gpio-key,wakeup");
0777
0778 fwnode_property_read_u32(child, "wakeup-event-action",
0779 &button->wakeup_event_action);
0780
0781 button->can_disable =
0782 fwnode_property_read_bool(child, "linux,can-disable");
0783
0784 if (fwnode_property_read_u32(child, "debounce-interval",
0785 &button->debounce_interval))
0786 button->debounce_interval = 5;
0787
0788 button++;
0789 }
0790
0791 return pdata;
0792 }
0793
0794 static const struct of_device_id gpio_keys_of_match[] = {
0795 { .compatible = "gpio-keys", },
0796 { },
0797 };
0798 MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
0799
0800 static int gpio_keys_probe(struct platform_device *pdev)
0801 {
0802 struct device *dev = &pdev->dev;
0803 const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
0804 struct fwnode_handle *child = NULL;
0805 struct gpio_keys_drvdata *ddata;
0806 struct input_dev *input;
0807 int i, error;
0808 int wakeup = 0;
0809
0810 if (!pdata) {
0811 pdata = gpio_keys_get_devtree_pdata(dev);
0812 if (IS_ERR(pdata))
0813 return PTR_ERR(pdata);
0814 }
0815
0816 ddata = devm_kzalloc(dev, struct_size(ddata, data, pdata->nbuttons),
0817 GFP_KERNEL);
0818 if (!ddata) {
0819 dev_err(dev, "failed to allocate state\n");
0820 return -ENOMEM;
0821 }
0822
0823 ddata->keymap = devm_kcalloc(dev,
0824 pdata->nbuttons, sizeof(ddata->keymap[0]),
0825 GFP_KERNEL);
0826 if (!ddata->keymap)
0827 return -ENOMEM;
0828
0829 input = devm_input_allocate_device(dev);
0830 if (!input) {
0831 dev_err(dev, "failed to allocate input device\n");
0832 return -ENOMEM;
0833 }
0834
0835 ddata->pdata = pdata;
0836 ddata->input = input;
0837 mutex_init(&ddata->disable_lock);
0838
0839 platform_set_drvdata(pdev, ddata);
0840 input_set_drvdata(input, ddata);
0841
0842 input->name = pdata->name ? : pdev->name;
0843 input->phys = "gpio-keys/input0";
0844 input->dev.parent = dev;
0845 input->open = gpio_keys_open;
0846 input->close = gpio_keys_close;
0847
0848 input->id.bustype = BUS_HOST;
0849 input->id.vendor = 0x0001;
0850 input->id.product = 0x0001;
0851 input->id.version = 0x0100;
0852
0853 input->keycode = ddata->keymap;
0854 input->keycodesize = sizeof(ddata->keymap[0]);
0855 input->keycodemax = pdata->nbuttons;
0856
0857
0858 if (pdata->rep)
0859 __set_bit(EV_REP, input->evbit);
0860
0861 for (i = 0; i < pdata->nbuttons; i++) {
0862 const struct gpio_keys_button *button = &pdata->buttons[i];
0863
0864 if (!dev_get_platdata(dev)) {
0865 child = device_get_next_child_node(dev, child);
0866 if (!child) {
0867 dev_err(dev,
0868 "missing child device node for entry %d\n",
0869 i);
0870 return -EINVAL;
0871 }
0872 }
0873
0874 error = gpio_keys_setup_key(pdev, input, ddata,
0875 button, i, child);
0876 if (error) {
0877 fwnode_handle_put(child);
0878 return error;
0879 }
0880
0881 if (button->wakeup)
0882 wakeup = 1;
0883 }
0884
0885 fwnode_handle_put(child);
0886
0887 error = input_register_device(input);
0888 if (error) {
0889 dev_err(dev, "Unable to register input device, error: %d\n",
0890 error);
0891 return error;
0892 }
0893
0894 device_init_wakeup(dev, wakeup);
0895
0896 return 0;
0897 }
0898
0899 static int __maybe_unused
0900 gpio_keys_button_enable_wakeup(struct gpio_button_data *bdata)
0901 {
0902 int error;
0903
0904 error = enable_irq_wake(bdata->irq);
0905 if (error) {
0906 dev_err(bdata->input->dev.parent,
0907 "failed to configure IRQ %d as wakeup source: %d\n",
0908 bdata->irq, error);
0909 return error;
0910 }
0911
0912 if (bdata->wakeup_trigger_type) {
0913 error = irq_set_irq_type(bdata->irq,
0914 bdata->wakeup_trigger_type);
0915 if (error) {
0916 dev_err(bdata->input->dev.parent,
0917 "failed to set wakeup trigger %08x for IRQ %d: %d\n",
0918 bdata->wakeup_trigger_type, bdata->irq, error);
0919 disable_irq_wake(bdata->irq);
0920 return error;
0921 }
0922 }
0923
0924 return 0;
0925 }
0926
0927 static void __maybe_unused
0928 gpio_keys_button_disable_wakeup(struct gpio_button_data *bdata)
0929 {
0930 int error;
0931
0932
0933
0934
0935
0936 if (bdata->wakeup_trigger_type) {
0937 error = irq_set_irq_type(bdata->irq, IRQ_TYPE_EDGE_BOTH);
0938 if (error)
0939 dev_warn(bdata->input->dev.parent,
0940 "failed to restore interrupt trigger for IRQ %d: %d\n",
0941 bdata->irq, error);
0942 }
0943
0944 error = disable_irq_wake(bdata->irq);
0945 if (error)
0946 dev_warn(bdata->input->dev.parent,
0947 "failed to disable IRQ %d as wake source: %d\n",
0948 bdata->irq, error);
0949 }
0950
0951 static int __maybe_unused
0952 gpio_keys_enable_wakeup(struct gpio_keys_drvdata *ddata)
0953 {
0954 struct gpio_button_data *bdata;
0955 int error;
0956 int i;
0957
0958 for (i = 0; i < ddata->pdata->nbuttons; i++) {
0959 bdata = &ddata->data[i];
0960 if (bdata->button->wakeup) {
0961 error = gpio_keys_button_enable_wakeup(bdata);
0962 if (error)
0963 goto err_out;
0964 }
0965 bdata->suspended = true;
0966 }
0967
0968 return 0;
0969
0970 err_out:
0971 while (i--) {
0972 bdata = &ddata->data[i];
0973 if (bdata->button->wakeup)
0974 gpio_keys_button_disable_wakeup(bdata);
0975 bdata->suspended = false;
0976 }
0977
0978 return error;
0979 }
0980
0981 static void __maybe_unused
0982 gpio_keys_disable_wakeup(struct gpio_keys_drvdata *ddata)
0983 {
0984 struct gpio_button_data *bdata;
0985 int i;
0986
0987 for (i = 0; i < ddata->pdata->nbuttons; i++) {
0988 bdata = &ddata->data[i];
0989 bdata->suspended = false;
0990 if (irqd_is_wakeup_set(irq_get_irq_data(bdata->irq)))
0991 gpio_keys_button_disable_wakeup(bdata);
0992 }
0993 }
0994
0995 static int __maybe_unused gpio_keys_suspend(struct device *dev)
0996 {
0997 struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
0998 struct input_dev *input = ddata->input;
0999 int error;
1000
1001 if (device_may_wakeup(dev)) {
1002 error = gpio_keys_enable_wakeup(ddata);
1003 if (error)
1004 return error;
1005 } else {
1006 mutex_lock(&input->mutex);
1007 if (input_device_enabled(input))
1008 gpio_keys_close(input);
1009 mutex_unlock(&input->mutex);
1010 }
1011
1012 return 0;
1013 }
1014
1015 static int __maybe_unused gpio_keys_resume(struct device *dev)
1016 {
1017 struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
1018 struct input_dev *input = ddata->input;
1019 int error = 0;
1020
1021 if (device_may_wakeup(dev)) {
1022 gpio_keys_disable_wakeup(ddata);
1023 } else {
1024 mutex_lock(&input->mutex);
1025 if (input_device_enabled(input))
1026 error = gpio_keys_open(input);
1027 mutex_unlock(&input->mutex);
1028 }
1029
1030 if (error)
1031 return error;
1032
1033 gpio_keys_report_state(ddata);
1034 return 0;
1035 }
1036
1037 static SIMPLE_DEV_PM_OPS(gpio_keys_pm_ops, gpio_keys_suspend, gpio_keys_resume);
1038
1039 static void gpio_keys_shutdown(struct platform_device *pdev)
1040 {
1041 int ret;
1042
1043 ret = gpio_keys_suspend(&pdev->dev);
1044 if (ret)
1045 dev_err(&pdev->dev, "failed to shutdown\n");
1046 }
1047
1048 static struct platform_driver gpio_keys_device_driver = {
1049 .probe = gpio_keys_probe,
1050 .shutdown = gpio_keys_shutdown,
1051 .driver = {
1052 .name = "gpio-keys",
1053 .pm = &gpio_keys_pm_ops,
1054 .of_match_table = gpio_keys_of_match,
1055 .dev_groups = gpio_keys_groups,
1056 }
1057 };
1058
1059 static int __init gpio_keys_init(void)
1060 {
1061 return platform_driver_register(&gpio_keys_device_driver);
1062 }
1063
1064 static void __exit gpio_keys_exit(void)
1065 {
1066 platform_driver_unregister(&gpio_keys_device_driver);
1067 }
1068
1069 late_initcall(gpio_keys_init);
1070 module_exit(gpio_keys_exit);
1071
1072 MODULE_LICENSE("GPL");
1073 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
1074 MODULE_DESCRIPTION("Keyboard driver for GPIOs");
1075 MODULE_ALIAS("platform:gpio-keys");