1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
5 #include <linux/module.h>
6 #include <linux/kernel.h>
7 #include <linux/errno.h>
8 #include <linux/slab.h>
9 #include <linux/input.h>
10 #include <linux/interrupt.h>
11 #include <linux/platform_device.h>
12 #include <linux/regmap.h>
13 #include <linux/log2.h>
15 #include <linux/of_device.h>
17 #define PON_CNTL_1 0x1C
18 #define PON_CNTL_PULL_UP BIT(7)
19 #define PON_CNTL_TRIG_DELAY_MASK (0x7)
20 #define PON_CNTL_1_PULL_UP_EN 0xe0
21 #define PON_CNTL_1_USB_PWR_EN 0x10
22 #define PON_CNTL_1_WD_EN_RESET 0x08
24 #define PM8058_SLEEP_CTRL 0x02b
25 #define PM8921_SLEEP_CTRL 0x10a
27 #define SLEEP_CTRL_SMPL_EN_RESET 0x04
29 /* Regulator master enable addresses */
30 #define REG_PM8058_VREG_EN_MSM 0x018
31 #define REG_PM8058_VREG_EN_GRP_5_4 0x1c8
33 /* Regulator control registers for shutdown/reset */
34 #define PM8058_S0_CTRL 0x004
35 #define PM8058_S1_CTRL 0x005
36 #define PM8058_S3_CTRL 0x111
37 #define PM8058_L21_CTRL 0x120
38 #define PM8058_L22_CTRL 0x121
40 #define PM8058_REGULATOR_ENABLE_MASK 0x80
41 #define PM8058_REGULATOR_ENABLE 0x80
42 #define PM8058_REGULATOR_DISABLE 0x00
43 #define PM8058_REGULATOR_PULL_DOWN_MASK 0x40
44 #define PM8058_REGULATOR_PULL_DOWN_EN 0x40
46 /* Buck CTRL register */
47 #define PM8058_SMPS_LEGACY_VREF_SEL 0x20
48 #define PM8058_SMPS_LEGACY_VPROG_MASK 0x1f
49 #define PM8058_SMPS_ADVANCED_BAND_MASK 0xC0
50 #define PM8058_SMPS_ADVANCED_BAND_SHIFT 6
51 #define PM8058_SMPS_ADVANCED_VPROG_MASK 0x3f
53 /* Buck TEST2 registers for shutdown/reset */
54 #define PM8058_S0_TEST2 0x084
55 #define PM8058_S1_TEST2 0x085
56 #define PM8058_S3_TEST2 0x11a
58 #define PM8058_REGULATOR_BANK_WRITE 0x80
59 #define PM8058_REGULATOR_BANK_MASK 0x70
60 #define PM8058_REGULATOR_BANK_SHIFT 4
61 #define PM8058_REGULATOR_BANK_SEL(n) ((n) << PM8058_REGULATOR_BANK_SHIFT)
63 /* Buck TEST2 register bank 1 */
64 #define PM8058_SMPS_LEGACY_VLOW_SEL 0x01
66 /* Buck TEST2 register bank 7 */
67 #define PM8058_SMPS_ADVANCED_MODE_MASK 0x02
68 #define PM8058_SMPS_ADVANCED_MODE 0x02
69 #define PM8058_SMPS_LEGACY_MODE 0x00
72 * struct pmic8xxx_pwrkey - pmic8xxx pwrkey information
73 * @key_press_irq: key press irq number
74 * @regmap: device regmap
75 * @shutdown_fn: shutdown configuration function
77 struct pmic8xxx_pwrkey
{
79 struct regmap
*regmap
;
80 int (*shutdown_fn
)(struct pmic8xxx_pwrkey
*, bool);
83 static irqreturn_t
pwrkey_press_irq(int irq
, void *_pwr
)
85 struct input_dev
*pwr
= _pwr
;
87 input_report_key(pwr
, KEY_POWER
, 1);
93 static irqreturn_t
pwrkey_release_irq(int irq
, void *_pwr
)
95 struct input_dev
*pwr
= _pwr
;
97 input_report_key(pwr
, KEY_POWER
, 0);
103 static int __maybe_unused
pmic8xxx_pwrkey_suspend(struct device
*dev
)
105 struct pmic8xxx_pwrkey
*pwrkey
= dev_get_drvdata(dev
);
107 if (device_may_wakeup(dev
))
108 enable_irq_wake(pwrkey
->key_press_irq
);
113 static int __maybe_unused
pmic8xxx_pwrkey_resume(struct device
*dev
)
115 struct pmic8xxx_pwrkey
*pwrkey
= dev_get_drvdata(dev
);
117 if (device_may_wakeup(dev
))
118 disable_irq_wake(pwrkey
->key_press_irq
);
123 static SIMPLE_DEV_PM_OPS(pm8xxx_pwr_key_pm_ops
,
124 pmic8xxx_pwrkey_suspend
, pmic8xxx_pwrkey_resume
);
126 static void pmic8xxx_pwrkey_shutdown(struct platform_device
*pdev
)
128 struct pmic8xxx_pwrkey
*pwrkey
= platform_get_drvdata(pdev
);
131 bool reset
= system_state
== SYSTEM_RESTART
;
133 if (pwrkey
->shutdown_fn
) {
134 error
= pwrkey
->shutdown_fn(pwrkey
, reset
);
140 * Select action to perform (reset or shutdown) when PS_HOLD goes low.
141 * Also ensure that KPD, CBL0, and CBL1 pull ups are enabled and that
142 * USB charging is enabled.
144 mask
= PON_CNTL_1_PULL_UP_EN
| PON_CNTL_1_USB_PWR_EN
;
145 mask
|= PON_CNTL_1_WD_EN_RESET
;
148 val
&= ~PON_CNTL_1_WD_EN_RESET
;
150 regmap_update_bits(pwrkey
->regmap
, PON_CNTL_1
, mask
, val
);
154 * Set an SMPS regulator to be disabled in its CTRL register, but enabled
155 * in the master enable register. Also set it's pull down enable bit.
156 * Take care to make sure that the output voltage doesn't change if switching
157 * from advanced mode to legacy mode.
159 static int pm8058_disable_smps_locally_set_pull_down(struct regmap
*regmap
,
160 u16 ctrl_addr
, u16 test2_addr
, u16 master_enable_addr
,
161 u8 master_enable_bit
)
164 u8 vref_sel
, vlow_sel
, band
, vprog
, bank
;
167 bank
= PM8058_REGULATOR_BANK_SEL(7);
168 error
= regmap_write(regmap
, test2_addr
, bank
);
172 error
= regmap_read(regmap
, test2_addr
, ®
);
176 reg
&= PM8058_SMPS_ADVANCED_MODE_MASK
;
177 /* Check if in advanced mode. */
178 if (reg
== PM8058_SMPS_ADVANCED_MODE
) {
179 /* Determine current output voltage. */
180 error
= regmap_read(regmap
, ctrl_addr
, ®
);
184 band
= reg
& PM8058_SMPS_ADVANCED_BAND_MASK
;
185 band
>>= PM8058_SMPS_ADVANCED_BAND_SHIFT
;
192 vref_sel
= PM8058_SMPS_LEGACY_VREF_SEL
;
196 vref_sel
= PM8058_SMPS_LEGACY_VREF_SEL
;
197 vlow_sel
= PM8058_SMPS_LEGACY_VLOW_SEL
;
200 pr_err("%s: regulator already disabled\n", __func__
);
203 vprog
= reg
& PM8058_SMPS_ADVANCED_VPROG_MASK
;
204 /* Round up if fine step is in use. */
205 vprog
= (vprog
+ 1) >> 1;
206 if (vprog
> PM8058_SMPS_LEGACY_VPROG_MASK
)
207 vprog
= PM8058_SMPS_LEGACY_VPROG_MASK
;
209 /* Set VLOW_SEL bit. */
210 bank
= PM8058_REGULATOR_BANK_SEL(1);
211 error
= regmap_write(regmap
, test2_addr
, bank
);
215 error
= regmap_update_bits(regmap
, test2_addr
,
216 PM8058_REGULATOR_BANK_WRITE
| PM8058_REGULATOR_BANK_MASK
217 | PM8058_SMPS_LEGACY_VLOW_SEL
,
218 PM8058_REGULATOR_BANK_WRITE
|
219 PM8058_REGULATOR_BANK_SEL(1) | vlow_sel
);
223 /* Switch to legacy mode */
224 bank
= PM8058_REGULATOR_BANK_SEL(7);
225 error
= regmap_write(regmap
, test2_addr
, bank
);
229 error
= regmap_update_bits(regmap
, test2_addr
,
230 PM8058_REGULATOR_BANK_WRITE
|
231 PM8058_REGULATOR_BANK_MASK
|
232 PM8058_SMPS_ADVANCED_MODE_MASK
,
233 PM8058_REGULATOR_BANK_WRITE
|
234 PM8058_REGULATOR_BANK_SEL(7) |
235 PM8058_SMPS_LEGACY_MODE
);
239 /* Enable locally, enable pull down, keep voltage the same. */
240 error
= regmap_update_bits(regmap
, ctrl_addr
,
241 PM8058_REGULATOR_ENABLE_MASK
|
242 PM8058_REGULATOR_PULL_DOWN_MASK
|
243 PM8058_SMPS_LEGACY_VREF_SEL
|
244 PM8058_SMPS_LEGACY_VPROG_MASK
,
245 PM8058_REGULATOR_ENABLE
| PM8058_REGULATOR_PULL_DOWN_EN
251 /* Enable in master control register. */
252 error
= regmap_update_bits(regmap
, master_enable_addr
,
253 master_enable_bit
, master_enable_bit
);
257 /* Disable locally and enable pull down. */
258 return regmap_update_bits(regmap
, ctrl_addr
,
259 PM8058_REGULATOR_ENABLE_MASK
| PM8058_REGULATOR_PULL_DOWN_MASK
,
260 PM8058_REGULATOR_DISABLE
| PM8058_REGULATOR_PULL_DOWN_EN
);
263 static int pm8058_disable_ldo_locally_set_pull_down(struct regmap
*regmap
,
264 u16 ctrl_addr
, u16 master_enable_addr
, u8 master_enable_bit
)
268 /* Enable LDO in master control register. */
269 error
= regmap_update_bits(regmap
, master_enable_addr
,
270 master_enable_bit
, master_enable_bit
);
274 /* Disable LDO in CTRL register and set pull down */
275 return regmap_update_bits(regmap
, ctrl_addr
,
276 PM8058_REGULATOR_ENABLE_MASK
| PM8058_REGULATOR_PULL_DOWN_MASK
,
277 PM8058_REGULATOR_DISABLE
| PM8058_REGULATOR_PULL_DOWN_EN
);
280 static int pm8058_pwrkey_shutdown(struct pmic8xxx_pwrkey
*pwrkey
, bool reset
)
283 struct regmap
*regmap
= pwrkey
->regmap
;
286 /* When shutting down, enable active pulldowns on important rails. */
288 /* Disable SMPS's 0,1,3 locally and set pulldown enable bits. */
289 pm8058_disable_smps_locally_set_pull_down(regmap
,
290 PM8058_S0_CTRL
, PM8058_S0_TEST2
,
291 REG_PM8058_VREG_EN_MSM
, BIT(7));
292 pm8058_disable_smps_locally_set_pull_down(regmap
,
293 PM8058_S1_CTRL
, PM8058_S1_TEST2
,
294 REG_PM8058_VREG_EN_MSM
, BIT(6));
295 pm8058_disable_smps_locally_set_pull_down(regmap
,
296 PM8058_S3_CTRL
, PM8058_S3_TEST2
,
297 REG_PM8058_VREG_EN_GRP_5_4
, BIT(7) | BIT(4));
298 /* Disable LDO 21 locally and set pulldown enable bit. */
299 pm8058_disable_ldo_locally_set_pull_down(regmap
,
300 PM8058_L21_CTRL
, REG_PM8058_VREG_EN_GRP_5_4
,
305 * Fix-up: Set regulator LDO22 to 1.225 V in high power mode. Leave its
306 * pull-down state intact. This ensures a safe shutdown.
308 error
= regmap_update_bits(regmap
, PM8058_L22_CTRL
, 0xbf, 0x93);
312 /* Enable SMPL if resetting is desired */
313 mask
= SLEEP_CTRL_SMPL_EN_RESET
;
317 return regmap_update_bits(regmap
, PM8058_SLEEP_CTRL
, mask
, val
);
320 static int pm8921_pwrkey_shutdown(struct pmic8xxx_pwrkey
*pwrkey
, bool reset
)
322 struct regmap
*regmap
= pwrkey
->regmap
;
323 u8 mask
= SLEEP_CTRL_SMPL_EN_RESET
;
326 /* Enable SMPL if resetting is desired */
329 return regmap_update_bits(regmap
, PM8921_SLEEP_CTRL
, mask
, val
);
332 static int pmic8xxx_pwrkey_probe(struct platform_device
*pdev
)
334 struct input_dev
*pwr
;
335 int key_release_irq
= platform_get_irq(pdev
, 0);
336 int key_press_irq
= platform_get_irq(pdev
, 1);
339 unsigned int pon_cntl
;
340 struct regmap
*regmap
;
341 struct pmic8xxx_pwrkey
*pwrkey
;
345 if (of_property_read_u32(pdev
->dev
.of_node
, "debounce", &kpd_delay
))
348 /* Valid range of pwr key trigger delay is 1/64 sec to 2 seconds. */
349 if (kpd_delay
> USEC_PER_SEC
* 2 || kpd_delay
< USEC_PER_SEC
/ 64) {
350 dev_err(&pdev
->dev
, "invalid power key trigger delay\n");
354 pull_up
= of_property_read_bool(pdev
->dev
.of_node
, "pull-up");
356 regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
358 dev_err(&pdev
->dev
, "failed to locate regmap for the device\n");
362 pwrkey
= devm_kzalloc(&pdev
->dev
, sizeof(*pwrkey
), GFP_KERNEL
);
366 pwrkey
->shutdown_fn
= of_device_get_match_data(&pdev
->dev
);
367 pwrkey
->regmap
= regmap
;
368 pwrkey
->key_press_irq
= key_press_irq
;
370 pwr
= devm_input_allocate_device(&pdev
->dev
);
372 dev_dbg(&pdev
->dev
, "Can't allocate power button\n");
376 input_set_capability(pwr
, EV_KEY
, KEY_POWER
);
378 pwr
->name
= "pmic8xxx_pwrkey";
379 pwr
->phys
= "pmic8xxx_pwrkey/input0";
381 delay
= (kpd_delay
<< 6) / USEC_PER_SEC
;
382 delay
= ilog2(delay
);
384 err
= regmap_read(regmap
, PON_CNTL_1
, &pon_cntl
);
386 dev_err(&pdev
->dev
, "failed reading PON_CNTL_1 err=%d\n", err
);
390 pon_cntl
&= ~PON_CNTL_TRIG_DELAY_MASK
;
391 pon_cntl
|= (delay
& PON_CNTL_TRIG_DELAY_MASK
);
393 pon_cntl
|= PON_CNTL_PULL_UP
;
395 pon_cntl
&= ~PON_CNTL_PULL_UP
;
397 err
= regmap_write(regmap
, PON_CNTL_1
, pon_cntl
);
399 dev_err(&pdev
->dev
, "failed writing PON_CNTL_1 err=%d\n", err
);
403 err
= devm_request_irq(&pdev
->dev
, key_press_irq
, pwrkey_press_irq
,
405 "pmic8xxx_pwrkey_press", pwr
);
407 dev_err(&pdev
->dev
, "Can't get %d IRQ for pwrkey: %d\n",
412 err
= devm_request_irq(&pdev
->dev
, key_release_irq
, pwrkey_release_irq
,
414 "pmic8xxx_pwrkey_release", pwr
);
416 dev_err(&pdev
->dev
, "Can't get %d IRQ for pwrkey: %d\n",
417 key_release_irq
, err
);
421 err
= input_register_device(pwr
);
423 dev_err(&pdev
->dev
, "Can't register power key: %d\n", err
);
427 platform_set_drvdata(pdev
, pwrkey
);
428 device_init_wakeup(&pdev
->dev
, 1);
433 static const struct of_device_id pm8xxx_pwr_key_id_table
[] = {
434 { .compatible
= "qcom,pm8058-pwrkey", .data
= &pm8058_pwrkey_shutdown
},
435 { .compatible
= "qcom,pm8921-pwrkey", .data
= &pm8921_pwrkey_shutdown
},
438 MODULE_DEVICE_TABLE(of
, pm8xxx_pwr_key_id_table
);
440 static struct platform_driver pmic8xxx_pwrkey_driver
= {
441 .probe
= pmic8xxx_pwrkey_probe
,
442 .shutdown
= pmic8xxx_pwrkey_shutdown
,
444 .name
= "pm8xxx-pwrkey",
445 .pm
= &pm8xxx_pwr_key_pm_ops
,
446 .of_match_table
= pm8xxx_pwr_key_id_table
,
449 module_platform_driver(pmic8xxx_pwrkey_driver
);
451 MODULE_ALIAS("platform:pmic8xxx_pwrkey");
452 MODULE_DESCRIPTION("PMIC8XXX Power Key driver");
453 MODULE_LICENSE("GPL v2");
454 MODULE_AUTHOR("Trilok Soni <tsoni@codeaurora.org>");