Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 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. |
| 6 | * |
| 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. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 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> |
Stephen Boyd | 1e63bd9 | 2013-12-15 03:24:45 -0800 | [diff] [blame] | 20 | #include <linux/regmap.h> |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 21 | #include <linux/log2.h> |
Stephen Boyd | 57918df | 2014-03-29 12:39:38 -0700 | [diff] [blame] | 22 | #include <linux/of.h> |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 23 | |
| 24 | #define PON_CNTL_1 0x1C |
| 25 | #define PON_CNTL_PULL_UP BIT(7) |
| 26 | #define PON_CNTL_TRIG_DELAY_MASK (0x7) |
| 27 | |
| 28 | /** |
| 29 | * struct pmic8xxx_pwrkey - pmic8xxx pwrkey information |
| 30 | * @key_press_irq: key press irq number |
| 31 | */ |
| 32 | struct pmic8xxx_pwrkey { |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 33 | int key_press_irq; |
| 34 | }; |
| 35 | |
Stephen Boyd | b27f8fe | 2013-12-15 03:14:47 -0800 | [diff] [blame] | 36 | static irqreturn_t pwrkey_press_irq(int irq, void *_pwr) |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 37 | { |
Stephen Boyd | b27f8fe | 2013-12-15 03:14:47 -0800 | [diff] [blame] | 38 | struct input_dev *pwr = _pwr; |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 39 | |
Stephen Boyd | b27f8fe | 2013-12-15 03:14:47 -0800 | [diff] [blame] | 40 | input_report_key(pwr, KEY_POWER, 1); |
| 41 | input_sync(pwr); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 42 | |
| 43 | return IRQ_HANDLED; |
| 44 | } |
| 45 | |
Stephen Boyd | b27f8fe | 2013-12-15 03:14:47 -0800 | [diff] [blame] | 46 | static irqreturn_t pwrkey_release_irq(int irq, void *_pwr) |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 47 | { |
Stephen Boyd | b27f8fe | 2013-12-15 03:14:47 -0800 | [diff] [blame] | 48 | struct input_dev *pwr = _pwr; |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 49 | |
Stephen Boyd | b27f8fe | 2013-12-15 03:14:47 -0800 | [diff] [blame] | 50 | input_report_key(pwr, KEY_POWER, 0); |
| 51 | input_sync(pwr); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 52 | |
| 53 | return IRQ_HANDLED; |
| 54 | } |
| 55 | |
Jingoo Han | 97a652a | 2014-11-02 00:02:46 -0700 | [diff] [blame] | 56 | static int __maybe_unused pmic8xxx_pwrkey_suspend(struct device *dev) |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 57 | { |
| 58 | struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev); |
| 59 | |
| 60 | if (device_may_wakeup(dev)) |
| 61 | enable_irq_wake(pwrkey->key_press_irq); |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
Jingoo Han | 97a652a | 2014-11-02 00:02:46 -0700 | [diff] [blame] | 66 | static int __maybe_unused pmic8xxx_pwrkey_resume(struct device *dev) |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 67 | { |
| 68 | struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev); |
| 69 | |
| 70 | if (device_may_wakeup(dev)) |
| 71 | disable_irq_wake(pwrkey->key_press_irq); |
| 72 | |
| 73 | return 0; |
| 74 | } |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 75 | |
| 76 | static SIMPLE_DEV_PM_OPS(pm8xxx_pwr_key_pm_ops, |
| 77 | pmic8xxx_pwrkey_suspend, pmic8xxx_pwrkey_resume); |
| 78 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 79 | static int pmic8xxx_pwrkey_probe(struct platform_device *pdev) |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 80 | { |
| 81 | struct input_dev *pwr; |
| 82 | int key_release_irq = platform_get_irq(pdev, 0); |
| 83 | int key_press_irq = platform_get_irq(pdev, 1); |
| 84 | int err; |
| 85 | unsigned int delay; |
Stephen Boyd | 1e63bd9 | 2013-12-15 03:24:45 -0800 | [diff] [blame] | 86 | unsigned int pon_cntl; |
| 87 | struct regmap *regmap; |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 88 | struct pmic8xxx_pwrkey *pwrkey; |
Stephen Boyd | 57918df | 2014-03-29 12:39:38 -0700 | [diff] [blame] | 89 | u32 kpd_delay; |
| 90 | bool pull_up; |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 91 | |
Stephen Boyd | 57918df | 2014-03-29 12:39:38 -0700 | [diff] [blame] | 92 | if (of_property_read_u32(pdev->dev.of_node, "debounce", &kpd_delay)) |
Stephen Boyd | 70a2607 | 2014-05-14 11:58:55 -0700 | [diff] [blame] | 93 | kpd_delay = 15625; |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 94 | |
Stephen Boyd | 70a2607 | 2014-05-14 11:58:55 -0700 | [diff] [blame] | 95 | if (kpd_delay > 62500 || kpd_delay == 0) { |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 96 | dev_err(&pdev->dev, "invalid power key trigger delay\n"); |
| 97 | return -EINVAL; |
| 98 | } |
| 99 | |
Stephen Boyd | 70a2607 | 2014-05-14 11:58:55 -0700 | [diff] [blame] | 100 | pull_up = of_property_read_bool(pdev->dev.of_node, "pull-up"); |
| 101 | |
Stephen Boyd | 1e63bd9 | 2013-12-15 03:24:45 -0800 | [diff] [blame] | 102 | regmap = dev_get_regmap(pdev->dev.parent, NULL); |
| 103 | if (!regmap) { |
| 104 | dev_err(&pdev->dev, "failed to locate regmap for the device\n"); |
| 105 | return -ENODEV; |
| 106 | } |
| 107 | |
Dmitry Torokhov | eb735d3 | 2013-12-15 03:28:26 -0800 | [diff] [blame] | 108 | pwrkey = devm_kzalloc(&pdev->dev, sizeof(*pwrkey), GFP_KERNEL); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 109 | if (!pwrkey) |
| 110 | return -ENOMEM; |
| 111 | |
Dmitry Torokhov | eb735d3 | 2013-12-15 03:28:26 -0800 | [diff] [blame] | 112 | pwrkey->key_press_irq = key_press_irq; |
| 113 | |
| 114 | pwr = devm_input_allocate_device(&pdev->dev); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 115 | if (!pwr) { |
| 116 | dev_dbg(&pdev->dev, "Can't allocate power button\n"); |
Dmitry Torokhov | eb735d3 | 2013-12-15 03:28:26 -0800 | [diff] [blame] | 117 | return -ENOMEM; |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | input_set_capability(pwr, EV_KEY, KEY_POWER); |
| 121 | |
| 122 | pwr->name = "pmic8xxx_pwrkey"; |
| 123 | pwr->phys = "pmic8xxx_pwrkey/input0"; |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 124 | |
Stephen Boyd | 57918df | 2014-03-29 12:39:38 -0700 | [diff] [blame] | 125 | delay = (kpd_delay << 10) / USEC_PER_SEC; |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 126 | delay = 1 + ilog2(delay); |
| 127 | |
Stephen Boyd | 1e63bd9 | 2013-12-15 03:24:45 -0800 | [diff] [blame] | 128 | err = regmap_read(regmap, PON_CNTL_1, &pon_cntl); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 129 | if (err < 0) { |
| 130 | dev_err(&pdev->dev, "failed reading PON_CNTL_1 err=%d\n", err); |
Dmitry Torokhov | eb735d3 | 2013-12-15 03:28:26 -0800 | [diff] [blame] | 131 | return err; |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | pon_cntl &= ~PON_CNTL_TRIG_DELAY_MASK; |
| 135 | pon_cntl |= (delay & PON_CNTL_TRIG_DELAY_MASK); |
Stephen Boyd | 57918df | 2014-03-29 12:39:38 -0700 | [diff] [blame] | 136 | if (pull_up) |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 137 | pon_cntl |= PON_CNTL_PULL_UP; |
| 138 | else |
| 139 | pon_cntl &= ~PON_CNTL_PULL_UP; |
| 140 | |
Stephen Boyd | 1e63bd9 | 2013-12-15 03:24:45 -0800 | [diff] [blame] | 141 | err = regmap_write(regmap, PON_CNTL_1, pon_cntl); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 142 | if (err < 0) { |
| 143 | dev_err(&pdev->dev, "failed writing PON_CNTL_1 err=%d\n", err); |
Dmitry Torokhov | eb735d3 | 2013-12-15 03:28:26 -0800 | [diff] [blame] | 144 | return err; |
| 145 | } |
| 146 | |
| 147 | err = devm_request_irq(&pdev->dev, key_press_irq, pwrkey_press_irq, |
| 148 | IRQF_TRIGGER_RISING, |
| 149 | "pmic8xxx_pwrkey_press", pwr); |
| 150 | if (err) { |
| 151 | dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n", |
| 152 | key_press_irq, err); |
| 153 | return err; |
| 154 | } |
| 155 | |
| 156 | err = devm_request_irq(&pdev->dev, key_release_irq, pwrkey_release_irq, |
| 157 | IRQF_TRIGGER_RISING, |
| 158 | "pmic8xxx_pwrkey_release", pwr); |
| 159 | if (err) { |
| 160 | dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n", |
| 161 | key_release_irq, err); |
| 162 | return err; |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | err = input_register_device(pwr); |
| 166 | if (err) { |
Dmitry Torokhov | eb735d3 | 2013-12-15 03:28:26 -0800 | [diff] [blame] | 167 | dev_err(&pdev->dev, "Can't register power key: %d\n", err); |
| 168 | return err; |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 169 | } |
| 170 | |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 171 | platform_set_drvdata(pdev, pwrkey); |
Stephen Boyd | 57918df | 2014-03-29 12:39:38 -0700 | [diff] [blame] | 172 | device_init_wakeup(&pdev->dev, 1); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 173 | |
| 174 | return 0; |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 175 | } |
| 176 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 177 | static int pmic8xxx_pwrkey_remove(struct platform_device *pdev) |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 178 | { |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 179 | device_init_wakeup(&pdev->dev, 0); |
| 180 | |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 181 | return 0; |
| 182 | } |
| 183 | |
Stephen Boyd | 57918df | 2014-03-29 12:39:38 -0700 | [diff] [blame] | 184 | static const struct of_device_id pm8xxx_pwr_key_id_table[] = { |
| 185 | { .compatible = "qcom,pm8058-pwrkey" }, |
| 186 | { .compatible = "qcom,pm8921-pwrkey" }, |
| 187 | { } |
| 188 | }; |
| 189 | MODULE_DEVICE_TABLE(of, pm8xxx_pwr_key_id_table); |
| 190 | |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 191 | static struct platform_driver pmic8xxx_pwrkey_driver = { |
| 192 | .probe = pmic8xxx_pwrkey_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 193 | .remove = pmic8xxx_pwrkey_remove, |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 194 | .driver = { |
Stephen Boyd | 57918df | 2014-03-29 12:39:38 -0700 | [diff] [blame] | 195 | .name = "pm8xxx-pwrkey", |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 196 | .pm = &pm8xxx_pwr_key_pm_ops, |
Stephen Boyd | 57918df | 2014-03-29 12:39:38 -0700 | [diff] [blame] | 197 | .of_match_table = pm8xxx_pwr_key_id_table, |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 198 | }, |
| 199 | }; |
JJ Ding | 840a746 | 2011-11-29 11:08:40 -0800 | [diff] [blame] | 200 | module_platform_driver(pmic8xxx_pwrkey_driver); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 201 | |
| 202 | MODULE_ALIAS("platform:pmic8xxx_pwrkey"); |
| 203 | MODULE_DESCRIPTION("PMIC8XXX Power Key driver"); |
| 204 | MODULE_LICENSE("GPL v2"); |
| 205 | MODULE_AUTHOR("Trilok Soni <tsoni@codeaurora.org>"); |