1*4882a593Smuzhiyun /**
2*4882a593Smuzhiyun * twl4030-pwrbutton.c - TWL4030 Power Button Input Driver
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2008-2009 Nokia Corporation
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Written by Peter De Schrijver <peter.de-schrijver@nokia.com>
7*4882a593Smuzhiyun * Several fixes by Felipe Balbi <felipe.balbi@nokia.com>
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * This file is subject to the terms and conditions of the GNU General
10*4882a593Smuzhiyun * Public License. See the file "COPYING" in the main directory of this
11*4882a593Smuzhiyun * archive for more details.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * This program is distributed in the hope that it will be useful,
14*4882a593Smuzhiyun * but WITHOUT ANY WARRANTY; without even the implied warranty of
15*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*4882a593Smuzhiyun * GNU General Public License for more details.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * You should have received a copy of the GNU General Public License
19*4882a593Smuzhiyun * along with this program; if not, write to the Free Software
20*4882a593Smuzhiyun * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*4882a593Smuzhiyun */
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include <linux/module.h>
24*4882a593Smuzhiyun #include <linux/init.h>
25*4882a593Smuzhiyun #include <linux/kernel.h>
26*4882a593Smuzhiyun #include <linux/errno.h>
27*4882a593Smuzhiyun #include <linux/input.h>
28*4882a593Smuzhiyun #include <linux/interrupt.h>
29*4882a593Smuzhiyun #include <linux/platform_device.h>
30*4882a593Smuzhiyun #include <linux/mfd/twl.h>
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #define PWR_PWRON_IRQ (1 << 0)
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #define STS_HW_CONDITIONS 0xf
35*4882a593Smuzhiyun
powerbutton_irq(int irq,void * _pwr)36*4882a593Smuzhiyun static irqreturn_t powerbutton_irq(int irq, void *_pwr)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun struct input_dev *pwr = _pwr;
39*4882a593Smuzhiyun int err;
40*4882a593Smuzhiyun u8 value;
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &value, STS_HW_CONDITIONS);
43*4882a593Smuzhiyun if (!err) {
44*4882a593Smuzhiyun pm_wakeup_event(pwr->dev.parent, 0);
45*4882a593Smuzhiyun input_report_key(pwr, KEY_POWER, value & PWR_PWRON_IRQ);
46*4882a593Smuzhiyun input_sync(pwr);
47*4882a593Smuzhiyun } else {
48*4882a593Smuzhiyun dev_err(pwr->dev.parent, "twl4030: i2c error %d while reading"
49*4882a593Smuzhiyun " TWL4030 PM_MASTER STS_HW_CONDITIONS register\n", err);
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun return IRQ_HANDLED;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
twl4030_pwrbutton_probe(struct platform_device * pdev)55*4882a593Smuzhiyun static int twl4030_pwrbutton_probe(struct platform_device *pdev)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun struct input_dev *pwr;
58*4882a593Smuzhiyun int irq = platform_get_irq(pdev, 0);
59*4882a593Smuzhiyun int err;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun pwr = devm_input_allocate_device(&pdev->dev);
62*4882a593Smuzhiyun if (!pwr) {
63*4882a593Smuzhiyun dev_err(&pdev->dev, "Can't allocate power button\n");
64*4882a593Smuzhiyun return -ENOMEM;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun input_set_capability(pwr, EV_KEY, KEY_POWER);
68*4882a593Smuzhiyun pwr->name = "twl4030_pwrbutton";
69*4882a593Smuzhiyun pwr->phys = "twl4030_pwrbutton/input0";
70*4882a593Smuzhiyun pwr->dev.parent = &pdev->dev;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun err = devm_request_threaded_irq(&pdev->dev, irq, NULL, powerbutton_irq,
73*4882a593Smuzhiyun IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
74*4882a593Smuzhiyun IRQF_ONESHOT,
75*4882a593Smuzhiyun "twl4030_pwrbutton", pwr);
76*4882a593Smuzhiyun if (err < 0) {
77*4882a593Smuzhiyun dev_err(&pdev->dev, "Can't get IRQ for pwrbutton: %d\n", err);
78*4882a593Smuzhiyun return err;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun err = input_register_device(pwr);
82*4882a593Smuzhiyun if (err) {
83*4882a593Smuzhiyun dev_err(&pdev->dev, "Can't register power button: %d\n", err);
84*4882a593Smuzhiyun return err;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun device_init_wakeup(&pdev->dev, true);
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun return 0;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun #ifdef CONFIG_OF
93*4882a593Smuzhiyun static const struct of_device_id twl4030_pwrbutton_dt_match_table[] = {
94*4882a593Smuzhiyun { .compatible = "ti,twl4030-pwrbutton" },
95*4882a593Smuzhiyun {},
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, twl4030_pwrbutton_dt_match_table);
98*4882a593Smuzhiyun #endif
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun static struct platform_driver twl4030_pwrbutton_driver = {
101*4882a593Smuzhiyun .probe = twl4030_pwrbutton_probe,
102*4882a593Smuzhiyun .driver = {
103*4882a593Smuzhiyun .name = "twl4030_pwrbutton",
104*4882a593Smuzhiyun .of_match_table = of_match_ptr(twl4030_pwrbutton_dt_match_table),
105*4882a593Smuzhiyun },
106*4882a593Smuzhiyun };
107*4882a593Smuzhiyun module_platform_driver(twl4030_pwrbutton_driver);
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun MODULE_ALIAS("platform:twl4030_pwrbutton");
110*4882a593Smuzhiyun MODULE_DESCRIPTION("Triton2 Power Button");
111*4882a593Smuzhiyun MODULE_LICENSE("GPL");
112*4882a593Smuzhiyun MODULE_AUTHOR("Peter De Schrijver <peter.de-schrijver@nokia.com>");
113*4882a593Smuzhiyun MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
114*4882a593Smuzhiyun
115