xref: /linux/drivers/input/misc/twl4030-pwrbutton.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 /*
2  * 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/bits.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/input.h>
29 #include <linux/interrupt.h>
30 #include <linux/mod_devicetable.h>
31 #include <linux/property.h>
32 #include <linux/platform_device.h>
33 #include <linux/mfd/twl.h>
34 
35 #define PWR_PWRON_IRQ BIT(0)
36 
37 struct twl_pwrbutton_chipdata {
38 	u8 status_reg;
39 	bool need_manual_irq;
40 };
41 
42 static const struct twl_pwrbutton_chipdata twl4030_chipdata = {
43 	.status_reg = 0xf,
44 	.need_manual_irq = false,
45 };
46 
47 static const struct twl_pwrbutton_chipdata twl6030_chipdata = {
48 	.status_reg = 0x2,
49 	.need_manual_irq = true,
50 };
51 
52 static irqreturn_t powerbutton_irq(int irq, void *_pwr)
53 {
54 	struct input_dev *pwr = _pwr;
55 	const struct twl_pwrbutton_chipdata *pdata = dev_get_drvdata(pwr->dev.parent);
56 	int err;
57 	u8 value;
58 
59 	err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &value, pdata->status_reg);
60 	if (!err)  {
61 		pm_wakeup_event(pwr->dev.parent, 0);
62 		input_report_key(pwr, KEY_POWER, value & PWR_PWRON_IRQ);
63 		input_sync(pwr);
64 	} else {
65 		dev_err(pwr->dev.parent, "twl4030: i2c error %d while reading"
66 			" TWL4030 PM_MASTER STS_HW_CONDITIONS register\n", err);
67 	}
68 
69 	return IRQ_HANDLED;
70 }
71 
72 static int twl4030_pwrbutton_probe(struct platform_device *pdev)
73 {
74 	const struct twl_pwrbutton_chipdata *pdata;
75 	struct input_dev *pwr;
76 	int irq = platform_get_irq(pdev, 0);
77 	int err;
78 
79 	pdata = device_get_match_data(&pdev->dev);
80 	if (!pdata)
81 		return -EINVAL;
82 
83 	platform_set_drvdata(pdev, (void *)pdata);
84 
85 	pwr = devm_input_allocate_device(&pdev->dev);
86 	if (!pwr) {
87 		dev_err(&pdev->dev, "Can't allocate power button\n");
88 		return -ENOMEM;
89 	}
90 
91 	input_set_capability(pwr, EV_KEY, KEY_POWER);
92 	pwr->name = "twl4030_pwrbutton";
93 	pwr->phys = "twl4030_pwrbutton/input0";
94 	pwr->dev.parent = &pdev->dev;
95 
96 	err = devm_request_threaded_irq(&pdev->dev, irq, NULL, powerbutton_irq,
97 			IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
98 			IRQF_ONESHOT,
99 			"twl4030_pwrbutton", pwr);
100 	if (err < 0) {
101 		dev_err(&pdev->dev, "Can't get IRQ for pwrbutton: %d\n", err);
102 		return err;
103 	}
104 
105 	err = input_register_device(pwr);
106 	if (err) {
107 		dev_err(&pdev->dev, "Can't register power button: %d\n", err);
108 		return err;
109 	}
110 
111 	if (pdata->need_manual_irq) {
112 		err = twl6030_interrupt_unmask(0x01, REG_INT_MSK_LINE_A);
113 		if (err)
114 			return err;
115 
116 		err = twl6030_interrupt_unmask(0x01, REG_INT_MSK_STS_A);
117 		if (err)
118 			return err;
119 	}
120 
121 	device_init_wakeup(&pdev->dev, true);
122 
123 	return 0;
124 }
125 
126 static void twl4030_pwrbutton_remove(struct platform_device *pdev)
127 {
128 	const struct twl_pwrbutton_chipdata *pdata = platform_get_drvdata(pdev);
129 
130 	if (pdata->need_manual_irq) {
131 		twl6030_interrupt_mask(0x01, REG_INT_MSK_LINE_A);
132 		twl6030_interrupt_mask(0x01, REG_INT_MSK_STS_A);
133 	}
134 }
135 
136 static const struct of_device_id twl4030_pwrbutton_dt_match_table[] = {
137 	{
138 		.compatible = "ti,twl4030-pwrbutton",
139 		.data = &twl4030_chipdata,
140 	},
141 	{
142 		.compatible = "ti,twl6030-pwrbutton",
143 		.data = &twl6030_chipdata,
144 	},
145 	{ }
146 };
147 MODULE_DEVICE_TABLE(of, twl4030_pwrbutton_dt_match_table);
148 
149 static struct platform_driver twl4030_pwrbutton_driver = {
150 	.probe		= twl4030_pwrbutton_probe,
151 	.remove		= twl4030_pwrbutton_remove,
152 	.driver		= {
153 		.name	= "twl4030_pwrbutton",
154 		.of_match_table = twl4030_pwrbutton_dt_match_table,
155 	},
156 };
157 module_platform_driver(twl4030_pwrbutton_driver);
158 
159 MODULE_ALIAS("platform:twl4030_pwrbutton");
160 MODULE_DESCRIPTION("Triton2 Power Button");
161 MODULE_LICENSE("GPL");
162 MODULE_AUTHOR("Peter De Schrijver <peter.de-schrijver@nokia.com>");
163 MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
164 
165