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