1 /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/input.h>
18 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 #include <linux/log2.h>
23 #include <linux/of_device.h>
25 #define PON_CNTL_1 0x1C
26 #define PON_CNTL_PULL_UP BIT(7)
27 #define PON_CNTL_TRIG_DELAY_MASK (0x7)
28 #define PON_CNTL_1_PULL_UP_EN 0xe0
29 #define PON_CNTL_1_USB_PWR_EN 0x10
30 #define PON_CNTL_1_WD_EN_RESET 0x08
32 #define PM8058_SLEEP_CTRL 0x02b
33 #define PM8921_SLEEP_CTRL 0x10a
35 #define SLEEP_CTRL_SMPL_EN_RESET 0x04
37 /* Regulator master enable addresses */
38 #define REG_PM8058_VREG_EN_MSM 0x018
39 #define REG_PM8058_VREG_EN_GRP_5_4 0x1c8
41 /* Regulator control registers for shutdown/reset */
42 #define PM8058_S0_CTRL 0x004
43 #define PM8058_S1_CTRL 0x005
44 #define PM8058_S3_CTRL 0x111
45 #define PM8058_L21_CTRL 0x120
46 #define PM8058_L22_CTRL 0x121
48 #define PM8058_REGULATOR_ENABLE_MASK 0x80
49 #define PM8058_REGULATOR_ENABLE 0x80
50 #define PM8058_REGULATOR_DISABLE 0x00
51 #define PM8058_REGULATOR_PULL_DOWN_MASK 0x40
52 #define PM8058_REGULATOR_PULL_DOWN_EN 0x40
54 /* Buck CTRL register */
55 #define PM8058_SMPS_LEGACY_VREF_SEL 0x20
56 #define PM8058_SMPS_LEGACY_VPROG_MASK 0x1f
57 #define PM8058_SMPS_ADVANCED_BAND_MASK 0xC0
58 #define PM8058_SMPS_ADVANCED_BAND_SHIFT 6
59 #define PM8058_SMPS_ADVANCED_VPROG_MASK 0x3f
61 /* Buck TEST2 registers for shutdown/reset */
62 #define PM8058_S0_TEST2 0x084
63 #define PM8058_S1_TEST2 0x085
64 #define PM8058_S3_TEST2 0x11a
66 #define PM8058_REGULATOR_BANK_WRITE 0x80
67 #define PM8058_REGULATOR_BANK_MASK 0x70
68 #define PM8058_REGULATOR_BANK_SHIFT 4
69 #define PM8058_REGULATOR_BANK_SEL(n) ((n) << PM8058_REGULATOR_BANK_SHIFT)
71 /* Buck TEST2 register bank 1 */
72 #define PM8058_SMPS_LEGACY_VLOW_SEL 0x01
74 /* Buck TEST2 register bank 7 */
75 #define PM8058_SMPS_ADVANCED_MODE_MASK 0x02
76 #define PM8058_SMPS_ADVANCED_MODE 0x02
77 #define PM8058_SMPS_LEGACY_MODE 0x00
80 * struct pmic8xxx_pwrkey - pmic8xxx pwrkey information
81 * @key_press_irq: key press irq number
82 * @regmap: device regmap
83 * @shutdown_fn: shutdown configuration function
85 struct pmic8xxx_pwrkey {
87 struct regmap *regmap;
88 int (*shutdown_fn)(struct pmic8xxx_pwrkey *, bool);
91 static irqreturn_t pwrkey_press_irq(int irq, void *_pwr)
93 struct input_dev *pwr = _pwr;
95 input_report_key(pwr, KEY_POWER, 1);
101 static irqreturn_t pwrkey_release_irq(int irq, void *_pwr)
103 struct input_dev *pwr = _pwr;
105 input_report_key(pwr, KEY_POWER, 0);
111 static int __maybe_unused pmic8xxx_pwrkey_suspend(struct device *dev)
113 struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev);
115 if (device_may_wakeup(dev))
116 enable_irq_wake(pwrkey->key_press_irq);
121 static int __maybe_unused pmic8xxx_pwrkey_resume(struct device *dev)
123 struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev);
125 if (device_may_wakeup(dev))
126 disable_irq_wake(pwrkey->key_press_irq);
131 static SIMPLE_DEV_PM_OPS(pm8xxx_pwr_key_pm_ops,
132 pmic8xxx_pwrkey_suspend, pmic8xxx_pwrkey_resume);
134 static void pmic8xxx_pwrkey_shutdown(struct platform_device *pdev)
136 struct pmic8xxx_pwrkey *pwrkey = platform_get_drvdata(pdev);
139 bool reset = system_state == SYSTEM_RESTART;
141 if (pwrkey->shutdown_fn) {
142 error = pwrkey->shutdown_fn(pwrkey, reset);
148 * Select action to perform (reset or shutdown) when PS_HOLD goes low.
149 * Also ensure that KPD, CBL0, and CBL1 pull ups are enabled and that
150 * USB charging is enabled.
152 mask = PON_CNTL_1_PULL_UP_EN | PON_CNTL_1_USB_PWR_EN;
153 mask |= PON_CNTL_1_WD_EN_RESET;
156 val &= ~PON_CNTL_1_WD_EN_RESET;
158 regmap_update_bits(pwrkey->regmap, PON_CNTL_1, mask, val);
162 * Set an SMPS regulator to be disabled in its CTRL register, but enabled
163 * in the master enable register. Also set it's pull down enable bit.
164 * Take care to make sure that the output voltage doesn't change if switching
165 * from advanced mode to legacy mode.
167 static int pm8058_disable_smps_locally_set_pull_down(struct regmap *regmap,
168 u16 ctrl_addr, u16 test2_addr, u16 master_enable_addr,
169 u8 master_enable_bit)
172 u8 vref_sel, vlow_sel, band, vprog, bank;
175 bank = PM8058_REGULATOR_BANK_SEL(7);
176 error = regmap_write(regmap, test2_addr, bank);
180 error = regmap_read(regmap, test2_addr, ®);
184 reg &= PM8058_SMPS_ADVANCED_MODE_MASK;
185 /* Check if in advanced mode. */
186 if (reg == PM8058_SMPS_ADVANCED_MODE) {
187 /* Determine current output voltage. */
188 error = regmap_read(regmap, ctrl_addr, ®);
192 band = reg & PM8058_SMPS_ADVANCED_BAND_MASK;
193 band >>= PM8058_SMPS_ADVANCED_BAND_SHIFT;
200 vref_sel = PM8058_SMPS_LEGACY_VREF_SEL;
204 vref_sel = PM8058_SMPS_LEGACY_VREF_SEL;
205 vlow_sel = PM8058_SMPS_LEGACY_VLOW_SEL;
208 pr_err("%s: regulator already disabled\n", __func__);
211 vprog = reg & PM8058_SMPS_ADVANCED_VPROG_MASK;
212 /* Round up if fine step is in use. */
213 vprog = (vprog + 1) >> 1;
214 if (vprog > PM8058_SMPS_LEGACY_VPROG_MASK)
215 vprog = PM8058_SMPS_LEGACY_VPROG_MASK;
217 /* Set VLOW_SEL bit. */
218 bank = PM8058_REGULATOR_BANK_SEL(1);
219 error = regmap_write(regmap, test2_addr, bank);
223 error = regmap_update_bits(regmap, test2_addr,
224 PM8058_REGULATOR_BANK_WRITE | PM8058_REGULATOR_BANK_MASK
225 | PM8058_SMPS_LEGACY_VLOW_SEL,
226 PM8058_REGULATOR_BANK_WRITE |
227 PM8058_REGULATOR_BANK_SEL(1) | vlow_sel);
231 /* Switch to legacy mode */
232 bank = PM8058_REGULATOR_BANK_SEL(7);
233 error = regmap_write(regmap, test2_addr, bank);
237 error = regmap_update_bits(regmap, test2_addr,
238 PM8058_REGULATOR_BANK_WRITE |
239 PM8058_REGULATOR_BANK_MASK |
240 PM8058_SMPS_ADVANCED_MODE_MASK,
241 PM8058_REGULATOR_BANK_WRITE |
242 PM8058_REGULATOR_BANK_SEL(7) |
243 PM8058_SMPS_LEGACY_MODE);
247 /* Enable locally, enable pull down, keep voltage the same. */
248 error = regmap_update_bits(regmap, ctrl_addr,
249 PM8058_REGULATOR_ENABLE_MASK |
250 PM8058_REGULATOR_PULL_DOWN_MASK |
251 PM8058_SMPS_LEGACY_VREF_SEL |
252 PM8058_SMPS_LEGACY_VPROG_MASK,
253 PM8058_REGULATOR_ENABLE | PM8058_REGULATOR_PULL_DOWN_EN
259 /* Enable in master control register. */
260 error = regmap_update_bits(regmap, master_enable_addr,
261 master_enable_bit, master_enable_bit);
265 /* Disable locally and enable pull down. */
266 return regmap_update_bits(regmap, ctrl_addr,
267 PM8058_REGULATOR_ENABLE_MASK | PM8058_REGULATOR_PULL_DOWN_MASK,
268 PM8058_REGULATOR_DISABLE | PM8058_REGULATOR_PULL_DOWN_EN);
271 static int pm8058_disable_ldo_locally_set_pull_down(struct regmap *regmap,
272 u16 ctrl_addr, u16 master_enable_addr, u8 master_enable_bit)
276 /* Enable LDO in master control register. */
277 error = regmap_update_bits(regmap, master_enable_addr,
278 master_enable_bit, master_enable_bit);
282 /* Disable LDO in CTRL register and set pull down */
283 return regmap_update_bits(regmap, ctrl_addr,
284 PM8058_REGULATOR_ENABLE_MASK | PM8058_REGULATOR_PULL_DOWN_MASK,
285 PM8058_REGULATOR_DISABLE | PM8058_REGULATOR_PULL_DOWN_EN);
288 static int pm8058_pwrkey_shutdown(struct pmic8xxx_pwrkey *pwrkey, bool reset)
291 struct regmap *regmap = pwrkey->regmap;
294 /* When shutting down, enable active pulldowns on important rails. */
296 /* Disable SMPS's 0,1,3 locally and set pulldown enable bits. */
297 pm8058_disable_smps_locally_set_pull_down(regmap,
298 PM8058_S0_CTRL, PM8058_S0_TEST2,
299 REG_PM8058_VREG_EN_MSM, BIT(7));
300 pm8058_disable_smps_locally_set_pull_down(regmap,
301 PM8058_S1_CTRL, PM8058_S1_TEST2,
302 REG_PM8058_VREG_EN_MSM, BIT(6));
303 pm8058_disable_smps_locally_set_pull_down(regmap,
304 PM8058_S3_CTRL, PM8058_S3_TEST2,
305 REG_PM8058_VREG_EN_GRP_5_4, BIT(7) | BIT(4));
306 /* Disable LDO 21 locally and set pulldown enable bit. */
307 pm8058_disable_ldo_locally_set_pull_down(regmap,
308 PM8058_L21_CTRL, REG_PM8058_VREG_EN_GRP_5_4,
313 * Fix-up: Set regulator LDO22 to 1.225 V in high power mode. Leave its
314 * pull-down state intact. This ensures a safe shutdown.
316 error = regmap_update_bits(regmap, PM8058_L22_CTRL, 0xbf, 0x93);
320 /* Enable SMPL if resetting is desired */
321 mask = SLEEP_CTRL_SMPL_EN_RESET;
325 return regmap_update_bits(regmap, PM8058_SLEEP_CTRL, mask, val);
328 static int pm8921_pwrkey_shutdown(struct pmic8xxx_pwrkey *pwrkey, bool reset)
330 struct regmap *regmap = pwrkey->regmap;
331 u8 mask = SLEEP_CTRL_SMPL_EN_RESET;
334 /* Enable SMPL if resetting is desired */
337 return regmap_update_bits(regmap, PM8921_SLEEP_CTRL, mask, val);
340 static int pmic8xxx_pwrkey_probe(struct platform_device *pdev)
342 struct input_dev *pwr;
343 int key_release_irq = platform_get_irq(pdev, 0);
344 int key_press_irq = platform_get_irq(pdev, 1);
347 unsigned int pon_cntl;
348 struct regmap *regmap;
349 struct pmic8xxx_pwrkey *pwrkey;
353 if (of_property_read_u32(pdev->dev.of_node, "debounce", &kpd_delay))
356 /* Valid range of pwr key trigger delay is 1/64 sec to 2 seconds. */
357 if (kpd_delay > USEC_PER_SEC * 2 || kpd_delay < USEC_PER_SEC / 64) {
358 dev_err(&pdev->dev, "invalid power key trigger delay\n");
362 pull_up = of_property_read_bool(pdev->dev.of_node, "pull-up");
364 regmap = dev_get_regmap(pdev->dev.parent, NULL);
366 dev_err(&pdev->dev, "failed to locate regmap for the device\n");
370 pwrkey = devm_kzalloc(&pdev->dev, sizeof(*pwrkey), GFP_KERNEL);
374 pwrkey->shutdown_fn = of_device_get_match_data(&pdev->dev);
375 pwrkey->regmap = regmap;
376 pwrkey->key_press_irq = key_press_irq;
378 pwr = devm_input_allocate_device(&pdev->dev);
380 dev_dbg(&pdev->dev, "Can't allocate power button\n");
384 input_set_capability(pwr, EV_KEY, KEY_POWER);
386 pwr->name = "pmic8xxx_pwrkey";
387 pwr->phys = "pmic8xxx_pwrkey/input0";
389 delay = (kpd_delay << 6) / USEC_PER_SEC;
390 delay = ilog2(delay);
392 err = regmap_read(regmap, PON_CNTL_1, &pon_cntl);
394 dev_err(&pdev->dev, "failed reading PON_CNTL_1 err=%d\n", err);
398 pon_cntl &= ~PON_CNTL_TRIG_DELAY_MASK;
399 pon_cntl |= (delay & PON_CNTL_TRIG_DELAY_MASK);
401 pon_cntl |= PON_CNTL_PULL_UP;
403 pon_cntl &= ~PON_CNTL_PULL_UP;
405 err = regmap_write(regmap, PON_CNTL_1, pon_cntl);
407 dev_err(&pdev->dev, "failed writing PON_CNTL_1 err=%d\n", err);
411 err = devm_request_irq(&pdev->dev, key_press_irq, pwrkey_press_irq,
413 "pmic8xxx_pwrkey_press", pwr);
415 dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
420 err = devm_request_irq(&pdev->dev, key_release_irq, pwrkey_release_irq,
422 "pmic8xxx_pwrkey_release", pwr);
424 dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
425 key_release_irq, err);
429 err = input_register_device(pwr);
431 dev_err(&pdev->dev, "Can't register power key: %d\n", err);
435 platform_set_drvdata(pdev, pwrkey);
436 device_init_wakeup(&pdev->dev, 1);
441 static const struct of_device_id pm8xxx_pwr_key_id_table[] = {
442 { .compatible = "qcom,pm8058-pwrkey", .data = &pm8058_pwrkey_shutdown },
443 { .compatible = "qcom,pm8921-pwrkey", .data = &pm8921_pwrkey_shutdown },
446 MODULE_DEVICE_TABLE(of, pm8xxx_pwr_key_id_table);
448 static struct platform_driver pmic8xxx_pwrkey_driver = {
449 .probe = pmic8xxx_pwrkey_probe,
450 .shutdown = pmic8xxx_pwrkey_shutdown,
452 .name = "pm8xxx-pwrkey",
453 .pm = &pm8xxx_pwr_key_pm_ops,
454 .of_match_table = pm8xxx_pwr_key_id_table,
457 module_platform_driver(pmic8xxx_pwrkey_driver);
459 MODULE_ALIAS("platform:pmic8xxx_pwrkey");
460 MODULE_DESCRIPTION("PMIC8XXX Power Key driver");
461 MODULE_LICENSE("GPL v2");
462 MODULE_AUTHOR("Trilok Soni <tsoni@codeaurora.org>");