1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
4 * Copyright (c) 2014, Sony Mobile Communications Inc.
5 */
6
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/log2.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/reboot.h>
18 #include <linux/regmap.h>
19
20 #define PON_REV2 0x01
21
22 #define PON_RT_STS 0x10
23 #define PON_KPDPWR_N_SET BIT(0)
24 #define PON_RESIN_N_SET BIT(1)
25
26 #define PON_PS_HOLD_RST_CTL 0x5a
27 #define PON_PS_HOLD_RST_CTL2 0x5b
28 #define PON_PS_HOLD_ENABLE BIT(7)
29 #define PON_PS_HOLD_TYPE_MASK 0x0f
30 #define PON_PS_HOLD_TYPE_SHUTDOWN 4
31 #define PON_PS_HOLD_TYPE_HARD_RESET 7
32
33 #define PON_PULL_CTL 0x70
34 #define PON_KPDPWR_PULL_UP BIT(1)
35 #define PON_RESIN_PULL_UP BIT(0)
36
37 #define PON_DBC_CTL 0x71
38 #define PON_DBC_DELAY_MASK 0x7
39
40 struct pm8941_data {
41 unsigned int pull_up_bit;
42 unsigned int status_bit;
43 };
44
45 struct pm8941_pwrkey {
46 struct device *dev;
47 int irq;
48 u32 baseaddr;
49 struct regmap *regmap;
50 struct input_dev *input;
51
52 unsigned int revision;
53 struct notifier_block reboot_notifier;
54
55 u32 code;
56 const struct pm8941_data *data;
57 };
58
pm8941_reboot_notify(struct notifier_block * nb,unsigned long code,void * unused)59 static int pm8941_reboot_notify(struct notifier_block *nb,
60 unsigned long code, void *unused)
61 {
62 struct pm8941_pwrkey *pwrkey = container_of(nb, struct pm8941_pwrkey,
63 reboot_notifier);
64 unsigned int enable_reg;
65 unsigned int reset_type;
66 int error;
67
68 /* PMICs with revision 0 have the enable bit in same register as ctrl */
69 if (pwrkey->revision == 0)
70 enable_reg = PON_PS_HOLD_RST_CTL;
71 else
72 enable_reg = PON_PS_HOLD_RST_CTL2;
73
74 error = regmap_update_bits(pwrkey->regmap,
75 pwrkey->baseaddr + enable_reg,
76 PON_PS_HOLD_ENABLE,
77 0);
78 if (error)
79 dev_err(pwrkey->dev,
80 "unable to clear ps hold reset enable: %d\n",
81 error);
82
83 /*
84 * Updates of PON_PS_HOLD_ENABLE requires 3 sleep cycles between
85 * writes.
86 */
87 usleep_range(100, 1000);
88
89 switch (code) {
90 case SYS_HALT:
91 case SYS_POWER_OFF:
92 reset_type = PON_PS_HOLD_TYPE_SHUTDOWN;
93 break;
94 case SYS_RESTART:
95 default:
96 reset_type = PON_PS_HOLD_TYPE_HARD_RESET;
97 break;
98 }
99
100 error = regmap_update_bits(pwrkey->regmap,
101 pwrkey->baseaddr + PON_PS_HOLD_RST_CTL,
102 PON_PS_HOLD_TYPE_MASK,
103 reset_type);
104 if (error)
105 dev_err(pwrkey->dev, "unable to set ps hold reset type: %d\n",
106 error);
107
108 error = regmap_update_bits(pwrkey->regmap,
109 pwrkey->baseaddr + enable_reg,
110 PON_PS_HOLD_ENABLE,
111 PON_PS_HOLD_ENABLE);
112 if (error)
113 dev_err(pwrkey->dev, "unable to re-set enable: %d\n", error);
114
115 return NOTIFY_DONE;
116 }
117
pm8941_pwrkey_irq(int irq,void * _data)118 static irqreturn_t pm8941_pwrkey_irq(int irq, void *_data)
119 {
120 struct pm8941_pwrkey *pwrkey = _data;
121 unsigned int sts;
122 int error;
123
124 error = regmap_read(pwrkey->regmap,
125 pwrkey->baseaddr + PON_RT_STS, &sts);
126 if (error)
127 return IRQ_HANDLED;
128
129 input_report_key(pwrkey->input, pwrkey->code,
130 sts & pwrkey->data->status_bit);
131 input_sync(pwrkey->input);
132
133 return IRQ_HANDLED;
134 }
135
pm8941_pwrkey_suspend(struct device * dev)136 static int __maybe_unused pm8941_pwrkey_suspend(struct device *dev)
137 {
138 struct pm8941_pwrkey *pwrkey = dev_get_drvdata(dev);
139
140 if (device_may_wakeup(dev))
141 enable_irq_wake(pwrkey->irq);
142
143 return 0;
144 }
145
pm8941_pwrkey_resume(struct device * dev)146 static int __maybe_unused pm8941_pwrkey_resume(struct device *dev)
147 {
148 struct pm8941_pwrkey *pwrkey = dev_get_drvdata(dev);
149
150 if (device_may_wakeup(dev))
151 disable_irq_wake(pwrkey->irq);
152
153 return 0;
154 }
155
156 static SIMPLE_DEV_PM_OPS(pm8941_pwr_key_pm_ops,
157 pm8941_pwrkey_suspend, pm8941_pwrkey_resume);
158
pm8941_pwrkey_probe(struct platform_device * pdev)159 static int pm8941_pwrkey_probe(struct platform_device *pdev)
160 {
161 struct pm8941_pwrkey *pwrkey;
162 bool pull_up;
163 struct device *parent;
164 u32 req_delay;
165 int error;
166
167 if (of_property_read_u32(pdev->dev.of_node, "debounce", &req_delay))
168 req_delay = 15625;
169
170 if (req_delay > 2000000 || req_delay == 0) {
171 dev_err(&pdev->dev, "invalid debounce time: %u\n", req_delay);
172 return -EINVAL;
173 }
174
175 pull_up = of_property_read_bool(pdev->dev.of_node, "bias-pull-up");
176
177 pwrkey = devm_kzalloc(&pdev->dev, sizeof(*pwrkey), GFP_KERNEL);
178 if (!pwrkey)
179 return -ENOMEM;
180
181 pwrkey->dev = &pdev->dev;
182 pwrkey->data = of_device_get_match_data(&pdev->dev);
183
184 parent = pdev->dev.parent;
185 pwrkey->regmap = dev_get_regmap(parent, NULL);
186 if (!pwrkey->regmap) {
187 /*
188 * We failed to get regmap for parent. Let's see if we are
189 * a child of pon node and read regmap and reg from its
190 * parent.
191 */
192 pwrkey->regmap = dev_get_regmap(parent->parent, NULL);
193 if (!pwrkey->regmap) {
194 dev_err(&pdev->dev, "failed to locate regmap\n");
195 return -ENODEV;
196 }
197
198 error = of_property_read_u32(parent->of_node,
199 "reg", &pwrkey->baseaddr);
200 } else {
201 error = of_property_read_u32(pdev->dev.of_node, "reg",
202 &pwrkey->baseaddr);
203 }
204 if (error)
205 return error;
206
207 pwrkey->irq = platform_get_irq(pdev, 0);
208 if (pwrkey->irq < 0)
209 return pwrkey->irq;
210
211 error = regmap_read(pwrkey->regmap, pwrkey->baseaddr + PON_REV2,
212 &pwrkey->revision);
213 if (error) {
214 dev_err(&pdev->dev, "failed to set debounce: %d\n", error);
215 return error;
216 }
217
218 error = of_property_read_u32(pdev->dev.of_node, "linux,code",
219 &pwrkey->code);
220 if (error) {
221 dev_dbg(&pdev->dev,
222 "no linux,code assuming power (%d)\n", error);
223 pwrkey->code = KEY_POWER;
224 }
225
226 pwrkey->input = devm_input_allocate_device(&pdev->dev);
227 if (!pwrkey->input) {
228 dev_dbg(&pdev->dev, "unable to allocate input device\n");
229 return -ENOMEM;
230 }
231
232 input_set_capability(pwrkey->input, EV_KEY, pwrkey->code);
233
234 pwrkey->input->name = "pm8941_pwrkey";
235 pwrkey->input->phys = "pm8941_pwrkey/input0";
236
237 req_delay = (req_delay << 6) / USEC_PER_SEC;
238 req_delay = ilog2(req_delay);
239
240 error = regmap_update_bits(pwrkey->regmap,
241 pwrkey->baseaddr + PON_DBC_CTL,
242 PON_DBC_DELAY_MASK,
243 req_delay);
244 if (error) {
245 dev_err(&pdev->dev, "failed to set debounce: %d\n", error);
246 return error;
247 }
248
249 error = regmap_update_bits(pwrkey->regmap,
250 pwrkey->baseaddr + PON_PULL_CTL,
251 pwrkey->data->pull_up_bit,
252 pull_up ? pwrkey->data->pull_up_bit : 0);
253 if (error) {
254 dev_err(&pdev->dev, "failed to set pull: %d\n", error);
255 return error;
256 }
257
258 error = devm_request_threaded_irq(&pdev->dev, pwrkey->irq,
259 NULL, pm8941_pwrkey_irq,
260 IRQF_ONESHOT,
261 "pm8941_pwrkey", pwrkey);
262 if (error) {
263 dev_err(&pdev->dev, "failed requesting IRQ: %d\n", error);
264 return error;
265 }
266
267 error = input_register_device(pwrkey->input);
268 if (error) {
269 dev_err(&pdev->dev, "failed to register input device: %d\n",
270 error);
271 return error;
272 }
273
274 pwrkey->reboot_notifier.notifier_call = pm8941_reboot_notify,
275 error = register_reboot_notifier(&pwrkey->reboot_notifier);
276 if (error) {
277 dev_err(&pdev->dev, "failed to register reboot notifier: %d\n",
278 error);
279 return error;
280 }
281
282 platform_set_drvdata(pdev, pwrkey);
283 device_init_wakeup(&pdev->dev, 1);
284
285 return 0;
286 }
287
pm8941_pwrkey_remove(struct platform_device * pdev)288 static int pm8941_pwrkey_remove(struct platform_device *pdev)
289 {
290 struct pm8941_pwrkey *pwrkey = platform_get_drvdata(pdev);
291
292 unregister_reboot_notifier(&pwrkey->reboot_notifier);
293
294 return 0;
295 }
296
297 static const struct pm8941_data pwrkey_data = {
298 .pull_up_bit = PON_KPDPWR_PULL_UP,
299 .status_bit = PON_KPDPWR_N_SET,
300 };
301
302 static const struct pm8941_data resin_data = {
303 .pull_up_bit = PON_RESIN_PULL_UP,
304 .status_bit = PON_RESIN_N_SET,
305 };
306
307 static const struct of_device_id pm8941_pwr_key_id_table[] = {
308 { .compatible = "qcom,pm8941-pwrkey", .data = &pwrkey_data },
309 { .compatible = "qcom,pm8941-resin", .data = &resin_data },
310 { }
311 };
312 MODULE_DEVICE_TABLE(of, pm8941_pwr_key_id_table);
313
314 static struct platform_driver pm8941_pwrkey_driver = {
315 .probe = pm8941_pwrkey_probe,
316 .remove = pm8941_pwrkey_remove,
317 .driver = {
318 .name = "pm8941-pwrkey",
319 .pm = &pm8941_pwr_key_pm_ops,
320 .of_match_table = of_match_ptr(pm8941_pwr_key_id_table),
321 },
322 };
323 module_platform_driver(pm8941_pwrkey_driver);
324
325 MODULE_DESCRIPTION("PM8941 Power Key driver");
326 MODULE_LICENSE("GPL v2");
327