xref: /linux/drivers/input/misc/twl4030-pwrbutton.c (revision 60e13231561b3a4c5269bfa1ef6c0569ad6f28ec)
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/platform_device.h>
30 #include <linux/i2c/twl.h>
31 
32 #define PWR_PWRON_IRQ (1 << 0)
33 
34 #define STS_HW_CONDITIONS 0xf
35 
36 static irqreturn_t powerbutton_irq(int irq, void *_pwr)
37 {
38 	struct input_dev *pwr = _pwr;
39 	int err;
40 	u8 value;
41 
42 	err = twl_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &value,
43 				STS_HW_CONDITIONS);
44 	if (!err)  {
45 		input_report_key(pwr, KEY_POWER, value & PWR_PWRON_IRQ);
46 		input_sync(pwr);
47 	} else {
48 		dev_err(pwr->dev.parent, "twl4030: i2c error %d while reading"
49 			" TWL4030 PM_MASTER STS_HW_CONDITIONS register\n", err);
50 	}
51 
52 	return IRQ_HANDLED;
53 }
54 
55 static int __init twl4030_pwrbutton_probe(struct platform_device *pdev)
56 {
57 	struct input_dev *pwr;
58 	int irq = platform_get_irq(pdev, 0);
59 	int err;
60 
61 	pwr = input_allocate_device();
62 	if (!pwr) {
63 		dev_dbg(&pdev->dev, "Can't allocate power button\n");
64 		return -ENOMEM;
65 	}
66 
67 	pwr->evbit[0] = BIT_MASK(EV_KEY);
68 	pwr->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
69 	pwr->name = "twl4030_pwrbutton";
70 	pwr->phys = "twl4030_pwrbutton/input0";
71 	pwr->dev.parent = &pdev->dev;
72 
73 	err = request_threaded_irq(irq, NULL, powerbutton_irq,
74 			IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
75 			"twl4030_pwrbutton", pwr);
76 	if (err < 0) {
77 		dev_dbg(&pdev->dev, "Can't get IRQ for pwrbutton: %d\n", err);
78 		goto free_input_dev;
79 	}
80 
81 	err = input_register_device(pwr);
82 	if (err) {
83 		dev_dbg(&pdev->dev, "Can't register power button: %d\n", err);
84 		goto free_irq;
85 	}
86 
87 	platform_set_drvdata(pdev, pwr);
88 
89 	return 0;
90 
91 free_irq:
92 	free_irq(irq, pwr);
93 free_input_dev:
94 	input_free_device(pwr);
95 	return err;
96 }
97 
98 static int __exit twl4030_pwrbutton_remove(struct platform_device *pdev)
99 {
100 	struct input_dev *pwr = platform_get_drvdata(pdev);
101 	int irq = platform_get_irq(pdev, 0);
102 
103 	free_irq(irq, pwr);
104 	input_unregister_device(pwr);
105 
106 	return 0;
107 }
108 
109 static struct platform_driver twl4030_pwrbutton_driver = {
110 	.remove		= __exit_p(twl4030_pwrbutton_remove),
111 	.driver		= {
112 		.name	= "twl4030_pwrbutton",
113 		.owner	= THIS_MODULE,
114 	},
115 };
116 
117 static int __init twl4030_pwrbutton_init(void)
118 {
119 	return platform_driver_probe(&twl4030_pwrbutton_driver,
120 			twl4030_pwrbutton_probe);
121 }
122 module_init(twl4030_pwrbutton_init);
123 
124 static void __exit twl4030_pwrbutton_exit(void)
125 {
126 	platform_driver_unregister(&twl4030_pwrbutton_driver);
127 }
128 module_exit(twl4030_pwrbutton_exit);
129 
130 MODULE_ALIAS("platform:twl4030_pwrbutton");
131 MODULE_DESCRIPTION("Triton2 Power Button");
132 MODULE_LICENSE("GPL");
133 MODULE_AUTHOR("Peter De Schrijver <peter.de-schrijver@nokia.com>");
134 MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
135 
136