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