1 // SPDX-License-Identifier: GPL-2.0
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>
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
{
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
;
41 static int imx_sc_key_notify(struct notifier_block
*nb
,
42 unsigned long event
, void *group
)
44 struct imx_key_drv_data
*priv
=
46 struct imx_key_drv_data
,
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);
58 static void imx_sc_check_for_events(struct work_struct
*work
)
60 struct imx_key_drv_data
*priv
=
62 struct imx_key_drv_data
,
64 struct input_dev
*input
= priv
->input
;
65 struct imx_sc_msg_key msg
;
66 struct imx_sc_rpc_msg
*hdr
= &msg
.hdr
;
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
;
75 error
= imx_scu_call_rpc(priv
->key_ipc_handle
, &msg
, true);
77 dev_err(&input
->dev
, "read imx sc key failed, error %d\n", error
);
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
);
94 pm_relax(priv
->input
->dev
.parent
);
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
;
117 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
121 error
= imx_scu_get_handle(&priv
->key_ipc_handle
);
125 if (device_property_read_u32(&pdev
->dev
, "linux,keycodes",
127 dev_err(&pdev
->dev
, "missing linux,keycodes property\n");
131 INIT_DELAYED_WORK(&priv
->check_work
, imx_sc_check_for_events
);
133 input
= devm_input_allocate_device(&pdev
->dev
);
135 dev_err(&pdev
->dev
, "failed to allocate the input device\n");
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
);
147 dev_err(&pdev
->dev
, "failed to register input device\n");
152 platform_set_drvdata(pdev
, priv
);
154 error
= imx_scu_irq_group_enable(SC_IRQ_GROUP_WAKE
, SC_IRQ_BUTTON
,
157 dev_err(&pdev
->dev
, "failed to enable scu group irq\n");
161 error
= devm_add_action_or_reset(&pdev
->dev
, imx_sc_key_action
, &priv
);
165 priv
->key_notifier
.notifier_call
= imx_sc_key_notify
;
166 error
= imx_scu_irq_register_notifier(&priv
->key_notifier
);
168 dev_err(&pdev
->dev
, "failed to register scu notifier\n");
173 static const struct of_device_id imx_sc_key_ids
[] = {
174 { .compatible
= "fsl,imx-sc-key" },
177 MODULE_DEVICE_TABLE(of
, imx_sc_key_ids
);
179 static struct platform_driver imx_sc_key_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");