xref: /openbmc/linux/drivers/input/misc/e3x0-button.c (revision d2f4a190)
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 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 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 DEFINE_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 		return irq_press;
70 
71 	irq_release = platform_get_irq_byname(pdev, "release");
72 	if (irq_release < 0)
73 		return irq_release;
74 
75 	input = devm_input_allocate_device(&pdev->dev);
76 	if (!input)
77 		return -ENOMEM;
78 
79 	input->name = "NI Ettus Research USRP E3x0 Button Driver";
80 	input->phys = "e3x0_button/input0";
81 	input->dev.parent = &pdev->dev;
82 
83 	input_set_capability(input, EV_KEY, KEY_POWER);
84 
85 	error = devm_request_irq(&pdev->dev, irq_press,
86 				 e3x0_button_press_handler, 0,
87 				 "e3x0-button", input);
88 	if (error) {
89 		dev_err(&pdev->dev, "Failed to request 'press' IRQ#%d: %d\n",
90 			irq_press, error);
91 		return error;
92 	}
93 
94 	error = devm_request_irq(&pdev->dev, irq_release,
95 				 e3x0_button_release_handler, 0,
96 				 "e3x0-button", input);
97 	if (error) {
98 		dev_err(&pdev->dev, "Failed to request 'release' IRQ#%d: %d\n",
99 			irq_release, error);
100 		return error;
101 	}
102 
103 	error = input_register_device(input);
104 	if (error) {
105 		dev_err(&pdev->dev, "Can't register input device: %d\n", error);
106 		return error;
107 	}
108 
109 	device_init_wakeup(&pdev->dev, 1);
110 	return 0;
111 }
112 
113 #ifdef CONFIG_OF
114 static const struct of_device_id e3x0_button_match[] = {
115 	{ .compatible = "ettus,e3x0-button", },
116 	{ }
117 };
118 MODULE_DEVICE_TABLE(of, e3x0_button_match);
119 #endif
120 
121 static struct platform_driver e3x0_button_driver = {
122 	.driver		= {
123 		.name	= "e3x0-button",
124 		.of_match_table = of_match_ptr(e3x0_button_match),
125 		.pm	= pm_sleep_ptr(&e3x0_button_pm_ops),
126 	},
127 	.probe		= e3x0_button_probe,
128 };
129 
130 module_platform_driver(e3x0_button_driver);
131 
132 MODULE_LICENSE("GPL v2");
133 MODULE_AUTHOR("Moritz Fischer <moritz.fischer@ettus.com>");
134 MODULE_DESCRIPTION("NI Ettus Research USRP E3x0 Button driver");
135 MODULE_ALIAS("platform:e3x0-button");
136