1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // Driver for the IMX SNVS ON/OFF Power Key 4 // Copyright (C) 2015 Freescale Semiconductor, Inc. All Rights Reserved. 5 6 #include <linux/clk.h> 7 #include <linux/device.h> 8 #include <linux/err.h> 9 #include <linux/init.h> 10 #include <linux/input.h> 11 #include <linux/interrupt.h> 12 #include <linux/io.h> 13 #include <linux/jiffies.h> 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/of.h> 17 #include <linux/of_address.h> 18 #include <linux/platform_device.h> 19 #include <linux/pm_wakeirq.h> 20 #include <linux/mfd/syscon.h> 21 #include <linux/regmap.h> 22 23 #define SNVS_HPVIDR1_REG 0xBF8 24 #define SNVS_LPSR_REG 0x4C /* LP Status Register */ 25 #define SNVS_LPCR_REG 0x38 /* LP Control Register */ 26 #define SNVS_HPSR_REG 0x14 27 #define SNVS_HPSR_BTN BIT(6) 28 #define SNVS_LPSR_SPO BIT(18) 29 #define SNVS_LPCR_DEP_EN BIT(5) 30 #define SNVS_LPCR_BPT_SHIFT 16 31 #define SNVS_LPCR_BPT_MASK (3 << SNVS_LPCR_BPT_SHIFT) 32 33 #define DEBOUNCE_TIME 30 34 #define REPEAT_INTERVAL 60 35 36 struct pwrkey_drv_data { 37 struct regmap *snvs; 38 int irq; 39 int keycode; 40 int keystate; /* 1:pressed */ 41 int wakeup; 42 bool suspended; /* Track suspend state */ 43 bool pending_press; /* Key pressed during suspend, report from timer callback */ 44 spinlock_t lock; /* Protects keystate, suspended and pending_press */ 45 struct timer_list check_timer; 46 struct input_dev *input; 47 u8 minor_rev; 48 }; 49 50 static void imx_imx_snvs_check_for_events(struct timer_list *t) 51 { 52 struct pwrkey_drv_data *pdata = timer_container_of(pdata, t, 53 check_timer); 54 struct input_dev *input = pdata->input; 55 bool state_changed = false; 56 bool pending_press; 57 u32 state; 58 59 regmap_read(pdata->snvs, SNVS_HPSR_REG, &state); 60 state = state & SNVS_HPSR_BTN ? 1 : 0; 61 62 scoped_guard(spinlock_irqsave, &pdata->lock) { 63 pending_press = pdata->pending_press; 64 if (pending_press) { 65 pdata->pending_press = false; 66 pdata->keystate = 1; 67 } 68 /* only report new event if status changed */ 69 if (state ^ pdata->keystate) { 70 pdata->keystate = state; 71 state_changed = true; 72 } 73 } 74 75 /* 76 * Report a press event latched during suspend. If the key is still 77 * held, state_changed will be 0 (keystate already set to 1 above), 78 * so no duplicate press is reported. If already released, 79 * state_changed will fire next to report the release. 80 */ 81 if (pending_press) { 82 input_report_key(input, pdata->keycode, 1); 83 input_sync(input); 84 } 85 86 if (state_changed) { 87 input_event(input, EV_KEY, pdata->keycode, state); 88 input_sync(input); 89 pm_relax(pdata->input->dev.parent); 90 } 91 92 /* repeat check if pressed long */ 93 if (state) { 94 mod_timer(&pdata->check_timer, 95 jiffies + msecs_to_jiffies(REPEAT_INTERVAL)); 96 } 97 } 98 99 static irqreturn_t imx_snvs_pwrkey_interrupt(int irq, void *dev_id) 100 { 101 struct platform_device *pdev = dev_id; 102 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev); 103 struct input_dev *input = pdata->input; 104 u32 lp_status; 105 106 pm_wakeup_event(input->dev.parent, 0); 107 108 regmap_read(pdata->snvs, SNVS_LPSR_REG, &lp_status); 109 if (lp_status & SNVS_LPSR_SPO) { 110 if (pdata->minor_rev == 0) { 111 /* 112 * The first generation i.MX6 SoCs only sends an 113 * interrupt on button release. To mimic power-key 114 * usage, we'll prepend a press event. 115 */ 116 input_report_key(input, pdata->keycode, 1); 117 input_sync(input); 118 input_report_key(input, pdata->keycode, 0); 119 input_sync(input); 120 pm_relax(input->dev.parent); 121 } else { 122 /* 123 * If the key is pressed during suspend, latch it so 124 * the timer callback can report the press event in 125 * softirq context, avoiding out-of-order events. 126 */ 127 scoped_guard(spinlock_irqsave, &pdata->lock) { 128 if (pdata->suspended) 129 pdata->pending_press = true; 130 } 131 mod_timer(&pdata->check_timer, 132 jiffies + msecs_to_jiffies(DEBOUNCE_TIME)); 133 } 134 } 135 136 /* clear SPO status */ 137 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO); 138 139 return IRQ_HANDLED; 140 } 141 142 static void imx_snvs_pwrkey_act(void *pdata) 143 { 144 struct pwrkey_drv_data *pd = pdata; 145 146 timer_delete_sync(&pd->check_timer); 147 } 148 149 static int imx_snvs_pwrkey_probe(struct platform_device *pdev) 150 { 151 struct device *dev = &pdev->dev; 152 struct pwrkey_drv_data *pdata; 153 struct input_dev *input; 154 struct device_node *np; 155 struct clk *clk; 156 int error; 157 unsigned int val; 158 unsigned int bpt; 159 u32 vid; 160 161 /* Get SNVS register Page */ 162 np = dev->of_node; 163 if (!np) 164 return dev_err_probe(dev, -ENODEV, "Device tree node not found\n"); 165 166 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 167 if (!pdata) 168 return -ENOMEM; 169 170 pdata->snvs = syscon_regmap_lookup_by_phandle(np, "regmap"); 171 if (IS_ERR(pdata->snvs)) 172 return dev_err_probe(dev, PTR_ERR(pdata->snvs), "Can't get snvs syscon\n"); 173 174 if (of_property_read_u32(np, "linux,keycode", &pdata->keycode)) { 175 pdata->keycode = KEY_POWER; 176 dev_warn(dev, "KEY_POWER without setting in dts\n"); 177 } 178 179 clk = devm_clk_get_optional_enabled(dev, NULL); 180 if (IS_ERR(clk)) 181 return dev_err_probe(dev, PTR_ERR(clk), 182 "Failed to get snvs clock (%pe)\n", clk); 183 184 pdata->wakeup = of_property_read_bool(np, "wakeup-source"); 185 186 pdata->irq = platform_get_irq(pdev, 0); 187 if (pdata->irq < 0) 188 return pdata->irq; 189 190 spin_lock_init(&pdata->lock); 191 error = of_property_read_u32(np, "power-off-time-sec", &val); 192 if (!error) { 193 switch (val) { 194 case 0: 195 bpt = 0x3; 196 break; 197 case 5: 198 case 10: 199 case 15: 200 bpt = (val / 5) - 1; 201 break; 202 default: 203 return dev_err_probe(dev, -EINVAL, 204 "power-off-time-sec %d out of range\n", val); 205 } 206 207 regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_BPT_MASK, 208 bpt << SNVS_LPCR_BPT_SHIFT); 209 } 210 211 regmap_read(pdata->snvs, SNVS_HPVIDR1_REG, &vid); 212 pdata->minor_rev = vid & 0xff; 213 214 regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_DEP_EN, SNVS_LPCR_DEP_EN); 215 216 /* clear the unexpected interrupt before driver ready */ 217 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO); 218 219 timer_setup(&pdata->check_timer, imx_imx_snvs_check_for_events, 0); 220 221 input = devm_input_allocate_device(dev); 222 if (!input) 223 return dev_err_probe(dev, -ENOMEM, "failed to allocate the input device\n"); 224 225 input->name = pdev->name; 226 input->phys = "snvs-pwrkey/input0"; 227 input->id.bustype = BUS_HOST; 228 229 input_set_capability(input, EV_KEY, pdata->keycode); 230 231 /* input customer action to cancel release timer */ 232 error = devm_add_action(dev, imx_snvs_pwrkey_act, pdata); 233 if (error) 234 return dev_err_probe(dev, error, "failed to register remove action\n"); 235 236 pdata->input = input; 237 platform_set_drvdata(pdev, pdata); 238 239 error = devm_request_irq(dev, pdata->irq, 240 imx_snvs_pwrkey_interrupt, 241 0, pdev->name, pdev); 242 if (error) 243 return dev_err_probe(dev, error, "interrupt not available.\n"); 244 245 error = input_register_device(input); 246 if (error < 0) 247 return dev_err_probe(dev, error, "failed to register input device\n"); 248 249 device_init_wakeup(dev, pdata->wakeup); 250 error = dev_pm_set_wake_irq(dev, pdata->irq); 251 if (error) 252 dev_err(dev, "irq wake enable failed.\n"); 253 254 return 0; 255 } 256 257 static int imx_snvs_pwrkey_suspend(struct device *dev) 258 { 259 struct platform_device *pdev = to_platform_device(dev); 260 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev); 261 262 guard(spinlock_irq)(&pdata->lock); 263 pdata->suspended = true; 264 265 return 0; 266 } 267 268 static int imx_snvs_pwrkey_resume(struct device *dev) 269 { 270 struct platform_device *pdev = to_platform_device(dev); 271 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev); 272 273 guard(spinlock_irq)(&pdata->lock); 274 pdata->suspended = false; 275 276 return 0; 277 } 278 279 static DEFINE_SIMPLE_DEV_PM_OPS(imx_snvs_pwrkey_pm_ops, 280 imx_snvs_pwrkey_suspend, 281 imx_snvs_pwrkey_resume); 282 283 static const struct of_device_id imx_snvs_pwrkey_ids[] = { 284 { .compatible = "fsl,sec-v4.0-pwrkey" }, 285 { /* sentinel */ } 286 }; 287 MODULE_DEVICE_TABLE(of, imx_snvs_pwrkey_ids); 288 289 static struct platform_driver imx_snvs_pwrkey_driver = { 290 .driver = { 291 .name = "snvs_pwrkey", 292 .of_match_table = imx_snvs_pwrkey_ids, 293 .pm = pm_ptr(&imx_snvs_pwrkey_pm_ops), 294 }, 295 .probe = imx_snvs_pwrkey_probe, 296 }; 297 module_platform_driver(imx_snvs_pwrkey_driver); 298 299 MODULE_AUTHOR("Freescale Semiconductor"); 300 MODULE_DESCRIPTION("i.MX snvs power key Driver"); 301 MODULE_LICENSE("GPL"); 302