1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2014, National Instruments Corp. All rights reserved. 4 * 5 * Driver for NI Ettus Research USRP E3x0 Button Driver 6 */ 7 8 #include <linux/device.h> 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/platform_device.h> 12 #include <linux/input.h> 13 #include <linux/interrupt.h> 14 #include <linux/of.h> 15 #include <linux/slab.h> 16 17 static irqreturn_t e3x0_button_release_handler(int irq, void *data) 18 { 19 struct input_dev *idev = data; 20 21 input_report_key(idev, KEY_POWER, 0); 22 input_sync(idev); 23 24 return IRQ_HANDLED; 25 } 26 27 static irqreturn_t e3x0_button_press_handler(int irq, void *data) 28 { 29 struct input_dev *idev = data; 30 31 input_report_key(idev, KEY_POWER, 1); 32 pm_wakeup_event(idev->dev.parent, 0); 33 input_sync(idev); 34 35 return IRQ_HANDLED; 36 } 37 38 static int __maybe_unused e3x0_button_suspend(struct device *dev) 39 { 40 struct platform_device *pdev = to_platform_device(dev); 41 42 if (device_may_wakeup(dev)) 43 enable_irq_wake(platform_get_irq_byname(pdev, "press")); 44 45 return 0; 46 } 47 48 static int __maybe_unused e3x0_button_resume(struct device *dev) 49 { 50 struct platform_device *pdev = to_platform_device(dev); 51 52 if (device_may_wakeup(dev)) 53 disable_irq_wake(platform_get_irq_byname(pdev, "press")); 54 55 return 0; 56 } 57 58 static SIMPLE_DEV_PM_OPS(e3x0_button_pm_ops, 59 e3x0_button_suspend, e3x0_button_resume); 60 61 static int e3x0_button_probe(struct platform_device *pdev) 62 { 63 struct input_dev *input; 64 int irq_press, irq_release; 65 int error; 66 67 irq_press = platform_get_irq_byname(pdev, "press"); 68 if (irq_press < 0) { 69 dev_err(&pdev->dev, "No IRQ for 'press', error=%d\n", 70 irq_press); 71 return irq_press; 72 } 73 74 irq_release = platform_get_irq_byname(pdev, "release"); 75 if (irq_release < 0) { 76 dev_err(&pdev->dev, "No IRQ for 'release', error=%d\n", 77 irq_release); 78 return irq_release; 79 } 80 81 input = devm_input_allocate_device(&pdev->dev); 82 if (!input) 83 return -ENOMEM; 84 85 input->name = "NI Ettus Research USRP E3x0 Button Driver"; 86 input->phys = "e3x0_button/input0"; 87 input->dev.parent = &pdev->dev; 88 89 input_set_capability(input, EV_KEY, KEY_POWER); 90 91 error = devm_request_irq(&pdev->dev, irq_press, 92 e3x0_button_press_handler, 0, 93 "e3x0-button", input); 94 if (error) { 95 dev_err(&pdev->dev, "Failed to request 'press' IRQ#%d: %d\n", 96 irq_press, error); 97 return error; 98 } 99 100 error = devm_request_irq(&pdev->dev, irq_release, 101 e3x0_button_release_handler, 0, 102 "e3x0-button", input); 103 if (error) { 104 dev_err(&pdev->dev, "Failed to request 'release' IRQ#%d: %d\n", 105 irq_release, error); 106 return error; 107 } 108 109 error = input_register_device(input); 110 if (error) { 111 dev_err(&pdev->dev, "Can't register input device: %d\n", error); 112 return error; 113 } 114 115 device_init_wakeup(&pdev->dev, 1); 116 return 0; 117 } 118 119 #ifdef CONFIG_OF 120 static const struct of_device_id e3x0_button_match[] = { 121 { .compatible = "ettus,e3x0-button", }, 122 { } 123 }; 124 MODULE_DEVICE_TABLE(of, e3x0_button_match); 125 #endif 126 127 static struct platform_driver e3x0_button_driver = { 128 .driver = { 129 .name = "e3x0-button", 130 .of_match_table = of_match_ptr(e3x0_button_match), 131 .pm = &e3x0_button_pm_ops, 132 }, 133 .probe = e3x0_button_probe, 134 }; 135 136 module_platform_driver(e3x0_button_driver); 137 138 MODULE_LICENSE("GPL v2"); 139 MODULE_AUTHOR("Moritz Fischer <moritz.fischer@ettus.com>"); 140 MODULE_DESCRIPTION("NI Ettus Research USRP E3x0 Button driver"); 141 MODULE_ALIAS("platform:e3x0-button"); 142