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 int imx_sc_key_probe(struct platform_device
*pdev
)
104 struct imx_key_drv_data
*priv
;
105 struct input_dev
*input
;
108 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
112 error
= imx_scu_get_handle(&priv
->key_ipc_handle
);
116 if (device_property_read_u32(&pdev
->dev
, "linux,keycodes",
118 dev_err(&pdev
->dev
, "missing linux,keycodes property\n");
122 INIT_DELAYED_WORK(&priv
->check_work
, imx_sc_check_for_events
);
124 input
= devm_input_allocate_device(&pdev
->dev
);
126 dev_err(&pdev
->dev
, "failed to allocate the input device\n");
130 input
->name
= pdev
->name
;
131 input
->phys
= "imx-sc-key/input0";
132 input
->id
.bustype
= BUS_HOST
;
134 input_set_capability(input
, EV_KEY
, priv
->keycode
);
136 error
= input_register_device(input
);
138 dev_err(&pdev
->dev
, "failed to register input device\n");
143 platform_set_drvdata(pdev
, priv
);
145 error
= imx_scu_irq_group_enable(SC_IRQ_GROUP_WAKE
, SC_IRQ_BUTTON
,
148 dev_err(&pdev
->dev
, "failed to enable scu group irq\n");
152 priv
->key_notifier
.notifier_call
= imx_sc_key_notify
;
153 error
= imx_scu_irq_register_notifier(&priv
->key_notifier
);
155 imx_scu_irq_group_enable(SC_IRQ_GROUP_WAKE
, SC_IRQ_BUTTON
,
157 dev_err(&pdev
->dev
, "failed to register scu notifier\n");
164 static int imx_sc_key_remove(struct platform_device
*pdev
)
166 struct imx_key_drv_data
*priv
= platform_get_drvdata(pdev
);
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
);
175 static const struct of_device_id imx_sc_key_ids
[] = {
176 { .compatible
= "fsl,imx-sc-key" },
179 MODULE_DEVICE_TABLE(of
, imx_sc_key_ids
);
181 static struct platform_driver imx_sc_key_driver
= {
183 .name
= "imx-sc-key",
184 .of_match_table
= imx_sc_key_ids
,
186 .probe
= imx_sc_key_probe
,
187 .remove
= imx_sc_key_remove
,
189 module_platform_driver(imx_sc_key_driver
);
191 MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
192 MODULE_DESCRIPTION("i.MX System Controller Key Driver");
193 MODULE_LICENSE("GPL v2");