1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2010-2011, 2020-2021, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2014, Sony Mobile Communications Inc.
7 #include <linux/delay.h>
8 #include <linux/errno.h>
9 #include <linux/input.h>
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/ktime.h>
13 #include <linux/log2.h>
14 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/platform_device.h>
18 #include <linux/reboot.h>
19 #include <linux/regmap.h>
23 #define PON_SUBTYPE 0x05
25 #define PON_SUBTYPE_PRIMARY 0x01
26 #define PON_SUBTYPE_SECONDARY 0x02
27 #define PON_SUBTYPE_1REG 0x03
28 #define PON_SUBTYPE_GEN2_PRIMARY 0x04
29 #define PON_SUBTYPE_GEN2_SECONDARY 0x05
30 #define PON_SUBTYPE_GEN3_PBS 0x08
31 #define PON_SUBTYPE_GEN3_HLOS 0x09
33 #define PON_RT_STS 0x10
34 #define PON_KPDPWR_N_SET BIT(0)
35 #define PON_RESIN_N_SET BIT(1)
36 #define PON_GEN3_RESIN_N_SET BIT(6)
37 #define PON_GEN3_KPDPWR_N_SET BIT(7)
39 #define PON_PS_HOLD_RST_CTL 0x5a
40 #define PON_PS_HOLD_RST_CTL2 0x5b
41 #define PON_PS_HOLD_ENABLE BIT(7)
42 #define PON_PS_HOLD_TYPE_MASK 0x0f
43 #define PON_PS_HOLD_TYPE_WARM_RESET 1
44 #define PON_PS_HOLD_TYPE_SHUTDOWN 4
45 #define PON_PS_HOLD_TYPE_HARD_RESET 7
47 #define PON_PULL_CTL 0x70
48 #define PON_KPDPWR_PULL_UP BIT(1)
49 #define PON_RESIN_PULL_UP BIT(0)
51 #define PON_DBC_CTL 0x71
52 #define PON_DBC_DELAY_MASK_GEN1 0x7
53 #define PON_DBC_DELAY_MASK_GEN2 0xf
54 #define PON_DBC_SHIFT_GEN1 6
55 #define PON_DBC_SHIFT_GEN2 14
58 unsigned int pull_up_bit;
59 unsigned int status_bit;
60 bool supports_ps_hold_poff_config;
61 bool supports_debounce_config;
67 struct pm8941_pwrkey {
72 struct regmap *regmap;
73 struct input_dev *input;
75 unsigned int revision;
77 struct notifier_block reboot_notifier;
80 u32 sw_debounce_time_us;
81 ktime_t sw_debounce_end_time;
83 const struct pm8941_data *data;
86 static int pm8941_reboot_notify(struct notifier_block *nb,
87 unsigned long code, void *unused)
89 struct pm8941_pwrkey *pwrkey = container_of(nb, struct pm8941_pwrkey,
91 unsigned int enable_reg;
92 unsigned int reset_type;
95 /* PMICs with revision 0 have the enable bit in same register as ctrl */
96 if (pwrkey->revision == 0)
97 enable_reg = PON_PS_HOLD_RST_CTL;
99 enable_reg = PON_PS_HOLD_RST_CTL2;
101 error = regmap_update_bits(pwrkey->regmap,
102 pwrkey->baseaddr + enable_reg,
107 "unable to clear ps hold reset enable: %d\n",
111 * Updates of PON_PS_HOLD_ENABLE requires 3 sleep cycles between
114 usleep_range(100, 1000);
119 reset_type = PON_PS_HOLD_TYPE_SHUTDOWN;
123 if (reboot_mode == REBOOT_WARM)
124 reset_type = PON_PS_HOLD_TYPE_WARM_RESET;
126 reset_type = PON_PS_HOLD_TYPE_HARD_RESET;
130 error = regmap_update_bits(pwrkey->regmap,
131 pwrkey->baseaddr + PON_PS_HOLD_RST_CTL,
132 PON_PS_HOLD_TYPE_MASK,
135 dev_err(pwrkey->dev, "unable to set ps hold reset type: %d\n",
138 error = regmap_update_bits(pwrkey->regmap,
139 pwrkey->baseaddr + enable_reg,
143 dev_err(pwrkey->dev, "unable to re-set enable: %d\n", error);
148 static irqreturn_t pm8941_pwrkey_irq(int irq, void *_data)
150 struct pm8941_pwrkey *pwrkey = _data;
154 if (pwrkey->sw_debounce_time_us) {
155 if (ktime_before(ktime_get(), pwrkey->sw_debounce_end_time)) {
157 "ignoring key event received before debounce end %llu us\n",
158 pwrkey->sw_debounce_end_time);
163 err = regmap_read(pwrkey->regmap, pwrkey->baseaddr + PON_RT_STS, &sts);
167 sts &= pwrkey->data->status_bit;
169 if (pwrkey->sw_debounce_time_us && !sts)
170 pwrkey->sw_debounce_end_time = ktime_add_us(ktime_get(),
171 pwrkey->sw_debounce_time_us);
174 * Simulate a press event in case a release event occurred without a
175 * corresponding press event.
177 if (!pwrkey->last_status && !sts) {
178 input_report_key(pwrkey->input, pwrkey->code, 1);
179 input_sync(pwrkey->input);
181 pwrkey->last_status = sts;
183 input_report_key(pwrkey->input, pwrkey->code, sts);
184 input_sync(pwrkey->input);
189 static int pm8941_pwrkey_sw_debounce_init(struct pm8941_pwrkey *pwrkey)
191 unsigned int val, addr, mask;
194 if (pwrkey->data->has_pon_pbs && !pwrkey->pon_pbs_baseaddr) {
196 "PON_PBS address missing, can't read HW debounce time\n");
200 if (pwrkey->pon_pbs_baseaddr)
201 addr = pwrkey->pon_pbs_baseaddr + PON_DBC_CTL;
203 addr = pwrkey->baseaddr + PON_DBC_CTL;
204 error = regmap_read(pwrkey->regmap, addr, &val);
208 if (pwrkey->subtype >= PON_SUBTYPE_GEN2_PRIMARY)
213 pwrkey->sw_debounce_time_us =
214 2 * USEC_PER_SEC / (1 << (mask - (val & mask)));
216 dev_dbg(pwrkey->dev, "SW debounce time = %u us\n",
217 pwrkey->sw_debounce_time_us);
222 static int pm8941_pwrkey_suspend(struct device *dev)
224 struct pm8941_pwrkey *pwrkey = dev_get_drvdata(dev);
226 if (device_may_wakeup(dev))
227 enable_irq_wake(pwrkey->irq);
232 static int pm8941_pwrkey_resume(struct device *dev)
234 struct pm8941_pwrkey *pwrkey = dev_get_drvdata(dev);
236 if (device_may_wakeup(dev))
237 disable_irq_wake(pwrkey->irq);
242 static DEFINE_SIMPLE_DEV_PM_OPS(pm8941_pwr_key_pm_ops,
243 pm8941_pwrkey_suspend, pm8941_pwrkey_resume);
245 static int pm8941_pwrkey_probe(struct platform_device *pdev)
247 struct pm8941_pwrkey *pwrkey;
249 struct device *parent;
250 struct device_node *regmap_node;
252 u32 req_delay, mask, delay_shift;
255 if (of_property_read_u32(pdev->dev.of_node, "debounce", &req_delay))
258 if (req_delay > 2000000 || req_delay == 0) {
259 dev_err(&pdev->dev, "invalid debounce time: %u\n", req_delay);
263 pull_up = of_property_read_bool(pdev->dev.of_node, "bias-pull-up");
265 pwrkey = devm_kzalloc(&pdev->dev, sizeof(*pwrkey), GFP_KERNEL);
269 pwrkey->dev = &pdev->dev;
270 pwrkey->data = of_device_get_match_data(&pdev->dev);
272 parent = pdev->dev.parent;
273 regmap_node = pdev->dev.of_node;
274 pwrkey->regmap = dev_get_regmap(parent, NULL);
275 if (!pwrkey->regmap) {
276 regmap_node = parent->of_node;
278 * We failed to get regmap for parent. Let's see if we are
279 * a child of pon node and read regmap and reg from its
282 pwrkey->regmap = dev_get_regmap(parent->parent, NULL);
283 if (!pwrkey->regmap) {
284 dev_err(&pdev->dev, "failed to locate regmap\n");
289 addr = of_get_address(regmap_node, 0, NULL, NULL);
291 dev_err(&pdev->dev, "reg property missing\n");
294 pwrkey->baseaddr = be32_to_cpup(addr);
296 if (pwrkey->data->has_pon_pbs) {
297 /* PON_PBS base address is optional */
298 addr = of_get_address(regmap_node, 1, NULL, NULL);
300 pwrkey->pon_pbs_baseaddr = be32_to_cpup(addr);
303 pwrkey->irq = platform_get_irq(pdev, 0);
307 error = regmap_read(pwrkey->regmap, pwrkey->baseaddr + PON_REV2,
310 dev_err(&pdev->dev, "failed to read revision: %d\n", error);
314 error = regmap_read(pwrkey->regmap, pwrkey->baseaddr + PON_SUBTYPE,
317 dev_err(&pdev->dev, "failed to read subtype: %d\n", error);
321 error = of_property_read_u32(pdev->dev.of_node, "linux,code",
325 "no linux,code assuming power (%d)\n", error);
326 pwrkey->code = KEY_POWER;
329 pwrkey->input = devm_input_allocate_device(&pdev->dev);
330 if (!pwrkey->input) {
331 dev_dbg(&pdev->dev, "unable to allocate input device\n");
335 input_set_capability(pwrkey->input, EV_KEY, pwrkey->code);
337 pwrkey->input->name = pwrkey->data->name;
338 pwrkey->input->phys = pwrkey->data->phys;
340 if (pwrkey->data->supports_debounce_config) {
341 if (pwrkey->subtype >= PON_SUBTYPE_GEN2_PRIMARY) {
342 mask = PON_DBC_DELAY_MASK_GEN2;
343 delay_shift = PON_DBC_SHIFT_GEN2;
345 mask = PON_DBC_DELAY_MASK_GEN1;
346 delay_shift = PON_DBC_SHIFT_GEN1;
349 req_delay = (req_delay << delay_shift) / USEC_PER_SEC;
350 req_delay = ilog2(req_delay);
352 error = regmap_update_bits(pwrkey->regmap,
353 pwrkey->baseaddr + PON_DBC_CTL,
357 dev_err(&pdev->dev, "failed to set debounce: %d\n",
363 error = pm8941_pwrkey_sw_debounce_init(pwrkey);
367 if (pwrkey->data->pull_up_bit) {
368 error = regmap_update_bits(pwrkey->regmap,
369 pwrkey->baseaddr + PON_PULL_CTL,
370 pwrkey->data->pull_up_bit,
371 pull_up ? pwrkey->data->pull_up_bit :
374 dev_err(&pdev->dev, "failed to set pull: %d\n", error);
379 error = devm_request_threaded_irq(&pdev->dev, pwrkey->irq,
380 NULL, pm8941_pwrkey_irq,
382 pwrkey->data->name, pwrkey);
384 dev_err(&pdev->dev, "failed requesting IRQ: %d\n", error);
388 error = input_register_device(pwrkey->input);
390 dev_err(&pdev->dev, "failed to register input device: %d\n",
395 if (pwrkey->data->supports_ps_hold_poff_config) {
396 pwrkey->reboot_notifier.notifier_call = pm8941_reboot_notify;
397 error = register_reboot_notifier(&pwrkey->reboot_notifier);
399 dev_err(&pdev->dev, "failed to register reboot notifier: %d\n",
405 platform_set_drvdata(pdev, pwrkey);
406 device_init_wakeup(&pdev->dev, 1);
411 static int pm8941_pwrkey_remove(struct platform_device *pdev)
413 struct pm8941_pwrkey *pwrkey = platform_get_drvdata(pdev);
415 if (pwrkey->data->supports_ps_hold_poff_config)
416 unregister_reboot_notifier(&pwrkey->reboot_notifier);
421 static const struct pm8941_data pwrkey_data = {
422 .pull_up_bit = PON_KPDPWR_PULL_UP,
423 .status_bit = PON_KPDPWR_N_SET,
424 .name = "pm8941_pwrkey",
425 .phys = "pm8941_pwrkey/input0",
426 .supports_ps_hold_poff_config = true,
427 .supports_debounce_config = true,
428 .has_pon_pbs = false,
431 static const struct pm8941_data resin_data = {
432 .pull_up_bit = PON_RESIN_PULL_UP,
433 .status_bit = PON_RESIN_N_SET,
434 .name = "pm8941_resin",
435 .phys = "pm8941_resin/input0",
436 .supports_ps_hold_poff_config = true,
437 .supports_debounce_config = true,
438 .has_pon_pbs = false,
441 static const struct pm8941_data pon_gen3_pwrkey_data = {
442 .status_bit = PON_GEN3_KPDPWR_N_SET,
443 .name = "pmic_pwrkey",
444 .phys = "pmic_pwrkey/input0",
445 .supports_ps_hold_poff_config = false,
446 .supports_debounce_config = false,
450 static const struct pm8941_data pon_gen3_resin_data = {
451 .status_bit = PON_GEN3_RESIN_N_SET,
452 .name = "pmic_resin",
453 .phys = "pmic_resin/input0",
454 .supports_ps_hold_poff_config = false,
455 .supports_debounce_config = false,
459 static const struct of_device_id pm8941_pwr_key_id_table[] = {
460 { .compatible = "qcom,pm8941-pwrkey", .data = &pwrkey_data },
461 { .compatible = "qcom,pm8941-resin", .data = &resin_data },
462 { .compatible = "qcom,pmk8350-pwrkey", .data = &pon_gen3_pwrkey_data },
463 { .compatible = "qcom,pmk8350-resin", .data = &pon_gen3_resin_data },
466 MODULE_DEVICE_TABLE(of, pm8941_pwr_key_id_table);
468 static struct platform_driver pm8941_pwrkey_driver = {
469 .probe = pm8941_pwrkey_probe,
470 .remove = pm8941_pwrkey_remove,
472 .name = "pm8941-pwrkey",
473 .pm = pm_sleep_ptr(&pm8941_pwr_key_pm_ops),
474 .of_match_table = of_match_ptr(pm8941_pwr_key_id_table),
477 module_platform_driver(pm8941_pwrkey_driver);
479 MODULE_DESCRIPTION("PM8941 Power Key driver");
480 MODULE_LICENSE("GPL v2");