Robin Gong | d3dc6e2 | 2015-05-27 00:26:00 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the IMX SNVS ON/OFF Power Key |
| 3 | * Copyright (C) 2015 Freescale Semiconductor, Inc. All Rights Reserved. |
| 4 | * |
| 5 | * The code contained herein is licensed under the GNU General Public |
| 6 | * License. You may obtain a copy of the GNU General Public License |
| 7 | * Version 2 or later at the following locations: |
| 8 | * |
| 9 | * http://www.opensource.org/licenses/gpl-license.html |
| 10 | * http://www.gnu.org/copyleft/gpl.html |
| 11 | */ |
| 12 | |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/input.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/jiffies.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/of_address.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/mfd/syscon.h> |
| 26 | #include <linux/regmap.h> |
| 27 | |
| 28 | #define SNVS_LPSR_REG 0x4C /* LP Status Register */ |
| 29 | #define SNVS_LPCR_REG 0x38 /* LP Control Register */ |
| 30 | #define SNVS_HPSR_REG 0x14 |
| 31 | #define SNVS_HPSR_BTN BIT(6) |
| 32 | #define SNVS_LPSR_SPO BIT(18) |
| 33 | #define SNVS_LPCR_DEP_EN BIT(5) |
| 34 | |
| 35 | #define DEBOUNCE_TIME 30 |
| 36 | #define REPEAT_INTERVAL 60 |
| 37 | |
| 38 | struct pwrkey_drv_data { |
| 39 | struct regmap *snvs; |
| 40 | int irq; |
| 41 | int keycode; |
| 42 | int keystate; /* 1:pressed */ |
| 43 | int wakeup; |
| 44 | struct timer_list check_timer; |
| 45 | struct input_dev *input; |
| 46 | }; |
| 47 | |
Kees Cook | 4ea40278 | 2017-10-24 09:40:57 -0700 | [diff] [blame] | 48 | static void imx_imx_snvs_check_for_events(struct timer_list *t) |
Robin Gong | d3dc6e2 | 2015-05-27 00:26:00 +0800 | [diff] [blame] | 49 | { |
Kees Cook | 4ea40278 | 2017-10-24 09:40:57 -0700 | [diff] [blame] | 50 | struct pwrkey_drv_data *pdata = from_timer(pdata, t, check_timer); |
Robin Gong | d3dc6e2 | 2015-05-27 00:26:00 +0800 | [diff] [blame] | 51 | struct input_dev *input = pdata->input; |
| 52 | u32 state; |
| 53 | |
| 54 | regmap_read(pdata->snvs, SNVS_HPSR_REG, &state); |
| 55 | state = state & SNVS_HPSR_BTN ? 1 : 0; |
| 56 | |
| 57 | /* only report new event if status changed */ |
| 58 | if (state ^ pdata->keystate) { |
| 59 | pdata->keystate = state; |
| 60 | input_event(input, EV_KEY, pdata->keycode, state); |
| 61 | input_sync(input); |
| 62 | pm_relax(pdata->input->dev.parent); |
| 63 | } |
| 64 | |
| 65 | /* repeat check if pressed long */ |
| 66 | if (state) { |
| 67 | mod_timer(&pdata->check_timer, |
| 68 | jiffies + msecs_to_jiffies(REPEAT_INTERVAL)); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static irqreturn_t imx_snvs_pwrkey_interrupt(int irq, void *dev_id) |
| 73 | { |
| 74 | struct platform_device *pdev = dev_id; |
| 75 | struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev); |
| 76 | u32 lp_status; |
| 77 | |
| 78 | pm_wakeup_event(pdata->input->dev.parent, 0); |
| 79 | |
| 80 | regmap_read(pdata->snvs, SNVS_LPSR_REG, &lp_status); |
| 81 | if (lp_status & SNVS_LPSR_SPO) |
| 82 | mod_timer(&pdata->check_timer, jiffies + msecs_to_jiffies(DEBOUNCE_TIME)); |
| 83 | |
| 84 | /* clear SPO status */ |
| 85 | regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO); |
| 86 | |
| 87 | return IRQ_HANDLED; |
| 88 | } |
| 89 | |
| 90 | static void imx_snvs_pwrkey_act(void *pdata) |
| 91 | { |
| 92 | struct pwrkey_drv_data *pd = pdata; |
| 93 | |
| 94 | del_timer_sync(&pd->check_timer); |
| 95 | } |
| 96 | |
| 97 | static int imx_snvs_pwrkey_probe(struct platform_device *pdev) |
| 98 | { |
| 99 | struct pwrkey_drv_data *pdata = NULL; |
| 100 | struct input_dev *input = NULL; |
| 101 | struct device_node *np; |
| 102 | int error; |
| 103 | |
| 104 | /* Get SNVS register Page */ |
| 105 | np = pdev->dev.of_node; |
| 106 | if (!np) |
| 107 | return -ENODEV; |
| 108 | |
| 109 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 110 | if (!pdata) |
| 111 | return -ENOMEM; |
| 112 | |
Javier Martinez Canillas | 9ace41a | 2015-10-26 01:45:01 -0700 | [diff] [blame] | 113 | pdata->snvs = syscon_regmap_lookup_by_phandle(np, "regmap"); |
Vladimir Zapolskiy | 9979c1c | 2016-03-08 11:06:39 -0800 | [diff] [blame] | 114 | if (IS_ERR(pdata->snvs)) { |
Robin Gong | d3dc6e2 | 2015-05-27 00:26:00 +0800 | [diff] [blame] | 115 | dev_err(&pdev->dev, "Can't get snvs syscon\n"); |
Vladimir Zapolskiy | 9979c1c | 2016-03-08 11:06:39 -0800 | [diff] [blame] | 116 | return PTR_ERR(pdata->snvs); |
Robin Gong | d3dc6e2 | 2015-05-27 00:26:00 +0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if (of_property_read_u32(np, "linux,keycode", &pdata->keycode)) { |
| 120 | pdata->keycode = KEY_POWER; |
| 121 | dev_warn(&pdev->dev, "KEY_POWER without setting in dts\n"); |
| 122 | } |
| 123 | |
Shawn Guo | def56bb | 2015-07-15 10:36:37 +0800 | [diff] [blame] | 124 | pdata->wakeup = of_property_read_bool(np, "wakeup-source"); |
Robin Gong | d3dc6e2 | 2015-05-27 00:26:00 +0800 | [diff] [blame] | 125 | |
| 126 | pdata->irq = platform_get_irq(pdev, 0); |
| 127 | if (pdata->irq < 0) { |
| 128 | dev_err(&pdev->dev, "no irq defined in platform data\n"); |
| 129 | return -EINVAL; |
| 130 | } |
| 131 | |
| 132 | regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_DEP_EN, SNVS_LPCR_DEP_EN); |
| 133 | |
| 134 | /* clear the unexpected interrupt before driver ready */ |
| 135 | regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO); |
| 136 | |
Kees Cook | 4ea40278 | 2017-10-24 09:40:57 -0700 | [diff] [blame] | 137 | timer_setup(&pdata->check_timer, imx_imx_snvs_check_for_events, 0); |
Robin Gong | d3dc6e2 | 2015-05-27 00:26:00 +0800 | [diff] [blame] | 138 | |
| 139 | input = devm_input_allocate_device(&pdev->dev); |
| 140 | if (!input) { |
| 141 | dev_err(&pdev->dev, "failed to allocate the input device\n"); |
| 142 | return -ENOMEM; |
| 143 | } |
| 144 | |
| 145 | input->name = pdev->name; |
| 146 | input->phys = "snvs-pwrkey/input0"; |
| 147 | input->id.bustype = BUS_HOST; |
| 148 | |
| 149 | input_set_capability(input, EV_KEY, pdata->keycode); |
| 150 | |
| 151 | /* input customer action to cancel release timer */ |
| 152 | error = devm_add_action(&pdev->dev, imx_snvs_pwrkey_act, pdata); |
| 153 | if (error) { |
| 154 | dev_err(&pdev->dev, "failed to register remove action\n"); |
| 155 | return error; |
| 156 | } |
| 157 | |
| 158 | error = devm_request_irq(&pdev->dev, pdata->irq, |
| 159 | imx_snvs_pwrkey_interrupt, |
| 160 | 0, pdev->name, pdev); |
| 161 | |
| 162 | if (error) { |
| 163 | dev_err(&pdev->dev, "interrupt not available.\n"); |
| 164 | return error; |
| 165 | } |
| 166 | |
| 167 | error = input_register_device(input); |
| 168 | if (error < 0) { |
| 169 | dev_err(&pdev->dev, "failed to register input device\n"); |
Robin Gong | d3dc6e2 | 2015-05-27 00:26:00 +0800 | [diff] [blame] | 170 | return error; |
| 171 | } |
| 172 | |
| 173 | pdata->input = input; |
| 174 | platform_set_drvdata(pdev, pdata); |
| 175 | |
| 176 | device_init_wakeup(&pdev->dev, pdata->wakeup); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
Arnd Bergmann | 2d1a792 | 2016-03-02 09:11:46 -0800 | [diff] [blame] | 181 | static int __maybe_unused imx_snvs_pwrkey_suspend(struct device *dev) |
Robin Gong | d3dc6e2 | 2015-05-27 00:26:00 +0800 | [diff] [blame] | 182 | { |
| 183 | struct platform_device *pdev = to_platform_device(dev); |
| 184 | struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev); |
| 185 | |
| 186 | if (device_may_wakeup(&pdev->dev)) |
| 187 | enable_irq_wake(pdata->irq); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Arnd Bergmann | 2d1a792 | 2016-03-02 09:11:46 -0800 | [diff] [blame] | 192 | static int __maybe_unused imx_snvs_pwrkey_resume(struct device *dev) |
Robin Gong | d3dc6e2 | 2015-05-27 00:26:00 +0800 | [diff] [blame] | 193 | { |
| 194 | struct platform_device *pdev = to_platform_device(dev); |
| 195 | struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev); |
| 196 | |
| 197 | if (device_may_wakeup(&pdev->dev)) |
| 198 | disable_irq_wake(pdata->irq); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static const struct of_device_id imx_snvs_pwrkey_ids[] = { |
| 204 | { .compatible = "fsl,sec-v4.0-pwrkey" }, |
| 205 | { /* sentinel */ } |
| 206 | }; |
| 207 | MODULE_DEVICE_TABLE(of, imx_snvs_pwrkey_ids); |
| 208 | |
| 209 | static SIMPLE_DEV_PM_OPS(imx_snvs_pwrkey_pm_ops, imx_snvs_pwrkey_suspend, |
| 210 | imx_snvs_pwrkey_resume); |
| 211 | |
| 212 | static struct platform_driver imx_snvs_pwrkey_driver = { |
| 213 | .driver = { |
| 214 | .name = "snvs_pwrkey", |
| 215 | .pm = &imx_snvs_pwrkey_pm_ops, |
| 216 | .of_match_table = imx_snvs_pwrkey_ids, |
| 217 | }, |
| 218 | .probe = imx_snvs_pwrkey_probe, |
| 219 | }; |
| 220 | module_platform_driver(imx_snvs_pwrkey_driver); |
| 221 | |
| 222 | MODULE_AUTHOR("Freescale Semiconductor"); |
| 223 | MODULE_DESCRIPTION("i.MX snvs power key Driver"); |
| 224 | MODULE_LICENSE("GPL"); |