• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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>
23 
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  */
32 struct pmic8xxx_pwrkey {
33 	int key_press_irq;
34 };
35 
pwrkey_press_irq(int irq,void * _pwr)36 static irqreturn_t pwrkey_press_irq(int irq, void *_pwr)
37 {
38 	struct input_dev *pwr = _pwr;
39 
40 	input_report_key(pwr, KEY_POWER, 1);
41 	input_sync(pwr);
42 
43 	return IRQ_HANDLED;
44 }
45 
pwrkey_release_irq(int irq,void * _pwr)46 static irqreturn_t pwrkey_release_irq(int irq, void *_pwr)
47 {
48 	struct input_dev *pwr = _pwr;
49 
50 	input_report_key(pwr, KEY_POWER, 0);
51 	input_sync(pwr);
52 
53 	return IRQ_HANDLED;
54 }
55 
56 #ifdef CONFIG_PM_SLEEP
pmic8xxx_pwrkey_suspend(struct device * dev)57 static 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 
pmic8xxx_pwrkey_resume(struct device * dev)67 static 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 
78 static SIMPLE_DEV_PM_OPS(pm8xxx_pwr_key_pm_ops,
79 		pmic8xxx_pwrkey_suspend, pmic8xxx_pwrkey_resume);
80 
pmic8xxx_pwrkey_probe(struct platform_device * pdev)81 static int pmic8xxx_pwrkey_probe(struct platform_device *pdev)
82 {
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;
88 	unsigned int pon_cntl;
89 	struct regmap *regmap;
90 	struct pmic8xxx_pwrkey *pwrkey;
91 	u32 kpd_delay;
92 	bool pull_up;
93 
94 	if (of_property_read_u32(pdev->dev.of_node, "debounce", &kpd_delay))
95 		kpd_delay = 15625;
96 
97 	/* Valid range of pwr key trigger delay is 1/64 sec to 2 seconds. */
98 	if (kpd_delay > USEC_PER_SEC * 2 || kpd_delay < USEC_PER_SEC / 64) {
99 		dev_err(&pdev->dev, "invalid power key trigger delay\n");
100 		return -EINVAL;
101 	}
102 
103 	pull_up = of_property_read_bool(pdev->dev.of_node, "pull-up");
104 
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;
109 	}
110 
111 	pwrkey = devm_kzalloc(&pdev->dev, sizeof(*pwrkey), GFP_KERNEL);
112 	if (!pwrkey)
113 		return -ENOMEM;
114 
115 	pwrkey->key_press_irq = key_press_irq;
116 
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;
121 	}
122 
123 	input_set_capability(pwr, EV_KEY, KEY_POWER);
124 
125 	pwr->name = "pmic8xxx_pwrkey";
126 	pwr->phys = "pmic8xxx_pwrkey/input0";
127 
128 	delay = (kpd_delay << 6) / USEC_PER_SEC;
129 	delay = ilog2(delay);
130 
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;
135 	}
136 
137 	pon_cntl &= ~PON_CNTL_TRIG_DELAY_MASK;
138 	pon_cntl |= (delay & PON_CNTL_TRIG_DELAY_MASK);
139 	if (pull_up)
140 		pon_cntl |= PON_CNTL_PULL_UP;
141 	else
142 		pon_cntl &= ~PON_CNTL_PULL_UP;
143 
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;
148 	}
149 
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;
157 	}
158 
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;
166 	}
167 
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;
172 	}
173 
174 	platform_set_drvdata(pdev, pwrkey);
175 	device_init_wakeup(&pdev->dev, 1);
176 
177 	return 0;
178 }
179 
pmic8xxx_pwrkey_remove(struct platform_device * pdev)180 static int pmic8xxx_pwrkey_remove(struct platform_device *pdev)
181 {
182 	device_init_wakeup(&pdev->dev, 0);
183 
184 	return 0;
185 }
186 
187 static const struct of_device_id pm8xxx_pwr_key_id_table[] = {
188 	{ .compatible = "qcom,pm8058-pwrkey" },
189 	{ .compatible = "qcom,pm8921-pwrkey" },
190 	{ }
191 };
192 MODULE_DEVICE_TABLE(of, pm8xxx_pwr_key_id_table);
193 
194 static struct platform_driver pmic8xxx_pwrkey_driver = {
195 	.probe		= pmic8xxx_pwrkey_probe,
196 	.remove		= pmic8xxx_pwrkey_remove,
197 	.driver		= {
198 		.name	= "pm8xxx-pwrkey",
199 		.owner	= THIS_MODULE,
200 		.pm	= &pm8xxx_pwr_key_pm_ops,
201 		.of_match_table = pm8xxx_pwr_key_id_table,
202 	},
203 };
204 module_platform_driver(pmic8xxx_pwrkey_driver);
205 
206 MODULE_ALIAS("platform:pmic8xxx_pwrkey");
207 MODULE_DESCRIPTION("PMIC8XXX Power Key driver");
208 MODULE_LICENSE("GPL v2");
209 MODULE_AUTHOR("Trilok Soni <tsoni@codeaurora.org>");
210