blob: bb3b57bea8ba4578df5d29c72230882ac29bd62a [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/interrupt.h>
13#include <linux/irq.h>
14#include <linux/workqueue.h>
15#include <linux/errno.h>
16#include <linux/pm.h>
17#include <linux/platform_device.h>
18#include <linux/input.h>
19#include <linux/i2c.h>
Xiaolong Chenba9f5072010-07-26 01:01:11 -070020#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Michael Hennerich88751dd2009-09-17 22:39:38 -070022
23#include <linux/i2c/adp5588.h>
24
Michael Hennerich88751dd2009-09-17 22:39:38 -070025/* Key Event Register xy */
26#define KEY_EV_PRESSED (1 << 7)
27#define KEY_EV_MASK (0x7F)
28
29#define KP_SEL(x) (0xFFFF >> (16 - x)) /* 2^x-1 */
30
31#define KEYP_MAX_EVENT 10
32
33/*
34 * Early pre 4.0 Silicon required to delay readout by at least 25ms,
35 * since the Event Counter Register updated 25ms after the interrupt
36 * asserted.
37 */
38#define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
39
40struct adp5588_kpad {
41 struct i2c_client *client;
42 struct input_dev *input;
43 struct delayed_work work;
44 unsigned long delay;
45 unsigned short keycode[ADP5588_KEYMAPSIZE];
Xiaolong CHEN69a4af62010-06-24 19:10:40 -070046 const struct adp5588_gpi_map *gpimap;
47 unsigned short gpimapsize;
Xiaolong Chenba9f5072010-07-26 01:01:11 -070048#ifdef CONFIG_GPIOLIB
Michael Hennerich95716c02010-11-02 11:33:05 -070049 unsigned char gpiomap[ADP5588_MAXGPIO];
Xiaolong Chenba9f5072010-07-26 01:01:11 -070050 bool export_gpio;
51 struct gpio_chip gc;
52 struct mutex gpio_lock; /* Protect cached dir, dat_out */
53 u8 dat_out[3];
54 u8 dir[3];
55#endif
Michael Hennerich88751dd2009-09-17 22:39:38 -070056};
57
58static int adp5588_read(struct i2c_client *client, u8 reg)
59{
60 int ret = i2c_smbus_read_byte_data(client, reg);
61
62 if (ret < 0)
63 dev_err(&client->dev, "Read Error\n");
64
65 return ret;
66}
67
68static int adp5588_write(struct i2c_client *client, u8 reg, u8 val)
69{
70 return i2c_smbus_write_byte_data(client, reg, val);
71}
72
Xiaolong Chenba9f5072010-07-26 01:01:11 -070073#ifdef CONFIG_GPIOLIB
74static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
75{
76 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
Michael Hennerich95716c02010-11-02 11:33:05 -070077 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
78 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
Xiaolong Chenba9f5072010-07-26 01:01:11 -070079
80 return !!(adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank) & bit);
81}
82
83static void adp5588_gpio_set_value(struct gpio_chip *chip,
84 unsigned off, int val)
85{
86 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
Michael Hennerich95716c02010-11-02 11:33:05 -070087 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
88 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
Xiaolong Chenba9f5072010-07-26 01:01:11 -070089
90 mutex_lock(&kpad->gpio_lock);
91
92 if (val)
93 kpad->dat_out[bank] |= bit;
94 else
95 kpad->dat_out[bank] &= ~bit;
96
97 adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
98 kpad->dat_out[bank]);
99
100 mutex_unlock(&kpad->gpio_lock);
101}
102
103static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
104{
105 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
Michael Hennerich95716c02010-11-02 11:33:05 -0700106 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
107 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700108 int ret;
109
110 mutex_lock(&kpad->gpio_lock);
111
112 kpad->dir[bank] &= ~bit;
113 ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
114
115 mutex_unlock(&kpad->gpio_lock);
116
117 return ret;
118}
119
120static int adp5588_gpio_direction_output(struct gpio_chip *chip,
121 unsigned off, int val)
122{
123 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
Michael Hennerich95716c02010-11-02 11:33:05 -0700124 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
125 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700126 int ret;
127
128 mutex_lock(&kpad->gpio_lock);
129
130 kpad->dir[bank] |= bit;
131
132 if (val)
133 kpad->dat_out[bank] |= bit;
134 else
135 kpad->dat_out[bank] &= ~bit;
136
137 ret = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
138 kpad->dat_out[bank]);
139 ret |= adp5588_write(kpad->client, GPIO_DIR1 + bank,
140 kpad->dir[bank]);
141
142 mutex_unlock(&kpad->gpio_lock);
143
144 return ret;
145}
146
Bill Pemberton5298cc42012-11-23 21:38:25 -0800147static int adp5588_build_gpiomap(struct adp5588_kpad *kpad,
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700148 const struct adp5588_kpad_platform_data *pdata)
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700149{
Michael Hennerich95716c02010-11-02 11:33:05 -0700150 bool pin_used[ADP5588_MAXGPIO];
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700151 int n_unused = 0;
152 int i;
153
154 memset(pin_used, 0, sizeof(pin_used));
155
156 for (i = 0; i < pdata->rows; i++)
157 pin_used[i] = true;
158
159 for (i = 0; i < pdata->cols; i++)
160 pin_used[i + GPI_PIN_COL_BASE - GPI_PIN_BASE] = true;
161
162 for (i = 0; i < kpad->gpimapsize; i++)
163 pin_used[kpad->gpimap[i].pin - GPI_PIN_BASE] = true;
164
Michael Hennerich95716c02010-11-02 11:33:05 -0700165 for (i = 0; i < ADP5588_MAXGPIO; i++)
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700166 if (!pin_used[i])
167 kpad->gpiomap[n_unused++] = i;
168
169 return n_unused;
170}
171
Bill Pemberton5298cc42012-11-23 21:38:25 -0800172static int adp5588_gpio_add(struct adp5588_kpad *kpad)
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700173{
174 struct device *dev = &kpad->client->dev;
Jingoo Hanc838cb32013-12-05 19:21:10 -0800175 const struct adp5588_kpad_platform_data *pdata = dev_get_platdata(dev);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700176 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
177 int i, error;
178
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700179 if (!gpio_data)
180 return 0;
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700181
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700182 kpad->gc.ngpio = adp5588_build_gpiomap(kpad, pdata);
183 if (kpad->gc.ngpio == 0) {
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700184 dev_info(dev, "No unused gpios left to export\n");
185 return 0;
186 }
187
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700188 kpad->export_gpio = true;
189
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700190 kpad->gc.direction_input = adp5588_gpio_direction_input;
191 kpad->gc.direction_output = adp5588_gpio_direction_output;
192 kpad->gc.get = adp5588_gpio_get_value;
193 kpad->gc.set = adp5588_gpio_set_value;
194 kpad->gc.can_sleep = 1;
195
196 kpad->gc.base = gpio_data->gpio_start;
197 kpad->gc.label = kpad->client->name;
198 kpad->gc.owner = THIS_MODULE;
Jean-François Dagenaisd0a34572012-05-10 22:32:00 -0700199 kpad->gc.names = gpio_data->names;
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700200
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
Bill Pembertone2619cf2012-11-23 21:50:47 -0800226static void 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;
Jingoo Hanc838cb32013-12-05 19:21:10 -0800229 const struct adp5588_kpad_platform_data *pdata = dev_get_platdata(dev);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700230 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
Bill Pemberton5298cc42012-11-23 21:38:25 -0800321static int adp5588_setup(struct i2c_client *client)
Michael Hennerich88751dd2009-09-17 22:39:38 -0700322{
Jingoo Hanc838cb32013-12-05 19:21:10 -0800323 const struct adp5588_kpad_platform_data *pdata =
324 dev_get_platdata(&client->dev);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700325 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
Bill Pemberton5298cc42012-11-23 21:38:25 -0800385static void adp5588_report_switch_state(struct adp5588_kpad *kpad)
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700386{
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
Bill Pemberton5298cc42012-11-23 21:38:25 -0800423static int adp5588_probe(struct i2c_client *client,
424 const struct i2c_device_id *id)
Michael Hennerich88751dd2009-09-17 22:39:38 -0700425{
426 struct adp5588_kpad *kpad;
Jingoo Hanc838cb32013-12-05 19:21:10 -0800427 const struct adp5588_kpad_platform_data *pdata =
428 dev_get_platdata(&client->dev);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700429 struct input_dev *input;
430 unsigned int revid;
431 int ret, i;
432 int error;
433
434 if (!i2c_check_functionality(client->adapter,
435 I2C_FUNC_SMBUS_BYTE_DATA)) {
436 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
437 return -EIO;
438 }
439
440 if (!pdata) {
441 dev_err(&client->dev, "no platform data?\n");
442 return -EINVAL;
443 }
444
445 if (!pdata->rows || !pdata->cols || !pdata->keymap) {
446 dev_err(&client->dev, "no rows, cols or keymap from pdata\n");
447 return -EINVAL;
448 }
449
450 if (pdata->keymapsize != ADP5588_KEYMAPSIZE) {
451 dev_err(&client->dev, "invalid keymapsize\n");
452 return -EINVAL;
453 }
454
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700455 if (!pdata->gpimap && pdata->gpimapsize) {
456 dev_err(&client->dev, "invalid gpimap from pdata\n");
457 return -EINVAL;
458 }
459
460 if (pdata->gpimapsize > ADP5588_GPIMAPSIZE_MAX) {
461 dev_err(&client->dev, "invalid gpimapsize\n");
462 return -EINVAL;
463 }
464
465 for (i = 0; i < pdata->gpimapsize; i++) {
466 unsigned short pin = pdata->gpimap[i].pin;
467
468 if (pin < GPI_PIN_BASE || pin > GPI_PIN_END) {
469 dev_err(&client->dev, "invalid gpi pin data\n");
470 return -EINVAL;
471 }
472
473 if (pin <= GPI_PIN_ROW_END) {
474 if (pin - GPI_PIN_ROW_BASE + 1 <= pdata->rows) {
475 dev_err(&client->dev, "invalid gpi row data\n");
476 return -EINVAL;
477 }
478 } else {
479 if (pin - GPI_PIN_COL_BASE + 1 <= pdata->cols) {
480 dev_err(&client->dev, "invalid gpi col data\n");
481 return -EINVAL;
482 }
483 }
484 }
485
Michael Hennerich88751dd2009-09-17 22:39:38 -0700486 if (!client->irq) {
487 dev_err(&client->dev, "no IRQ?\n");
488 return -EINVAL;
489 }
490
491 kpad = kzalloc(sizeof(*kpad), GFP_KERNEL);
492 input = input_allocate_device();
493 if (!kpad || !input) {
494 error = -ENOMEM;
495 goto err_free_mem;
496 }
497
498 kpad->client = client;
499 kpad->input = input;
500 INIT_DELAYED_WORK(&kpad->work, adp5588_work);
501
502 ret = adp5588_read(client, DEV_ID);
503 if (ret < 0) {
504 error = ret;
505 goto err_free_mem;
506 }
507
508 revid = (u8) ret & ADP5588_DEVICE_ID_MASK;
509 if (WA_DELAYED_READOUT_REVID(revid))
510 kpad->delay = msecs_to_jiffies(30);
511
512 input->name = client->name;
513 input->phys = "adp5588-keys/input0";
514 input->dev.parent = &client->dev;
515
516 input_set_drvdata(input, kpad);
517
518 input->id.bustype = BUS_I2C;
519 input->id.vendor = 0x0001;
520 input->id.product = 0x0001;
521 input->id.version = revid;
522
523 input->keycodesize = sizeof(kpad->keycode[0]);
524 input->keycodemax = pdata->keymapsize;
525 input->keycode = kpad->keycode;
526
527 memcpy(kpad->keycode, pdata->keymap,
528 pdata->keymapsize * input->keycodesize);
529
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700530 kpad->gpimap = pdata->gpimap;
531 kpad->gpimapsize = pdata->gpimapsize;
532
Michael Hennerich88751dd2009-09-17 22:39:38 -0700533 /* setup input device */
534 __set_bit(EV_KEY, input->evbit);
535
536 if (pdata->repeat)
537 __set_bit(EV_REP, input->evbit);
538
539 for (i = 0; i < input->keycodemax; i++)
Andrew Liue4cfb032013-11-23 10:06:36 -0800540 if (kpad->keycode[i] <= KEY_MAX)
541 __set_bit(kpad->keycode[i], input->keybit);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700542 __clear_bit(KEY_RESERVED, input->keybit);
543
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700544 if (kpad->gpimapsize)
545 __set_bit(EV_SW, input->evbit);
546 for (i = 0; i < kpad->gpimapsize; i++)
547 __set_bit(kpad->gpimap[i].sw_evt, input->swbit);
548
Michael Hennerich88751dd2009-09-17 22:39:38 -0700549 error = input_register_device(input);
550 if (error) {
551 dev_err(&client->dev, "unable to register input device\n");
552 goto err_free_mem;
553 }
554
555 error = request_irq(client->irq, adp5588_irq,
Yong Zhangec4665c2011-09-07 14:04:16 -0700556 IRQF_TRIGGER_FALLING,
Michael Hennerich88751dd2009-09-17 22:39:38 -0700557 client->dev.driver->name, kpad);
558 if (error) {
559 dev_err(&client->dev, "irq %d busy?\n", client->irq);
560 goto err_unreg_dev;
561 }
562
563 error = adp5588_setup(client);
564 if (error)
565 goto err_free_irq;
566
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700567 if (kpad->gpimapsize)
568 adp5588_report_switch_state(kpad);
569
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700570 error = adp5588_gpio_add(kpad);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700571 if (error)
572 goto err_free_irq;
573
Michael Hennerich88751dd2009-09-17 22:39:38 -0700574 device_init_wakeup(&client->dev, 1);
575 i2c_set_clientdata(client, kpad);
576
577 dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
578 return 0;
579
580 err_free_irq:
581 free_irq(client->irq, kpad);
582 err_unreg_dev:
583 input_unregister_device(input);
584 input = NULL;
585 err_free_mem:
586 input_free_device(input);
587 kfree(kpad);
588
589 return error;
590}
591
Bill Pembertone2619cf2012-11-23 21:50:47 -0800592static int adp5588_remove(struct i2c_client *client)
Michael Hennerich88751dd2009-09-17 22:39:38 -0700593{
594 struct adp5588_kpad *kpad = i2c_get_clientdata(client);
595
596 adp5588_write(client, CFG, 0);
597 free_irq(client->irq, kpad);
598 cancel_delayed_work_sync(&kpad->work);
599 input_unregister_device(kpad->input);
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700600 adp5588_gpio_remove(kpad);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700601 kfree(kpad);
602
603 return 0;
604}
605
606#ifdef CONFIG_PM
607static int adp5588_suspend(struct device *dev)
608{
609 struct adp5588_kpad *kpad = dev_get_drvdata(dev);
610 struct i2c_client *client = kpad->client;
611
612 disable_irq(client->irq);
613 cancel_delayed_work_sync(&kpad->work);
614
615 if (device_may_wakeup(&client->dev))
616 enable_irq_wake(client->irq);
617
618 return 0;
619}
620
621static int adp5588_resume(struct device *dev)
622{
623 struct adp5588_kpad *kpad = dev_get_drvdata(dev);
624 struct i2c_client *client = kpad->client;
625
626 if (device_may_wakeup(&client->dev))
627 disable_irq_wake(client->irq);
628
629 enable_irq(client->irq);
630
631 return 0;
632}
633
Alexey Dobriyan47145212009-12-14 18:00:08 -0800634static const struct dev_pm_ops adp5588_dev_pm_ops = {
Michael Hennerich88751dd2009-09-17 22:39:38 -0700635 .suspend = adp5588_suspend,
636 .resume = adp5588_resume,
637};
638#endif
639
640static const struct i2c_device_id adp5588_id[] = {
Michael Hennerichd5371552010-10-18 17:46:03 -0700641 { "adp5588-keys", 0 },
Michael Hennerich5a9003d2010-01-19 00:28:44 -0800642 { "adp5587-keys", 0 },
Michael Hennerich88751dd2009-09-17 22:39:38 -0700643 { }
644};
645MODULE_DEVICE_TABLE(i2c, adp5588_id);
646
647static struct i2c_driver adp5588_driver = {
648 .driver = {
649 .name = KBUILD_MODNAME,
650#ifdef CONFIG_PM
651 .pm = &adp5588_dev_pm_ops,
652#endif
653 },
654 .probe = adp5588_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800655 .remove = adp5588_remove,
Michael Hennerich88751dd2009-09-17 22:39:38 -0700656 .id_table = adp5588_id,
657};
658
Axel Lin1b92c1c2012-03-16 23:05:41 -0700659module_i2c_driver(adp5588_driver);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700660
661MODULE_LICENSE("GPL");
662MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
Michael Hennerich5a9003d2010-01-19 00:28:44 -0800663MODULE_DESCRIPTION("ADP5588/87 Keypad driver");