rcu: Maintain special bits at bottom of ->dynticks counter
[linux/fpc-iii.git] / drivers / input / misc / pmic8xxx-pwrkey.c
blob73323b0c72c145e453871df967f1187a337ded05
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>
22 #include <linux/of.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
79 /**
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 {
86 int key_press_irq;
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);
96 input_sync(pwr);
98 return IRQ_HANDLED;
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);
106 input_sync(pwr);
108 return IRQ_HANDLED;
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);
118 return 0;
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);
128 return 0;
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);
137 int error;
138 u8 mask, val;
139 bool reset = system_state == SYSTEM_RESTART;
141 if (pwrkey->shutdown_fn) {
142 error = pwrkey->shutdown_fn(pwrkey, reset);
143 if (error)
144 return;
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;
154 val = mask;
155 if (!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)
171 int error;
172 u8 vref_sel, vlow_sel, band, vprog, bank;
173 unsigned int reg;
175 bank = PM8058_REGULATOR_BANK_SEL(7);
176 error = regmap_write(regmap, test2_addr, bank);
177 if (error)
178 return error;
180 error = regmap_read(regmap, test2_addr, &reg);
181 if (error)
182 return error;
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, &reg);
189 if (error)
190 return error;
192 band = reg & PM8058_SMPS_ADVANCED_BAND_MASK;
193 band >>= PM8058_SMPS_ADVANCED_BAND_SHIFT;
194 switch (band) {
195 case 3:
196 vref_sel = 0;
197 vlow_sel = 0;
198 break;
199 case 2:
200 vref_sel = PM8058_SMPS_LEGACY_VREF_SEL;
201 vlow_sel = 0;
202 break;
203 case 1:
204 vref_sel = PM8058_SMPS_LEGACY_VREF_SEL;
205 vlow_sel = PM8058_SMPS_LEGACY_VLOW_SEL;
206 break;
207 default:
208 pr_err("%s: regulator already disabled\n", __func__);
209 return -EPERM;
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);
220 if (error)
221 return error;
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);
228 if (error)
229 return error;
231 /* Switch to legacy mode */
232 bank = PM8058_REGULATOR_BANK_SEL(7);
233 error = regmap_write(regmap, test2_addr, bank);
234 if (error)
235 return error;
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);
244 if (error)
245 return error;
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
254 | vref_sel | vprog);
255 if (error)
256 return error;
259 /* Enable in master control register. */
260 error = regmap_update_bits(regmap, master_enable_addr,
261 master_enable_bit, master_enable_bit);
262 if (error)
263 return error;
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)
274 int error;
276 /* Enable LDO in master control register. */
277 error = regmap_update_bits(regmap, master_enable_addr,
278 master_enable_bit, master_enable_bit);
279 if (error)
280 return error;
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)
290 int error;
291 struct regmap *regmap = pwrkey->regmap;
292 u8 mask, val;
294 /* When shutting down, enable active pulldowns on important rails. */
295 if (!reset) {
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,
309 BIT(1));
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);
317 if (error)
318 return error;
320 /* Enable SMPL if resetting is desired */
321 mask = SLEEP_CTRL_SMPL_EN_RESET;
322 val = 0;
323 if (reset)
324 val = mask;
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;
332 u8 val = 0;
334 /* Enable SMPL if resetting is desired */
335 if (reset)
336 val = mask;
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);
345 int err;
346 unsigned int delay;
347 unsigned int pon_cntl;
348 struct regmap *regmap;
349 struct pmic8xxx_pwrkey *pwrkey;
350 u32 kpd_delay;
351 bool pull_up;
353 if (of_property_read_u32(pdev->dev.of_node, "debounce", &kpd_delay))
354 kpd_delay = 15625;
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");
359 return -EINVAL;
362 pull_up = of_property_read_bool(pdev->dev.of_node, "pull-up");
364 regmap = dev_get_regmap(pdev->dev.parent, NULL);
365 if (!regmap) {
366 dev_err(&pdev->dev, "failed to locate regmap for the device\n");
367 return -ENODEV;
370 pwrkey = devm_kzalloc(&pdev->dev, sizeof(*pwrkey), GFP_KERNEL);
371 if (!pwrkey)
372 return -ENOMEM;
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);
379 if (!pwr) {
380 dev_dbg(&pdev->dev, "Can't allocate power button\n");
381 return -ENOMEM;
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);
393 if (err < 0) {
394 dev_err(&pdev->dev, "failed reading PON_CNTL_1 err=%d\n", err);
395 return err;
398 pon_cntl &= ~PON_CNTL_TRIG_DELAY_MASK;
399 pon_cntl |= (delay & PON_CNTL_TRIG_DELAY_MASK);
400 if (pull_up)
401 pon_cntl |= PON_CNTL_PULL_UP;
402 else
403 pon_cntl &= ~PON_CNTL_PULL_UP;
405 err = regmap_write(regmap, PON_CNTL_1, pon_cntl);
406 if (err < 0) {
407 dev_err(&pdev->dev, "failed writing PON_CNTL_1 err=%d\n", err);
408 return err;
411 err = devm_request_irq(&pdev->dev, key_press_irq, pwrkey_press_irq,
412 IRQF_TRIGGER_RISING,
413 "pmic8xxx_pwrkey_press", pwr);
414 if (err) {
415 dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
416 key_press_irq, err);
417 return err;
420 err = devm_request_irq(&pdev->dev, key_release_irq, pwrkey_release_irq,
421 IRQF_TRIGGER_RISING,
422 "pmic8xxx_pwrkey_release", pwr);
423 if (err) {
424 dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
425 key_release_irq, err);
426 return err;
429 err = input_register_device(pwr);
430 if (err) {
431 dev_err(&pdev->dev, "Can't register power key: %d\n", err);
432 return err;
435 platform_set_drvdata(pdev, pwrkey);
436 device_init_wakeup(&pdev->dev, 1);
438 return 0;
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,
451 .driver = {
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>");