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 state = (bool)msg.state; 82 83 if (state ^ priv->keystate) { 84 priv->keystate = state; 85 input_event(input, EV_KEY, priv->keycode, state); 86 input_sync(input); 87 if (!priv->keystate) 88 pm_relax(priv->input->dev.parent); 89 } 90 91 if (state) 92 schedule_delayed_work(&priv->check_work, 93 msecs_to_jiffies(REPEAT_INTERVAL)); 94 } 95 96 static int imx_sc_key_probe(struct platform_device *pdev) 97 { 98 struct imx_key_drv_data *priv; 99 struct input_dev *input; 100 int error; 101 102 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 103 if (!priv) 104 return -ENOMEM; 105 106 error = imx_scu_get_handle(&priv->key_ipc_handle); 107 if (error) 108 return error; 109 110 if (device_property_read_u32(&pdev->dev, "linux,keycodes", 111 &priv->keycode)) { 112 dev_err(&pdev->dev, "missing linux,keycodes property\n"); 113 return -EINVAL; 114 } 115 116 INIT_DELAYED_WORK(&priv->check_work, imx_sc_check_for_events); 117 118 input = devm_input_allocate_device(&pdev->dev); 119 if (!input) { 120 dev_err(&pdev->dev, "failed to allocate the input device\n"); 121 return -ENOMEM; 122 } 123 124 input->name = pdev->name; 125 input->phys = "imx-sc-key/input0"; 126 input->id.bustype = BUS_HOST; 127 128 input_set_capability(input, EV_KEY, priv->keycode); 129 130 error = input_register_device(input); 131 if (error) { 132 dev_err(&pdev->dev, "failed to register input device\n"); 133 return error; 134 } 135 136 priv->input = input; 137 platform_set_drvdata(pdev, priv); 138 139 error = imx_scu_irq_group_enable(SC_IRQ_GROUP_WAKE, SC_IRQ_BUTTON, 140 true); 141 if (error) { 142 dev_err(&pdev->dev, "failed to enable scu group irq\n"); 143 return error; 144 } 145 146 priv->key_notifier.notifier_call = imx_sc_key_notify; 147 error = imx_scu_irq_register_notifier(&priv->key_notifier); 148 if (error) { 149 imx_scu_irq_group_enable(SC_IRQ_GROUP_WAKE, SC_IRQ_BUTTON, 150 false); 151 dev_err(&pdev->dev, "failed to register scu notifier\n"); 152 return error; 153 } 154 155 return 0; 156 } 157 158 static int imx_sc_key_remove(struct platform_device *pdev) 159 { 160 struct imx_key_drv_data *priv = platform_get_drvdata(pdev); 161 162 imx_scu_irq_group_enable(SC_IRQ_GROUP_WAKE, SC_IRQ_BUTTON, false); 163 imx_scu_irq_unregister_notifier(&priv->key_notifier); 164 cancel_delayed_work_sync(&priv->check_work); 165 166 return 0; 167 } 168 169 static const struct of_device_id imx_sc_key_ids[] = { 170 { .compatible = "fsl,imx-sc-key" }, 171 { /* sentinel */ } 172 }; 173 MODULE_DEVICE_TABLE(of, imx_sc_key_ids); 174 175 static struct platform_driver imx_sc_key_driver = { 176 .driver = { 177 .name = "imx-sc-key", 178 .of_match_table = imx_sc_key_ids, 179 }, 180 .probe = imx_sc_key_probe, 181 .remove = imx_sc_key_remove, 182 }; 183 module_platform_driver(imx_sc_key_driver); 184 185 MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>"); 186 MODULE_DESCRIPTION("i.MX System Controller Key Driver"); 187 MODULE_LICENSE("GPL v2"); 188