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> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/input.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/platform_device.h> |
Stephen Boyd | 1e63bd9 | 2013-12-15 03:24:45 -0800 | [diff] [blame^] | 21 | #include <linux/regmap.h> |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 22 | #include <linux/log2.h> |
| 23 | |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 24 | #include <linux/input/pmic8xxx-pwrkey.h> |
| 25 | |
| 26 | #define PON_CNTL_1 0x1C |
| 27 | #define PON_CNTL_PULL_UP BIT(7) |
| 28 | #define PON_CNTL_TRIG_DELAY_MASK (0x7) |
| 29 | |
| 30 | /** |
| 31 | * struct pmic8xxx_pwrkey - pmic8xxx pwrkey information |
| 32 | * @key_press_irq: key press irq number |
| 33 | */ |
| 34 | struct pmic8xxx_pwrkey { |
| 35 | struct input_dev *pwr; |
| 36 | int key_press_irq; |
| 37 | }; |
| 38 | |
Stephen Boyd | b27f8fe | 2013-12-15 03:14:47 -0800 | [diff] [blame] | 39 | static irqreturn_t pwrkey_press_irq(int irq, void *_pwr) |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 40 | { |
Stephen Boyd | b27f8fe | 2013-12-15 03:14:47 -0800 | [diff] [blame] | 41 | struct input_dev *pwr = _pwr; |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 42 | |
Stephen Boyd | b27f8fe | 2013-12-15 03:14:47 -0800 | [diff] [blame] | 43 | input_report_key(pwr, KEY_POWER, 1); |
| 44 | input_sync(pwr); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 45 | |
| 46 | return IRQ_HANDLED; |
| 47 | } |
| 48 | |
Stephen Boyd | b27f8fe | 2013-12-15 03:14:47 -0800 | [diff] [blame] | 49 | static irqreturn_t pwrkey_release_irq(int irq, void *_pwr) |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 50 | { |
Stephen Boyd | b27f8fe | 2013-12-15 03:14:47 -0800 | [diff] [blame] | 51 | struct input_dev *pwr = _pwr; |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 52 | |
Stephen Boyd | b27f8fe | 2013-12-15 03:14:47 -0800 | [diff] [blame] | 53 | input_report_key(pwr, KEY_POWER, 0); |
| 54 | input_sync(pwr); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 55 | |
| 56 | return IRQ_HANDLED; |
| 57 | } |
| 58 | |
| 59 | #ifdef CONFIG_PM_SLEEP |
| 60 | static int pmic8xxx_pwrkey_suspend(struct device *dev) |
| 61 | { |
| 62 | struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev); |
| 63 | |
| 64 | if (device_may_wakeup(dev)) |
| 65 | enable_irq_wake(pwrkey->key_press_irq); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int pmic8xxx_pwrkey_resume(struct device *dev) |
| 71 | { |
| 72 | struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev); |
| 73 | |
| 74 | if (device_may_wakeup(dev)) |
| 75 | disable_irq_wake(pwrkey->key_press_irq); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | #endif |
| 80 | |
| 81 | static SIMPLE_DEV_PM_OPS(pm8xxx_pwr_key_pm_ops, |
| 82 | pmic8xxx_pwrkey_suspend, pmic8xxx_pwrkey_resume); |
| 83 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 84 | static int pmic8xxx_pwrkey_probe(struct platform_device *pdev) |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 85 | { |
| 86 | struct input_dev *pwr; |
| 87 | int key_release_irq = platform_get_irq(pdev, 0); |
| 88 | int key_press_irq = platform_get_irq(pdev, 1); |
| 89 | int err; |
| 90 | unsigned int delay; |
Stephen Boyd | 1e63bd9 | 2013-12-15 03:24:45 -0800 | [diff] [blame^] | 91 | unsigned int pon_cntl; |
| 92 | struct regmap *regmap; |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 93 | struct pmic8xxx_pwrkey *pwrkey; |
Samuel Ortiz | 63ef1124f | 2011-06-08 11:06:30 +0200 | [diff] [blame] | 94 | const struct pm8xxx_pwrkey_platform_data *pdata = |
| 95 | dev_get_platdata(&pdev->dev); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 96 | |
| 97 | if (!pdata) { |
| 98 | dev_err(&pdev->dev, "power key platform data not supplied\n"); |
| 99 | return -EINVAL; |
| 100 | } |
| 101 | |
| 102 | if (pdata->kpd_trigger_delay_us > 62500) { |
| 103 | dev_err(&pdev->dev, "invalid power key trigger delay\n"); |
| 104 | return -EINVAL; |
| 105 | } |
| 106 | |
Stephen Boyd | 1e63bd9 | 2013-12-15 03:24:45 -0800 | [diff] [blame^] | 107 | regmap = dev_get_regmap(pdev->dev.parent, NULL); |
| 108 | if (!regmap) { |
| 109 | dev_err(&pdev->dev, "failed to locate regmap for the device\n"); |
| 110 | return -ENODEV; |
| 111 | } |
| 112 | |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 113 | pwrkey = kzalloc(sizeof(*pwrkey), GFP_KERNEL); |
| 114 | if (!pwrkey) |
| 115 | return -ENOMEM; |
| 116 | |
| 117 | pwr = input_allocate_device(); |
| 118 | if (!pwr) { |
| 119 | dev_dbg(&pdev->dev, "Can't allocate power button\n"); |
| 120 | err = -ENOMEM; |
| 121 | goto free_pwrkey; |
| 122 | } |
| 123 | |
| 124 | input_set_capability(pwr, EV_KEY, KEY_POWER); |
| 125 | |
| 126 | pwr->name = "pmic8xxx_pwrkey"; |
| 127 | pwr->phys = "pmic8xxx_pwrkey/input0"; |
| 128 | pwr->dev.parent = &pdev->dev; |
| 129 | |
| 130 | delay = (pdata->kpd_trigger_delay_us << 10) / USEC_PER_SEC; |
| 131 | delay = 1 + ilog2(delay); |
| 132 | |
Stephen Boyd | 1e63bd9 | 2013-12-15 03:24:45 -0800 | [diff] [blame^] | 133 | err = regmap_read(regmap, PON_CNTL_1, &pon_cntl); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 134 | if (err < 0) { |
| 135 | dev_err(&pdev->dev, "failed reading PON_CNTL_1 err=%d\n", err); |
| 136 | goto free_input_dev; |
| 137 | } |
| 138 | |
| 139 | pon_cntl &= ~PON_CNTL_TRIG_DELAY_MASK; |
| 140 | pon_cntl |= (delay & PON_CNTL_TRIG_DELAY_MASK); |
| 141 | if (pdata->pull_up) |
| 142 | pon_cntl |= PON_CNTL_PULL_UP; |
| 143 | else |
| 144 | pon_cntl &= ~PON_CNTL_PULL_UP; |
| 145 | |
Stephen Boyd | 1e63bd9 | 2013-12-15 03:24:45 -0800 | [diff] [blame^] | 146 | err = regmap_write(regmap, PON_CNTL_1, pon_cntl); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 147 | if (err < 0) { |
| 148 | dev_err(&pdev->dev, "failed writing PON_CNTL_1 err=%d\n", err); |
| 149 | goto free_input_dev; |
| 150 | } |
| 151 | |
| 152 | err = input_register_device(pwr); |
| 153 | if (err) { |
| 154 | dev_dbg(&pdev->dev, "Can't register power key: %d\n", err); |
| 155 | goto free_input_dev; |
| 156 | } |
| 157 | |
| 158 | pwrkey->key_press_irq = key_press_irq; |
| 159 | pwrkey->pwr = pwr; |
| 160 | |
| 161 | platform_set_drvdata(pdev, pwrkey); |
| 162 | |
| 163 | err = request_irq(key_press_irq, pwrkey_press_irq, |
Stephen Boyd | b27f8fe | 2013-12-15 03:14:47 -0800 | [diff] [blame] | 164 | IRQF_TRIGGER_RISING, "pmic8xxx_pwrkey_press", pwr); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 165 | if (err < 0) { |
| 166 | dev_dbg(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n", |
| 167 | key_press_irq, err); |
| 168 | goto unreg_input_dev; |
| 169 | } |
| 170 | |
| 171 | err = request_irq(key_release_irq, pwrkey_release_irq, |
Stephen Boyd | b27f8fe | 2013-12-15 03:14:47 -0800 | [diff] [blame] | 172 | IRQF_TRIGGER_RISING, "pmic8xxx_pwrkey_release", pwr); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 173 | if (err < 0) { |
| 174 | dev_dbg(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n", |
| 175 | key_release_irq, err); |
| 176 | |
| 177 | goto free_press_irq; |
| 178 | } |
| 179 | |
| 180 | device_init_wakeup(&pdev->dev, pdata->wakeup); |
| 181 | |
| 182 | return 0; |
| 183 | |
| 184 | free_press_irq: |
Lars-Peter Clausen | 139097a | 2013-05-23 09:30:19 -0700 | [diff] [blame] | 185 | free_irq(key_press_irq, pwrkey); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 186 | unreg_input_dev: |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 187 | input_unregister_device(pwr); |
| 188 | pwr = NULL; |
| 189 | free_input_dev: |
| 190 | input_free_device(pwr); |
| 191 | free_pwrkey: |
| 192 | kfree(pwrkey); |
| 193 | return err; |
| 194 | } |
| 195 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 196 | static int pmic8xxx_pwrkey_remove(struct platform_device *pdev) |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 197 | { |
| 198 | struct pmic8xxx_pwrkey *pwrkey = platform_get_drvdata(pdev); |
| 199 | int key_release_irq = platform_get_irq(pdev, 0); |
| 200 | int key_press_irq = platform_get_irq(pdev, 1); |
| 201 | |
| 202 | device_init_wakeup(&pdev->dev, 0); |
| 203 | |
Stephen Boyd | b27f8fe | 2013-12-15 03:14:47 -0800 | [diff] [blame] | 204 | free_irq(key_press_irq, pwrkey->pwr); |
| 205 | free_irq(key_release_irq, pwrkey->pwr); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 206 | input_unregister_device(pwrkey->pwr); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 207 | kfree(pwrkey); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static struct platform_driver pmic8xxx_pwrkey_driver = { |
| 213 | .probe = pmic8xxx_pwrkey_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 214 | .remove = pmic8xxx_pwrkey_remove, |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 215 | .driver = { |
| 216 | .name = PM8XXX_PWRKEY_DEV_NAME, |
| 217 | .owner = THIS_MODULE, |
| 218 | .pm = &pm8xxx_pwr_key_pm_ops, |
| 219 | }, |
| 220 | }; |
JJ Ding | 840a746 | 2011-11-29 11:08:40 -0800 | [diff] [blame] | 221 | module_platform_driver(pmic8xxx_pwrkey_driver); |
Trilok Soni | 92d57a7 | 2011-05-13 15:17:51 +0530 | [diff] [blame] | 222 | |
| 223 | MODULE_ALIAS("platform:pmic8xxx_pwrkey"); |
| 224 | MODULE_DESCRIPTION("PMIC8XXX Power Key driver"); |
| 225 | MODULE_LICENSE("GPL v2"); |
| 226 | MODULE_AUTHOR("Trilok Soni <tsoni@codeaurora.org>"); |