1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Power Button driver for RAVE SP
4 //
5 // Copyright (C) 2017 Zodiac Inflight Innovations
6 //
7 //
8 
9 #include <linux/input.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/mfd/rave-sp.h>
13 #include <linux/platform_device.h>
14 
15 #define RAVE_SP_EVNT_BUTTON_PRESS	(RAVE_SP_EVNT_BASE + 0x00)
16 
17 struct rave_sp_power_button {
18 	struct input_dev *idev;
19 	struct notifier_block nb;
20 };
21 
22 static int rave_sp_power_button_event(struct notifier_block *nb,
23 				      unsigned long action, void *data)
24 {
25 	struct rave_sp_power_button *pb =
26 		container_of(nb, struct rave_sp_power_button, nb);
27 	const u8 event = rave_sp_action_unpack_event(action);
28 	const u8 value = rave_sp_action_unpack_value(action);
29 	struct input_dev *idev = pb->idev;
30 
31 	if (event == RAVE_SP_EVNT_BUTTON_PRESS) {
32 		input_report_key(idev, KEY_POWER, value);
33 		input_sync(idev);
34 
35 		return NOTIFY_STOP;
36 	}
37 
38 	return NOTIFY_DONE;
39 }
40 
41 static int rave_sp_pwrbutton_probe(struct platform_device *pdev)
42 {
43 	struct device *dev = &pdev->dev;
44 	struct rave_sp_power_button *pb;
45 	struct input_dev *idev;
46 	int error;
47 
48 	pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL);
49 	if (!pb)
50 		return -ENOMEM;
51 
52 	idev = devm_input_allocate_device(dev);
53 	if (!idev)
54 		return -ENOMEM;
55 
56 	idev->name = pdev->name;
57 
58 	input_set_capability(idev, EV_KEY, KEY_POWER);
59 
60 	error = input_register_device(idev);
61 	if (error)
62 		return error;
63 
64 	pb->idev = idev;
65 	pb->nb.notifier_call = rave_sp_power_button_event;
66 	pb->nb.priority = 128;
67 
68 	error = devm_rave_sp_register_event_notifier(dev, &pb->nb);
69 	if (error)
70 		return error;
71 
72 	return 0;
73 }
74 
75 static const struct of_device_id rave_sp_pwrbutton_of_match[] = {
76 	{ .compatible = "zii,rave-sp-pwrbutton" },
77 	{}
78 };
79 
80 static struct platform_driver rave_sp_pwrbutton_driver = {
81 	.probe = rave_sp_pwrbutton_probe,
82 	.driver	= {
83 		.name = KBUILD_MODNAME,
84 		.of_match_table = rave_sp_pwrbutton_of_match,
85 	},
86 };
87 module_platform_driver(rave_sp_pwrbutton_driver);
88 
89 MODULE_DEVICE_TABLE(of, rave_sp_pwrbutton_of_match);
90 MODULE_LICENSE("GPL");
91 MODULE_AUTHOR("Andrey Vostrikov <andrey.vostrikov@cogentembedded.com>");
92 MODULE_AUTHOR("Nikita Yushchenko <nikita.yoush@cogentembedded.com>");
93 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
94 MODULE_DESCRIPTION("RAVE SP Power Button driver");
95