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