1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2019 NXP.
4  */
5 
6 #include <linux/err.h>
7 #include <linux/device.h>
8 #include <linux/firmware/imx/sci.h>
9 #include <linux/init.h>
10 #include <linux/input.h>
11 #include <linux/interrupt.h>
12 #include <linux/jiffies.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 
19 #define DEBOUNCE_TIME				30
20 #define REPEAT_INTERVAL				60
21 
22 #define SC_IRQ_BUTTON				1
23 #define SC_IRQ_GROUP_WAKE			3
24 
25 #define IMX_SC_MISC_FUNC_GET_BUTTON_STATUS	18
26 
27 struct imx_key_drv_data {
28 	u32 keycode;
29 	bool keystate;  /* true: pressed, false: released */
30 	struct delayed_work check_work;
31 	struct input_dev *input;
32 	struct imx_sc_ipc *key_ipc_handle;
33 	struct notifier_block key_notifier;
34 };
35 
36 struct imx_sc_msg_key {
37 	struct imx_sc_rpc_msg hdr;
38 	u32 state;
39 };
40 
41 static int imx_sc_key_notify(struct notifier_block *nb,
42 			     unsigned long event, void *group)
43 {
44 	struct imx_key_drv_data *priv =
45 				 container_of(nb,
46 					      struct imx_key_drv_data,
47 					      key_notifier);
48 
49 	if ((event & SC_IRQ_BUTTON) && (*(u8 *)group == SC_IRQ_GROUP_WAKE)) {
50 		schedule_delayed_work(&priv->check_work,
51 				      msecs_to_jiffies(DEBOUNCE_TIME));
52 		pm_wakeup_event(priv->input->dev.parent, 0);
53 	}
54 
55 	return 0;
56 }
57 
58 static void imx_sc_check_for_events(struct work_struct *work)
59 {
60 	struct imx_key_drv_data *priv =
61 				 container_of(work,
62 					      struct imx_key_drv_data,
63 					      check_work.work);
64 	struct input_dev *input = priv->input;
65 	struct imx_sc_msg_key msg;
66 	struct imx_sc_rpc_msg *hdr = &msg.hdr;
67 	bool state;
68 	int error;
69 
70 	hdr->ver = IMX_SC_RPC_VERSION;
71 	hdr->svc = IMX_SC_RPC_SVC_MISC;
72 	hdr->func = IMX_SC_MISC_FUNC_GET_BUTTON_STATUS;
73 	hdr->size = 1;
74 
75 	error = imx_scu_call_rpc(priv->key_ipc_handle, &msg, true);
76 	if (error) {
77 		dev_err(&input->dev, "read imx sc key failed, error %d\n", error);
78 		return;
79 	}
80 
81 	/*
82 	 * The response data from SCU firmware is 4 bytes,
83 	 * but ONLY the first byte is the key state, other
84 	 * 3 bytes could be some dirty data, so we should
85 	 * ONLY take the first byte as key state.
86 	 */
87 	state = (bool)(msg.state & 0xff);
88 
89 	if (state ^ priv->keystate) {
90 		priv->keystate = state;
91 		input_event(input, EV_KEY, priv->keycode, state);
92 		input_sync(input);
93 		if (!priv->keystate)
94 			pm_relax(priv->input->dev.parent);
95 	}
96 
97 	if (state)
98 		schedule_delayed_work(&priv->check_work,
99 				      msecs_to_jiffies(REPEAT_INTERVAL));
100 }
101 
102 static int imx_sc_key_probe(struct platform_device *pdev)
103 {
104 	struct imx_key_drv_data *priv;
105 	struct input_dev *input;
106 	int error;
107 
108 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
109 	if (!priv)
110 		return -ENOMEM;
111 
112 	error = imx_scu_get_handle(&priv->key_ipc_handle);
113 	if (error)
114 		return error;
115 
116 	if (device_property_read_u32(&pdev->dev, "linux,keycodes",
117 				     &priv->keycode)) {
118 		dev_err(&pdev->dev, "missing linux,keycodes property\n");
119 		return -EINVAL;
120 	}
121 
122 	INIT_DELAYED_WORK(&priv->check_work, imx_sc_check_for_events);
123 
124 	input = devm_input_allocate_device(&pdev->dev);
125 	if (!input) {
126 		dev_err(&pdev->dev, "failed to allocate the input device\n");
127 		return -ENOMEM;
128 	}
129 
130 	input->name = pdev->name;
131 	input->phys = "imx-sc-key/input0";
132 	input->id.bustype = BUS_HOST;
133 
134 	input_set_capability(input, EV_KEY, priv->keycode);
135 
136 	error = input_register_device(input);
137 	if (error) {
138 		dev_err(&pdev->dev, "failed to register input device\n");
139 		return error;
140 	}
141 
142 	priv->input = input;
143 	platform_set_drvdata(pdev, priv);
144 
145 	error = imx_scu_irq_group_enable(SC_IRQ_GROUP_WAKE, SC_IRQ_BUTTON,
146 					 true);
147 	if (error) {
148 		dev_err(&pdev->dev, "failed to enable scu group irq\n");
149 		return error;
150 	}
151 
152 	priv->key_notifier.notifier_call = imx_sc_key_notify;
153 	error = imx_scu_irq_register_notifier(&priv->key_notifier);
154 	if (error) {
155 		imx_scu_irq_group_enable(SC_IRQ_GROUP_WAKE, SC_IRQ_BUTTON,
156 					 false);
157 		dev_err(&pdev->dev, "failed to register scu notifier\n");
158 		return error;
159 	}
160 
161 	return 0;
162 }
163 
164 static int imx_sc_key_remove(struct platform_device *pdev)
165 {
166 	struct imx_key_drv_data *priv = platform_get_drvdata(pdev);
167 
168 	imx_scu_irq_group_enable(SC_IRQ_GROUP_WAKE, SC_IRQ_BUTTON, false);
169 	imx_scu_irq_unregister_notifier(&priv->key_notifier);
170 	cancel_delayed_work_sync(&priv->check_work);
171 
172 	return 0;
173 }
174 
175 static const struct of_device_id imx_sc_key_ids[] = {
176 	{ .compatible = "fsl,imx-sc-key" },
177 	{ /* sentinel */ }
178 };
179 MODULE_DEVICE_TABLE(of, imx_sc_key_ids);
180 
181 static struct platform_driver imx_sc_key_driver = {
182 	.driver = {
183 		.name = "imx-sc-key",
184 		.of_match_table = imx_sc_key_ids,
185 	},
186 	.probe = imx_sc_key_probe,
187 	.remove = imx_sc_key_remove,
188 };
189 module_platform_driver(imx_sc_key_driver);
190 
191 MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
192 MODULE_DESCRIPTION("i.MX System Controller Key Driver");
193 MODULE_LICENSE("GPL v2");
194