gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / drivers / input / misc / pmic8xxx-pwrkey.c
blobb6d14bba6645292952936dee6b34039b3910e543
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>
24 #define PON_CNTL_1 0x1C
25 #define PON_CNTL_PULL_UP BIT(7)
26 #define PON_CNTL_TRIG_DELAY_MASK (0x7)
28 /**
29 * struct pmic8xxx_pwrkey - pmic8xxx pwrkey information
30 * @key_press_irq: key press irq number
32 struct pmic8xxx_pwrkey {
33 int key_press_irq;
36 static irqreturn_t pwrkey_press_irq(int irq, void *_pwr)
38 struct input_dev *pwr = _pwr;
40 input_report_key(pwr, KEY_POWER, 1);
41 input_sync(pwr);
43 return IRQ_HANDLED;
46 static irqreturn_t pwrkey_release_irq(int irq, void *_pwr)
48 struct input_dev *pwr = _pwr;
50 input_report_key(pwr, KEY_POWER, 0);
51 input_sync(pwr);
53 return IRQ_HANDLED;
56 static int __maybe_unused pmic8xxx_pwrkey_suspend(struct device *dev)
58 struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev);
60 if (device_may_wakeup(dev))
61 enable_irq_wake(pwrkey->key_press_irq);
63 return 0;
66 static int __maybe_unused pmic8xxx_pwrkey_resume(struct device *dev)
68 struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev);
70 if (device_may_wakeup(dev))
71 disable_irq_wake(pwrkey->key_press_irq);
73 return 0;
76 static SIMPLE_DEV_PM_OPS(pm8xxx_pwr_key_pm_ops,
77 pmic8xxx_pwrkey_suspend, pmic8xxx_pwrkey_resume);
79 static int pmic8xxx_pwrkey_probe(struct platform_device *pdev)
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;
86 unsigned int pon_cntl;
87 struct regmap *regmap;
88 struct pmic8xxx_pwrkey *pwrkey;
89 u32 kpd_delay;
90 bool pull_up;
92 if (of_property_read_u32(pdev->dev.of_node, "debounce", &kpd_delay))
93 kpd_delay = 15625;
95 /* Valid range of pwr key trigger delay is 1/64 sec to 2 seconds. */
96 if (kpd_delay > USEC_PER_SEC * 2 || kpd_delay < USEC_PER_SEC / 64) {
97 dev_err(&pdev->dev, "invalid power key trigger delay\n");
98 return -EINVAL;
101 pull_up = of_property_read_bool(pdev->dev.of_node, "pull-up");
103 regmap = dev_get_regmap(pdev->dev.parent, NULL);
104 if (!regmap) {
105 dev_err(&pdev->dev, "failed to locate regmap for the device\n");
106 return -ENODEV;
109 pwrkey = devm_kzalloc(&pdev->dev, sizeof(*pwrkey), GFP_KERNEL);
110 if (!pwrkey)
111 return -ENOMEM;
113 pwrkey->key_press_irq = key_press_irq;
115 pwr = devm_input_allocate_device(&pdev->dev);
116 if (!pwr) {
117 dev_dbg(&pdev->dev, "Can't allocate power button\n");
118 return -ENOMEM;
121 input_set_capability(pwr, EV_KEY, KEY_POWER);
123 pwr->name = "pmic8xxx_pwrkey";
124 pwr->phys = "pmic8xxx_pwrkey/input0";
126 delay = (kpd_delay << 6) / USEC_PER_SEC;
127 delay = ilog2(delay);
129 err = regmap_read(regmap, PON_CNTL_1, &pon_cntl);
130 if (err < 0) {
131 dev_err(&pdev->dev, "failed reading PON_CNTL_1 err=%d\n", err);
132 return err;
135 pon_cntl &= ~PON_CNTL_TRIG_DELAY_MASK;
136 pon_cntl |= (delay & PON_CNTL_TRIG_DELAY_MASK);
137 if (pull_up)
138 pon_cntl |= PON_CNTL_PULL_UP;
139 else
140 pon_cntl &= ~PON_CNTL_PULL_UP;
142 err = regmap_write(regmap, PON_CNTL_1, pon_cntl);
143 if (err < 0) {
144 dev_err(&pdev->dev, "failed writing PON_CNTL_1 err=%d\n", err);
145 return err;
148 err = devm_request_irq(&pdev->dev, key_press_irq, pwrkey_press_irq,
149 IRQF_TRIGGER_RISING,
150 "pmic8xxx_pwrkey_press", pwr);
151 if (err) {
152 dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
153 key_press_irq, err);
154 return err;
157 err = devm_request_irq(&pdev->dev, key_release_irq, pwrkey_release_irq,
158 IRQF_TRIGGER_RISING,
159 "pmic8xxx_pwrkey_release", pwr);
160 if (err) {
161 dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
162 key_release_irq, err);
163 return err;
166 err = input_register_device(pwr);
167 if (err) {
168 dev_err(&pdev->dev, "Can't register power key: %d\n", err);
169 return err;
172 platform_set_drvdata(pdev, pwrkey);
173 device_init_wakeup(&pdev->dev, 1);
175 return 0;
178 static int pmic8xxx_pwrkey_remove(struct platform_device *pdev)
180 device_init_wakeup(&pdev->dev, 0);
182 return 0;
185 static const struct of_device_id pm8xxx_pwr_key_id_table[] = {
186 { .compatible = "qcom,pm8058-pwrkey" },
187 { .compatible = "qcom,pm8921-pwrkey" },
190 MODULE_DEVICE_TABLE(of, pm8xxx_pwr_key_id_table);
192 static struct platform_driver pmic8xxx_pwrkey_driver = {
193 .probe = pmic8xxx_pwrkey_probe,
194 .remove = pmic8xxx_pwrkey_remove,
195 .driver = {
196 .name = "pm8xxx-pwrkey",
197 .pm = &pm8xxx_pwr_key_pm_ops,
198 .of_match_table = pm8xxx_pwr_key_id_table,
201 module_platform_driver(pmic8xxx_pwrkey_driver);
203 MODULE_ALIAS("platform:pmic8xxx_pwrkey");
204 MODULE_DESCRIPTION("PMIC8XXX Power Key driver");
205 MODULE_LICENSE("GPL v2");
206 MODULE_AUTHOR("Trilok Soni <tsoni@codeaurora.org>");