Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / input / misc / pmic8xxx-pwrkey.c
blob0e1a05f9585804cf4a987dd0bd34d50deb68722a
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/input/pmic8xxx-pwrkey.h>
25 #define PON_CNTL_1 0x1C
26 #define PON_CNTL_PULL_UP BIT(7)
27 #define PON_CNTL_TRIG_DELAY_MASK (0x7)
29 /**
30 * struct pmic8xxx_pwrkey - pmic8xxx pwrkey information
31 * @key_press_irq: key press irq number
33 struct pmic8xxx_pwrkey {
34 int key_press_irq;
37 static irqreturn_t pwrkey_press_irq(int irq, void *_pwr)
39 struct input_dev *pwr = _pwr;
41 input_report_key(pwr, KEY_POWER, 1);
42 input_sync(pwr);
44 return IRQ_HANDLED;
47 static irqreturn_t pwrkey_release_irq(int irq, void *_pwr)
49 struct input_dev *pwr = _pwr;
51 input_report_key(pwr, KEY_POWER, 0);
52 input_sync(pwr);
54 return IRQ_HANDLED;
57 #ifdef CONFIG_PM_SLEEP
58 static int pmic8xxx_pwrkey_suspend(struct device *dev)
60 struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev);
62 if (device_may_wakeup(dev))
63 enable_irq_wake(pwrkey->key_press_irq);
65 return 0;
68 static int pmic8xxx_pwrkey_resume(struct device *dev)
70 struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev);
72 if (device_may_wakeup(dev))
73 disable_irq_wake(pwrkey->key_press_irq);
75 return 0;
77 #endif
79 static SIMPLE_DEV_PM_OPS(pm8xxx_pwr_key_pm_ops,
80 pmic8xxx_pwrkey_suspend, pmic8xxx_pwrkey_resume);
82 static int pmic8xxx_pwrkey_probe(struct platform_device *pdev)
84 struct input_dev *pwr;
85 int key_release_irq = platform_get_irq(pdev, 0);
86 int key_press_irq = platform_get_irq(pdev, 1);
87 int err;
88 unsigned int delay;
89 unsigned int pon_cntl;
90 struct regmap *regmap;
91 struct pmic8xxx_pwrkey *pwrkey;
92 const struct pm8xxx_pwrkey_platform_data *pdata =
93 dev_get_platdata(&pdev->dev);
95 if (!pdata) {
96 dev_err(&pdev->dev, "power key platform data not supplied\n");
97 return -EINVAL;
100 if (pdata->kpd_trigger_delay_us > 62500) {
101 dev_err(&pdev->dev, "invalid power key trigger delay\n");
102 return -EINVAL;
105 regmap = dev_get_regmap(pdev->dev.parent, NULL);
106 if (!regmap) {
107 dev_err(&pdev->dev, "failed to locate regmap for the device\n");
108 return -ENODEV;
111 pwrkey = devm_kzalloc(&pdev->dev, sizeof(*pwrkey), GFP_KERNEL);
112 if (!pwrkey)
113 return -ENOMEM;
115 pwrkey->key_press_irq = key_press_irq;
117 pwr = devm_input_allocate_device(&pdev->dev);
118 if (!pwr) {
119 dev_dbg(&pdev->dev, "Can't allocate power button\n");
120 return -ENOMEM;
123 input_set_capability(pwr, EV_KEY, KEY_POWER);
125 pwr->name = "pmic8xxx_pwrkey";
126 pwr->phys = "pmic8xxx_pwrkey/input0";
128 delay = (pdata->kpd_trigger_delay_us << 10) / USEC_PER_SEC;
129 delay = 1 + ilog2(delay);
131 err = regmap_read(regmap, PON_CNTL_1, &pon_cntl);
132 if (err < 0) {
133 dev_err(&pdev->dev, "failed reading PON_CNTL_1 err=%d\n", err);
134 return err;
137 pon_cntl &= ~PON_CNTL_TRIG_DELAY_MASK;
138 pon_cntl |= (delay & PON_CNTL_TRIG_DELAY_MASK);
139 if (pdata->pull_up)
140 pon_cntl |= PON_CNTL_PULL_UP;
141 else
142 pon_cntl &= ~PON_CNTL_PULL_UP;
144 err = regmap_write(regmap, PON_CNTL_1, pon_cntl);
145 if (err < 0) {
146 dev_err(&pdev->dev, "failed writing PON_CNTL_1 err=%d\n", err);
147 return err;
150 err = devm_request_irq(&pdev->dev, key_press_irq, pwrkey_press_irq,
151 IRQF_TRIGGER_RISING,
152 "pmic8xxx_pwrkey_press", pwr);
153 if (err) {
154 dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
155 key_press_irq, err);
156 return err;
159 err = devm_request_irq(&pdev->dev, key_release_irq, pwrkey_release_irq,
160 IRQF_TRIGGER_RISING,
161 "pmic8xxx_pwrkey_release", pwr);
162 if (err) {
163 dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
164 key_release_irq, err);
165 return err;
168 err = input_register_device(pwr);
169 if (err) {
170 dev_err(&pdev->dev, "Can't register power key: %d\n", err);
171 return err;
174 platform_set_drvdata(pdev, pwrkey);
175 device_init_wakeup(&pdev->dev, pdata->wakeup);
177 return 0;
180 static int pmic8xxx_pwrkey_remove(struct platform_device *pdev)
182 device_init_wakeup(&pdev->dev, 0);
184 return 0;
187 static struct platform_driver pmic8xxx_pwrkey_driver = {
188 .probe = pmic8xxx_pwrkey_probe,
189 .remove = pmic8xxx_pwrkey_remove,
190 .driver = {
191 .name = PM8XXX_PWRKEY_DEV_NAME,
192 .owner = THIS_MODULE,
193 .pm = &pm8xxx_pwr_key_pm_ops,
196 module_platform_driver(pmic8xxx_pwrkey_driver);
198 MODULE_ALIAS("platform:pmic8xxx_pwrkey");
199 MODULE_DESCRIPTION("PMIC8XXX Power Key driver");
200 MODULE_LICENSE("GPL v2");
201 MODULE_AUTHOR("Trilok Soni <tsoni@codeaurora.org>");