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 struct timer_list check_timer; 43 struct input_dev *input; 44 u8 minor_rev; 45 }; 46 47 static void imx_imx_snvs_check_for_events(struct timer_list *t) 48 { 49 struct pwrkey_drv_data *pdata = from_timer(pdata, t, check_timer); 50 struct input_dev *input = pdata->input; 51 u32 state; 52 53 regmap_read(pdata->snvs, SNVS_HPSR_REG, &state); 54 state = state & SNVS_HPSR_BTN ? 1 : 0; 55 56 /* only report new event if status changed */ 57 if (state ^ pdata->keystate) { 58 pdata->keystate = state; 59 input_event(input, EV_KEY, pdata->keycode, state); 60 input_sync(input); 61 pm_relax(pdata->input->dev.parent); 62 } 63 64 /* repeat check if pressed long */ 65 if (state) { 66 mod_timer(&pdata->check_timer, 67 jiffies + msecs_to_jiffies(REPEAT_INTERVAL)); 68 } 69 } 70 71 static irqreturn_t imx_snvs_pwrkey_interrupt(int irq, void *dev_id) 72 { 73 struct platform_device *pdev = dev_id; 74 struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev); 75 struct input_dev *input = pdata->input; 76 u32 lp_status; 77 78 pm_wakeup_event(input->dev.parent, 0); 79 80 regmap_read(pdata->snvs, SNVS_LPSR_REG, &lp_status); 81 if (lp_status & SNVS_LPSR_SPO) { 82 if (pdata->minor_rev == 0) { 83 /* 84 * The first generation i.MX6 SoCs only sends an 85 * interrupt on button release. To mimic power-key 86 * usage, we'll prepend a press event. 87 */ 88 input_report_key(input, pdata->keycode, 1); 89 input_sync(input); 90 input_report_key(input, pdata->keycode, 0); 91 input_sync(input); 92 pm_relax(input->dev.parent); 93 } else { 94 mod_timer(&pdata->check_timer, 95 jiffies + msecs_to_jiffies(DEBOUNCE_TIME)); 96 } 97 } 98 99 /* clear SPO status */ 100 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO); 101 102 return IRQ_HANDLED; 103 } 104 105 static void imx_snvs_pwrkey_act(void *pdata) 106 { 107 struct pwrkey_drv_data *pd = pdata; 108 109 timer_delete_sync(&pd->check_timer); 110 } 111 112 static int imx_snvs_pwrkey_probe(struct platform_device *pdev) 113 { 114 struct pwrkey_drv_data *pdata; 115 struct input_dev *input; 116 struct device_node *np; 117 struct clk *clk; 118 int error; 119 unsigned int val; 120 unsigned int bpt; 121 u32 vid; 122 123 /* Get SNVS register Page */ 124 np = pdev->dev.of_node; 125 if (!np) 126 return -ENODEV; 127 128 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 129 if (!pdata) 130 return -ENOMEM; 131 132 pdata->snvs = syscon_regmap_lookup_by_phandle(np, "regmap"); 133 if (IS_ERR(pdata->snvs)) { 134 dev_err(&pdev->dev, "Can't get snvs syscon\n"); 135 return PTR_ERR(pdata->snvs); 136 } 137 138 if (of_property_read_u32(np, "linux,keycode", &pdata->keycode)) { 139 pdata->keycode = KEY_POWER; 140 dev_warn(&pdev->dev, "KEY_POWER without setting in dts\n"); 141 } 142 143 clk = devm_clk_get_optional_enabled(&pdev->dev, NULL); 144 if (IS_ERR(clk)) { 145 dev_err(&pdev->dev, "Failed to get snvs clock (%pe)\n", clk); 146 return PTR_ERR(clk); 147 } 148 149 pdata->wakeup = of_property_read_bool(np, "wakeup-source"); 150 151 pdata->irq = platform_get_irq(pdev, 0); 152 if (pdata->irq < 0) 153 return -EINVAL; 154 155 error = of_property_read_u32(np, "power-off-time-sec", &val); 156 if (!error) { 157 switch (val) { 158 case 0: 159 bpt = 0x3; 160 break; 161 case 5: 162 case 10: 163 case 15: 164 bpt = (val / 5) - 1; 165 break; 166 default: 167 dev_err(&pdev->dev, 168 "power-off-time-sec %d out of range\n", val); 169 return -EINVAL; 170 } 171 172 regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_BPT_MASK, 173 bpt << SNVS_LPCR_BPT_SHIFT); 174 } 175 176 regmap_read(pdata->snvs, SNVS_HPVIDR1_REG, &vid); 177 pdata->minor_rev = vid & 0xff; 178 179 regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_DEP_EN, SNVS_LPCR_DEP_EN); 180 181 /* clear the unexpected interrupt before driver ready */ 182 regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO); 183 184 timer_setup(&pdata->check_timer, imx_imx_snvs_check_for_events, 0); 185 186 input = devm_input_allocate_device(&pdev->dev); 187 if (!input) { 188 dev_err(&pdev->dev, "failed to allocate the input device\n"); 189 return -ENOMEM; 190 } 191 192 input->name = pdev->name; 193 input->phys = "snvs-pwrkey/input0"; 194 input->id.bustype = BUS_HOST; 195 196 input_set_capability(input, EV_KEY, pdata->keycode); 197 198 /* input customer action to cancel release timer */ 199 error = devm_add_action(&pdev->dev, imx_snvs_pwrkey_act, pdata); 200 if (error) { 201 dev_err(&pdev->dev, "failed to register remove action\n"); 202 return error; 203 } 204 205 pdata->input = input; 206 platform_set_drvdata(pdev, pdata); 207 208 error = devm_request_irq(&pdev->dev, pdata->irq, 209 imx_snvs_pwrkey_interrupt, 210 0, pdev->name, pdev); 211 if (error) { 212 dev_err(&pdev->dev, "interrupt not available.\n"); 213 return error; 214 } 215 216 error = input_register_device(input); 217 if (error < 0) { 218 dev_err(&pdev->dev, "failed to register input device\n"); 219 return error; 220 } 221 222 device_init_wakeup(&pdev->dev, pdata->wakeup); 223 error = dev_pm_set_wake_irq(&pdev->dev, pdata->irq); 224 if (error) 225 dev_err(&pdev->dev, "irq wake enable failed.\n"); 226 227 return 0; 228 } 229 230 static const struct of_device_id imx_snvs_pwrkey_ids[] = { 231 { .compatible = "fsl,sec-v4.0-pwrkey" }, 232 { /* sentinel */ } 233 }; 234 MODULE_DEVICE_TABLE(of, imx_snvs_pwrkey_ids); 235 236 static struct platform_driver imx_snvs_pwrkey_driver = { 237 .driver = { 238 .name = "snvs_pwrkey", 239 .of_match_table = imx_snvs_pwrkey_ids, 240 }, 241 .probe = imx_snvs_pwrkey_probe, 242 }; 243 module_platform_driver(imx_snvs_pwrkey_driver); 244 245 MODULE_AUTHOR("Freescale Semiconductor"); 246 MODULE_DESCRIPTION("i.MX snvs power key Driver"); 247 MODULE_LICENSE("GPL"); 248