xref: /linux/drivers/input/misc/max8925_onkey.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 /*
2  * MAX8925 ONKEY driver
3  *
4  * Copyright (C) 2009 Marvell International Ltd.
5  *      Haojian Zhuang <haojian.zhuang@marvell.com>
6  *
7  * This file is subject to the terms and conditions of the GNU General
8  * Public License. See the file "COPYING" in the main directory of this
9  * archive for more details.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/i2c.h>
25 #include <linux/input.h>
26 #include <linux/interrupt.h>
27 #include <linux/mfd/max8925.h>
28 #include <linux/slab.h>
29 #include <linux/device.h>
30 
31 #define SW_INPUT		(1 << 7)	/* 0/1 -- up/down */
32 #define HARDRESET_EN		(1 << 7)
33 #define PWREN_EN		(1 << 7)
34 
35 struct max8925_onkey_info {
36 	struct input_dev	*idev;
37 	struct i2c_client	*i2c;
38 	struct device		*dev;
39 	unsigned int		irq[2];
40 };
41 
42 /*
43  * MAX8925 gives us an interrupt when ONKEY is pressed or released.
44  * max8925_set_bits() operates I2C bus and may sleep. So implement
45  * it in thread IRQ handler.
46  */
47 static irqreturn_t max8925_onkey_handler(int irq, void *data)
48 {
49 	struct max8925_onkey_info *info = data;
50 	int state;
51 
52 	state = max8925_reg_read(info->i2c, MAX8925_ON_OFF_STATUS);
53 
54 	input_report_key(info->idev, KEY_POWER, state & SW_INPUT);
55 	input_sync(info->idev);
56 
57 	dev_dbg(info->dev, "onkey state:%d\n", state);
58 
59 	/* Enable hardreset to halt if system isn't shutdown on time */
60 	max8925_set_bits(info->i2c, MAX8925_SYSENSEL,
61 			 HARDRESET_EN, HARDRESET_EN);
62 
63 	return IRQ_HANDLED;
64 }
65 
66 static int max8925_onkey_probe(struct platform_device *pdev)
67 {
68 	struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
69 	struct max8925_onkey_info *info;
70 	struct input_dev *input;
71 	int irq[2], error;
72 
73 	irq[0] = platform_get_irq(pdev, 0);
74 	if (irq[0] < 0)
75 		return -EINVAL;
76 
77 	irq[1] = platform_get_irq(pdev, 1);
78 	if (irq[1] < 0)
79 		return -EINVAL;
80 
81 	info = devm_kzalloc_obj(&pdev->dev, struct max8925_onkey_info);
82 	if (!info)
83 		return -ENOMEM;
84 
85 	input = devm_input_allocate_device(&pdev->dev);
86 	if (!input)
87 		return -ENOMEM;
88 
89 	info->idev = input;
90 	info->i2c = chip->i2c;
91 	info->dev = &pdev->dev;
92 	info->irq[0] = irq[0];
93 	info->irq[1] = irq[1];
94 
95 	input->name = "max8925_on";
96 	input->phys = "max8925_on/input0";
97 	input->id.bustype = BUS_I2C;
98 	input_set_capability(input, EV_KEY, KEY_POWER);
99 
100 	error = devm_request_threaded_irq(&pdev->dev, irq[0], NULL,
101 					  max8925_onkey_handler, IRQF_ONESHOT,
102 					  "onkey-down", info);
103 	if (error < 0) {
104 		dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
105 			irq[0], error);
106 		return error;
107 	}
108 
109 	error = devm_request_threaded_irq(&pdev->dev, irq[1], NULL,
110 					  max8925_onkey_handler, IRQF_ONESHOT,
111 					  "onkey-up", info);
112 	if (error < 0) {
113 		dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
114 			irq[1], error);
115 		return error;
116 	}
117 
118 	error = input_register_device(info->idev);
119 	if (error) {
120 		dev_err(chip->dev, "Can't register input device: %d\n", error);
121 		return error;
122 	}
123 
124 	platform_set_drvdata(pdev, info);
125 	device_init_wakeup(&pdev->dev, 1);
126 
127 	return 0;
128 }
129 
130 static int max8925_onkey_suspend(struct device *dev)
131 {
132 	struct platform_device *pdev = to_platform_device(dev);
133 	struct max8925_onkey_info *info = platform_get_drvdata(pdev);
134 	struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
135 
136 	if (device_may_wakeup(dev)) {
137 		chip->wakeup_flag |= 1 << info->irq[0];
138 		chip->wakeup_flag |= 1 << info->irq[1];
139 	}
140 
141 	return 0;
142 }
143 
144 static int max8925_onkey_resume(struct device *dev)
145 {
146 	struct platform_device *pdev = to_platform_device(dev);
147 	struct max8925_onkey_info *info = platform_get_drvdata(pdev);
148 	struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
149 
150 	if (device_may_wakeup(dev)) {
151 		chip->wakeup_flag &= ~(1 << info->irq[0]);
152 		chip->wakeup_flag &= ~(1 << info->irq[1]);
153 	}
154 
155 	return 0;
156 }
157 
158 static DEFINE_SIMPLE_DEV_PM_OPS(max8925_onkey_pm_ops,
159 				max8925_onkey_suspend, max8925_onkey_resume);
160 
161 static struct platform_driver max8925_onkey_driver = {
162 	.driver		= {
163 		.name	= "max8925-onkey",
164 		.pm	= pm_sleep_ptr(&max8925_onkey_pm_ops),
165 	},
166 	.probe		= max8925_onkey_probe,
167 };
168 module_platform_driver(max8925_onkey_driver);
169 
170 MODULE_DESCRIPTION("Maxim MAX8925 ONKEY driver");
171 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
172 MODULE_LICENSE("GPL");
173