blob: 4a7f534cf64b3692e8702654a7f085eb306a477b [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>
Michael Hennerich88751dd2009-09-17 22:39:38 -070012#include <linux/init.h>
13#include <linux/interrupt.h>
14#include <linux/irq.h>
15#include <linux/workqueue.h>
16#include <linux/errno.h>
17#include <linux/pm.h>
18#include <linux/platform_device.h>
19#include <linux/input.h>
20#include <linux/i2c.h>
Xiaolong Chenba9f5072010-07-26 01:01:11 -070021#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Michael Hennerich88751dd2009-09-17 22:39:38 -070023
24#include <linux/i2c/adp5588.h>
25
Michael Hennerich88751dd2009-09-17 22:39:38 -070026/* Key Event Register xy */
27#define KEY_EV_PRESSED (1 << 7)
28#define KEY_EV_MASK (0x7F)
29
30#define KP_SEL(x) (0xFFFF >> (16 - x)) /* 2^x-1 */
31
32#define KEYP_MAX_EVENT 10
33
34/*
35 * Early pre 4.0 Silicon required to delay readout by at least 25ms,
36 * since the Event Counter Register updated 25ms after the interrupt
37 * asserted.
38 */
39#define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
40
41struct adp5588_kpad {
42 struct i2c_client *client;
43 struct input_dev *input;
44 struct delayed_work work;
45 unsigned long delay;
46 unsigned short keycode[ADP5588_KEYMAPSIZE];
Xiaolong CHEN69a4af62010-06-24 19:10:40 -070047 const struct adp5588_gpi_map *gpimap;
48 unsigned short gpimapsize;
Xiaolong Chenba9f5072010-07-26 01:01:11 -070049#ifdef CONFIG_GPIOLIB
Michael Hennerich95716c02010-11-02 11:33:05 -070050 unsigned char gpiomap[ADP5588_MAXGPIO];
Xiaolong Chenba9f5072010-07-26 01:01:11 -070051 bool export_gpio;
52 struct gpio_chip gc;
53 struct mutex gpio_lock; /* Protect cached dir, dat_out */
54 u8 dat_out[3];
55 u8 dir[3];
56#endif
Michael Hennerich88751dd2009-09-17 22:39:38 -070057};
58
59static int adp5588_read(struct i2c_client *client, u8 reg)
60{
61 int ret = i2c_smbus_read_byte_data(client, reg);
62
63 if (ret < 0)
64 dev_err(&client->dev, "Read Error\n");
65
66 return ret;
67}
68
69static int adp5588_write(struct i2c_client *client, u8 reg, u8 val)
70{
71 return i2c_smbus_write_byte_data(client, reg, val);
72}
73
Xiaolong Chenba9f5072010-07-26 01:01:11 -070074#ifdef CONFIG_GPIOLIB
75static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
76{
77 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
Michael Hennerich95716c02010-11-02 11:33:05 -070078 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
79 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
Xiaolong Chenba9f5072010-07-26 01:01:11 -070080
81 return !!(adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank) & bit);
82}
83
84static void adp5588_gpio_set_value(struct gpio_chip *chip,
85 unsigned off, int val)
86{
87 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
Michael Hennerich95716c02010-11-02 11:33:05 -070088 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
89 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
Xiaolong Chenba9f5072010-07-26 01:01:11 -070090
91 mutex_lock(&kpad->gpio_lock);
92
93 if (val)
94 kpad->dat_out[bank] |= bit;
95 else
96 kpad->dat_out[bank] &= ~bit;
97
98 adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
99 kpad->dat_out[bank]);
100
101 mutex_unlock(&kpad->gpio_lock);
102}
103
104static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
105{
106 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
Michael Hennerich95716c02010-11-02 11:33:05 -0700107 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
108 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700109 int ret;
110
111 mutex_lock(&kpad->gpio_lock);
112
113 kpad->dir[bank] &= ~bit;
114 ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
115
116 mutex_unlock(&kpad->gpio_lock);
117
118 return ret;
119}
120
121static int adp5588_gpio_direction_output(struct gpio_chip *chip,
122 unsigned off, int val)
123{
124 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
Michael Hennerich95716c02010-11-02 11:33:05 -0700125 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
126 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700127 int ret;
128
129 mutex_lock(&kpad->gpio_lock);
130
131 kpad->dir[bank] |= bit;
132
133 if (val)
134 kpad->dat_out[bank] |= bit;
135 else
136 kpad->dat_out[bank] &= ~bit;
137
138 ret = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
139 kpad->dat_out[bank]);
140 ret |= adp5588_write(kpad->client, GPIO_DIR1 + bank,
141 kpad->dir[bank]);
142
143 mutex_unlock(&kpad->gpio_lock);
144
145 return ret;
146}
147
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700148static int __devinit adp5588_build_gpiomap(struct adp5588_kpad *kpad,
149 const struct adp5588_kpad_platform_data *pdata)
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700150{
Michael Hennerich95716c02010-11-02 11:33:05 -0700151 bool pin_used[ADP5588_MAXGPIO];
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700152 int n_unused = 0;
153 int i;
154
155 memset(pin_used, 0, sizeof(pin_used));
156
157 for (i = 0; i < pdata->rows; i++)
158 pin_used[i] = true;
159
160 for (i = 0; i < pdata->cols; i++)
161 pin_used[i + GPI_PIN_COL_BASE - GPI_PIN_BASE] = true;
162
163 for (i = 0; i < kpad->gpimapsize; i++)
164 pin_used[kpad->gpimap[i].pin - GPI_PIN_BASE] = true;
165
Michael Hennerich95716c02010-11-02 11:33:05 -0700166 for (i = 0; i < ADP5588_MAXGPIO; i++)
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700167 if (!pin_used[i])
168 kpad->gpiomap[n_unused++] = i;
169
170 return n_unused;
171}
172
173static int __devinit adp5588_gpio_add(struct adp5588_kpad *kpad)
174{
175 struct device *dev = &kpad->client->dev;
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700176 const struct adp5588_kpad_platform_data *pdata = dev->platform_data;
177 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
178 int i, error;
179
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700180 if (!gpio_data)
181 return 0;
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700182
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700183 kpad->gc.ngpio = adp5588_build_gpiomap(kpad, pdata);
184 if (kpad->gc.ngpio == 0) {
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700185 dev_info(dev, "No unused gpios left to export\n");
186 return 0;
187 }
188
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700189 kpad->export_gpio = true;
190
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700191 kpad->gc.direction_input = adp5588_gpio_direction_input;
192 kpad->gc.direction_output = adp5588_gpio_direction_output;
193 kpad->gc.get = adp5588_gpio_get_value;
194 kpad->gc.set = adp5588_gpio_set_value;
195 kpad->gc.can_sleep = 1;
196
197 kpad->gc.base = gpio_data->gpio_start;
198 kpad->gc.label = kpad->client->name;
199 kpad->gc.owner = THIS_MODULE;
200
201 mutex_init(&kpad->gpio_lock);
202
203 error = gpiochip_add(&kpad->gc);
204 if (error) {
205 dev_err(dev, "gpiochip_add failed, err: %d\n", error);
206 return error;
207 }
208
Michael Hennerich95716c02010-11-02 11:33:05 -0700209 for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700210 kpad->dat_out[i] = adp5588_read(kpad->client,
211 GPIO_DAT_OUT1 + i);
212 kpad->dir[i] = adp5588_read(kpad->client, GPIO_DIR1 + i);
213 }
214
215 if (gpio_data->setup) {
216 error = gpio_data->setup(kpad->client,
217 kpad->gc.base, kpad->gc.ngpio,
218 gpio_data->context);
219 if (error)
220 dev_warn(dev, "setup failed, %d\n", error);
221 }
222
223 return 0;
224}
225
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700226static void __devexit adp5588_gpio_remove(struct adp5588_kpad *kpad)
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700227{
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700228 struct device *dev = &kpad->client->dev;
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700229 const struct adp5588_kpad_platform_data *pdata = dev->platform_data;
230 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
231 int error;
232
233 if (!kpad->export_gpio)
234 return;
235
236 if (gpio_data->teardown) {
237 error = gpio_data->teardown(kpad->client,
238 kpad->gc.base, kpad->gc.ngpio,
239 gpio_data->context);
240 if (error)
241 dev_warn(dev, "teardown failed %d\n", error);
242 }
243
244 error = gpiochip_remove(&kpad->gc);
245 if (error)
246 dev_warn(dev, "gpiochip_remove failed %d\n", error);
247}
248#else
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700249static inline int adp5588_gpio_add(struct adp5588_kpad *kpad)
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700250{
251 return 0;
252}
253
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700254static inline void adp5588_gpio_remove(struct adp5588_kpad *kpad)
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700255{
256}
257#endif
258
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700259static void adp5588_report_events(struct adp5588_kpad *kpad, int ev_cnt)
260{
261 int i, j;
262
263 for (i = 0; i < ev_cnt; i++) {
264 int key = adp5588_read(kpad->client, Key_EVENTA + i);
265 int key_val = key & KEY_EV_MASK;
266
267 if (key_val >= GPI_PIN_BASE && key_val <= GPI_PIN_END) {
268 for (j = 0; j < kpad->gpimapsize; j++) {
269 if (key_val == kpad->gpimap[j].pin) {
270 input_report_switch(kpad->input,
271 kpad->gpimap[j].sw_evt,
272 key & KEY_EV_PRESSED);
273 break;
274 }
275 }
276 } else {
277 input_report_key(kpad->input,
278 kpad->keycode[key_val - 1],
279 key & KEY_EV_PRESSED);
280 }
281 }
282}
283
Michael Hennerich88751dd2009-09-17 22:39:38 -0700284static void adp5588_work(struct work_struct *work)
285{
286 struct adp5588_kpad *kpad = container_of(work,
287 struct adp5588_kpad, work.work);
288 struct i2c_client *client = kpad->client;
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700289 int status, ev_cnt;
Michael Hennerich88751dd2009-09-17 22:39:38 -0700290
291 status = adp5588_read(client, INT_STAT);
292
Michael Hennerich95716c02010-11-02 11:33:05 -0700293 if (status & ADP5588_OVR_FLOW_INT) /* Unlikely and should never happen */
Michael Hennerich88751dd2009-09-17 22:39:38 -0700294 dev_err(&client->dev, "Event Overflow Error\n");
295
Michael Hennerich95716c02010-11-02 11:33:05 -0700296 if (status & ADP5588_KE_INT) {
297 ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & ADP5588_KEC;
Michael Hennerich88751dd2009-09-17 22:39:38 -0700298 if (ev_cnt) {
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700299 adp5588_report_events(kpad, ev_cnt);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700300 input_sync(kpad->input);
301 }
302 }
303 adp5588_write(client, INT_STAT, status); /* Status is W1C */
304}
305
306static irqreturn_t adp5588_irq(int irq, void *handle)
307{
308 struct adp5588_kpad *kpad = handle;
309
310 /*
311 * use keventd context to read the event fifo registers
312 * Schedule readout at least 25ms after notification for
313 * REVID < 4
314 */
315
316 schedule_delayed_work(&kpad->work, kpad->delay);
317
318 return IRQ_HANDLED;
319}
320
321static int __devinit adp5588_setup(struct i2c_client *client)
322{
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700323 const struct adp5588_kpad_platform_data *pdata = client->dev.platform_data;
324 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
Michael Hennerich88751dd2009-09-17 22:39:38 -0700325 int i, ret;
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700326 unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
Michael Hennerich88751dd2009-09-17 22:39:38 -0700327
328 ret = adp5588_write(client, KP_GPIO1, KP_SEL(pdata->rows));
329 ret |= adp5588_write(client, KP_GPIO2, KP_SEL(pdata->cols) & 0xFF);
330 ret |= adp5588_write(client, KP_GPIO3, KP_SEL(pdata->cols) >> 8);
331
332 if (pdata->en_keylock) {
333 ret |= adp5588_write(client, UNLOCK1, pdata->unlock_key1);
334 ret |= adp5588_write(client, UNLOCK2, pdata->unlock_key2);
Michael Hennerich95716c02010-11-02 11:33:05 -0700335 ret |= adp5588_write(client, KEY_LCK_EC_STAT, ADP5588_K_LCK_EN);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700336 }
337
338 for (i = 0; i < KEYP_MAX_EVENT; i++)
339 ret |= adp5588_read(client, Key_EVENTA);
340
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700341 for (i = 0; i < pdata->gpimapsize; i++) {
342 unsigned short pin = pdata->gpimap[i].pin;
343
344 if (pin <= GPI_PIN_ROW_END) {
345 evt_mode1 |= (1 << (pin - GPI_PIN_ROW_BASE));
346 } else {
347 evt_mode2 |= ((1 << (pin - GPI_PIN_COL_BASE)) & 0xFF);
348 evt_mode3 |= ((1 << (pin - GPI_PIN_COL_BASE)) >> 8);
349 }
350 }
351
352 if (pdata->gpimapsize) {
353 ret |= adp5588_write(client, GPI_EM1, evt_mode1);
354 ret |= adp5588_write(client, GPI_EM2, evt_mode2);
355 ret |= adp5588_write(client, GPI_EM3, evt_mode3);
356 }
357
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700358 if (gpio_data) {
Michael Hennerich95716c02010-11-02 11:33:05 -0700359 for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700360 int pull_mask = gpio_data->pullup_dis_mask;
361
362 ret |= adp5588_write(client, GPIO_PULL1 + i,
363 (pull_mask >> (8 * i)) & 0xFF);
364 }
365 }
366
Michael Hennerich95716c02010-11-02 11:33:05 -0700367 ret |= adp5588_write(client, INT_STAT,
368 ADP5588_CMP2_INT | ADP5588_CMP1_INT |
369 ADP5588_OVR_FLOW_INT | ADP5588_K_LCK_INT |
370 ADP5588_GPI_INT | ADP5588_KE_INT); /* Status is W1C */
Michael Hennerich88751dd2009-09-17 22:39:38 -0700371
Michael Hennerich95716c02010-11-02 11:33:05 -0700372 ret |= adp5588_write(client, CFG, ADP5588_INT_CFG |
373 ADP5588_OVR_FLOW_IEN |
374 ADP5588_KE_IEN);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700375
376 if (ret < 0) {
377 dev_err(&client->dev, "Write Error\n");
378 return ret;
379 }
380
381 return 0;
382}
383
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700384static void __devinit adp5588_report_switch_state(struct adp5588_kpad *kpad)
385{
386 int gpi_stat1 = adp5588_read(kpad->client, GPIO_DAT_STAT1);
387 int gpi_stat2 = adp5588_read(kpad->client, GPIO_DAT_STAT2);
388 int gpi_stat3 = adp5588_read(kpad->client, GPIO_DAT_STAT3);
389 int gpi_stat_tmp, pin_loc;
390 int i;
391
392 for (i = 0; i < kpad->gpimapsize; i++) {
393 unsigned short pin = kpad->gpimap[i].pin;
394
395 if (pin <= GPI_PIN_ROW_END) {
396 gpi_stat_tmp = gpi_stat1;
397 pin_loc = pin - GPI_PIN_ROW_BASE;
398 } else if ((pin - GPI_PIN_COL_BASE) < 8) {
399 gpi_stat_tmp = gpi_stat2;
400 pin_loc = pin - GPI_PIN_COL_BASE;
401 } else {
402 gpi_stat_tmp = gpi_stat3;
403 pin_loc = pin - GPI_PIN_COL_BASE - 8;
404 }
405
406 if (gpi_stat_tmp < 0) {
407 dev_err(&kpad->client->dev,
408 "Can't read GPIO_DAT_STAT switch %d default to OFF\n",
409 pin);
410 gpi_stat_tmp = 0;
411 }
412
413 input_report_switch(kpad->input,
414 kpad->gpimap[i].sw_evt,
415 !(gpi_stat_tmp & (1 << pin_loc)));
416 }
417
418 input_sync(kpad->input);
419}
420
421
Michael Hennerich88751dd2009-09-17 22:39:38 -0700422static int __devinit adp5588_probe(struct i2c_client *client,
423 const struct i2c_device_id *id)
424{
425 struct adp5588_kpad *kpad;
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700426 const struct adp5588_kpad_platform_data *pdata = client->dev.platform_data;
Michael Hennerich88751dd2009-09-17 22:39:38 -0700427 struct input_dev *input;
428 unsigned int revid;
429 int ret, i;
430 int error;
431
432 if (!i2c_check_functionality(client->adapter,
433 I2C_FUNC_SMBUS_BYTE_DATA)) {
434 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
435 return -EIO;
436 }
437
438 if (!pdata) {
439 dev_err(&client->dev, "no platform data?\n");
440 return -EINVAL;
441 }
442
443 if (!pdata->rows || !pdata->cols || !pdata->keymap) {
444 dev_err(&client->dev, "no rows, cols or keymap from pdata\n");
445 return -EINVAL;
446 }
447
448 if (pdata->keymapsize != ADP5588_KEYMAPSIZE) {
449 dev_err(&client->dev, "invalid keymapsize\n");
450 return -EINVAL;
451 }
452
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700453 if (!pdata->gpimap && pdata->gpimapsize) {
454 dev_err(&client->dev, "invalid gpimap from pdata\n");
455 return -EINVAL;
456 }
457
458 if (pdata->gpimapsize > ADP5588_GPIMAPSIZE_MAX) {
459 dev_err(&client->dev, "invalid gpimapsize\n");
460 return -EINVAL;
461 }
462
463 for (i = 0; i < pdata->gpimapsize; i++) {
464 unsigned short pin = pdata->gpimap[i].pin;
465
466 if (pin < GPI_PIN_BASE || pin > GPI_PIN_END) {
467 dev_err(&client->dev, "invalid gpi pin data\n");
468 return -EINVAL;
469 }
470
471 if (pin <= GPI_PIN_ROW_END) {
472 if (pin - GPI_PIN_ROW_BASE + 1 <= pdata->rows) {
473 dev_err(&client->dev, "invalid gpi row data\n");
474 return -EINVAL;
475 }
476 } else {
477 if (pin - GPI_PIN_COL_BASE + 1 <= pdata->cols) {
478 dev_err(&client->dev, "invalid gpi col data\n");
479 return -EINVAL;
480 }
481 }
482 }
483
Michael Hennerich88751dd2009-09-17 22:39:38 -0700484 if (!client->irq) {
485 dev_err(&client->dev, "no IRQ?\n");
486 return -EINVAL;
487 }
488
489 kpad = kzalloc(sizeof(*kpad), GFP_KERNEL);
490 input = input_allocate_device();
491 if (!kpad || !input) {
492 error = -ENOMEM;
493 goto err_free_mem;
494 }
495
496 kpad->client = client;
497 kpad->input = input;
498 INIT_DELAYED_WORK(&kpad->work, adp5588_work);
499
500 ret = adp5588_read(client, DEV_ID);
501 if (ret < 0) {
502 error = ret;
503 goto err_free_mem;
504 }
505
506 revid = (u8) ret & ADP5588_DEVICE_ID_MASK;
507 if (WA_DELAYED_READOUT_REVID(revid))
508 kpad->delay = msecs_to_jiffies(30);
509
510 input->name = client->name;
511 input->phys = "adp5588-keys/input0";
512 input->dev.parent = &client->dev;
513
514 input_set_drvdata(input, kpad);
515
516 input->id.bustype = BUS_I2C;
517 input->id.vendor = 0x0001;
518 input->id.product = 0x0001;
519 input->id.version = revid;
520
521 input->keycodesize = sizeof(kpad->keycode[0]);
522 input->keycodemax = pdata->keymapsize;
523 input->keycode = kpad->keycode;
524
525 memcpy(kpad->keycode, pdata->keymap,
526 pdata->keymapsize * input->keycodesize);
527
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700528 kpad->gpimap = pdata->gpimap;
529 kpad->gpimapsize = pdata->gpimapsize;
530
Michael Hennerich88751dd2009-09-17 22:39:38 -0700531 /* setup input device */
532 __set_bit(EV_KEY, input->evbit);
533
534 if (pdata->repeat)
535 __set_bit(EV_REP, input->evbit);
536
537 for (i = 0; i < input->keycodemax; i++)
538 __set_bit(kpad->keycode[i] & KEY_MAX, input->keybit);
539 __clear_bit(KEY_RESERVED, input->keybit);
540
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700541 if (kpad->gpimapsize)
542 __set_bit(EV_SW, input->evbit);
543 for (i = 0; i < kpad->gpimapsize; i++)
544 __set_bit(kpad->gpimap[i].sw_evt, input->swbit);
545
Michael Hennerich88751dd2009-09-17 22:39:38 -0700546 error = input_register_device(input);
547 if (error) {
548 dev_err(&client->dev, "unable to register input device\n");
549 goto err_free_mem;
550 }
551
552 error = request_irq(client->irq, adp5588_irq,
Yong Zhangec4665c2011-09-07 14:04:16 -0700553 IRQF_TRIGGER_FALLING,
Michael Hennerich88751dd2009-09-17 22:39:38 -0700554 client->dev.driver->name, kpad);
555 if (error) {
556 dev_err(&client->dev, "irq %d busy?\n", client->irq);
557 goto err_unreg_dev;
558 }
559
560 error = adp5588_setup(client);
561 if (error)
562 goto err_free_irq;
563
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700564 if (kpad->gpimapsize)
565 adp5588_report_switch_state(kpad);
566
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700567 error = adp5588_gpio_add(kpad);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700568 if (error)
569 goto err_free_irq;
570
Michael Hennerich88751dd2009-09-17 22:39:38 -0700571 device_init_wakeup(&client->dev, 1);
572 i2c_set_clientdata(client, kpad);
573
574 dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
575 return 0;
576
577 err_free_irq:
578 free_irq(client->irq, kpad);
579 err_unreg_dev:
580 input_unregister_device(input);
581 input = NULL;
582 err_free_mem:
583 input_free_device(input);
584 kfree(kpad);
585
586 return error;
587}
588
589static int __devexit adp5588_remove(struct i2c_client *client)
590{
591 struct adp5588_kpad *kpad = i2c_get_clientdata(client);
592
593 adp5588_write(client, CFG, 0);
594 free_irq(client->irq, kpad);
595 cancel_delayed_work_sync(&kpad->work);
596 input_unregister_device(kpad->input);
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700597 adp5588_gpio_remove(kpad);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700598 kfree(kpad);
599
600 return 0;
601}
602
603#ifdef CONFIG_PM
604static int adp5588_suspend(struct device *dev)
605{
606 struct adp5588_kpad *kpad = dev_get_drvdata(dev);
607 struct i2c_client *client = kpad->client;
608
609 disable_irq(client->irq);
610 cancel_delayed_work_sync(&kpad->work);
611
612 if (device_may_wakeup(&client->dev))
613 enable_irq_wake(client->irq);
614
615 return 0;
616}
617
618static int adp5588_resume(struct device *dev)
619{
620 struct adp5588_kpad *kpad = dev_get_drvdata(dev);
621 struct i2c_client *client = kpad->client;
622
623 if (device_may_wakeup(&client->dev))
624 disable_irq_wake(client->irq);
625
626 enable_irq(client->irq);
627
628 return 0;
629}
630
Alexey Dobriyan47145212009-12-14 18:00:08 -0800631static const struct dev_pm_ops adp5588_dev_pm_ops = {
Michael Hennerich88751dd2009-09-17 22:39:38 -0700632 .suspend = adp5588_suspend,
633 .resume = adp5588_resume,
634};
635#endif
636
637static const struct i2c_device_id adp5588_id[] = {
Michael Hennerichd5371552010-10-18 17:46:03 -0700638 { "adp5588-keys", 0 },
Michael Hennerich5a9003d2010-01-19 00:28:44 -0800639 { "adp5587-keys", 0 },
Michael Hennerich88751dd2009-09-17 22:39:38 -0700640 { }
641};
642MODULE_DEVICE_TABLE(i2c, adp5588_id);
643
644static struct i2c_driver adp5588_driver = {
645 .driver = {
646 .name = KBUILD_MODNAME,
647#ifdef CONFIG_PM
648 .pm = &adp5588_dev_pm_ops,
649#endif
650 },
651 .probe = adp5588_probe,
652 .remove = __devexit_p(adp5588_remove),
653 .id_table = adp5588_id,
654};
655
656static int __init adp5588_init(void)
657{
658 return i2c_add_driver(&adp5588_driver);
659}
660module_init(adp5588_init);
661
662static void __exit adp5588_exit(void)
663{
664 i2c_del_driver(&adp5588_driver);
665}
666module_exit(adp5588_exit);
667
668MODULE_LICENSE("GPL");
669MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
Michael Hennerich5a9003d2010-01-19 00:28:44 -0800670MODULE_DESCRIPTION("ADP5588/87 Keypad driver");