]>
Commit | Line | Data |
---|---|---|
3734574c | 1 | /** |
104a5f3c | 2 | * MAX8925 ONKEY driver |
3734574c HZ |
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 | ||
2d95ae3b | 30 | #define SW_INPUT (1 << 7) /* 0/1 -- up/down */ |
3734574c HZ |
31 | #define HARDRESET_EN (1 << 7) |
32 | #define PWREN_EN (1 << 7) | |
33 | ||
34 | struct max8925_onkey_info { | |
35 | struct input_dev *idev; | |
36 | struct i2c_client *i2c; | |
2d95ae3b | 37 | struct device *dev; |
104a5f3c | 38 | unsigned int irq[2]; |
3734574c HZ |
39 | }; |
40 | ||
41 | /* | |
2d95ae3b | 42 | * MAX8925 gives us an interrupt when ONKEY is pressed or released. |
3734574c HZ |
43 | * max8925_set_bits() operates I2C bus and may sleep. So implement |
44 | * it in thread IRQ handler. | |
45 | */ | |
46 | static irqreturn_t max8925_onkey_handler(int irq, void *data) | |
47 | { | |
48 | struct max8925_onkey_info *info = data; | |
104a5f3c DT |
49 | int state; |
50 | ||
51 | state = max8925_reg_read(info->i2c, MAX8925_ON_OFF_STATUS); | |
52 | ||
53 | input_report_key(info->idev, KEY_POWER, state & SW_INPUT); | |
3734574c HZ |
54 | input_sync(info->idev); |
55 | ||
104a5f3c | 56 | dev_dbg(info->dev, "onkey state:%d\n", state); |
2d95ae3b | 57 | |
3734574c HZ |
58 | /* Enable hardreset to halt if system isn't shutdown on time */ |
59 | max8925_set_bits(info->i2c, MAX8925_SYSENSEL, | |
60 | HARDRESET_EN, HARDRESET_EN); | |
61 | ||
62 | return IRQ_HANDLED; | |
63 | } | |
64 | ||
5298cc4c | 65 | static int max8925_onkey_probe(struct platform_device *pdev) |
3734574c HZ |
66 | { |
67 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); | |
68 | struct max8925_onkey_info *info; | |
104a5f3c | 69 | struct input_dev *input; |
2d95ae3b HZ |
70 | int irq[2], error; |
71 | ||
72 | irq[0] = platform_get_irq(pdev, 0); | |
73 | if (irq[0] < 0) { | |
74 | dev_err(&pdev->dev, "No IRQ resource!\n"); | |
75 | return -EINVAL; | |
76 | } | |
104a5f3c | 77 | |
2d95ae3b HZ |
78 | irq[1] = platform_get_irq(pdev, 1); |
79 | if (irq[1] < 0) { | |
80 | dev_err(&pdev->dev, "No IRQ resource!\n"); | |
81 | return -EINVAL; | |
82 | } | |
3734574c HZ |
83 | |
84 | info = kzalloc(sizeof(struct max8925_onkey_info), GFP_KERNEL); | |
104a5f3c DT |
85 | input = input_allocate_device(); |
86 | if (!info || !input) { | |
87 | error = -ENOMEM; | |
88 | goto err_free_mem; | |
89 | } | |
3734574c | 90 | |
104a5f3c | 91 | info->idev = input; |
3734574c | 92 | info->i2c = chip->i2c; |
2d95ae3b | 93 | info->dev = &pdev->dev; |
104a5f3c DT |
94 | info->irq[0] = irq[0]; |
95 | info->irq[1] = irq[1]; | |
96 | ||
97 | input->name = "max8925_on"; | |
98 | input->phys = "max8925_on/input0"; | |
99 | input->id.bustype = BUS_I2C; | |
100 | input->dev.parent = &pdev->dev; | |
101 | input_set_capability(input, EV_KEY, KEY_POWER); | |
102 | ||
2d95ae3b HZ |
103 | irq[0] += chip->irq_base; |
104 | irq[1] += chip->irq_base; | |
105 | ||
106 | error = request_threaded_irq(irq[0], NULL, max8925_onkey_handler, | |
107 | IRQF_ONESHOT, "onkey-down", info); | |
108 | if (error < 0) { | |
109 | dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", | |
110 | irq[0], error); | |
104a5f3c | 111 | goto err_free_mem; |
2d95ae3b | 112 | } |
104a5f3c | 113 | |
2d95ae3b HZ |
114 | error = request_threaded_irq(irq[1], NULL, max8925_onkey_handler, |
115 | IRQF_ONESHOT, "onkey-up", info); | |
116 | if (error < 0) { | |
117 | dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", | |
118 | irq[1], error); | |
104a5f3c | 119 | goto err_free_irq0; |
2d95ae3b | 120 | } |
3734574c | 121 | |
3734574c HZ |
122 | error = input_register_device(info->idev); |
123 | if (error) { | |
124 | dev_err(chip->dev, "Can't register input device: %d\n", error); | |
104a5f3c | 125 | goto err_free_irq1; |
3734574c HZ |
126 | } |
127 | ||
128 | platform_set_drvdata(pdev, info); | |
104a5f3c | 129 | device_init_wakeup(&pdev->dev, 1); |
3734574c HZ |
130 | |
131 | return 0; | |
132 | ||
104a5f3c DT |
133 | err_free_irq1: |
134 | free_irq(irq[1], info); | |
135 | err_free_irq0: | |
136 | free_irq(irq[0], info); | |
137 | err_free_mem: | |
138 | input_free_device(input); | |
3734574c | 139 | kfree(info); |
104a5f3c | 140 | |
3734574c HZ |
141 | return error; |
142 | } | |
143 | ||
144 | static int __devexit max8925_onkey_remove(struct platform_device *pdev) | |
145 | { | |
146 | struct max8925_onkey_info *info = platform_get_drvdata(pdev); | |
104a5f3c | 147 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); |
3734574c | 148 | |
104a5f3c DT |
149 | free_irq(info->irq[0] + chip->irq_base, info); |
150 | free_irq(info->irq[1] + chip->irq_base, info); | |
3734574c HZ |
151 | input_unregister_device(info->idev); |
152 | kfree(info); | |
153 | ||
154 | platform_set_drvdata(pdev, NULL); | |
155 | ||
156 | return 0; | |
157 | } | |
158 | ||
adab30d7 KL |
159 | #ifdef CONFIG_PM_SLEEP |
160 | static int max8925_onkey_suspend(struct device *dev) | |
161 | { | |
162 | struct platform_device *pdev = to_platform_device(dev); | |
163 | struct max8925_onkey_info *info = platform_get_drvdata(pdev); | |
164 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); | |
165 | ||
166 | if (device_may_wakeup(dev)) { | |
104a5f3c DT |
167 | chip->wakeup_flag |= 1 << info->irq[0]; |
168 | chip->wakeup_flag |= 1 << info->irq[1]; | |
adab30d7 KL |
169 | } |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
174 | static int max8925_onkey_resume(struct device *dev) | |
175 | { | |
176 | struct platform_device *pdev = to_platform_device(dev); | |
177 | struct max8925_onkey_info *info = platform_get_drvdata(pdev); | |
178 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); | |
179 | ||
180 | if (device_may_wakeup(dev)) { | |
104a5f3c DT |
181 | chip->wakeup_flag &= ~(1 << info->irq[0]); |
182 | chip->wakeup_flag &= ~(1 << info->irq[1]); | |
adab30d7 KL |
183 | } |
184 | ||
185 | return 0; | |
186 | } | |
187 | #endif | |
188 | ||
189 | static SIMPLE_DEV_PM_OPS(max8925_onkey_pm_ops, max8925_onkey_suspend, max8925_onkey_resume); | |
190 | ||
3734574c HZ |
191 | static struct platform_driver max8925_onkey_driver = { |
192 | .driver = { | |
193 | .name = "max8925-onkey", | |
194 | .owner = THIS_MODULE, | |
adab30d7 | 195 | .pm = &max8925_onkey_pm_ops, |
3734574c HZ |
196 | }, |
197 | .probe = max8925_onkey_probe, | |
1cb0aa88 | 198 | .remove = max8925_onkey_remove, |
3734574c | 199 | }; |
840a746b | 200 | module_platform_driver(max8925_onkey_driver); |
3734574c HZ |
201 | |
202 | MODULE_DESCRIPTION("Maxim MAX8925 ONKEY driver"); | |
203 | MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>"); | |
204 | MODULE_LICENSE("GPL"); |