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>
16 #define PON_CNTL_1 0x1C
17 #define PON_CNTL_PULL_UP BIT(7)
18 #define PON_CNTL_TRIG_DELAY_MASK (0x7)
19 #define PON_CNTL_1_PULL_UP_EN 0xe0
20 #define PON_CNTL_1_USB_PWR_EN 0x10
21 #define PON_CNTL_1_WD_EN_RESET 0x08
23 #define PM8058_SLEEP_CTRL 0x02b
24 #define PM8921_SLEEP_CTRL 0x10a
26 #define SLEEP_CTRL_SMPL_EN_RESET 0x04
28 /* Regulator master enable addresses */
29 #define REG_PM8058_VREG_EN_MSM 0x018
30 #define REG_PM8058_VREG_EN_GRP_5_4 0x1c8
32 /* Regulator control registers for shutdown/reset */
33 #define PM8058_S0_CTRL 0x004
34 #define PM8058_S1_CTRL 0x005
35 #define PM8058_S3_CTRL 0x111
36 #define PM8058_L21_CTRL 0x120
37 #define PM8058_L22_CTRL 0x121
39 #define PM8058_REGULATOR_ENABLE_MASK 0x80
40 #define PM8058_REGULATOR_ENABLE 0x80
41 #define PM8058_REGULATOR_DISABLE 0x00
42 #define PM8058_REGULATOR_PULL_DOWN_MASK 0x40
43 #define PM8058_REGULATOR_PULL_DOWN_EN 0x40
45 /* Buck CTRL register */
46 #define PM8058_SMPS_LEGACY_VREF_SEL 0x20
47 #define PM8058_SMPS_LEGACY_VPROG_MASK 0x1f
48 #define PM8058_SMPS_ADVANCED_BAND_MASK 0xC0
49 #define PM8058_SMPS_ADVANCED_BAND_SHIFT 6
50 #define PM8058_SMPS_ADVANCED_VPROG_MASK 0x3f
52 /* Buck TEST2 registers for shutdown/reset */
53 #define PM8058_S0_TEST2 0x084
54 #define PM8058_S1_TEST2 0x085
55 #define PM8058_S3_TEST2 0x11a
57 #define PM8058_REGULATOR_BANK_WRITE 0x80
58 #define PM8058_REGULATOR_BANK_MASK 0x70
59 #define PM8058_REGULATOR_BANK_SHIFT 4
60 #define PM8058_REGULATOR_BANK_SEL(n) ((n) << PM8058_REGULATOR_BANK_SHIFT)
62 /* Buck TEST2 register bank 1 */
63 #define PM8058_SMPS_LEGACY_VLOW_SEL 0x01
65 /* Buck TEST2 register bank 7 */
66 #define PM8058_SMPS_ADVANCED_MODE_MASK 0x02
67 #define PM8058_SMPS_ADVANCED_MODE 0x02
68 #define PM8058_SMPS_LEGACY_MODE 0x00
71 * struct pmic8xxx_pwrkey - pmic8xxx pwrkey information
72 * @key_press_irq: key press irq number
73 * @regmap: device regmap
74 * @shutdown_fn: shutdown configuration function
76 struct pmic8xxx_pwrkey
{
78 struct regmap
*regmap
;
79 int (*shutdown_fn
)(struct pmic8xxx_pwrkey
*, bool);
82 static irqreturn_t
pwrkey_press_irq(int irq
, void *_pwr
)
84 struct input_dev
*pwr
= _pwr
;
86 input_report_key(pwr
, KEY_POWER
, 1);
92 static irqreturn_t
pwrkey_release_irq(int irq
, void *_pwr
)
94 struct input_dev
*pwr
= _pwr
;
96 input_report_key(pwr
, KEY_POWER
, 0);
102 static int pmic8xxx_pwrkey_suspend(struct device
*dev
)
104 struct pmic8xxx_pwrkey
*pwrkey
= dev_get_drvdata(dev
);
106 if (device_may_wakeup(dev
))
107 enable_irq_wake(pwrkey
->key_press_irq
);
112 static int pmic8xxx_pwrkey_resume(struct device
*dev
)
114 struct pmic8xxx_pwrkey
*pwrkey
= dev_get_drvdata(dev
);
116 if (device_may_wakeup(dev
))
117 disable_irq_wake(pwrkey
->key_press_irq
);
122 static DEFINE_SIMPLE_DEV_PM_OPS(pm8xxx_pwr_key_pm_ops
,
123 pmic8xxx_pwrkey_suspend
, pmic8xxx_pwrkey_resume
);
125 static void pmic8xxx_pwrkey_shutdown(struct platform_device
*pdev
)
127 struct pmic8xxx_pwrkey
*pwrkey
= platform_get_drvdata(pdev
);
130 bool reset
= system_state
== SYSTEM_RESTART
;
132 if (pwrkey
->shutdown_fn
) {
133 error
= pwrkey
->shutdown_fn(pwrkey
, reset
);
139 * Select action to perform (reset or shutdown) when PS_HOLD goes low.
140 * Also ensure that KPD, CBL0, and CBL1 pull ups are enabled and that
141 * USB charging is enabled.
143 mask
= PON_CNTL_1_PULL_UP_EN
| PON_CNTL_1_USB_PWR_EN
;
144 mask
|= PON_CNTL_1_WD_EN_RESET
;
147 val
&= ~PON_CNTL_1_WD_EN_RESET
;
149 regmap_update_bits(pwrkey
->regmap
, PON_CNTL_1
, mask
, val
);
153 * Set an SMPS regulator to be disabled in its CTRL register, but enabled
154 * in the master enable register. Also set it's pull down enable bit.
155 * Take care to make sure that the output voltage doesn't change if switching
156 * from advanced mode to legacy mode.
158 static int pm8058_disable_smps_locally_set_pull_down(struct regmap
*regmap
,
159 u16 ctrl_addr
, u16 test2_addr
, u16 master_enable_addr
,
160 u8 master_enable_bit
)
163 u8 vref_sel
, vlow_sel
, band
, vprog
, bank
;
166 bank
= PM8058_REGULATOR_BANK_SEL(7);
167 error
= regmap_write(regmap
, test2_addr
, bank
);
171 error
= regmap_read(regmap
, test2_addr
, ®
);
175 reg
&= PM8058_SMPS_ADVANCED_MODE_MASK
;
176 /* Check if in advanced mode. */
177 if (reg
== PM8058_SMPS_ADVANCED_MODE
) {
178 /* Determine current output voltage. */
179 error
= regmap_read(regmap
, ctrl_addr
, ®
);
183 band
= reg
& PM8058_SMPS_ADVANCED_BAND_MASK
;
184 band
>>= PM8058_SMPS_ADVANCED_BAND_SHIFT
;
191 vref_sel
= PM8058_SMPS_LEGACY_VREF_SEL
;
195 vref_sel
= PM8058_SMPS_LEGACY_VREF_SEL
;
196 vlow_sel
= PM8058_SMPS_LEGACY_VLOW_SEL
;
199 pr_err("%s: regulator already disabled\n", __func__
);
202 vprog
= reg
& PM8058_SMPS_ADVANCED_VPROG_MASK
;
203 /* Round up if fine step is in use. */
204 vprog
= (vprog
+ 1) >> 1;
205 if (vprog
> PM8058_SMPS_LEGACY_VPROG_MASK
)
206 vprog
= PM8058_SMPS_LEGACY_VPROG_MASK
;
208 /* Set VLOW_SEL bit. */
209 bank
= PM8058_REGULATOR_BANK_SEL(1);
210 error
= regmap_write(regmap
, test2_addr
, bank
);
214 error
= regmap_update_bits(regmap
, test2_addr
,
215 PM8058_REGULATOR_BANK_WRITE
| PM8058_REGULATOR_BANK_MASK
216 | PM8058_SMPS_LEGACY_VLOW_SEL
,
217 PM8058_REGULATOR_BANK_WRITE
|
218 PM8058_REGULATOR_BANK_SEL(1) | vlow_sel
);
222 /* Switch to legacy mode */
223 bank
= PM8058_REGULATOR_BANK_SEL(7);
224 error
= regmap_write(regmap
, test2_addr
, bank
);
228 error
= regmap_update_bits(regmap
, test2_addr
,
229 PM8058_REGULATOR_BANK_WRITE
|
230 PM8058_REGULATOR_BANK_MASK
|
231 PM8058_SMPS_ADVANCED_MODE_MASK
,
232 PM8058_REGULATOR_BANK_WRITE
|
233 PM8058_REGULATOR_BANK_SEL(7) |
234 PM8058_SMPS_LEGACY_MODE
);
238 /* Enable locally, enable pull down, keep voltage the same. */
239 error
= regmap_update_bits(regmap
, ctrl_addr
,
240 PM8058_REGULATOR_ENABLE_MASK
|
241 PM8058_REGULATOR_PULL_DOWN_MASK
|
242 PM8058_SMPS_LEGACY_VREF_SEL
|
243 PM8058_SMPS_LEGACY_VPROG_MASK
,
244 PM8058_REGULATOR_ENABLE
| PM8058_REGULATOR_PULL_DOWN_EN
250 /* Enable in master control register. */
251 error
= regmap_update_bits(regmap
, master_enable_addr
,
252 master_enable_bit
, master_enable_bit
);
256 /* Disable locally and enable pull down. */
257 return regmap_update_bits(regmap
, ctrl_addr
,
258 PM8058_REGULATOR_ENABLE_MASK
| PM8058_REGULATOR_PULL_DOWN_MASK
,
259 PM8058_REGULATOR_DISABLE
| PM8058_REGULATOR_PULL_DOWN_EN
);
262 static int pm8058_disable_ldo_locally_set_pull_down(struct regmap
*regmap
,
263 u16 ctrl_addr
, u16 master_enable_addr
, u8 master_enable_bit
)
267 /* Enable LDO in master control register. */
268 error
= regmap_update_bits(regmap
, master_enable_addr
,
269 master_enable_bit
, master_enable_bit
);
273 /* Disable LDO in CTRL register and set pull down */
274 return regmap_update_bits(regmap
, ctrl_addr
,
275 PM8058_REGULATOR_ENABLE_MASK
| PM8058_REGULATOR_PULL_DOWN_MASK
,
276 PM8058_REGULATOR_DISABLE
| PM8058_REGULATOR_PULL_DOWN_EN
);
279 static int pm8058_pwrkey_shutdown(struct pmic8xxx_pwrkey
*pwrkey
, bool reset
)
282 struct regmap
*regmap
= pwrkey
->regmap
;
285 /* When shutting down, enable active pulldowns on important rails. */
287 /* Disable SMPS's 0,1,3 locally and set pulldown enable bits. */
288 pm8058_disable_smps_locally_set_pull_down(regmap
,
289 PM8058_S0_CTRL
, PM8058_S0_TEST2
,
290 REG_PM8058_VREG_EN_MSM
, BIT(7));
291 pm8058_disable_smps_locally_set_pull_down(regmap
,
292 PM8058_S1_CTRL
, PM8058_S1_TEST2
,
293 REG_PM8058_VREG_EN_MSM
, BIT(6));
294 pm8058_disable_smps_locally_set_pull_down(regmap
,
295 PM8058_S3_CTRL
, PM8058_S3_TEST2
,
296 REG_PM8058_VREG_EN_GRP_5_4
, BIT(7) | BIT(4));
297 /* Disable LDO 21 locally and set pulldown enable bit. */
298 pm8058_disable_ldo_locally_set_pull_down(regmap
,
299 PM8058_L21_CTRL
, REG_PM8058_VREG_EN_GRP_5_4
,
304 * Fix-up: Set regulator LDO22 to 1.225 V in high power mode. Leave its
305 * pull-down state intact. This ensures a safe shutdown.
307 error
= regmap_update_bits(regmap
, PM8058_L22_CTRL
, 0xbf, 0x93);
311 /* Enable SMPL if resetting is desired */
312 mask
= SLEEP_CTRL_SMPL_EN_RESET
;
316 return regmap_update_bits(regmap
, PM8058_SLEEP_CTRL
, mask
, val
);
319 static int pm8921_pwrkey_shutdown(struct pmic8xxx_pwrkey
*pwrkey
, bool reset
)
321 struct regmap
*regmap
= pwrkey
->regmap
;
322 u8 mask
= SLEEP_CTRL_SMPL_EN_RESET
;
325 /* Enable SMPL if resetting is desired */
328 return regmap_update_bits(regmap
, PM8921_SLEEP_CTRL
, mask
, val
);
331 static int pmic8xxx_pwrkey_probe(struct platform_device
*pdev
)
333 struct input_dev
*pwr
;
334 int key_release_irq
= platform_get_irq(pdev
, 0);
335 int key_press_irq
= platform_get_irq(pdev
, 1);
338 unsigned int pon_cntl
;
339 struct regmap
*regmap
;
340 struct pmic8xxx_pwrkey
*pwrkey
;
344 if (of_property_read_u32(pdev
->dev
.of_node
, "debounce", &kpd_delay
))
347 /* Valid range of pwr key trigger delay is 1/64 sec to 2 seconds. */
348 if (kpd_delay
> USEC_PER_SEC
* 2 || kpd_delay
< USEC_PER_SEC
/ 64) {
349 dev_err(&pdev
->dev
, "invalid power key trigger delay\n");
353 pull_up
= of_property_read_bool(pdev
->dev
.of_node
, "pull-up");
355 regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
357 dev_err(&pdev
->dev
, "failed to locate regmap for the device\n");
361 pwrkey
= devm_kzalloc(&pdev
->dev
, sizeof(*pwrkey
), GFP_KERNEL
);
365 pwrkey
->shutdown_fn
= of_device_get_match_data(&pdev
->dev
);
366 pwrkey
->regmap
= regmap
;
367 pwrkey
->key_press_irq
= key_press_irq
;
369 pwr
= devm_input_allocate_device(&pdev
->dev
);
371 dev_dbg(&pdev
->dev
, "Can't allocate power button\n");
375 input_set_capability(pwr
, EV_KEY
, KEY_POWER
);
377 pwr
->name
= "pmic8xxx_pwrkey";
378 pwr
->phys
= "pmic8xxx_pwrkey/input0";
380 delay
= (kpd_delay
<< 6) / USEC_PER_SEC
;
381 delay
= ilog2(delay
);
383 err
= regmap_read(regmap
, PON_CNTL_1
, &pon_cntl
);
385 dev_err(&pdev
->dev
, "failed reading PON_CNTL_1 err=%d\n", err
);
389 pon_cntl
&= ~PON_CNTL_TRIG_DELAY_MASK
;
390 pon_cntl
|= (delay
& PON_CNTL_TRIG_DELAY_MASK
);
392 pon_cntl
|= PON_CNTL_PULL_UP
;
394 pon_cntl
&= ~PON_CNTL_PULL_UP
;
396 err
= regmap_write(regmap
, PON_CNTL_1
, pon_cntl
);
398 dev_err(&pdev
->dev
, "failed writing PON_CNTL_1 err=%d\n", err
);
402 err
= devm_request_irq(&pdev
->dev
, key_press_irq
, pwrkey_press_irq
,
404 "pmic8xxx_pwrkey_press", pwr
);
406 dev_err(&pdev
->dev
, "Can't get %d IRQ for pwrkey: %d\n",
411 err
= devm_request_irq(&pdev
->dev
, key_release_irq
, pwrkey_release_irq
,
413 "pmic8xxx_pwrkey_release", pwr
);
415 dev_err(&pdev
->dev
, "Can't get %d IRQ for pwrkey: %d\n",
416 key_release_irq
, err
);
420 err
= input_register_device(pwr
);
422 dev_err(&pdev
->dev
, "Can't register power key: %d\n", err
);
426 platform_set_drvdata(pdev
, pwrkey
);
427 device_init_wakeup(&pdev
->dev
, 1);
432 static const struct of_device_id pm8xxx_pwr_key_id_table
[] = {
433 { .compatible
= "qcom,pm8058-pwrkey", .data
= &pm8058_pwrkey_shutdown
},
434 { .compatible
= "qcom,pm8921-pwrkey", .data
= &pm8921_pwrkey_shutdown
},
437 MODULE_DEVICE_TABLE(of
, pm8xxx_pwr_key_id_table
);
439 static struct platform_driver pmic8xxx_pwrkey_driver
= {
440 .probe
= pmic8xxx_pwrkey_probe
,
441 .shutdown
= pmic8xxx_pwrkey_shutdown
,
443 .name
= "pm8xxx-pwrkey",
444 .pm
= pm_sleep_ptr(&pm8xxx_pwr_key_pm_ops
),
445 .of_match_table
= pm8xxx_pwr_key_id_table
,
448 module_platform_driver(pmic8xxx_pwrkey_driver
);
450 MODULE_ALIAS("platform:pmic8xxx_pwrkey");
451 MODULE_DESCRIPTION("PMIC8XXX Power Key driver");
452 MODULE_LICENSE("GPL v2");
453 MODULE_AUTHOR("Trilok Soni <tsoni@codeaurora.org>");