blob: af45d275f6862924979f48def9b1aa43ece095eb [file] [log] [blame]
Michael Hennerich88751dd2009-09-17 22:39:38 -07001/*
2 * File: drivers/input/keyboard/adp5588_keys.c
Michael Hennerich5a9003d2010-01-19 00:28:44 -08003 * Description: keypad driver for ADP5588 and ADP5587
4 * I2C QWERTY Keypad and IO Expander
Michael Hennerich88751dd2009-09-17 22:39:38 -07005 * Bugs: Enter bugs at http://blackfin.uclinux.org/
6 *
Michael Hennerich95716c02010-11-02 11:33:05 -07007 * Copyright (C) 2008-2010 Analog Devices Inc.
Michael Hennerich88751dd2009-09-17 22:39:38 -07008 * 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>
Xiaolong Chenba9f5072010-07-26 01:01:11 -070022#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Michael Hennerich88751dd2009-09-17 22:39:38 -070024
25#include <linux/i2c/adp5588.h>
26
Michael Hennerich88751dd2009-09-17 22:39:38 -070027/* Key Event Register xy */
28#define KEY_EV_PRESSED (1 << 7)
29#define KEY_EV_MASK (0x7F)
30
31#define KP_SEL(x) (0xFFFF >> (16 - x)) /* 2^x-1 */
32
33#define KEYP_MAX_EVENT 10
34
35/*
36 * Early pre 4.0 Silicon required to delay readout by at least 25ms,
37 * since the Event Counter Register updated 25ms after the interrupt
38 * asserted.
39 */
40#define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
41
42struct adp5588_kpad {
43 struct i2c_client *client;
44 struct input_dev *input;
45 struct delayed_work work;
46 unsigned long delay;
47 unsigned short keycode[ADP5588_KEYMAPSIZE];
Xiaolong CHEN69a4af62010-06-24 19:10:40 -070048 const struct adp5588_gpi_map *gpimap;
49 unsigned short gpimapsize;
Xiaolong Chenba9f5072010-07-26 01:01:11 -070050#ifdef CONFIG_GPIOLIB
Michael Hennerich95716c02010-11-02 11:33:05 -070051 unsigned char gpiomap[ADP5588_MAXGPIO];
Xiaolong Chenba9f5072010-07-26 01:01:11 -070052 bool export_gpio;
53 struct gpio_chip gc;
54 struct mutex gpio_lock; /* Protect cached dir, dat_out */
55 u8 dat_out[3];
56 u8 dir[3];
57#endif
Michael Hennerich88751dd2009-09-17 22:39:38 -070058};
59
60static int adp5588_read(struct i2c_client *client, u8 reg)
61{
62 int ret = i2c_smbus_read_byte_data(client, reg);
63
64 if (ret < 0)
65 dev_err(&client->dev, "Read Error\n");
66
67 return ret;
68}
69
70static int adp5588_write(struct i2c_client *client, u8 reg, u8 val)
71{
72 return i2c_smbus_write_byte_data(client, reg, val);
73}
74
Xiaolong Chenba9f5072010-07-26 01:01:11 -070075#ifdef CONFIG_GPIOLIB
76static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
77{
78 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
Michael Hennerich95716c02010-11-02 11:33:05 -070079 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
80 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
Xiaolong Chenba9f5072010-07-26 01:01:11 -070081
82 return !!(adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank) & bit);
83}
84
85static void adp5588_gpio_set_value(struct gpio_chip *chip,
86 unsigned off, int val)
87{
88 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
Michael Hennerich95716c02010-11-02 11:33:05 -070089 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
90 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
Xiaolong Chenba9f5072010-07-26 01:01:11 -070091
92 mutex_lock(&kpad->gpio_lock);
93
94 if (val)
95 kpad->dat_out[bank] |= bit;
96 else
97 kpad->dat_out[bank] &= ~bit;
98
99 adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
100 kpad->dat_out[bank]);
101
102 mutex_unlock(&kpad->gpio_lock);
103}
104
105static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
106{
107 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
Michael Hennerich95716c02010-11-02 11:33:05 -0700108 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
109 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700110 int ret;
111
112 mutex_lock(&kpad->gpio_lock);
113
114 kpad->dir[bank] &= ~bit;
115 ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
116
117 mutex_unlock(&kpad->gpio_lock);
118
119 return ret;
120}
121
122static int adp5588_gpio_direction_output(struct gpio_chip *chip,
123 unsigned off, int val)
124{
125 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
Michael Hennerich95716c02010-11-02 11:33:05 -0700126 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
127 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700128 int ret;
129
130 mutex_lock(&kpad->gpio_lock);
131
132 kpad->dir[bank] |= bit;
133
134 if (val)
135 kpad->dat_out[bank] |= bit;
136 else
137 kpad->dat_out[bank] &= ~bit;
138
139 ret = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
140 kpad->dat_out[bank]);
141 ret |= adp5588_write(kpad->client, GPIO_DIR1 + bank,
142 kpad->dir[bank]);
143
144 mutex_unlock(&kpad->gpio_lock);
145
146 return ret;
147}
148
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700149static int __devinit adp5588_build_gpiomap(struct adp5588_kpad *kpad,
150 const struct adp5588_kpad_platform_data *pdata)
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700151{
Michael Hennerich95716c02010-11-02 11:33:05 -0700152 bool pin_used[ADP5588_MAXGPIO];
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700153 int n_unused = 0;
154 int i;
155
156 memset(pin_used, 0, sizeof(pin_used));
157
158 for (i = 0; i < pdata->rows; i++)
159 pin_used[i] = true;
160
161 for (i = 0; i < pdata->cols; i++)
162 pin_used[i + GPI_PIN_COL_BASE - GPI_PIN_BASE] = true;
163
164 for (i = 0; i < kpad->gpimapsize; i++)
165 pin_used[kpad->gpimap[i].pin - GPI_PIN_BASE] = true;
166
Michael Hennerich95716c02010-11-02 11:33:05 -0700167 for (i = 0; i < ADP5588_MAXGPIO; i++)
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700168 if (!pin_used[i])
169 kpad->gpiomap[n_unused++] = i;
170
171 return n_unused;
172}
173
174static int __devinit adp5588_gpio_add(struct adp5588_kpad *kpad)
175{
176 struct device *dev = &kpad->client->dev;
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700177 const struct adp5588_kpad_platform_data *pdata = dev->platform_data;
178 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
179 int i, error;
180
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700181 if (!gpio_data)
182 return 0;
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700183
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700184 kpad->gc.ngpio = adp5588_build_gpiomap(kpad, pdata);
185 if (kpad->gc.ngpio == 0) {
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700186 dev_info(dev, "No unused gpios left to export\n");
187 return 0;
188 }
189
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700190 kpad->export_gpio = true;
191
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700192 kpad->gc.direction_input = adp5588_gpio_direction_input;
193 kpad->gc.direction_output = adp5588_gpio_direction_output;
194 kpad->gc.get = adp5588_gpio_get_value;
195 kpad->gc.set = adp5588_gpio_set_value;
196 kpad->gc.can_sleep = 1;
197
198 kpad->gc.base = gpio_data->gpio_start;
199 kpad->gc.label = kpad->client->name;
200 kpad->gc.owner = THIS_MODULE;
201
202 mutex_init(&kpad->gpio_lock);
203
204 error = gpiochip_add(&kpad->gc);
205 if (error) {
206 dev_err(dev, "gpiochip_add failed, err: %d\n", error);
207 return error;
208 }
209
Michael Hennerich95716c02010-11-02 11:33:05 -0700210 for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700211 kpad->dat_out[i] = adp5588_read(kpad->client,
212 GPIO_DAT_OUT1 + i);
213 kpad->dir[i] = adp5588_read(kpad->client, GPIO_DIR1 + i);
214 }
215
216 if (gpio_data->setup) {
217 error = gpio_data->setup(kpad->client,
218 kpad->gc.base, kpad->gc.ngpio,
219 gpio_data->context);
220 if (error)
221 dev_warn(dev, "setup failed, %d\n", error);
222 }
223
224 return 0;
225}
226
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700227static void __devexit adp5588_gpio_remove(struct adp5588_kpad *kpad)
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700228{
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700229 struct device *dev = &kpad->client->dev;
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700230 const struct adp5588_kpad_platform_data *pdata = dev->platform_data;
231 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
232 int error;
233
234 if (!kpad->export_gpio)
235 return;
236
237 if (gpio_data->teardown) {
238 error = gpio_data->teardown(kpad->client,
239 kpad->gc.base, kpad->gc.ngpio,
240 gpio_data->context);
241 if (error)
242 dev_warn(dev, "teardown failed %d\n", error);
243 }
244
245 error = gpiochip_remove(&kpad->gc);
246 if (error)
247 dev_warn(dev, "gpiochip_remove failed %d\n", error);
248}
249#else
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700250static inline int adp5588_gpio_add(struct adp5588_kpad *kpad)
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700251{
252 return 0;
253}
254
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700255static inline void adp5588_gpio_remove(struct adp5588_kpad *kpad)
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700256{
257}
258#endif
259
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700260static void adp5588_report_events(struct adp5588_kpad *kpad, int ev_cnt)
261{
262 int i, j;
263
264 for (i = 0; i < ev_cnt; i++) {
265 int key = adp5588_read(kpad->client, Key_EVENTA + i);
266 int key_val = key & KEY_EV_MASK;
267
268 if (key_val >= GPI_PIN_BASE && key_val <= GPI_PIN_END) {
269 for (j = 0; j < kpad->gpimapsize; j++) {
270 if (key_val == kpad->gpimap[j].pin) {
271 input_report_switch(kpad->input,
272 kpad->gpimap[j].sw_evt,
273 key & KEY_EV_PRESSED);
274 break;
275 }
276 }
277 } else {
278 input_report_key(kpad->input,
279 kpad->keycode[key_val - 1],
280 key & KEY_EV_PRESSED);
281 }
282 }
283}
284
Michael Hennerich88751dd2009-09-17 22:39:38 -0700285static void adp5588_work(struct work_struct *work)
286{
287 struct adp5588_kpad *kpad = container_of(work,
288 struct adp5588_kpad, work.work);
289 struct i2c_client *client = kpad->client;
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700290 int status, ev_cnt;
Michael Hennerich88751dd2009-09-17 22:39:38 -0700291
292 status = adp5588_read(client, INT_STAT);
293
Michael Hennerich95716c02010-11-02 11:33:05 -0700294 if (status & ADP5588_OVR_FLOW_INT) /* Unlikely and should never happen */
Michael Hennerich88751dd2009-09-17 22:39:38 -0700295 dev_err(&client->dev, "Event Overflow Error\n");
296
Michael Hennerich95716c02010-11-02 11:33:05 -0700297 if (status & ADP5588_KE_INT) {
298 ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & ADP5588_KEC;
Michael Hennerich88751dd2009-09-17 22:39:38 -0700299 if (ev_cnt) {
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700300 adp5588_report_events(kpad, ev_cnt);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700301 input_sync(kpad->input);
302 }
303 }
304 adp5588_write(client, INT_STAT, status); /* Status is W1C */
305}
306
307static irqreturn_t adp5588_irq(int irq, void *handle)
308{
309 struct adp5588_kpad *kpad = handle;
310
311 /*
312 * use keventd context to read the event fifo registers
313 * Schedule readout at least 25ms after notification for
314 * REVID < 4
315 */
316
317 schedule_delayed_work(&kpad->work, kpad->delay);
318
319 return IRQ_HANDLED;
320}
321
322static int __devinit adp5588_setup(struct i2c_client *client)
323{
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700324 const struct adp5588_kpad_platform_data *pdata = client->dev.platform_data;
325 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
Michael Hennerich88751dd2009-09-17 22:39:38 -0700326 int i, ret;
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700327 unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
Michael Hennerich88751dd2009-09-17 22:39:38 -0700328
329 ret = adp5588_write(client, KP_GPIO1, KP_SEL(pdata->rows));
330 ret |= adp5588_write(client, KP_GPIO2, KP_SEL(pdata->cols) & 0xFF);
331 ret |= adp5588_write(client, KP_GPIO3, KP_SEL(pdata->cols) >> 8);
332
333 if (pdata->en_keylock) {
334 ret |= adp5588_write(client, UNLOCK1, pdata->unlock_key1);
335 ret |= adp5588_write(client, UNLOCK2, pdata->unlock_key2);
Michael Hennerich95716c02010-11-02 11:33:05 -0700336 ret |= adp5588_write(client, KEY_LCK_EC_STAT, ADP5588_K_LCK_EN);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700337 }
338
339 for (i = 0; i < KEYP_MAX_EVENT; i++)
340 ret |= adp5588_read(client, Key_EVENTA);
341
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700342 for (i = 0; i < pdata->gpimapsize; i++) {
343 unsigned short pin = pdata->gpimap[i].pin;
344
345 if (pin <= GPI_PIN_ROW_END) {
346 evt_mode1 |= (1 << (pin - GPI_PIN_ROW_BASE));
347 } else {
348 evt_mode2 |= ((1 << (pin - GPI_PIN_COL_BASE)) & 0xFF);
349 evt_mode3 |= ((1 << (pin - GPI_PIN_COL_BASE)) >> 8);
350 }
351 }
352
353 if (pdata->gpimapsize) {
354 ret |= adp5588_write(client, GPI_EM1, evt_mode1);
355 ret |= adp5588_write(client, GPI_EM2, evt_mode2);
356 ret |= adp5588_write(client, GPI_EM3, evt_mode3);
357 }
358
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700359 if (gpio_data) {
Michael Hennerich95716c02010-11-02 11:33:05 -0700360 for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700361 int pull_mask = gpio_data->pullup_dis_mask;
362
363 ret |= adp5588_write(client, GPIO_PULL1 + i,
364 (pull_mask >> (8 * i)) & 0xFF);
365 }
366 }
367
Michael Hennerich95716c02010-11-02 11:33:05 -0700368 ret |= adp5588_write(client, INT_STAT,
369 ADP5588_CMP2_INT | ADP5588_CMP1_INT |
370 ADP5588_OVR_FLOW_INT | ADP5588_K_LCK_INT |
371 ADP5588_GPI_INT | ADP5588_KE_INT); /* Status is W1C */
Michael Hennerich88751dd2009-09-17 22:39:38 -0700372
Michael Hennerich95716c02010-11-02 11:33:05 -0700373 ret |= adp5588_write(client, CFG, ADP5588_INT_CFG |
374 ADP5588_OVR_FLOW_IEN |
375 ADP5588_KE_IEN);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700376
377 if (ret < 0) {
378 dev_err(&client->dev, "Write Error\n");
379 return ret;
380 }
381
382 return 0;
383}
384
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700385static void __devinit adp5588_report_switch_state(struct adp5588_kpad *kpad)
386{
387 int gpi_stat1 = adp5588_read(kpad->client, GPIO_DAT_STAT1);
388 int gpi_stat2 = adp5588_read(kpad->client, GPIO_DAT_STAT2);
389 int gpi_stat3 = adp5588_read(kpad->client, GPIO_DAT_STAT3);
390 int gpi_stat_tmp, pin_loc;
391 int i;
392
393 for (i = 0; i < kpad->gpimapsize; i++) {
394 unsigned short pin = kpad->gpimap[i].pin;
395
396 if (pin <= GPI_PIN_ROW_END) {
397 gpi_stat_tmp = gpi_stat1;
398 pin_loc = pin - GPI_PIN_ROW_BASE;
399 } else if ((pin - GPI_PIN_COL_BASE) < 8) {
400 gpi_stat_tmp = gpi_stat2;
401 pin_loc = pin - GPI_PIN_COL_BASE;
402 } else {
403 gpi_stat_tmp = gpi_stat3;
404 pin_loc = pin - GPI_PIN_COL_BASE - 8;
405 }
406
407 if (gpi_stat_tmp < 0) {
408 dev_err(&kpad->client->dev,
409 "Can't read GPIO_DAT_STAT switch %d default to OFF\n",
410 pin);
411 gpi_stat_tmp = 0;
412 }
413
414 input_report_switch(kpad->input,
415 kpad->gpimap[i].sw_evt,
416 !(gpi_stat_tmp & (1 << pin_loc)));
417 }
418
419 input_sync(kpad->input);
420}
421
422
Michael Hennerich88751dd2009-09-17 22:39:38 -0700423static int __devinit adp5588_probe(struct i2c_client *client,
424 const struct i2c_device_id *id)
425{
426 struct adp5588_kpad *kpad;
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700427 const struct adp5588_kpad_platform_data *pdata = client->dev.platform_data;
Michael Hennerich88751dd2009-09-17 22:39:38 -0700428 struct input_dev *input;
429 unsigned int revid;
430 int ret, i;
431 int error;
432
433 if (!i2c_check_functionality(client->adapter,
434 I2C_FUNC_SMBUS_BYTE_DATA)) {
435 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
436 return -EIO;
437 }
438
439 if (!pdata) {
440 dev_err(&client->dev, "no platform data?\n");
441 return -EINVAL;
442 }
443
444 if (!pdata->rows || !pdata->cols || !pdata->keymap) {
445 dev_err(&client->dev, "no rows, cols or keymap from pdata\n");
446 return -EINVAL;
447 }
448
449 if (pdata->keymapsize != ADP5588_KEYMAPSIZE) {
450 dev_err(&client->dev, "invalid keymapsize\n");
451 return -EINVAL;
452 }
453
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700454 if (!pdata->gpimap && pdata->gpimapsize) {
455 dev_err(&client->dev, "invalid gpimap from pdata\n");
456 return -EINVAL;
457 }
458
459 if (pdata->gpimapsize > ADP5588_GPIMAPSIZE_MAX) {
460 dev_err(&client->dev, "invalid gpimapsize\n");
461 return -EINVAL;
462 }
463
464 for (i = 0; i < pdata->gpimapsize; i++) {
465 unsigned short pin = pdata->gpimap[i].pin;
466
467 if (pin < GPI_PIN_BASE || pin > GPI_PIN_END) {
468 dev_err(&client->dev, "invalid gpi pin data\n");
469 return -EINVAL;
470 }
471
472 if (pin <= GPI_PIN_ROW_END) {
473 if (pin - GPI_PIN_ROW_BASE + 1 <= pdata->rows) {
474 dev_err(&client->dev, "invalid gpi row data\n");
475 return -EINVAL;
476 }
477 } else {
478 if (pin - GPI_PIN_COL_BASE + 1 <= pdata->cols) {
479 dev_err(&client->dev, "invalid gpi col data\n");
480 return -EINVAL;
481 }
482 }
483 }
484
Michael Hennerich88751dd2009-09-17 22:39:38 -0700485 if (!client->irq) {
486 dev_err(&client->dev, "no IRQ?\n");
487 return -EINVAL;
488 }
489
490 kpad = kzalloc(sizeof(*kpad), GFP_KERNEL);
491 input = input_allocate_device();
492 if (!kpad || !input) {
493 error = -ENOMEM;
494 goto err_free_mem;
495 }
496
497 kpad->client = client;
498 kpad->input = input;
499 INIT_DELAYED_WORK(&kpad->work, adp5588_work);
500
501 ret = adp5588_read(client, DEV_ID);
502 if (ret < 0) {
503 error = ret;
504 goto err_free_mem;
505 }
506
507 revid = (u8) ret & ADP5588_DEVICE_ID_MASK;
508 if (WA_DELAYED_READOUT_REVID(revid))
509 kpad->delay = msecs_to_jiffies(30);
510
511 input->name = client->name;
512 input->phys = "adp5588-keys/input0";
513 input->dev.parent = &client->dev;
514
515 input_set_drvdata(input, kpad);
516
517 input->id.bustype = BUS_I2C;
518 input->id.vendor = 0x0001;
519 input->id.product = 0x0001;
520 input->id.version = revid;
521
522 input->keycodesize = sizeof(kpad->keycode[0]);
523 input->keycodemax = pdata->keymapsize;
524 input->keycode = kpad->keycode;
525
526 memcpy(kpad->keycode, pdata->keymap,
527 pdata->keymapsize * input->keycodesize);
528
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700529 kpad->gpimap = pdata->gpimap;
530 kpad->gpimapsize = pdata->gpimapsize;
531
Michael Hennerich88751dd2009-09-17 22:39:38 -0700532 /* setup input device */
533 __set_bit(EV_KEY, input->evbit);
534
535 if (pdata->repeat)
536 __set_bit(EV_REP, input->evbit);
537
538 for (i = 0; i < input->keycodemax; i++)
539 __set_bit(kpad->keycode[i] & KEY_MAX, input->keybit);
540 __clear_bit(KEY_RESERVED, input->keybit);
541
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700542 if (kpad->gpimapsize)
543 __set_bit(EV_SW, input->evbit);
544 for (i = 0; i < kpad->gpimapsize; i++)
545 __set_bit(kpad->gpimap[i].sw_evt, input->swbit);
546
Michael Hennerich88751dd2009-09-17 22:39:38 -0700547 error = input_register_device(input);
548 if (error) {
549 dev_err(&client->dev, "unable to register input device\n");
550 goto err_free_mem;
551 }
552
553 error = request_irq(client->irq, adp5588_irq,
554 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
555 client->dev.driver->name, kpad);
556 if (error) {
557 dev_err(&client->dev, "irq %d busy?\n", client->irq);
558 goto err_unreg_dev;
559 }
560
561 error = adp5588_setup(client);
562 if (error)
563 goto err_free_irq;
564
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700565 if (kpad->gpimapsize)
566 adp5588_report_switch_state(kpad);
567
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700568 error = adp5588_gpio_add(kpad);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700569 if (error)
570 goto err_free_irq;
571
Michael Hennerich88751dd2009-09-17 22:39:38 -0700572 device_init_wakeup(&client->dev, 1);
573 i2c_set_clientdata(client, kpad);
574
575 dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
576 return 0;
577
578 err_free_irq:
579 free_irq(client->irq, kpad);
580 err_unreg_dev:
581 input_unregister_device(input);
582 input = NULL;
583 err_free_mem:
584 input_free_device(input);
585 kfree(kpad);
586
587 return error;
588}
589
590static int __devexit adp5588_remove(struct i2c_client *client)
591{
592 struct adp5588_kpad *kpad = i2c_get_clientdata(client);
593
594 adp5588_write(client, CFG, 0);
595 free_irq(client->irq, kpad);
596 cancel_delayed_work_sync(&kpad->work);
597 input_unregister_device(kpad->input);
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700598 adp5588_gpio_remove(kpad);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700599 kfree(kpad);
600
601 return 0;
602}
603
604#ifdef CONFIG_PM
605static int adp5588_suspend(struct device *dev)
606{
607 struct adp5588_kpad *kpad = dev_get_drvdata(dev);
608 struct i2c_client *client = kpad->client;
609
610 disable_irq(client->irq);
611 cancel_delayed_work_sync(&kpad->work);
612
613 if (device_may_wakeup(&client->dev))
614 enable_irq_wake(client->irq);
615
616 return 0;
617}
618
619static int adp5588_resume(struct device *dev)
620{
621 struct adp5588_kpad *kpad = dev_get_drvdata(dev);
622 struct i2c_client *client = kpad->client;
623
624 if (device_may_wakeup(&client->dev))
625 disable_irq_wake(client->irq);
626
627 enable_irq(client->irq);
628
629 return 0;
630}
631
Alexey Dobriyan47145212009-12-14 18:00:08 -0800632static const struct dev_pm_ops adp5588_dev_pm_ops = {
Michael Hennerich88751dd2009-09-17 22:39:38 -0700633 .suspend = adp5588_suspend,
634 .resume = adp5588_resume,
635};
636#endif
637
638static const struct i2c_device_id adp5588_id[] = {
Michael Hennerichd5371552010-10-18 17:46:03 -0700639 { "adp5588-keys", 0 },
Michael Hennerich5a9003d2010-01-19 00:28:44 -0800640 { "adp5587-keys", 0 },
Michael Hennerich88751dd2009-09-17 22:39:38 -0700641 { }
642};
643MODULE_DEVICE_TABLE(i2c, adp5588_id);
644
645static struct i2c_driver adp5588_driver = {
646 .driver = {
647 .name = KBUILD_MODNAME,
648#ifdef CONFIG_PM
649 .pm = &adp5588_dev_pm_ops,
650#endif
651 },
652 .probe = adp5588_probe,
653 .remove = __devexit_p(adp5588_remove),
654 .id_table = adp5588_id,
655};
656
657static int __init adp5588_init(void)
658{
659 return i2c_add_driver(&adp5588_driver);
660}
661module_init(adp5588_init);
662
663static void __exit adp5588_exit(void)
664{
665 i2c_del_driver(&adp5588_driver);
666}
667module_exit(adp5588_exit);
668
669MODULE_LICENSE("GPL");
670MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
Michael Hennerich5a9003d2010-01-19 00:28:44 -0800671MODULE_DESCRIPTION("ADP5588/87 Keypad driver");
Michael Hennerich88751dd2009-09-17 22:39:38 -0700672MODULE_ALIAS("platform:adp5588-keys");