Input: dlink-dir685-touchkeys - fix a typo in driver name
[linux/fpc-iii.git] / drivers / input / misc / pm8941-pwrkey.c
blob48153e0ca19a3638be9aa004c107a3dfbfab0f52
1 /*
2 * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
3 * Copyright (c) 2014, Sony Mobile Communications Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/delay.h>
16 #include <linux/errno.h>
17 #include <linux/input.h>
18 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/log2.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/of_device.h>
24 #include <linux/platform_device.h>
25 #include <linux/reboot.h>
26 #include <linux/regmap.h>
28 #define PON_REV2 0x01
30 #define PON_RT_STS 0x10
31 #define PON_KPDPWR_N_SET BIT(0)
32 #define PON_RESIN_N_SET BIT(1)
34 #define PON_PS_HOLD_RST_CTL 0x5a
35 #define PON_PS_HOLD_RST_CTL2 0x5b
36 #define PON_PS_HOLD_ENABLE BIT(7)
37 #define PON_PS_HOLD_TYPE_MASK 0x0f
38 #define PON_PS_HOLD_TYPE_SHUTDOWN 4
39 #define PON_PS_HOLD_TYPE_HARD_RESET 7
41 #define PON_PULL_CTL 0x70
42 #define PON_KPDPWR_PULL_UP BIT(1)
43 #define PON_RESIN_PULL_UP BIT(0)
45 #define PON_DBC_CTL 0x71
46 #define PON_DBC_DELAY_MASK 0x7
48 struct pm8941_data {
49 unsigned int pull_up_bit;
50 unsigned int status_bit;
53 struct pm8941_pwrkey {
54 struct device *dev;
55 int irq;
56 u32 baseaddr;
57 struct regmap *regmap;
58 struct input_dev *input;
60 unsigned int revision;
61 struct notifier_block reboot_notifier;
63 u32 code;
64 const struct pm8941_data *data;
67 static int pm8941_reboot_notify(struct notifier_block *nb,
68 unsigned long code, void *unused)
70 struct pm8941_pwrkey *pwrkey = container_of(nb, struct pm8941_pwrkey,
71 reboot_notifier);
72 unsigned int enable_reg;
73 unsigned int reset_type;
74 int error;
76 /* PMICs with revision 0 have the enable bit in same register as ctrl */
77 if (pwrkey->revision == 0)
78 enable_reg = PON_PS_HOLD_RST_CTL;
79 else
80 enable_reg = PON_PS_HOLD_RST_CTL2;
82 error = regmap_update_bits(pwrkey->regmap,
83 pwrkey->baseaddr + enable_reg,
84 PON_PS_HOLD_ENABLE,
85 0);
86 if (error)
87 dev_err(pwrkey->dev,
88 "unable to clear ps hold reset enable: %d\n",
89 error);
92 * Updates of PON_PS_HOLD_ENABLE requires 3 sleep cycles between
93 * writes.
95 usleep_range(100, 1000);
97 switch (code) {
98 case SYS_HALT:
99 case SYS_POWER_OFF:
100 reset_type = PON_PS_HOLD_TYPE_SHUTDOWN;
101 break;
102 case SYS_RESTART:
103 default:
104 reset_type = PON_PS_HOLD_TYPE_HARD_RESET;
105 break;
108 error = regmap_update_bits(pwrkey->regmap,
109 pwrkey->baseaddr + PON_PS_HOLD_RST_CTL,
110 PON_PS_HOLD_TYPE_MASK,
111 reset_type);
112 if (error)
113 dev_err(pwrkey->dev, "unable to set ps hold reset type: %d\n",
114 error);
116 error = regmap_update_bits(pwrkey->regmap,
117 pwrkey->baseaddr + enable_reg,
118 PON_PS_HOLD_ENABLE,
119 PON_PS_HOLD_ENABLE);
120 if (error)
121 dev_err(pwrkey->dev, "unable to re-set enable: %d\n", error);
123 return NOTIFY_DONE;
126 static irqreturn_t pm8941_pwrkey_irq(int irq, void *_data)
128 struct pm8941_pwrkey *pwrkey = _data;
129 unsigned int sts;
130 int error;
132 error = regmap_read(pwrkey->regmap,
133 pwrkey->baseaddr + PON_RT_STS, &sts);
134 if (error)
135 return IRQ_HANDLED;
137 input_report_key(pwrkey->input, pwrkey->code,
138 sts & pwrkey->data->status_bit);
139 input_sync(pwrkey->input);
141 return IRQ_HANDLED;
144 static int __maybe_unused pm8941_pwrkey_suspend(struct device *dev)
146 struct pm8941_pwrkey *pwrkey = dev_get_drvdata(dev);
148 if (device_may_wakeup(dev))
149 enable_irq_wake(pwrkey->irq);
151 return 0;
154 static int __maybe_unused pm8941_pwrkey_resume(struct device *dev)
156 struct pm8941_pwrkey *pwrkey = dev_get_drvdata(dev);
158 if (device_may_wakeup(dev))
159 disable_irq_wake(pwrkey->irq);
161 return 0;
164 static SIMPLE_DEV_PM_OPS(pm8941_pwr_key_pm_ops,
165 pm8941_pwrkey_suspend, pm8941_pwrkey_resume);
167 static int pm8941_pwrkey_probe(struct platform_device *pdev)
169 struct pm8941_pwrkey *pwrkey;
170 bool pull_up;
171 struct device *parent;
172 u32 req_delay;
173 int error;
175 if (of_property_read_u32(pdev->dev.of_node, "debounce", &req_delay))
176 req_delay = 15625;
178 if (req_delay > 2000000 || req_delay == 0) {
179 dev_err(&pdev->dev, "invalid debounce time: %u\n", req_delay);
180 return -EINVAL;
183 pull_up = of_property_read_bool(pdev->dev.of_node, "bias-pull-up");
185 pwrkey = devm_kzalloc(&pdev->dev, sizeof(*pwrkey), GFP_KERNEL);
186 if (!pwrkey)
187 return -ENOMEM;
189 pwrkey->dev = &pdev->dev;
190 pwrkey->data = of_device_get_match_data(&pdev->dev);
192 parent = pdev->dev.parent;
193 pwrkey->regmap = dev_get_regmap(parent, NULL);
194 if (!pwrkey->regmap) {
196 * We failed to get regmap for parent. Let's see if we are
197 * a child of pon node and read regmap and reg from its
198 * parent.
200 pwrkey->regmap = dev_get_regmap(parent->parent, NULL);
201 if (!pwrkey->regmap) {
202 dev_err(&pdev->dev, "failed to locate regmap\n");
203 return -ENODEV;
206 error = of_property_read_u32(parent->of_node,
207 "reg", &pwrkey->baseaddr);
208 } else {
209 error = of_property_read_u32(pdev->dev.of_node, "reg",
210 &pwrkey->baseaddr);
212 if (error)
213 return error;
215 pwrkey->irq = platform_get_irq(pdev, 0);
216 if (pwrkey->irq < 0) {
217 dev_err(&pdev->dev, "failed to get irq\n");
218 return pwrkey->irq;
221 error = regmap_read(pwrkey->regmap, pwrkey->baseaddr + PON_REV2,
222 &pwrkey->revision);
223 if (error) {
224 dev_err(&pdev->dev, "failed to set debounce: %d\n", error);
225 return error;
228 error = of_property_read_u32(pdev->dev.of_node, "linux,code",
229 &pwrkey->code);
230 if (error) {
231 dev_dbg(&pdev->dev,
232 "no linux,code assuming power (%d)\n", error);
233 pwrkey->code = KEY_POWER;
236 pwrkey->input = devm_input_allocate_device(&pdev->dev);
237 if (!pwrkey->input) {
238 dev_dbg(&pdev->dev, "unable to allocate input device\n");
239 return -ENOMEM;
242 input_set_capability(pwrkey->input, EV_KEY, pwrkey->code);
244 pwrkey->input->name = "pm8941_pwrkey";
245 pwrkey->input->phys = "pm8941_pwrkey/input0";
247 req_delay = (req_delay << 6) / USEC_PER_SEC;
248 req_delay = ilog2(req_delay);
250 error = regmap_update_bits(pwrkey->regmap,
251 pwrkey->baseaddr + PON_DBC_CTL,
252 PON_DBC_DELAY_MASK,
253 req_delay);
254 if (error) {
255 dev_err(&pdev->dev, "failed to set debounce: %d\n", error);
256 return error;
259 error = regmap_update_bits(pwrkey->regmap,
260 pwrkey->baseaddr + PON_PULL_CTL,
261 pwrkey->data->pull_up_bit,
262 pull_up ? pwrkey->data->pull_up_bit : 0);
263 if (error) {
264 dev_err(&pdev->dev, "failed to set pull: %d\n", error);
265 return error;
268 error = devm_request_threaded_irq(&pdev->dev, pwrkey->irq,
269 NULL, pm8941_pwrkey_irq,
270 IRQF_ONESHOT,
271 "pm8941_pwrkey", pwrkey);
272 if (error) {
273 dev_err(&pdev->dev, "failed requesting IRQ: %d\n", error);
274 return error;
277 error = input_register_device(pwrkey->input);
278 if (error) {
279 dev_err(&pdev->dev, "failed to register input device: %d\n",
280 error);
281 return error;
284 pwrkey->reboot_notifier.notifier_call = pm8941_reboot_notify,
285 error = register_reboot_notifier(&pwrkey->reboot_notifier);
286 if (error) {
287 dev_err(&pdev->dev, "failed to register reboot notifier: %d\n",
288 error);
289 return error;
292 platform_set_drvdata(pdev, pwrkey);
293 device_init_wakeup(&pdev->dev, 1);
295 return 0;
298 static int pm8941_pwrkey_remove(struct platform_device *pdev)
300 struct pm8941_pwrkey *pwrkey = platform_get_drvdata(pdev);
302 unregister_reboot_notifier(&pwrkey->reboot_notifier);
304 return 0;
307 static const struct pm8941_data pwrkey_data = {
308 .pull_up_bit = PON_KPDPWR_PULL_UP,
309 .status_bit = PON_KPDPWR_N_SET,
312 static const struct pm8941_data resin_data = {
313 .pull_up_bit = PON_RESIN_PULL_UP,
314 .status_bit = PON_RESIN_N_SET,
317 static const struct of_device_id pm8941_pwr_key_id_table[] = {
318 { .compatible = "qcom,pm8941-pwrkey", .data = &pwrkey_data },
319 { .compatible = "qcom,pm8941-resin", .data = &resin_data },
322 MODULE_DEVICE_TABLE(of, pm8941_pwr_key_id_table);
324 static struct platform_driver pm8941_pwrkey_driver = {
325 .probe = pm8941_pwrkey_probe,
326 .remove = pm8941_pwrkey_remove,
327 .driver = {
328 .name = "pm8941-pwrkey",
329 .pm = &pm8941_pwr_key_pm_ops,
330 .of_match_table = of_match_ptr(pm8941_pwr_key_id_table),
333 module_platform_driver(pm8941_pwrkey_driver);
335 MODULE_DESCRIPTION("PM8941 Power Key driver");
336 MODULE_LICENSE("GPL v2");