blob: ef938405a9c6c1ffdc04859be97f791e813da28e [file] [log] [blame]
Trilok Soni92d57a72011-05-13 15:17:51 +05301/* 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 Boyd1e63bd92013-12-15 03:24:45 -080021#include <linux/regmap.h>
Trilok Soni92d57a72011-05-13 15:17:51 +053022#include <linux/log2.h>
23
Trilok Soni92d57a72011-05-13 15:17:51 +053024#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 */
34struct pmic8xxx_pwrkey {
35 struct input_dev *pwr;
36 int key_press_irq;
37};
38
Stephen Boydb27f8fe2013-12-15 03:14:47 -080039static irqreturn_t pwrkey_press_irq(int irq, void *_pwr)
Trilok Soni92d57a72011-05-13 15:17:51 +053040{
Stephen Boydb27f8fe2013-12-15 03:14:47 -080041 struct input_dev *pwr = _pwr;
Trilok Soni92d57a72011-05-13 15:17:51 +053042
Stephen Boydb27f8fe2013-12-15 03:14:47 -080043 input_report_key(pwr, KEY_POWER, 1);
44 input_sync(pwr);
Trilok Soni92d57a72011-05-13 15:17:51 +053045
46 return IRQ_HANDLED;
47}
48
Stephen Boydb27f8fe2013-12-15 03:14:47 -080049static irqreturn_t pwrkey_release_irq(int irq, void *_pwr)
Trilok Soni92d57a72011-05-13 15:17:51 +053050{
Stephen Boydb27f8fe2013-12-15 03:14:47 -080051 struct input_dev *pwr = _pwr;
Trilok Soni92d57a72011-05-13 15:17:51 +053052
Stephen Boydb27f8fe2013-12-15 03:14:47 -080053 input_report_key(pwr, KEY_POWER, 0);
54 input_sync(pwr);
Trilok Soni92d57a72011-05-13 15:17:51 +053055
56 return IRQ_HANDLED;
57}
58
59#ifdef CONFIG_PM_SLEEP
60static 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
70static 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
81static SIMPLE_DEV_PM_OPS(pm8xxx_pwr_key_pm_ops,
82 pmic8xxx_pwrkey_suspend, pmic8xxx_pwrkey_resume);
83
Bill Pemberton5298cc42012-11-23 21:38:25 -080084static int pmic8xxx_pwrkey_probe(struct platform_device *pdev)
Trilok Soni92d57a72011-05-13 15:17:51 +053085{
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 Boyd1e63bd92013-12-15 03:24:45 -080091 unsigned int pon_cntl;
92 struct regmap *regmap;
Trilok Soni92d57a72011-05-13 15:17:51 +053093 struct pmic8xxx_pwrkey *pwrkey;
Samuel Ortiz63ef1124f2011-06-08 11:06:30 +020094 const struct pm8xxx_pwrkey_platform_data *pdata =
95 dev_get_platdata(&pdev->dev);
Trilok Soni92d57a72011-05-13 15:17:51 +053096
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 Boyd1e63bd92013-12-15 03:24:45 -0800107 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 Soni92d57a72011-05-13 15:17:51 +0530113 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 Boyd1e63bd92013-12-15 03:24:45 -0800133 err = regmap_read(regmap, PON_CNTL_1, &pon_cntl);
Trilok Soni92d57a72011-05-13 15:17:51 +0530134 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 Boyd1e63bd92013-12-15 03:24:45 -0800146 err = regmap_write(regmap, PON_CNTL_1, pon_cntl);
Trilok Soni92d57a72011-05-13 15:17:51 +0530147 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 Boydb27f8fe2013-12-15 03:14:47 -0800164 IRQF_TRIGGER_RISING, "pmic8xxx_pwrkey_press", pwr);
Trilok Soni92d57a72011-05-13 15:17:51 +0530165 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 Boydb27f8fe2013-12-15 03:14:47 -0800172 IRQF_TRIGGER_RISING, "pmic8xxx_pwrkey_release", pwr);
Trilok Soni92d57a72011-05-13 15:17:51 +0530173 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
184free_press_irq:
Lars-Peter Clausen139097a2013-05-23 09:30:19 -0700185 free_irq(key_press_irq, pwrkey);
Trilok Soni92d57a72011-05-13 15:17:51 +0530186unreg_input_dev:
Trilok Soni92d57a72011-05-13 15:17:51 +0530187 input_unregister_device(pwr);
188 pwr = NULL;
189free_input_dev:
190 input_free_device(pwr);
191free_pwrkey:
192 kfree(pwrkey);
193 return err;
194}
195
Bill Pembertone2619cf2012-11-23 21:50:47 -0800196static int pmic8xxx_pwrkey_remove(struct platform_device *pdev)
Trilok Soni92d57a72011-05-13 15:17:51 +0530197{
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 Boydb27f8fe2013-12-15 03:14:47 -0800204 free_irq(key_press_irq, pwrkey->pwr);
205 free_irq(key_release_irq, pwrkey->pwr);
Trilok Soni92d57a72011-05-13 15:17:51 +0530206 input_unregister_device(pwrkey->pwr);
Trilok Soni92d57a72011-05-13 15:17:51 +0530207 kfree(pwrkey);
208
209 return 0;
210}
211
212static struct platform_driver pmic8xxx_pwrkey_driver = {
213 .probe = pmic8xxx_pwrkey_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800214 .remove = pmic8xxx_pwrkey_remove,
Trilok Soni92d57a72011-05-13 15:17:51 +0530215 .driver = {
216 .name = PM8XXX_PWRKEY_DEV_NAME,
217 .owner = THIS_MODULE,
218 .pm = &pm8xxx_pwr_key_pm_ops,
219 },
220};
JJ Ding840a7462011-11-29 11:08:40 -0800221module_platform_driver(pmic8xxx_pwrkey_driver);
Trilok Soni92d57a72011-05-13 15:17:51 +0530222
223MODULE_ALIAS("platform:pmic8xxx_pwrkey");
224MODULE_DESCRIPTION("PMIC8XXX Power Key driver");
225MODULE_LICENSE("GPL v2");
226MODULE_AUTHOR("Trilok Soni <tsoni@codeaurora.org>");