blob: c91e3d33aea90f0053717bad2c0594224bf8d1fb [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>
Trilok Soni92d57a72011-05-13 15:17:51 +053014#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 Boyd1e63bd92013-12-15 03:24:45 -080020#include <linux/regmap.h>
Trilok Soni92d57a72011-05-13 15:17:51 +053021#include <linux/log2.h>
Stephen Boyd57918df2014-03-29 12:39:38 -070022#include <linux/of.h>
Trilok Soni92d57a72011-05-13 15:17:51 +053023
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 */
32struct pmic8xxx_pwrkey {
Trilok Soni92d57a72011-05-13 15:17:51 +053033 int key_press_irq;
34};
35
Stephen Boydb27f8fe2013-12-15 03:14:47 -080036static irqreturn_t pwrkey_press_irq(int irq, void *_pwr)
Trilok Soni92d57a72011-05-13 15:17:51 +053037{
Stephen Boydb27f8fe2013-12-15 03:14:47 -080038 struct input_dev *pwr = _pwr;
Trilok Soni92d57a72011-05-13 15:17:51 +053039
Stephen Boydb27f8fe2013-12-15 03:14:47 -080040 input_report_key(pwr, KEY_POWER, 1);
41 input_sync(pwr);
Trilok Soni92d57a72011-05-13 15:17:51 +053042
43 return IRQ_HANDLED;
44}
45
Stephen Boydb27f8fe2013-12-15 03:14:47 -080046static irqreturn_t pwrkey_release_irq(int irq, void *_pwr)
Trilok Soni92d57a72011-05-13 15:17:51 +053047{
Stephen Boydb27f8fe2013-12-15 03:14:47 -080048 struct input_dev *pwr = _pwr;
Trilok Soni92d57a72011-05-13 15:17:51 +053049
Stephen Boydb27f8fe2013-12-15 03:14:47 -080050 input_report_key(pwr, KEY_POWER, 0);
51 input_sync(pwr);
Trilok Soni92d57a72011-05-13 15:17:51 +053052
53 return IRQ_HANDLED;
54}
55
56#ifdef CONFIG_PM_SLEEP
57static int pmic8xxx_pwrkey_suspend(struct device *dev)
58{
59 struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev);
60
61 if (device_may_wakeup(dev))
62 enable_irq_wake(pwrkey->key_press_irq);
63
64 return 0;
65}
66
67static int pmic8xxx_pwrkey_resume(struct device *dev)
68{
69 struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev);
70
71 if (device_may_wakeup(dev))
72 disable_irq_wake(pwrkey->key_press_irq);
73
74 return 0;
75}
76#endif
77
78static SIMPLE_DEV_PM_OPS(pm8xxx_pwr_key_pm_ops,
79 pmic8xxx_pwrkey_suspend, pmic8xxx_pwrkey_resume);
80
Bill Pemberton5298cc42012-11-23 21:38:25 -080081static int pmic8xxx_pwrkey_probe(struct platform_device *pdev)
Trilok Soni92d57a72011-05-13 15:17:51 +053082{
83 struct input_dev *pwr;
84 int key_release_irq = platform_get_irq(pdev, 0);
85 int key_press_irq = platform_get_irq(pdev, 1);
86 int err;
87 unsigned int delay;
Stephen Boyd1e63bd92013-12-15 03:24:45 -080088 unsigned int pon_cntl;
89 struct regmap *regmap;
Trilok Soni92d57a72011-05-13 15:17:51 +053090 struct pmic8xxx_pwrkey *pwrkey;
Stephen Boyd57918df2014-03-29 12:39:38 -070091 u32 kpd_delay;
92 bool pull_up;
Trilok Soni92d57a72011-05-13 15:17:51 +053093
Stephen Boyd57918df2014-03-29 12:39:38 -070094 if (of_property_read_u32(pdev->dev.of_node, "debounce", &kpd_delay))
Stephen Boyd70a26072014-05-14 11:58:55 -070095 kpd_delay = 15625;
Trilok Soni92d57a72011-05-13 15:17:51 +053096
Stephen Boyd70a26072014-05-14 11:58:55 -070097 if (kpd_delay > 62500 || kpd_delay == 0) {
Trilok Soni92d57a72011-05-13 15:17:51 +053098 dev_err(&pdev->dev, "invalid power key trigger delay\n");
99 return -EINVAL;
100 }
101
Stephen Boyd70a26072014-05-14 11:58:55 -0700102 pull_up = of_property_read_bool(pdev->dev.of_node, "pull-up");
103
Stephen Boyd1e63bd92013-12-15 03:24:45 -0800104 regmap = dev_get_regmap(pdev->dev.parent, NULL);
105 if (!regmap) {
106 dev_err(&pdev->dev, "failed to locate regmap for the device\n");
107 return -ENODEV;
108 }
109
Dmitry Torokhoveb735d32013-12-15 03:28:26 -0800110 pwrkey = devm_kzalloc(&pdev->dev, sizeof(*pwrkey), GFP_KERNEL);
Trilok Soni92d57a72011-05-13 15:17:51 +0530111 if (!pwrkey)
112 return -ENOMEM;
113
Dmitry Torokhoveb735d32013-12-15 03:28:26 -0800114 pwrkey->key_press_irq = key_press_irq;
115
116 pwr = devm_input_allocate_device(&pdev->dev);
Trilok Soni92d57a72011-05-13 15:17:51 +0530117 if (!pwr) {
118 dev_dbg(&pdev->dev, "Can't allocate power button\n");
Dmitry Torokhoveb735d32013-12-15 03:28:26 -0800119 return -ENOMEM;
Trilok Soni92d57a72011-05-13 15:17:51 +0530120 }
121
122 input_set_capability(pwr, EV_KEY, KEY_POWER);
123
124 pwr->name = "pmic8xxx_pwrkey";
125 pwr->phys = "pmic8xxx_pwrkey/input0";
Trilok Soni92d57a72011-05-13 15:17:51 +0530126
Stephen Boyd57918df2014-03-29 12:39:38 -0700127 delay = (kpd_delay << 10) / USEC_PER_SEC;
Trilok Soni92d57a72011-05-13 15:17:51 +0530128 delay = 1 + ilog2(delay);
129
Stephen Boyd1e63bd92013-12-15 03:24:45 -0800130 err = regmap_read(regmap, PON_CNTL_1, &pon_cntl);
Trilok Soni92d57a72011-05-13 15:17:51 +0530131 if (err < 0) {
132 dev_err(&pdev->dev, "failed reading PON_CNTL_1 err=%d\n", err);
Dmitry Torokhoveb735d32013-12-15 03:28:26 -0800133 return err;
Trilok Soni92d57a72011-05-13 15:17:51 +0530134 }
135
136 pon_cntl &= ~PON_CNTL_TRIG_DELAY_MASK;
137 pon_cntl |= (delay & PON_CNTL_TRIG_DELAY_MASK);
Stephen Boyd57918df2014-03-29 12:39:38 -0700138 if (pull_up)
Trilok Soni92d57a72011-05-13 15:17:51 +0530139 pon_cntl |= PON_CNTL_PULL_UP;
140 else
141 pon_cntl &= ~PON_CNTL_PULL_UP;
142
Stephen Boyd1e63bd92013-12-15 03:24:45 -0800143 err = regmap_write(regmap, PON_CNTL_1, pon_cntl);
Trilok Soni92d57a72011-05-13 15:17:51 +0530144 if (err < 0) {
145 dev_err(&pdev->dev, "failed writing PON_CNTL_1 err=%d\n", err);
Dmitry Torokhoveb735d32013-12-15 03:28:26 -0800146 return err;
147 }
148
149 err = devm_request_irq(&pdev->dev, key_press_irq, pwrkey_press_irq,
150 IRQF_TRIGGER_RISING,
151 "pmic8xxx_pwrkey_press", pwr);
152 if (err) {
153 dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
154 key_press_irq, err);
155 return err;
156 }
157
158 err = devm_request_irq(&pdev->dev, key_release_irq, pwrkey_release_irq,
159 IRQF_TRIGGER_RISING,
160 "pmic8xxx_pwrkey_release", pwr);
161 if (err) {
162 dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
163 key_release_irq, err);
164 return err;
Trilok Soni92d57a72011-05-13 15:17:51 +0530165 }
166
167 err = input_register_device(pwr);
168 if (err) {
Dmitry Torokhoveb735d32013-12-15 03:28:26 -0800169 dev_err(&pdev->dev, "Can't register power key: %d\n", err);
170 return err;
Trilok Soni92d57a72011-05-13 15:17:51 +0530171 }
172
Trilok Soni92d57a72011-05-13 15:17:51 +0530173 platform_set_drvdata(pdev, pwrkey);
Stephen Boyd57918df2014-03-29 12:39:38 -0700174 device_init_wakeup(&pdev->dev, 1);
Trilok Soni92d57a72011-05-13 15:17:51 +0530175
176 return 0;
Trilok Soni92d57a72011-05-13 15:17:51 +0530177}
178
Bill Pembertone2619cf2012-11-23 21:50:47 -0800179static int pmic8xxx_pwrkey_remove(struct platform_device *pdev)
Trilok Soni92d57a72011-05-13 15:17:51 +0530180{
Trilok Soni92d57a72011-05-13 15:17:51 +0530181 device_init_wakeup(&pdev->dev, 0);
182
Trilok Soni92d57a72011-05-13 15:17:51 +0530183 return 0;
184}
185
Stephen Boyd57918df2014-03-29 12:39:38 -0700186static const struct of_device_id pm8xxx_pwr_key_id_table[] = {
187 { .compatible = "qcom,pm8058-pwrkey" },
188 { .compatible = "qcom,pm8921-pwrkey" },
189 { }
190};
191MODULE_DEVICE_TABLE(of, pm8xxx_pwr_key_id_table);
192
Trilok Soni92d57a72011-05-13 15:17:51 +0530193static struct platform_driver pmic8xxx_pwrkey_driver = {
194 .probe = pmic8xxx_pwrkey_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800195 .remove = pmic8xxx_pwrkey_remove,
Trilok Soni92d57a72011-05-13 15:17:51 +0530196 .driver = {
Stephen Boyd57918df2014-03-29 12:39:38 -0700197 .name = "pm8xxx-pwrkey",
Trilok Soni92d57a72011-05-13 15:17:51 +0530198 .owner = THIS_MODULE,
199 .pm = &pm8xxx_pwr_key_pm_ops,
Stephen Boyd57918df2014-03-29 12:39:38 -0700200 .of_match_table = pm8xxx_pwr_key_id_table,
Trilok Soni92d57a72011-05-13 15:17:51 +0530201 },
202};
JJ Ding840a7462011-11-29 11:08:40 -0800203module_platform_driver(pmic8xxx_pwrkey_driver);
Trilok Soni92d57a72011-05-13 15:17:51 +0530204
205MODULE_ALIAS("platform:pmic8xxx_pwrkey");
206MODULE_DESCRIPTION("PMIC8XXX Power Key driver");
207MODULE_LICENSE("GPL v2");
208MODULE_AUTHOR("Trilok Soni <tsoni@codeaurora.org>");