blob: 5ef7fcf0e2509b8196cf69a993247febae4c20b7 [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]);
Jean-Francois Dagenais910a9f52014-02-12 15:14:28 -080079 int val;
Xiaolong Chenba9f5072010-07-26 01:01:11 -070080
Jean-Francois Dagenais910a9f52014-02-12 15:14:28 -080081 mutex_lock(&kpad->gpio_lock);
82
83 if (kpad->dir[bank] & bit)
84 val = kpad->dat_out[bank];
85 else
86 val = adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank);
87
88 mutex_unlock(&kpad->gpio_lock);
89
90 return !!(val & bit);
Xiaolong Chenba9f5072010-07-26 01:01:11 -070091}
92
93static void adp5588_gpio_set_value(struct gpio_chip *chip,
94 unsigned off, int val)
95{
96 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
Michael Hennerich95716c02010-11-02 11:33:05 -070097 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
98 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
Xiaolong Chenba9f5072010-07-26 01:01:11 -070099
100 mutex_lock(&kpad->gpio_lock);
101
102 if (val)
103 kpad->dat_out[bank] |= bit;
104 else
105 kpad->dat_out[bank] &= ~bit;
106
107 adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
108 kpad->dat_out[bank]);
109
110 mutex_unlock(&kpad->gpio_lock);
111}
112
113static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
114{
115 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
Michael Hennerich95716c02010-11-02 11:33:05 -0700116 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
117 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700118 int ret;
119
120 mutex_lock(&kpad->gpio_lock);
121
122 kpad->dir[bank] &= ~bit;
123 ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]);
124
125 mutex_unlock(&kpad->gpio_lock);
126
127 return ret;
128}
129
130static int adp5588_gpio_direction_output(struct gpio_chip *chip,
131 unsigned off, int val)
132{
133 struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc);
Michael Hennerich95716c02010-11-02 11:33:05 -0700134 unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]);
135 unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700136 int ret;
137
138 mutex_lock(&kpad->gpio_lock);
139
140 kpad->dir[bank] |= bit;
141
142 if (val)
143 kpad->dat_out[bank] |= bit;
144 else
145 kpad->dat_out[bank] &= ~bit;
146
147 ret = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank,
148 kpad->dat_out[bank]);
149 ret |= adp5588_write(kpad->client, GPIO_DIR1 + bank,
150 kpad->dir[bank]);
151
152 mutex_unlock(&kpad->gpio_lock);
153
154 return ret;
155}
156
Bill Pemberton5298cc42012-11-23 21:38:25 -0800157static int adp5588_build_gpiomap(struct adp5588_kpad *kpad,
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700158 const struct adp5588_kpad_platform_data *pdata)
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700159{
Michael Hennerich95716c02010-11-02 11:33:05 -0700160 bool pin_used[ADP5588_MAXGPIO];
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700161 int n_unused = 0;
162 int i;
163
164 memset(pin_used, 0, sizeof(pin_used));
165
166 for (i = 0; i < pdata->rows; i++)
167 pin_used[i] = true;
168
169 for (i = 0; i < pdata->cols; i++)
170 pin_used[i + GPI_PIN_COL_BASE - GPI_PIN_BASE] = true;
171
172 for (i = 0; i < kpad->gpimapsize; i++)
173 pin_used[kpad->gpimap[i].pin - GPI_PIN_BASE] = true;
174
Michael Hennerich95716c02010-11-02 11:33:05 -0700175 for (i = 0; i < ADP5588_MAXGPIO; i++)
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700176 if (!pin_used[i])
177 kpad->gpiomap[n_unused++] = i;
178
179 return n_unused;
180}
181
Bill Pemberton5298cc42012-11-23 21:38:25 -0800182static int adp5588_gpio_add(struct adp5588_kpad *kpad)
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700183{
184 struct device *dev = &kpad->client->dev;
Jingoo Hanc838cb32013-12-05 19:21:10 -0800185 const struct adp5588_kpad_platform_data *pdata = dev_get_platdata(dev);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700186 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
187 int i, error;
188
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700189 if (!gpio_data)
190 return 0;
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700191
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700192 kpad->gc.ngpio = adp5588_build_gpiomap(kpad, pdata);
193 if (kpad->gc.ngpio == 0) {
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700194 dev_info(dev, "No unused gpios left to export\n");
195 return 0;
196 }
197
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700198 kpad->export_gpio = true;
199
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700200 kpad->gc.direction_input = adp5588_gpio_direction_input;
201 kpad->gc.direction_output = adp5588_gpio_direction_output;
202 kpad->gc.get = adp5588_gpio_get_value;
203 kpad->gc.set = adp5588_gpio_set_value;
204 kpad->gc.can_sleep = 1;
205
206 kpad->gc.base = gpio_data->gpio_start;
207 kpad->gc.label = kpad->client->name;
208 kpad->gc.owner = THIS_MODULE;
Jean-François Dagenaisd0a34572012-05-10 22:32:00 -0700209 kpad->gc.names = gpio_data->names;
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700210
211 mutex_init(&kpad->gpio_lock);
212
213 error = gpiochip_add(&kpad->gc);
214 if (error) {
215 dev_err(dev, "gpiochip_add failed, err: %d\n", error);
216 return error;
217 }
218
Michael Hennerich95716c02010-11-02 11:33:05 -0700219 for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700220 kpad->dat_out[i] = adp5588_read(kpad->client,
221 GPIO_DAT_OUT1 + i);
222 kpad->dir[i] = adp5588_read(kpad->client, GPIO_DIR1 + i);
223 }
224
225 if (gpio_data->setup) {
226 error = gpio_data->setup(kpad->client,
227 kpad->gc.base, kpad->gc.ngpio,
228 gpio_data->context);
229 if (error)
230 dev_warn(dev, "setup failed, %d\n", error);
231 }
232
233 return 0;
234}
235
Bill Pembertone2619cf2012-11-23 21:50:47 -0800236static void adp5588_gpio_remove(struct adp5588_kpad *kpad)
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700237{
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700238 struct device *dev = &kpad->client->dev;
Jingoo Hanc838cb32013-12-05 19:21:10 -0800239 const struct adp5588_kpad_platform_data *pdata = dev_get_platdata(dev);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700240 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
241 int error;
242
243 if (!kpad->export_gpio)
244 return;
245
246 if (gpio_data->teardown) {
247 error = gpio_data->teardown(kpad->client,
248 kpad->gc.base, kpad->gc.ngpio,
249 gpio_data->context);
250 if (error)
251 dev_warn(dev, "teardown failed %d\n", error);
252 }
253
254 error = gpiochip_remove(&kpad->gc);
255 if (error)
256 dev_warn(dev, "gpiochip_remove failed %d\n", error);
257}
258#else
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700259static inline int adp5588_gpio_add(struct adp5588_kpad *kpad)
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700260{
261 return 0;
262}
263
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700264static inline void adp5588_gpio_remove(struct adp5588_kpad *kpad)
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700265{
266}
267#endif
268
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700269static void adp5588_report_events(struct adp5588_kpad *kpad, int ev_cnt)
270{
271 int i, j;
272
273 for (i = 0; i < ev_cnt; i++) {
274 int key = adp5588_read(kpad->client, Key_EVENTA + i);
275 int key_val = key & KEY_EV_MASK;
276
277 if (key_val >= GPI_PIN_BASE && key_val <= GPI_PIN_END) {
278 for (j = 0; j < kpad->gpimapsize; j++) {
279 if (key_val == kpad->gpimap[j].pin) {
280 input_report_switch(kpad->input,
281 kpad->gpimap[j].sw_evt,
282 key & KEY_EV_PRESSED);
283 break;
284 }
285 }
286 } else {
287 input_report_key(kpad->input,
288 kpad->keycode[key_val - 1],
289 key & KEY_EV_PRESSED);
290 }
291 }
292}
293
Michael Hennerich88751dd2009-09-17 22:39:38 -0700294static void adp5588_work(struct work_struct *work)
295{
296 struct adp5588_kpad *kpad = container_of(work,
297 struct adp5588_kpad, work.work);
298 struct i2c_client *client = kpad->client;
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700299 int status, ev_cnt;
Michael Hennerich88751dd2009-09-17 22:39:38 -0700300
301 status = adp5588_read(client, INT_STAT);
302
Michael Hennerich95716c02010-11-02 11:33:05 -0700303 if (status & ADP5588_OVR_FLOW_INT) /* Unlikely and should never happen */
Michael Hennerich88751dd2009-09-17 22:39:38 -0700304 dev_err(&client->dev, "Event Overflow Error\n");
305
Michael Hennerich95716c02010-11-02 11:33:05 -0700306 if (status & ADP5588_KE_INT) {
307 ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & ADP5588_KEC;
Michael Hennerich88751dd2009-09-17 22:39:38 -0700308 if (ev_cnt) {
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700309 adp5588_report_events(kpad, ev_cnt);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700310 input_sync(kpad->input);
311 }
312 }
313 adp5588_write(client, INT_STAT, status); /* Status is W1C */
314}
315
316static irqreturn_t adp5588_irq(int irq, void *handle)
317{
318 struct adp5588_kpad *kpad = handle;
319
320 /*
321 * use keventd context to read the event fifo registers
322 * Schedule readout at least 25ms after notification for
323 * REVID < 4
324 */
325
326 schedule_delayed_work(&kpad->work, kpad->delay);
327
328 return IRQ_HANDLED;
329}
330
Bill Pemberton5298cc42012-11-23 21:38:25 -0800331static int adp5588_setup(struct i2c_client *client)
Michael Hennerich88751dd2009-09-17 22:39:38 -0700332{
Jingoo Hanc838cb32013-12-05 19:21:10 -0800333 const struct adp5588_kpad_platform_data *pdata =
334 dev_get_platdata(&client->dev);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700335 const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data;
Michael Hennerich88751dd2009-09-17 22:39:38 -0700336 int i, ret;
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700337 unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
Michael Hennerich88751dd2009-09-17 22:39:38 -0700338
339 ret = adp5588_write(client, KP_GPIO1, KP_SEL(pdata->rows));
340 ret |= adp5588_write(client, KP_GPIO2, KP_SEL(pdata->cols) & 0xFF);
341 ret |= adp5588_write(client, KP_GPIO3, KP_SEL(pdata->cols) >> 8);
342
343 if (pdata->en_keylock) {
344 ret |= adp5588_write(client, UNLOCK1, pdata->unlock_key1);
345 ret |= adp5588_write(client, UNLOCK2, pdata->unlock_key2);
Michael Hennerich95716c02010-11-02 11:33:05 -0700346 ret |= adp5588_write(client, KEY_LCK_EC_STAT, ADP5588_K_LCK_EN);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700347 }
348
349 for (i = 0; i < KEYP_MAX_EVENT; i++)
350 ret |= adp5588_read(client, Key_EVENTA);
351
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700352 for (i = 0; i < pdata->gpimapsize; i++) {
353 unsigned short pin = pdata->gpimap[i].pin;
354
355 if (pin <= GPI_PIN_ROW_END) {
356 evt_mode1 |= (1 << (pin - GPI_PIN_ROW_BASE));
357 } else {
358 evt_mode2 |= ((1 << (pin - GPI_PIN_COL_BASE)) & 0xFF);
359 evt_mode3 |= ((1 << (pin - GPI_PIN_COL_BASE)) >> 8);
360 }
361 }
362
363 if (pdata->gpimapsize) {
364 ret |= adp5588_write(client, GPI_EM1, evt_mode1);
365 ret |= adp5588_write(client, GPI_EM2, evt_mode2);
366 ret |= adp5588_write(client, GPI_EM3, evt_mode3);
367 }
368
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700369 if (gpio_data) {
Michael Hennerich95716c02010-11-02 11:33:05 -0700370 for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700371 int pull_mask = gpio_data->pullup_dis_mask;
372
373 ret |= adp5588_write(client, GPIO_PULL1 + i,
374 (pull_mask >> (8 * i)) & 0xFF);
375 }
376 }
377
Michael Hennerich95716c02010-11-02 11:33:05 -0700378 ret |= adp5588_write(client, INT_STAT,
379 ADP5588_CMP2_INT | ADP5588_CMP1_INT |
380 ADP5588_OVR_FLOW_INT | ADP5588_K_LCK_INT |
381 ADP5588_GPI_INT | ADP5588_KE_INT); /* Status is W1C */
Michael Hennerich88751dd2009-09-17 22:39:38 -0700382
Michael Hennerich95716c02010-11-02 11:33:05 -0700383 ret |= adp5588_write(client, CFG, ADP5588_INT_CFG |
384 ADP5588_OVR_FLOW_IEN |
385 ADP5588_KE_IEN);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700386
387 if (ret < 0) {
388 dev_err(&client->dev, "Write Error\n");
389 return ret;
390 }
391
392 return 0;
393}
394
Bill Pemberton5298cc42012-11-23 21:38:25 -0800395static void adp5588_report_switch_state(struct adp5588_kpad *kpad)
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700396{
397 int gpi_stat1 = adp5588_read(kpad->client, GPIO_DAT_STAT1);
398 int gpi_stat2 = adp5588_read(kpad->client, GPIO_DAT_STAT2);
399 int gpi_stat3 = adp5588_read(kpad->client, GPIO_DAT_STAT3);
400 int gpi_stat_tmp, pin_loc;
401 int i;
402
403 for (i = 0; i < kpad->gpimapsize; i++) {
404 unsigned short pin = kpad->gpimap[i].pin;
405
406 if (pin <= GPI_PIN_ROW_END) {
407 gpi_stat_tmp = gpi_stat1;
408 pin_loc = pin - GPI_PIN_ROW_BASE;
409 } else if ((pin - GPI_PIN_COL_BASE) < 8) {
410 gpi_stat_tmp = gpi_stat2;
411 pin_loc = pin - GPI_PIN_COL_BASE;
412 } else {
413 gpi_stat_tmp = gpi_stat3;
414 pin_loc = pin - GPI_PIN_COL_BASE - 8;
415 }
416
417 if (gpi_stat_tmp < 0) {
418 dev_err(&kpad->client->dev,
419 "Can't read GPIO_DAT_STAT switch %d default to OFF\n",
420 pin);
421 gpi_stat_tmp = 0;
422 }
423
424 input_report_switch(kpad->input,
425 kpad->gpimap[i].sw_evt,
426 !(gpi_stat_tmp & (1 << pin_loc)));
427 }
428
429 input_sync(kpad->input);
430}
431
432
Bill Pemberton5298cc42012-11-23 21:38:25 -0800433static int adp5588_probe(struct i2c_client *client,
434 const struct i2c_device_id *id)
Michael Hennerich88751dd2009-09-17 22:39:38 -0700435{
436 struct adp5588_kpad *kpad;
Jingoo Hanc838cb32013-12-05 19:21:10 -0800437 const struct adp5588_kpad_platform_data *pdata =
438 dev_get_platdata(&client->dev);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700439 struct input_dev *input;
440 unsigned int revid;
441 int ret, i;
442 int error;
443
444 if (!i2c_check_functionality(client->adapter,
445 I2C_FUNC_SMBUS_BYTE_DATA)) {
446 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
447 return -EIO;
448 }
449
450 if (!pdata) {
451 dev_err(&client->dev, "no platform data?\n");
452 return -EINVAL;
453 }
454
455 if (!pdata->rows || !pdata->cols || !pdata->keymap) {
456 dev_err(&client->dev, "no rows, cols or keymap from pdata\n");
457 return -EINVAL;
458 }
459
460 if (pdata->keymapsize != ADP5588_KEYMAPSIZE) {
461 dev_err(&client->dev, "invalid keymapsize\n");
462 return -EINVAL;
463 }
464
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700465 if (!pdata->gpimap && pdata->gpimapsize) {
466 dev_err(&client->dev, "invalid gpimap from pdata\n");
467 return -EINVAL;
468 }
469
470 if (pdata->gpimapsize > ADP5588_GPIMAPSIZE_MAX) {
471 dev_err(&client->dev, "invalid gpimapsize\n");
472 return -EINVAL;
473 }
474
475 for (i = 0; i < pdata->gpimapsize; i++) {
476 unsigned short pin = pdata->gpimap[i].pin;
477
478 if (pin < GPI_PIN_BASE || pin > GPI_PIN_END) {
479 dev_err(&client->dev, "invalid gpi pin data\n");
480 return -EINVAL;
481 }
482
483 if (pin <= GPI_PIN_ROW_END) {
484 if (pin - GPI_PIN_ROW_BASE + 1 <= pdata->rows) {
485 dev_err(&client->dev, "invalid gpi row data\n");
486 return -EINVAL;
487 }
488 } else {
489 if (pin - GPI_PIN_COL_BASE + 1 <= pdata->cols) {
490 dev_err(&client->dev, "invalid gpi col data\n");
491 return -EINVAL;
492 }
493 }
494 }
495
Michael Hennerich88751dd2009-09-17 22:39:38 -0700496 if (!client->irq) {
497 dev_err(&client->dev, "no IRQ?\n");
498 return -EINVAL;
499 }
500
501 kpad = kzalloc(sizeof(*kpad), GFP_KERNEL);
502 input = input_allocate_device();
503 if (!kpad || !input) {
504 error = -ENOMEM;
505 goto err_free_mem;
506 }
507
508 kpad->client = client;
509 kpad->input = input;
510 INIT_DELAYED_WORK(&kpad->work, adp5588_work);
511
512 ret = adp5588_read(client, DEV_ID);
513 if (ret < 0) {
514 error = ret;
515 goto err_free_mem;
516 }
517
518 revid = (u8) ret & ADP5588_DEVICE_ID_MASK;
519 if (WA_DELAYED_READOUT_REVID(revid))
520 kpad->delay = msecs_to_jiffies(30);
521
522 input->name = client->name;
523 input->phys = "adp5588-keys/input0";
524 input->dev.parent = &client->dev;
525
526 input_set_drvdata(input, kpad);
527
528 input->id.bustype = BUS_I2C;
529 input->id.vendor = 0x0001;
530 input->id.product = 0x0001;
531 input->id.version = revid;
532
533 input->keycodesize = sizeof(kpad->keycode[0]);
534 input->keycodemax = pdata->keymapsize;
535 input->keycode = kpad->keycode;
536
537 memcpy(kpad->keycode, pdata->keymap,
538 pdata->keymapsize * input->keycodesize);
539
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700540 kpad->gpimap = pdata->gpimap;
541 kpad->gpimapsize = pdata->gpimapsize;
542
Michael Hennerich88751dd2009-09-17 22:39:38 -0700543 /* setup input device */
544 __set_bit(EV_KEY, input->evbit);
545
546 if (pdata->repeat)
547 __set_bit(EV_REP, input->evbit);
548
549 for (i = 0; i < input->keycodemax; i++)
Andrew Liue4cfb032013-11-23 10:06:36 -0800550 if (kpad->keycode[i] <= KEY_MAX)
551 __set_bit(kpad->keycode[i], input->keybit);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700552 __clear_bit(KEY_RESERVED, input->keybit);
553
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700554 if (kpad->gpimapsize)
555 __set_bit(EV_SW, input->evbit);
556 for (i = 0; i < kpad->gpimapsize; i++)
557 __set_bit(kpad->gpimap[i].sw_evt, input->swbit);
558
Michael Hennerich88751dd2009-09-17 22:39:38 -0700559 error = input_register_device(input);
560 if (error) {
561 dev_err(&client->dev, "unable to register input device\n");
562 goto err_free_mem;
563 }
564
565 error = request_irq(client->irq, adp5588_irq,
Yong Zhangec4665c2011-09-07 14:04:16 -0700566 IRQF_TRIGGER_FALLING,
Michael Hennerich88751dd2009-09-17 22:39:38 -0700567 client->dev.driver->name, kpad);
568 if (error) {
569 dev_err(&client->dev, "irq %d busy?\n", client->irq);
570 goto err_unreg_dev;
571 }
572
573 error = adp5588_setup(client);
574 if (error)
575 goto err_free_irq;
576
Xiaolong CHEN69a4af62010-06-24 19:10:40 -0700577 if (kpad->gpimapsize)
578 adp5588_report_switch_state(kpad);
579
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700580 error = adp5588_gpio_add(kpad);
Xiaolong Chenba9f5072010-07-26 01:01:11 -0700581 if (error)
582 goto err_free_irq;
583
Michael Hennerich88751dd2009-09-17 22:39:38 -0700584 device_init_wakeup(&client->dev, 1);
585 i2c_set_clientdata(client, kpad);
586
587 dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
588 return 0;
589
590 err_free_irq:
591 free_irq(client->irq, kpad);
592 err_unreg_dev:
593 input_unregister_device(input);
594 input = NULL;
595 err_free_mem:
596 input_free_device(input);
597 kfree(kpad);
598
599 return error;
600}
601
Bill Pembertone2619cf2012-11-23 21:50:47 -0800602static int adp5588_remove(struct i2c_client *client)
Michael Hennerich88751dd2009-09-17 22:39:38 -0700603{
604 struct adp5588_kpad *kpad = i2c_get_clientdata(client);
605
606 adp5588_write(client, CFG, 0);
607 free_irq(client->irq, kpad);
608 cancel_delayed_work_sync(&kpad->work);
609 input_unregister_device(kpad->input);
Dmitry Torokhov0d87c722010-08-02 18:33:26 -0700610 adp5588_gpio_remove(kpad);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700611 kfree(kpad);
612
613 return 0;
614}
615
616#ifdef CONFIG_PM
617static int adp5588_suspend(struct device *dev)
618{
619 struct adp5588_kpad *kpad = dev_get_drvdata(dev);
620 struct i2c_client *client = kpad->client;
621
622 disable_irq(client->irq);
623 cancel_delayed_work_sync(&kpad->work);
624
625 if (device_may_wakeup(&client->dev))
626 enable_irq_wake(client->irq);
627
628 return 0;
629}
630
631static int adp5588_resume(struct device *dev)
632{
633 struct adp5588_kpad *kpad = dev_get_drvdata(dev);
634 struct i2c_client *client = kpad->client;
635
636 if (device_may_wakeup(&client->dev))
637 disable_irq_wake(client->irq);
638
639 enable_irq(client->irq);
640
641 return 0;
642}
643
Alexey Dobriyan47145212009-12-14 18:00:08 -0800644static const struct dev_pm_ops adp5588_dev_pm_ops = {
Michael Hennerich88751dd2009-09-17 22:39:38 -0700645 .suspend = adp5588_suspend,
646 .resume = adp5588_resume,
647};
648#endif
649
650static const struct i2c_device_id adp5588_id[] = {
Michael Hennerichd5371552010-10-18 17:46:03 -0700651 { "adp5588-keys", 0 },
Michael Hennerich5a9003d2010-01-19 00:28:44 -0800652 { "adp5587-keys", 0 },
Michael Hennerich88751dd2009-09-17 22:39:38 -0700653 { }
654};
655MODULE_DEVICE_TABLE(i2c, adp5588_id);
656
657static struct i2c_driver adp5588_driver = {
658 .driver = {
659 .name = KBUILD_MODNAME,
660#ifdef CONFIG_PM
661 .pm = &adp5588_dev_pm_ops,
662#endif
663 },
664 .probe = adp5588_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800665 .remove = adp5588_remove,
Michael Hennerich88751dd2009-09-17 22:39:38 -0700666 .id_table = adp5588_id,
667};
668
Axel Lin1b92c1c2012-03-16 23:05:41 -0700669module_i2c_driver(adp5588_driver);
Michael Hennerich88751dd2009-09-17 22:39:38 -0700670
671MODULE_LICENSE("GPL");
672MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
Michael Hennerich5a9003d2010-01-19 00:28:44 -0800673MODULE_DESCRIPTION("ADP5588/87 Keypad driver");