]>
Commit | Line | Data |
---|---|---|
88751dd6 MH |
1 | /* |
2 | * File: drivers/input/keyboard/adp5588_keys.c | |
5a9003db MH |
3 | * Description: keypad driver for ADP5588 and ADP5587 |
4 | * I2C QWERTY Keypad and IO Expander | |
88751dd6 MH |
5 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ |
6 | * | |
7 | * Copyright (C) 2008-2009 Analog Devices Inc. | |
8 | * Licensed under the GPL-2 or later. | |
9 | */ | |
10 | ||
11 | #include <linux/module.h> | |
12 | #include <linux/version.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/interrupt.h> | |
15 | #include <linux/irq.h> | |
16 | #include <linux/workqueue.h> | |
17 | #include <linux/errno.h> | |
18 | #include <linux/pm.h> | |
19 | #include <linux/platform_device.h> | |
20 | #include <linux/input.h> | |
21 | #include <linux/i2c.h> | |
5a0e3ad6 | 22 | #include <linux/slab.h> |
88751dd6 MH |
23 | |
24 | #include <linux/i2c/adp5588.h> | |
25 | ||
26 | /* Configuration Register1 */ | |
27 | #define AUTO_INC (1 << 7) | |
28 | #define GPIEM_CFG (1 << 6) | |
29 | #define OVR_FLOW_M (1 << 5) | |
30 | #define INT_CFG (1 << 4) | |
31 | #define OVR_FLOW_IEN (1 << 3) | |
32 | #define K_LCK_IM (1 << 2) | |
33 | #define GPI_IEN (1 << 1) | |
34 | #define KE_IEN (1 << 0) | |
35 | ||
36 | /* Interrupt Status Register */ | |
37 | #define CMP2_INT (1 << 5) | |
38 | #define CMP1_INT (1 << 4) | |
39 | #define OVR_FLOW_INT (1 << 3) | |
40 | #define K_LCK_INT (1 << 2) | |
41 | #define GPI_INT (1 << 1) | |
42 | #define KE_INT (1 << 0) | |
43 | ||
44 | /* Key Lock and Event Counter Register */ | |
45 | #define K_LCK_EN (1 << 6) | |
46 | #define LCK21 0x30 | |
47 | #define KEC 0xF | |
48 | ||
49 | /* Key Event Register xy */ | |
50 | #define KEY_EV_PRESSED (1 << 7) | |
51 | #define KEY_EV_MASK (0x7F) | |
52 | ||
53 | #define KP_SEL(x) (0xFFFF >> (16 - x)) /* 2^x-1 */ | |
54 | ||
55 | #define KEYP_MAX_EVENT 10 | |
56 | ||
57 | /* | |
58 | * Early pre 4.0 Silicon required to delay readout by at least 25ms, | |
59 | * since the Event Counter Register updated 25ms after the interrupt | |
60 | * asserted. | |
61 | */ | |
62 | #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4) | |
63 | ||
64 | struct adp5588_kpad { | |
65 | struct i2c_client *client; | |
66 | struct input_dev *input; | |
67 | struct delayed_work work; | |
68 | unsigned long delay; | |
69 | unsigned short keycode[ADP5588_KEYMAPSIZE]; | |
70 | }; | |
71 | ||
72 | static int adp5588_read(struct i2c_client *client, u8 reg) | |
73 | { | |
74 | int ret = i2c_smbus_read_byte_data(client, reg); | |
75 | ||
76 | if (ret < 0) | |
77 | dev_err(&client->dev, "Read Error\n"); | |
78 | ||
79 | return ret; | |
80 | } | |
81 | ||
82 | static int adp5588_write(struct i2c_client *client, u8 reg, u8 val) | |
83 | { | |
84 | return i2c_smbus_write_byte_data(client, reg, val); | |
85 | } | |
86 | ||
87 | static void adp5588_work(struct work_struct *work) | |
88 | { | |
89 | struct adp5588_kpad *kpad = container_of(work, | |
90 | struct adp5588_kpad, work.work); | |
91 | struct i2c_client *client = kpad->client; | |
92 | int i, key, status, ev_cnt; | |
93 | ||
94 | status = adp5588_read(client, INT_STAT); | |
95 | ||
96 | if (status & OVR_FLOW_INT) /* Unlikely and should never happen */ | |
97 | dev_err(&client->dev, "Event Overflow Error\n"); | |
98 | ||
99 | if (status & KE_INT) { | |
100 | ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & KEC; | |
101 | if (ev_cnt) { | |
102 | for (i = 0; i < ev_cnt; i++) { | |
103 | key = adp5588_read(client, Key_EVENTA + i); | |
104 | input_report_key(kpad->input, | |
105 | kpad->keycode[(key & KEY_EV_MASK) - 1], | |
106 | key & KEY_EV_PRESSED); | |
107 | } | |
108 | input_sync(kpad->input); | |
109 | } | |
110 | } | |
111 | adp5588_write(client, INT_STAT, status); /* Status is W1C */ | |
112 | } | |
113 | ||
114 | static irqreturn_t adp5588_irq(int irq, void *handle) | |
115 | { | |
116 | struct adp5588_kpad *kpad = handle; | |
117 | ||
118 | /* | |
119 | * use keventd context to read the event fifo registers | |
120 | * Schedule readout at least 25ms after notification for | |
121 | * REVID < 4 | |
122 | */ | |
123 | ||
124 | schedule_delayed_work(&kpad->work, kpad->delay); | |
125 | ||
126 | return IRQ_HANDLED; | |
127 | } | |
128 | ||
129 | static int __devinit adp5588_setup(struct i2c_client *client) | |
130 | { | |
131 | struct adp5588_kpad_platform_data *pdata = client->dev.platform_data; | |
132 | int i, ret; | |
133 | ||
134 | ret = adp5588_write(client, KP_GPIO1, KP_SEL(pdata->rows)); | |
135 | ret |= adp5588_write(client, KP_GPIO2, KP_SEL(pdata->cols) & 0xFF); | |
136 | ret |= adp5588_write(client, KP_GPIO3, KP_SEL(pdata->cols) >> 8); | |
137 | ||
138 | if (pdata->en_keylock) { | |
139 | ret |= adp5588_write(client, UNLOCK1, pdata->unlock_key1); | |
140 | ret |= adp5588_write(client, UNLOCK2, pdata->unlock_key2); | |
141 | ret |= adp5588_write(client, KEY_LCK_EC_STAT, K_LCK_EN); | |
142 | } | |
143 | ||
144 | for (i = 0; i < KEYP_MAX_EVENT; i++) | |
145 | ret |= adp5588_read(client, Key_EVENTA); | |
146 | ||
147 | ret |= adp5588_write(client, INT_STAT, CMP2_INT | CMP1_INT | | |
148 | OVR_FLOW_INT | K_LCK_INT | | |
149 | GPI_INT | KE_INT); /* Status is W1C */ | |
150 | ||
151 | ret |= adp5588_write(client, CFG, INT_CFG | OVR_FLOW_IEN | KE_IEN); | |
152 | ||
153 | if (ret < 0) { | |
154 | dev_err(&client->dev, "Write Error\n"); | |
155 | return ret; | |
156 | } | |
157 | ||
158 | return 0; | |
159 | } | |
160 | ||
161 | static int __devinit adp5588_probe(struct i2c_client *client, | |
162 | const struct i2c_device_id *id) | |
163 | { | |
164 | struct adp5588_kpad *kpad; | |
165 | struct adp5588_kpad_platform_data *pdata = client->dev.platform_data; | |
166 | struct input_dev *input; | |
167 | unsigned int revid; | |
168 | int ret, i; | |
169 | int error; | |
170 | ||
171 | if (!i2c_check_functionality(client->adapter, | |
172 | I2C_FUNC_SMBUS_BYTE_DATA)) { | |
173 | dev_err(&client->dev, "SMBUS Byte Data not Supported\n"); | |
174 | return -EIO; | |
175 | } | |
176 | ||
177 | if (!pdata) { | |
178 | dev_err(&client->dev, "no platform data?\n"); | |
179 | return -EINVAL; | |
180 | } | |
181 | ||
182 | if (!pdata->rows || !pdata->cols || !pdata->keymap) { | |
183 | dev_err(&client->dev, "no rows, cols or keymap from pdata\n"); | |
184 | return -EINVAL; | |
185 | } | |
186 | ||
187 | if (pdata->keymapsize != ADP5588_KEYMAPSIZE) { | |
188 | dev_err(&client->dev, "invalid keymapsize\n"); | |
189 | return -EINVAL; | |
190 | } | |
191 | ||
192 | if (!client->irq) { | |
193 | dev_err(&client->dev, "no IRQ?\n"); | |
194 | return -EINVAL; | |
195 | } | |
196 | ||
197 | kpad = kzalloc(sizeof(*kpad), GFP_KERNEL); | |
198 | input = input_allocate_device(); | |
199 | if (!kpad || !input) { | |
200 | error = -ENOMEM; | |
201 | goto err_free_mem; | |
202 | } | |
203 | ||
204 | kpad->client = client; | |
205 | kpad->input = input; | |
206 | INIT_DELAYED_WORK(&kpad->work, adp5588_work); | |
207 | ||
208 | ret = adp5588_read(client, DEV_ID); | |
209 | if (ret < 0) { | |
210 | error = ret; | |
211 | goto err_free_mem; | |
212 | } | |
213 | ||
214 | revid = (u8) ret & ADP5588_DEVICE_ID_MASK; | |
215 | if (WA_DELAYED_READOUT_REVID(revid)) | |
216 | kpad->delay = msecs_to_jiffies(30); | |
217 | ||
218 | input->name = client->name; | |
219 | input->phys = "adp5588-keys/input0"; | |
220 | input->dev.parent = &client->dev; | |
221 | ||
222 | input_set_drvdata(input, kpad); | |
223 | ||
224 | input->id.bustype = BUS_I2C; | |
225 | input->id.vendor = 0x0001; | |
226 | input->id.product = 0x0001; | |
227 | input->id.version = revid; | |
228 | ||
229 | input->keycodesize = sizeof(kpad->keycode[0]); | |
230 | input->keycodemax = pdata->keymapsize; | |
231 | input->keycode = kpad->keycode; | |
232 | ||
233 | memcpy(kpad->keycode, pdata->keymap, | |
234 | pdata->keymapsize * input->keycodesize); | |
235 | ||
236 | /* setup input device */ | |
237 | __set_bit(EV_KEY, input->evbit); | |
238 | ||
239 | if (pdata->repeat) | |
240 | __set_bit(EV_REP, input->evbit); | |
241 | ||
242 | for (i = 0; i < input->keycodemax; i++) | |
243 | __set_bit(kpad->keycode[i] & KEY_MAX, input->keybit); | |
244 | __clear_bit(KEY_RESERVED, input->keybit); | |
245 | ||
246 | error = input_register_device(input); | |
247 | if (error) { | |
248 | dev_err(&client->dev, "unable to register input device\n"); | |
249 | goto err_free_mem; | |
250 | } | |
251 | ||
252 | error = request_irq(client->irq, adp5588_irq, | |
253 | IRQF_TRIGGER_FALLING | IRQF_DISABLED, | |
254 | client->dev.driver->name, kpad); | |
255 | if (error) { | |
256 | dev_err(&client->dev, "irq %d busy?\n", client->irq); | |
257 | goto err_unreg_dev; | |
258 | } | |
259 | ||
260 | error = adp5588_setup(client); | |
261 | if (error) | |
262 | goto err_free_irq; | |
263 | ||
264 | device_init_wakeup(&client->dev, 1); | |
265 | i2c_set_clientdata(client, kpad); | |
266 | ||
267 | dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq); | |
268 | return 0; | |
269 | ||
270 | err_free_irq: | |
271 | free_irq(client->irq, kpad); | |
272 | err_unreg_dev: | |
273 | input_unregister_device(input); | |
274 | input = NULL; | |
275 | err_free_mem: | |
276 | input_free_device(input); | |
277 | kfree(kpad); | |
278 | ||
279 | return error; | |
280 | } | |
281 | ||
282 | static int __devexit adp5588_remove(struct i2c_client *client) | |
283 | { | |
284 | struct adp5588_kpad *kpad = i2c_get_clientdata(client); | |
285 | ||
286 | adp5588_write(client, CFG, 0); | |
287 | free_irq(client->irq, kpad); | |
288 | cancel_delayed_work_sync(&kpad->work); | |
289 | input_unregister_device(kpad->input); | |
88751dd6 MH |
290 | kfree(kpad); |
291 | ||
292 | return 0; | |
293 | } | |
294 | ||
295 | #ifdef CONFIG_PM | |
296 | static int adp5588_suspend(struct device *dev) | |
297 | { | |
298 | struct adp5588_kpad *kpad = dev_get_drvdata(dev); | |
299 | struct i2c_client *client = kpad->client; | |
300 | ||
301 | disable_irq(client->irq); | |
302 | cancel_delayed_work_sync(&kpad->work); | |
303 | ||
304 | if (device_may_wakeup(&client->dev)) | |
305 | enable_irq_wake(client->irq); | |
306 | ||
307 | return 0; | |
308 | } | |
309 | ||
310 | static int adp5588_resume(struct device *dev) | |
311 | { | |
312 | struct adp5588_kpad *kpad = dev_get_drvdata(dev); | |
313 | struct i2c_client *client = kpad->client; | |
314 | ||
315 | if (device_may_wakeup(&client->dev)) | |
316 | disable_irq_wake(client->irq); | |
317 | ||
318 | enable_irq(client->irq); | |
319 | ||
320 | return 0; | |
321 | } | |
322 | ||
47145210 | 323 | static const struct dev_pm_ops adp5588_dev_pm_ops = { |
88751dd6 MH |
324 | .suspend = adp5588_suspend, |
325 | .resume = adp5588_resume, | |
326 | }; | |
327 | #endif | |
328 | ||
329 | static const struct i2c_device_id adp5588_id[] = { | |
330 | { KBUILD_MODNAME, 0 }, | |
5a9003db | 331 | { "adp5587-keys", 0 }, |
88751dd6 MH |
332 | { } |
333 | }; | |
334 | MODULE_DEVICE_TABLE(i2c, adp5588_id); | |
335 | ||
336 | static struct i2c_driver adp5588_driver = { | |
337 | .driver = { | |
338 | .name = KBUILD_MODNAME, | |
339 | #ifdef CONFIG_PM | |
340 | .pm = &adp5588_dev_pm_ops, | |
341 | #endif | |
342 | }, | |
343 | .probe = adp5588_probe, | |
344 | .remove = __devexit_p(adp5588_remove), | |
345 | .id_table = adp5588_id, | |
346 | }; | |
347 | ||
348 | static int __init adp5588_init(void) | |
349 | { | |
350 | return i2c_add_driver(&adp5588_driver); | |
351 | } | |
352 | module_init(adp5588_init); | |
353 | ||
354 | static void __exit adp5588_exit(void) | |
355 | { | |
356 | i2c_del_driver(&adp5588_driver); | |
357 | } | |
358 | module_exit(adp5588_exit); | |
359 | ||
360 | MODULE_LICENSE("GPL"); | |
361 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); | |
5a9003db | 362 | MODULE_DESCRIPTION("ADP5588/87 Keypad driver"); |
88751dd6 | 363 | MODULE_ALIAS("platform:adp5588-keys"); |