drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / input / keyboard / imx_sc_key.c
blobd18839f1f4f60dedb52f1b77fd9337ad724faa5d
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2019 NXP.
4 */
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>
19 #define DEBOUNCE_TIME 30
20 #define REPEAT_INTERVAL 60
22 #define SC_IRQ_BUTTON 1
23 #define SC_IRQ_GROUP_WAKE 3
25 #define IMX_SC_MISC_FUNC_GET_BUTTON_STATUS 18
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;
36 struct imx_sc_msg_key {
37 struct imx_sc_rpc_msg hdr;
38 u32 state;
41 static int imx_sc_key_notify(struct notifier_block *nb,
42 unsigned long event, void *group)
44 struct imx_key_drv_data *priv =
45 container_of(nb,
46 struct imx_key_drv_data,
47 key_notifier);
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);
55 return 0;
58 static void imx_sc_check_for_events(struct work_struct *work)
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;
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;
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;
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.
87 state = (bool)(msg.state & 0xff);
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);
97 if (state)
98 schedule_delayed_work(&priv->check_work,
99 msecs_to_jiffies(REPEAT_INTERVAL));
102 static void imx_sc_key_action(void *data)
104 struct imx_key_drv_data *priv = data;
106 imx_scu_irq_group_enable(SC_IRQ_GROUP_WAKE, SC_IRQ_BUTTON, false);
107 imx_scu_irq_unregister_notifier(&priv->key_notifier);
108 cancel_delayed_work_sync(&priv->check_work);
111 static int imx_sc_key_probe(struct platform_device *pdev)
113 struct imx_key_drv_data *priv;
114 struct input_dev *input;
115 int error;
117 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
118 if (!priv)
119 return -ENOMEM;
121 error = imx_scu_get_handle(&priv->key_ipc_handle);
122 if (error)
123 return error;
125 if (device_property_read_u32(&pdev->dev, "linux,keycodes",
126 &priv->keycode)) {
127 dev_err(&pdev->dev, "missing linux,keycodes property\n");
128 return -EINVAL;
131 INIT_DELAYED_WORK(&priv->check_work, imx_sc_check_for_events);
133 input = devm_input_allocate_device(&pdev->dev);
134 if (!input) {
135 dev_err(&pdev->dev, "failed to allocate the input device\n");
136 return -ENOMEM;
139 input->name = pdev->name;
140 input->phys = "imx-sc-key/input0";
141 input->id.bustype = BUS_HOST;
143 input_set_capability(input, EV_KEY, priv->keycode);
145 error = input_register_device(input);
146 if (error) {
147 dev_err(&pdev->dev, "failed to register input device\n");
148 return error;
151 priv->input = input;
152 platform_set_drvdata(pdev, priv);
154 error = imx_scu_irq_group_enable(SC_IRQ_GROUP_WAKE, SC_IRQ_BUTTON,
155 true);
156 if (error) {
157 dev_err(&pdev->dev, "failed to enable scu group irq\n");
158 return error;
161 error = devm_add_action_or_reset(&pdev->dev, imx_sc_key_action, &priv);
162 if (error)
163 return error;
165 priv->key_notifier.notifier_call = imx_sc_key_notify;
166 error = imx_scu_irq_register_notifier(&priv->key_notifier);
167 if (error)
168 dev_err(&pdev->dev, "failed to register scu notifier\n");
170 return error;
173 static const struct of_device_id imx_sc_key_ids[] = {
174 { .compatible = "fsl,imx-sc-key" },
175 { /* sentinel */ }
177 MODULE_DEVICE_TABLE(of, imx_sc_key_ids);
179 static struct platform_driver imx_sc_key_driver = {
180 .driver = {
181 .name = "imx-sc-key",
182 .of_match_table = imx_sc_key_ids,
184 .probe = imx_sc_key_probe,
186 module_platform_driver(imx_sc_key_driver);
188 MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
189 MODULE_DESCRIPTION("i.MX System Controller Key Driver");
190 MODULE_LICENSE("GPL v2");