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>
23 #include <linux/of_device.h>
24 #include <linux/platform_device.h>
25 #include <linux/reboot.h>
26 #include <linux/regmap.h>
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
49 unsigned int pull_up_bit
;
50 unsigned int status_bit
;
53 struct pm8941_pwrkey
{
57 struct regmap
*regmap
;
58 struct input_dev
*input
;
60 unsigned int revision
;
61 struct notifier_block reboot_notifier
;
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
,
72 unsigned int enable_reg
;
73 unsigned int reset_type
;
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
;
80 enable_reg
= PON_PS_HOLD_RST_CTL2
;
82 error
= regmap_update_bits(pwrkey
->regmap
,
83 pwrkey
->baseaddr
+ enable_reg
,
88 "unable to clear ps hold reset enable: %d\n",
92 * Updates of PON_PS_HOLD_ENABLE requires 3 sleep cycles between
95 usleep_range(100, 1000);
100 reset_type
= PON_PS_HOLD_TYPE_SHUTDOWN
;
104 reset_type
= PON_PS_HOLD_TYPE_HARD_RESET
;
108 error
= regmap_update_bits(pwrkey
->regmap
,
109 pwrkey
->baseaddr
+ PON_PS_HOLD_RST_CTL
,
110 PON_PS_HOLD_TYPE_MASK
,
113 dev_err(pwrkey
->dev
, "unable to set ps hold reset type: %d\n",
116 error
= regmap_update_bits(pwrkey
->regmap
,
117 pwrkey
->baseaddr
+ enable_reg
,
121 dev_err(pwrkey
->dev
, "unable to re-set enable: %d\n", error
);
126 static irqreturn_t
pm8941_pwrkey_irq(int irq
, void *_data
)
128 struct pm8941_pwrkey
*pwrkey
= _data
;
132 error
= regmap_read(pwrkey
->regmap
,
133 pwrkey
->baseaddr
+ PON_RT_STS
, &sts
);
137 input_report_key(pwrkey
->input
, pwrkey
->code
,
138 sts
& pwrkey
->data
->status_bit
);
139 input_sync(pwrkey
->input
);
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
);
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
);
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
;
171 struct device
*parent
;
175 if (of_property_read_u32(pdev
->dev
.of_node
, "debounce", &req_delay
))
178 if (req_delay
> 2000000 || req_delay
== 0) {
179 dev_err(&pdev
->dev
, "invalid debounce time: %u\n", req_delay
);
183 pull_up
= of_property_read_bool(pdev
->dev
.of_node
, "bias-pull-up");
185 pwrkey
= devm_kzalloc(&pdev
->dev
, sizeof(*pwrkey
), GFP_KERNEL
);
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
200 pwrkey
->regmap
= dev_get_regmap(parent
->parent
, NULL
);
201 if (!pwrkey
->regmap
) {
202 dev_err(&pdev
->dev
, "failed to locate regmap\n");
206 error
= of_property_read_u32(parent
->of_node
,
207 "reg", &pwrkey
->baseaddr
);
209 error
= of_property_read_u32(pdev
->dev
.of_node
, "reg",
215 pwrkey
->irq
= platform_get_irq(pdev
, 0);
216 if (pwrkey
->irq
< 0) {
217 dev_err(&pdev
->dev
, "failed to get irq\n");
221 error
= regmap_read(pwrkey
->regmap
, pwrkey
->baseaddr
+ PON_REV2
,
224 dev_err(&pdev
->dev
, "failed to set debounce: %d\n", error
);
228 error
= of_property_read_u32(pdev
->dev
.of_node
, "linux,code",
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");
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
,
255 dev_err(&pdev
->dev
, "failed to set debounce: %d\n", 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);
264 dev_err(&pdev
->dev
, "failed to set pull: %d\n", error
);
268 error
= devm_request_threaded_irq(&pdev
->dev
, pwrkey
->irq
,
269 NULL
, pm8941_pwrkey_irq
,
271 "pm8941_pwrkey", pwrkey
);
273 dev_err(&pdev
->dev
, "failed requesting IRQ: %d\n", error
);
277 error
= input_register_device(pwrkey
->input
);
279 dev_err(&pdev
->dev
, "failed to register input device: %d\n",
284 pwrkey
->reboot_notifier
.notifier_call
= pm8941_reboot_notify
,
285 error
= register_reboot_notifier(&pwrkey
->reboot_notifier
);
287 dev_err(&pdev
->dev
, "failed to register reboot notifier: %d\n",
292 platform_set_drvdata(pdev
, pwrkey
);
293 device_init_wakeup(&pdev
->dev
, 1);
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
);
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
,
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");