aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Hennerich <michael.hennerich@analog.com>2011-05-19 03:59:32 -0400
committerDmitry Torokhov <dmitry.torokhov@gmail.com>2011-05-19 03:59:55 -0400
commit9d2e173644bb5c42ff1b280fbdda3f195a7cf1f7 (patch)
tree723fa12e095a7dc00a02cb158028aa3f4ef2fc8a
parent891e376b1e864519bf7e49fa741f473078318530 (diff)
Input: ADP5589 - new driver for I2C Keypad Decoder and I/O Expander
From http://www.analog.com/ADP5589: The ADP5589 is an I/O port expander and keypad matrix decoder designed for QWERTY type phones that require a large keypad matrix and expanded I/O lines. Signed-off-by: Michael Hennerich <michael.hennerich@analog.com> Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
-rw-r--r--drivers/input/keyboard/Kconfig10
-rw-r--r--drivers/input/keyboard/Makefile1
-rw-r--r--drivers/input/keyboard/adp5589-keys.c771
-rw-r--r--include/linux/input/adp5589.h213
4 files changed, 995 insertions, 0 deletions
diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index dec090ac7d31..69badb4e06aa 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -32,6 +32,16 @@ config KEYBOARD_ADP5588
32 To compile this driver as a module, choose M here: the 32 To compile this driver as a module, choose M here: the
33 module will be called adp5588-keys. 33 module will be called adp5588-keys.
34 34
35config KEYBOARD_ADP5589
36 tristate "ADP5589 I2C QWERTY Keypad and IO Expander"
37 depends on I2C
38 help
39 Say Y here if you want to use a ADP5589 attached to your
40 system I2C bus.
41
42 To compile this driver as a module, choose M here: the
43 module will be called adp5589-keys.
44
35config KEYBOARD_AMIGA 45config KEYBOARD_AMIGA
36 tristate "Amiga keyboard" 46 tristate "Amiga keyboard"
37 depends on AMIGA 47 depends on AMIGA
diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
index b45361009f2e..c49cf8e04cd7 100644
--- a/drivers/input/keyboard/Makefile
+++ b/drivers/input/keyboard/Makefile
@@ -6,6 +6,7 @@
6 6
7obj-$(CONFIG_KEYBOARD_ADP5520) += adp5520-keys.o 7obj-$(CONFIG_KEYBOARD_ADP5520) += adp5520-keys.o
8obj-$(CONFIG_KEYBOARD_ADP5588) += adp5588-keys.o 8obj-$(CONFIG_KEYBOARD_ADP5588) += adp5588-keys.o
9obj-$(CONFIG_KEYBOARD_ADP5589) += adp5589-keys.o
9obj-$(CONFIG_KEYBOARD_AMIGA) += amikbd.o 10obj-$(CONFIG_KEYBOARD_AMIGA) += amikbd.o
10obj-$(CONFIG_KEYBOARD_ATARI) += atakbd.o 11obj-$(CONFIG_KEYBOARD_ATARI) += atakbd.o
11obj-$(CONFIG_KEYBOARD_ATKBD) += atkbd.o 12obj-$(CONFIG_KEYBOARD_ATKBD) += atkbd.o
diff --git a/drivers/input/keyboard/adp5589-keys.c b/drivers/input/keyboard/adp5589-keys.c
new file mode 100644
index 000000000000..631598663aab
--- /dev/null
+++ b/drivers/input/keyboard/adp5589-keys.c
@@ -0,0 +1,771 @@
1/*
2 * Description: keypad driver for ADP5589
3 * I2C QWERTY Keypad and IO Expander
4 * Bugs: Enter bugs at http://blackfin.uclinux.org/
5 *
6 * Copyright (C) 2010-2011 Analog Devices Inc.
7 * Licensed under the GPL-2.
8 */
9
10#include <linux/module.h>
11#include <linux/version.h>
12#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>
21#include <linux/gpio.h>
22#include <linux/slab.h>
23
24#include <linux/input/adp5589.h>
25
26/* GENERAL_CFG Register */
27#define OSC_EN (1 << 7)
28#define CORE_CLK(x) (((x) & 0x3) << 5)
29#define LCK_TRK_LOGIC (1 << 4)
30#define LCK_TRK_GPI (1 << 3)
31#define INT_CFG (1 << 1)
32#define RST_CFG (1 << 0)
33
34/* INT_EN Register */
35#define LOGIC2_IEN (1 << 5)
36#define LOGIC1_IEN (1 << 4)
37#define LOCK_IEN (1 << 3)
38#define OVRFLOW_IEN (1 << 2)
39#define GPI_IEN (1 << 1)
40#define EVENT_IEN (1 << 0)
41
42/* Interrupt Status Register */
43#define LOGIC2_INT (1 << 5)
44#define LOGIC1_INT (1 << 4)
45#define LOCK_INT (1 << 3)
46#define OVRFLOW_INT (1 << 2)
47#define GPI_INT (1 << 1)
48#define EVENT_INT (1 << 0)
49
50/* STATUS Register */
51
52#define LOGIC2_STAT (1 << 7)
53#define LOGIC1_STAT (1 << 6)
54#define LOCK_STAT (1 << 5)
55#define KEC 0xF
56
57/* PIN_CONFIG_D Register */
58#define C4_EXTEND_CFG (1 << 6) /* RESET2 */
59#define R4_EXTEND_CFG (1 << 5) /* RESET1 */
60
61/* LOCK_CFG */
62#define LOCK_EN (1 << 0)
63
64#define PTIME_MASK 0x3
65#define LTIME_MASK 0x3
66
67/* Key Event Register xy */
68#define KEY_EV_PRESSED (1 << 7)
69#define KEY_EV_MASK (0x7F)
70
71#define KEYP_MAX_EVENT 16
72
73#define MAXGPIO 19
74#define ADP_BANK(offs) ((offs) >> 3)
75#define ADP_BIT(offs) (1u << ((offs) & 0x7))
76
77struct adp5589_kpad {
78 struct i2c_client *client;
79 struct input_dev *input;
80 unsigned short keycode[ADP5589_KEYMAPSIZE];
81 const struct adp5589_gpi_map *gpimap;
82 unsigned short gpimapsize;
83 unsigned extend_cfg;
84#ifdef CONFIG_GPIOLIB
85 unsigned char gpiomap[MAXGPIO];
86 bool export_gpio;
87 struct gpio_chip gc;
88 struct mutex gpio_lock; /* Protect cached dir, dat_out */
89 u8 dat_out[3];
90 u8 dir[3];
91#endif
92};
93
94static int adp5589_read(struct i2c_client *client, u8 reg)
95{
96 int ret = i2c_smbus_read_byte_data(client, reg);
97
98 if (ret < 0)
99 dev_err(&client->dev, "Read Error\n");
100
101 return ret;
102}
103
104static int adp5589_write(struct i2c_client *client, u8 reg, u8 val)
105{
106 return i2c_smbus_write_byte_data(client, reg, val);
107}
108
109#ifdef CONFIG_GPIOLIB
110static int adp5589_gpio_get_value(struct gpio_chip *chip, unsigned off)
111{
112 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
113 unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
114 unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
115
116 return !!(adp5589_read(kpad->client, ADP5589_GPI_STATUS_A + bank) &
117 bit);
118}
119
120static void adp5589_gpio_set_value(struct gpio_chip *chip,
121 unsigned off, int val)
122{
123 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
124 unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
125 unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
126
127 mutex_lock(&kpad->gpio_lock);
128
129 if (val)
130 kpad->dat_out[bank] |= bit;
131 else
132 kpad->dat_out[bank] &= ~bit;
133
134 adp5589_write(kpad->client, ADP5589_GPO_DATA_OUT_A + bank,
135 kpad->dat_out[bank]);
136
137 mutex_unlock(&kpad->gpio_lock);
138}
139
140static int adp5589_gpio_direction_input(struct gpio_chip *chip, unsigned off)
141{
142 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
143 unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
144 unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
145 int ret;
146
147 mutex_lock(&kpad->gpio_lock);
148
149 kpad->dir[bank] &= ~bit;
150 ret = adp5589_write(kpad->client, ADP5589_GPIO_DIRECTION_A + bank,
151 kpad->dir[bank]);
152
153 mutex_unlock(&kpad->gpio_lock);
154
155 return ret;
156}
157
158static int adp5589_gpio_direction_output(struct gpio_chip *chip,
159 unsigned off, int val)
160{
161 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
162 unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
163 unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
164 int ret;
165
166 mutex_lock(&kpad->gpio_lock);
167
168 kpad->dir[bank] |= bit;
169
170 if (val)
171 kpad->dat_out[bank] |= bit;
172 else
173 kpad->dat_out[bank] &= ~bit;
174
175 ret = adp5589_write(kpad->client, ADP5589_GPO_DATA_OUT_A + bank,
176 kpad->dat_out[bank]);
177 ret |= adp5589_write(kpad->client, ADP5589_GPIO_DIRECTION_A + bank,
178 kpad->dir[bank]);
179
180 mutex_unlock(&kpad->gpio_lock);
181
182 return ret;
183}
184
185static int __devinit adp5589_build_gpiomap(struct adp5589_kpad *kpad,
186 const struct adp5589_kpad_platform_data *pdata)
187{
188 bool pin_used[MAXGPIO];
189 int n_unused = 0;
190 int i;
191
192 memset(pin_used, false, sizeof(pin_used));
193
194 for (i = 0; i < MAXGPIO; i++)
195 if (pdata->keypad_en_mask & (1 << i))
196 pin_used[i] = true;
197
198 for (i = 0; i < kpad->gpimapsize; i++)
199 pin_used[kpad->gpimap[i].pin - ADP5589_GPI_PIN_BASE] = true;
200
201 if (kpad->extend_cfg & R4_EXTEND_CFG)
202 pin_used[4] = true;
203
204 if (kpad->extend_cfg & C4_EXTEND_CFG)
205 pin_used[12] = true;
206
207 for (i = 0; i < MAXGPIO; i++)
208 if (!pin_used[i])
209 kpad->gpiomap[n_unused++] = i;
210
211 return n_unused;
212}
213
214static int __devinit adp5589_gpio_add(struct adp5589_kpad *kpad)
215{
216 struct device *dev = &kpad->client->dev;
217 const struct adp5589_kpad_platform_data *pdata = dev->platform_data;
218 const struct adp5589_gpio_platform_data *gpio_data = pdata->gpio_data;
219 int i, error;
220
221 if (!gpio_data)
222 return 0;
223
224 kpad->gc.ngpio = adp5589_build_gpiomap(kpad, pdata);
225 if (kpad->gc.ngpio == 0) {
226 dev_info(dev, "No unused gpios left to export\n");
227 return 0;
228 }
229
230 kpad->export_gpio = true;
231
232 kpad->gc.direction_input = adp5589_gpio_direction_input;
233 kpad->gc.direction_output = adp5589_gpio_direction_output;
234 kpad->gc.get = adp5589_gpio_get_value;
235 kpad->gc.set = adp5589_gpio_set_value;
236 kpad->gc.can_sleep = 1;
237
238 kpad->gc.base = gpio_data->gpio_start;
239 kpad->gc.label = kpad->client->name;
240 kpad->gc.owner = THIS_MODULE;
241
242 mutex_init(&kpad->gpio_lock);
243
244 error = gpiochip_add(&kpad->gc);
245 if (error) {
246 dev_err(dev, "gpiochip_add failed, err: %d\n", error);
247 return error;
248 }
249
250 for (i = 0; i <= ADP_BANK(MAXGPIO); i++) {
251 kpad->dat_out[i] = adp5589_read(kpad->client,
252 ADP5589_GPO_DATA_OUT_A + i);
253 kpad->dir[i] = adp5589_read(kpad->client,
254 ADP5589_GPIO_DIRECTION_A + i);
255 }
256
257 if (gpio_data->setup) {
258 error = gpio_data->setup(kpad->client,
259 kpad->gc.base, kpad->gc.ngpio,
260 gpio_data->context);
261 if (error)
262 dev_warn(dev, "setup failed, %d\n", error);
263 }
264
265 return 0;
266}
267
268static void __devexit adp5589_gpio_remove(struct adp5589_kpad *kpad)
269{
270 struct device *dev = &kpad->client->dev;
271 const struct adp5589_kpad_platform_data *pdata = dev->platform_data;
272 const struct adp5589_gpio_platform_data *gpio_data = pdata->gpio_data;
273 int error;
274
275 if (!kpad->export_gpio)
276 return;
277
278 if (gpio_data->teardown) {
279 error = gpio_data->teardown(kpad->client,
280 kpad->gc.base, kpad->gc.ngpio,
281 gpio_data->context);
282 if (error)
283 dev_warn(dev, "teardown failed %d\n", error);
284 }
285
286 error = gpiochip_remove(&kpad->gc);
287 if (error)
288 dev_warn(dev, "gpiochip_remove failed %d\n", error);
289}
290#else
291static inline int adp5589_gpio_add(struct adp5589_kpad *kpad)
292{
293 return 0;
294}
295
296static inline void adp5589_gpio_remove(struct adp5589_kpad *kpad)
297{
298}
299#endif
300
301static void adp5589_report_switches(struct adp5589_kpad *kpad,
302 int key, int key_val)
303{
304 int i;
305
306 for (i = 0; i < kpad->gpimapsize; i++) {
307 if (key_val == kpad->gpimap[i].pin) {
308 input_report_switch(kpad->input,
309 kpad->gpimap[i].sw_evt,
310 key & KEY_EV_PRESSED);
311 break;
312 }
313 }
314}
315
316static void adp5589_report_events(struct adp5589_kpad *kpad, int ev_cnt)
317{
318 int i;
319
320 for (i = 0; i < ev_cnt; i++) {
321 int key = adp5589_read(kpad->client, ADP5589_FIFO_1 + i);
322 int key_val = key & KEY_EV_MASK;
323
324 if (key_val >= ADP5589_GPI_PIN_BASE &&
325 key_val <= ADP5589_GPI_PIN_END) {
326 adp5589_report_switches(kpad, key, key_val);
327 } else {
328 input_report_key(kpad->input,
329 kpad->keycode[key_val - 1],
330 key & KEY_EV_PRESSED);
331 }
332 }
333}
334
335static irqreturn_t adp5589_irq(int irq, void *handle)
336{
337 struct adp5589_kpad *kpad = handle;
338 struct i2c_client *client = kpad->client;
339 int status, ev_cnt;
340
341 status = adp5589_read(client, ADP5589_INT_STATUS);
342
343 if (status & OVRFLOW_INT) /* Unlikely and should never happen */
344 dev_err(&client->dev, "Event Overflow Error\n");
345
346 if (status & EVENT_INT) {
347 ev_cnt = adp5589_read(client, ADP5589_STATUS) & KEC;
348 if (ev_cnt) {
349 adp5589_report_events(kpad, ev_cnt);
350 input_sync(kpad->input);
351 }
352 }
353
354 adp5589_write(client, ADP5589_INT_STATUS, status); /* Status is W1C */
355
356 return IRQ_HANDLED;
357}
358
359static int __devinit adp5589_get_evcode(struct adp5589_kpad *kpad, unsigned short key)
360{
361 int i;
362
363 for (i = 0; i < ADP5589_KEYMAPSIZE; i++)
364 if (key == kpad->keycode[i])
365 return (i + 1) | KEY_EV_PRESSED;
366
367 dev_err(&kpad->client->dev, "RESET/UNLOCK key not in keycode map\n");
368
369 return -EINVAL;
370}
371
372static int __devinit adp5589_setup(struct adp5589_kpad *kpad)
373{
374 struct i2c_client *client = kpad->client;
375 const struct adp5589_kpad_platform_data *pdata =
376 client->dev.platform_data;
377 int i, ret;
378 unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
379 unsigned char pull_mask = 0;
380
381 ret = adp5589_write(client, ADP5589_PIN_CONFIG_A,
382 pdata->keypad_en_mask & 0xFF);
383 ret |= adp5589_write(client, ADP5589_PIN_CONFIG_B,
384 (pdata->keypad_en_mask >> 8) & 0xFF);
385 ret |= adp5589_write(client, ADP5589_PIN_CONFIG_C,
386 (pdata->keypad_en_mask >> 16) & 0xFF);
387
388 if (pdata->en_keylock) {
389 ret |= adp5589_write(client, ADP5589_UNLOCK1,
390 pdata->unlock_key1);
391 ret |= adp5589_write(client, ADP5589_UNLOCK2,
392 pdata->unlock_key2);
393 ret |= adp5589_write(client, ADP5589_UNLOCK_TIMERS,
394 pdata->unlock_timer & LTIME_MASK);
395 ret |= adp5589_write(client, ADP5589_LOCK_CFG, LOCK_EN);
396 }
397
398 for (i = 0; i < KEYP_MAX_EVENT; i++)
399 ret |= adp5589_read(client, ADP5589_FIFO_1 + i);
400
401 for (i = 0; i < pdata->gpimapsize; i++) {
402 unsigned short pin = pdata->gpimap[i].pin;
403
404 if (pin <= ADP5589_GPI_PIN_ROW_END) {
405 evt_mode1 |= (1 << (pin - ADP5589_GPI_PIN_ROW_BASE));
406 } else {
407 evt_mode2 |=
408 ((1 << (pin - ADP5589_GPI_PIN_COL_BASE)) & 0xFF);
409 evt_mode3 |=
410 ((1 << (pin - ADP5589_GPI_PIN_COL_BASE)) >> 8);
411 }
412 }
413
414 if (pdata->gpimapsize) {
415 ret |= adp5589_write(client, ADP5589_GPI_EVENT_EN_A, evt_mode1);
416 ret |= adp5589_write(client, ADP5589_GPI_EVENT_EN_B, evt_mode2);
417 ret |= adp5589_write(client, ADP5589_GPI_EVENT_EN_C, evt_mode3);
418 }
419
420 if (pdata->pull_dis_mask & pdata->pullup_en_100k &
421 pdata->pullup_en_300k & pdata->pulldown_en_300k)
422 dev_warn(&client->dev, "Conflicting pull resistor config\n");
423
424 for (i = 0; i < MAXGPIO; i++) {
425 unsigned val = 0;
426
427 if (pdata->pullup_en_300k & (1 << i))
428 val = 0;
429 else if (pdata->pulldown_en_300k & (1 << i))
430 val = 1;
431 else if (pdata->pullup_en_100k & (1 << i))
432 val = 2;
433 else if (pdata->pull_dis_mask & (1 << i))
434 val = 3;
435
436 pull_mask |= val << (2 * (i & 0x3));
437
438 if ((i & 0x3) == 0x3 || i == MAXGPIO - 1) {
439 ret |= adp5589_write(client,
440 ADP5589_RPULL_CONFIG_A + (i >> 2),
441 pull_mask);
442 pull_mask = 0;
443 }
444 }
445
446 if (pdata->reset1_key_1 && pdata->reset1_key_2 && pdata->reset1_key_3) {
447 ret |= adp5589_write(client, ADP5589_RESET1_EVENT_A,
448 adp5589_get_evcode(kpad,
449 pdata->reset1_key_1));
450 ret |= adp5589_write(client, ADP5589_RESET1_EVENT_B,
451 adp5589_get_evcode(kpad,
452 pdata->reset1_key_2));
453 ret |= adp5589_write(client, ADP5589_RESET1_EVENT_C,
454 adp5589_get_evcode(kpad,
455 pdata->reset1_key_3));
456 kpad->extend_cfg |= R4_EXTEND_CFG;
457 }
458
459 if (pdata->reset2_key_1 && pdata->reset2_key_2) {
460 ret |= adp5589_write(client, ADP5589_RESET2_EVENT_A,
461 adp5589_get_evcode(kpad,
462 pdata->reset2_key_1));
463 ret |= adp5589_write(client, ADP5589_RESET2_EVENT_B,
464 adp5589_get_evcode(kpad,
465 pdata->reset2_key_2));
466 kpad->extend_cfg |= C4_EXTEND_CFG;
467 }
468
469 if (kpad->extend_cfg) {
470 ret |= adp5589_write(client, ADP5589_RESET_CFG,
471 pdata->reset_cfg);
472 ret |= adp5589_write(client, ADP5589_PIN_CONFIG_D,
473 kpad->extend_cfg);
474 }
475
476 for (i = 0; i <= ADP_BANK(MAXGPIO); i++)
477 ret |= adp5589_write(client, ADP5589_DEBOUNCE_DIS_A + i,
478 pdata->debounce_dis_mask >> (i * 8));
479
480 ret |= adp5589_write(client, ADP5589_POLL_PTIME_CFG,
481 pdata->scan_cycle_time & PTIME_MASK);
482 ret |= adp5589_write(client, ADP5589_INT_STATUS, LOGIC2_INT |
483 LOGIC1_INT | OVRFLOW_INT | LOCK_INT |
484 GPI_INT | EVENT_INT); /* Status is W1C */
485
486 ret |= adp5589_write(client, ADP5589_GENERAL_CFG,
487 INT_CFG | OSC_EN | CORE_CLK(3));
488 ret |= adp5589_write(client, ADP5589_INT_EN,
489 OVRFLOW_IEN | GPI_IEN | EVENT_IEN);
490
491 if (ret < 0) {
492 dev_err(&client->dev, "Write Error\n");
493 return ret;
494 }
495
496 return 0;
497}
498
499static void __devinit adp5589_report_switch_state(struct adp5589_kpad *kpad)
500{
501 int gpi_stat1 = adp5589_read(kpad->client, ADP5589_GPI_STATUS_A);
502 int gpi_stat2 = adp5589_read(kpad->client, ADP5589_GPI_STATUS_B);
503 int gpi_stat3 = adp5589_read(kpad->client, ADP5589_GPI_STATUS_C);
504 int gpi_stat_tmp, pin_loc;
505 int i;
506
507 for (i = 0; i < kpad->gpimapsize; i++) {
508 unsigned short pin = kpad->gpimap[i].pin;
509
510 if (pin <= ADP5589_GPI_PIN_ROW_END) {
511 gpi_stat_tmp = gpi_stat1;
512 pin_loc = pin - ADP5589_GPI_PIN_ROW_BASE;
513 } else if ((pin - ADP5589_GPI_PIN_COL_BASE) < 8) {
514 gpi_stat_tmp = gpi_stat2;
515 pin_loc = pin - ADP5589_GPI_PIN_COL_BASE;
516 } else {
517 gpi_stat_tmp = gpi_stat3;
518 pin_loc = pin - ADP5589_GPI_PIN_COL_BASE - 8;
519 }
520
521 if (gpi_stat_tmp < 0) {
522 dev_err(&kpad->client->dev,
523 "Can't read GPIO_DAT_STAT switch"
524 " %d default to OFF\n", pin);
525 gpi_stat_tmp = 0;
526 }
527
528 input_report_switch(kpad->input,
529 kpad->gpimap[i].sw_evt,
530 !(gpi_stat_tmp & (1 << pin_loc)));
531 }
532
533 input_sync(kpad->input);
534}
535
536static int __devinit adp5589_probe(struct i2c_client *client,
537 const struct i2c_device_id *id)
538{
539 struct adp5589_kpad *kpad;
540 const struct adp5589_kpad_platform_data *pdata;
541 struct input_dev *input;
542 unsigned int revid;
543 int ret, i;
544 int error;
545
546 if (!i2c_check_functionality(client->adapter,
547 I2C_FUNC_SMBUS_BYTE_DATA)) {
548 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
549 return -EIO;
550 }
551
552 pdata = client->dev.platform_data;
553 if (!pdata) {
554 dev_err(&client->dev, "no platform data?\n");
555 return -EINVAL;
556 }
557
558 if (!((pdata->keypad_en_mask & 0xFF) &&
559 (pdata->keypad_en_mask >> 8)) || !pdata->keymap) {
560 dev_err(&client->dev, "no rows, cols or keymap from pdata\n");
561 return -EINVAL;
562 }
563
564 if (pdata->keymapsize != ADP5589_KEYMAPSIZE) {
565 dev_err(&client->dev, "invalid keymapsize\n");
566 return -EINVAL;
567 }
568
569 if (!pdata->gpimap && pdata->gpimapsize) {
570 dev_err(&client->dev, "invalid gpimap from pdata\n");
571 return -EINVAL;
572 }
573
574 if (pdata->gpimapsize > ADP5589_GPIMAPSIZE_MAX) {
575 dev_err(&client->dev, "invalid gpimapsize\n");
576 return -EINVAL;
577 }
578
579 for (i = 0; i < pdata->gpimapsize; i++) {
580 unsigned short pin = pdata->gpimap[i].pin;
581
582 if (pin < ADP5589_GPI_PIN_BASE || pin > ADP5589_GPI_PIN_END) {
583 dev_err(&client->dev, "invalid gpi pin data\n");
584 return -EINVAL;
585 }
586
587 if ((1 << (pin - ADP5589_GPI_PIN_ROW_BASE)) &
588 pdata->keypad_en_mask) {
589 dev_err(&client->dev, "invalid gpi row/col data\n");
590 return -EINVAL;
591 }
592 }
593
594 if (!client->irq) {
595 dev_err(&client->dev, "no IRQ?\n");
596 return -EINVAL;
597 }
598
599 kpad = kzalloc(sizeof(*kpad), GFP_KERNEL);
600 input = input_allocate_device();
601 if (!kpad || !input) {
602 error = -ENOMEM;
603 goto err_free_mem;
604 }
605
606 kpad->client = client;
607 kpad->input = input;
608
609 ret = adp5589_read(client, ADP5589_ID);
610 if (ret < 0) {
611 error = ret;
612 goto err_free_mem;
613 }
614
615 revid = (u8) ret & ADP5589_DEVICE_ID_MASK;
616
617 input->name = client->name;
618 input->phys = "adp5589-keys/input0";
619 input->dev.parent = &client->dev;
620
621 input_set_drvdata(input, kpad);
622
623 input->id.bustype = BUS_I2C;
624 input->id.vendor = 0x0001;
625 input->id.product = 0x0001;
626 input->id.version = revid;
627
628 input->keycodesize = sizeof(kpad->keycode[0]);
629 input->keycodemax = pdata->keymapsize;
630 input->keycode = kpad->keycode;
631
632 memcpy(kpad->keycode, pdata->keymap,
633 pdata->keymapsize * input->keycodesize);
634
635 kpad->gpimap = pdata->gpimap;
636 kpad->gpimapsize = pdata->gpimapsize;
637
638 /* setup input device */
639 __set_bit(EV_KEY, input->evbit);
640
641 if (pdata->repeat)
642 __set_bit(EV_REP, input->evbit);
643
644 for (i = 0; i < input->keycodemax; i++)
645 __set_bit(kpad->keycode[i] & KEY_MAX, input->keybit);
646 __clear_bit(KEY_RESERVED, input->keybit);
647
648 if (kpad->gpimapsize)
649 __set_bit(EV_SW, input->evbit);
650 for (i = 0; i < kpad->gpimapsize; i++)
651 __set_bit(kpad->gpimap[i].sw_evt, input->swbit);
652
653 error = input_register_device(input);
654 if (error) {
655 dev_err(&client->dev, "unable to register input device\n");
656 goto err_free_mem;
657 }
658
659 error = request_threaded_irq(client->irq, NULL, adp5589_irq,
660 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
661 client->dev.driver->name, kpad);
662 if (error) {
663 dev_err(&client->dev, "irq %d busy?\n", client->irq);
664 goto err_unreg_dev;
665 }
666
667 error = adp5589_setup(kpad);
668 if (error)
669 goto err_free_irq;
670
671 if (kpad->gpimapsize)
672 adp5589_report_switch_state(kpad);
673
674 error = adp5589_gpio_add(kpad);
675 if (error)
676 goto err_free_irq;
677
678 device_init_wakeup(&client->dev, 1);
679 i2c_set_clientdata(client, kpad);
680
681 dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
682 return 0;
683
684err_free_irq:
685 free_irq(client->irq, kpad);
686err_unreg_dev:
687 input_unregister_device(input);
688 input = NULL;
689err_free_mem:
690 input_free_device(input);
691 kfree(kpad);
692
693 return error;
694}
695
696static int __devexit adp5589_remove(struct i2c_client *client)
697{
698 struct adp5589_kpad *kpad = i2c_get_clientdata(client);
699
700 adp5589_write(client, ADP5589_GENERAL_CFG, 0);
701 free_irq(client->irq, kpad);
702 input_unregister_device(kpad->input);
703 adp5589_gpio_remove(kpad);
704 kfree(kpad);
705
706 return 0;
707}
708
709#ifdef CONFIG_PM_SLEEP
710static int adp5589_suspend(struct device *dev)
711{
712 struct adp5589_kpad *kpad = dev_get_drvdata(dev);
713 struct i2c_client *client = kpad->client;
714
715 disable_irq(client->irq);
716
717 if (device_may_wakeup(&client->dev))
718 enable_irq_wake(client->irq);
719
720 return 0;
721}
722
723static int adp5589_resume(struct device *dev)
724{
725 struct adp5589_kpad *kpad = dev_get_drvdata(dev);
726 struct i2c_client *client = kpad->client;
727
728 if (device_may_wakeup(&client->dev))
729 disable_irq_wake(client->irq);
730
731 enable_irq(client->irq);
732
733 return 0;
734}
735#endif
736
737static SIMPLE_DEV_PM_OPS(adp5589_dev_pm_ops, adp5589_suspend, adp5589_resume);
738
739static const struct i2c_device_id adp5589_id[] = {
740 {"adp5589-keys", 0},
741 {}
742};
743
744MODULE_DEVICE_TABLE(i2c, adp5589_id);
745
746static struct i2c_driver adp5589_driver = {
747 .driver = {
748 .name = KBUILD_MODNAME,
749 .owner = THIS_MODULE,
750 .pm = &adp5589_dev_pm_ops,
751 },
752 .probe = adp5589_probe,
753 .remove = __devexit_p(adp5589_remove),
754 .id_table = adp5589_id,
755};
756
757static int __init adp5589_init(void)
758{
759 return i2c_add_driver(&adp5589_driver);
760}
761module_init(adp5589_init);
762
763static void __exit adp5589_exit(void)
764{
765 i2c_del_driver(&adp5589_driver);
766}
767module_exit(adp5589_exit);
768
769MODULE_LICENSE("GPL");
770MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
771MODULE_DESCRIPTION("ADP5589 Keypad driver");
diff --git a/include/linux/input/adp5589.h b/include/linux/input/adp5589.h
new file mode 100644
index 000000000000..ef792ecfaabf
--- /dev/null
+++ b/include/linux/input/adp5589.h
@@ -0,0 +1,213 @@
1/*
2 * Analog Devices ADP5589 I/O Expander and QWERTY Keypad Controller
3 *
4 * Copyright 2010-2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2.
7 */
8
9#ifndef _ADP5589_H
10#define _ADP5589_H
11
12#define ADP5589_ID 0x00
13#define ADP5589_INT_STATUS 0x01
14#define ADP5589_STATUS 0x02
15#define ADP5589_FIFO_1 0x03
16#define ADP5589_FIFO_2 0x04
17#define ADP5589_FIFO_3 0x05
18#define ADP5589_FIFO_4 0x06
19#define ADP5589_FIFO_5 0x07
20#define ADP5589_FIFO_6 0x08
21#define ADP5589_FIFO_7 0x09
22#define ADP5589_FIFO_8 0x0A
23#define ADP5589_FIFO_9 0x0B
24#define ADP5589_FIFO_10 0x0C
25#define ADP5589_FIFO_11 0x0D
26#define ADP5589_FIFO_12 0x0E
27#define ADP5589_FIFO_13 0x0F
28#define ADP5589_FIFO_14 0x10
29#define ADP5589_FIFO_15 0x11
30#define ADP5589_FIFO_16 0x12
31#define ADP5589_GPI_INT_STAT_A 0x13
32#define ADP5589_GPI_INT_STAT_B 0x14
33#define ADP5589_GPI_INT_STAT_C 0x15
34#define ADP5589_GPI_STATUS_A 0x16
35#define ADP5589_GPI_STATUS_B 0x17
36#define ADP5589_GPI_STATUS_C 0x18
37#define ADP5589_RPULL_CONFIG_A 0x19
38#define ADP5589_RPULL_CONFIG_B 0x1A
39#define ADP5589_RPULL_CONFIG_C 0x1B
40#define ADP5589_RPULL_CONFIG_D 0x1C
41#define ADP5589_RPULL_CONFIG_E 0x1D
42#define ADP5589_GPI_INT_LEVEL_A 0x1E
43#define ADP5589_GPI_INT_LEVEL_B 0x1F
44#define ADP5589_GPI_INT_LEVEL_C 0x20
45#define ADP5589_GPI_EVENT_EN_A 0x21
46#define ADP5589_GPI_EVENT_EN_B 0x22
47#define ADP5589_GPI_EVENT_EN_C 0x23
48#define ADP5589_GPI_INTERRUPT_EN_A 0x24
49#define ADP5589_GPI_INTERRUPT_EN_B 0x25
50#define ADP5589_GPI_INTERRUPT_EN_C 0x26
51#define ADP5589_DEBOUNCE_DIS_A 0x27
52#define ADP5589_DEBOUNCE_DIS_B 0x28
53#define ADP5589_DEBOUNCE_DIS_C 0x29
54#define ADP5589_GPO_DATA_OUT_A 0x2A
55#define ADP5589_GPO_DATA_OUT_B 0x2B
56#define ADP5589_GPO_DATA_OUT_C 0x2C
57#define ADP5589_GPO_OUT_MODE_A 0x2D
58#define ADP5589_GPO_OUT_MODE_B 0x2E
59#define ADP5589_GPO_OUT_MODE_C 0x2F
60#define ADP5589_GPIO_DIRECTION_A 0x30
61#define ADP5589_GPIO_DIRECTION_B 0x31
62#define ADP5589_GPIO_DIRECTION_C 0x32
63#define ADP5589_UNLOCK1 0x33
64#define ADP5589_UNLOCK2 0x34
65#define ADP5589_EXT_LOCK_EVENT 0x35
66#define ADP5589_UNLOCK_TIMERS 0x36
67#define ADP5589_LOCK_CFG 0x37
68#define ADP5589_RESET1_EVENT_A 0x38
69#define ADP5589_RESET1_EVENT_B 0x39
70#define ADP5589_RESET1_EVENT_C 0x3A
71#define ADP5589_RESET2_EVENT_A 0x3B
72#define ADP5589_RESET2_EVENT_B 0x3C
73#define ADP5589_RESET_CFG 0x3D
74#define ADP5589_PWM_OFFT_LOW 0x3E
75#define ADP5589_PWM_OFFT_HIGH 0x3F
76#define ADP5589_PWM_ONT_LOW 0x40
77#define ADP5589_PWM_ONT_HIGH 0x41
78#define ADP5589_PWM_CFG 0x42
79#define ADP5589_CLOCK_DIV_CFG 0x43
80#define ADP5589_LOGIC_1_CFG 0x44
81#define ADP5589_LOGIC_2_CFG 0x45
82#define ADP5589_LOGIC_FF_CFG 0x46
83#define ADP5589_LOGIC_INT_EVENT_EN 0x47
84#define ADP5589_POLL_PTIME_CFG 0x48
85#define ADP5589_PIN_CONFIG_A 0x49
86#define ADP5589_PIN_CONFIG_B 0x4A
87#define ADP5589_PIN_CONFIG_C 0x4B
88#define ADP5589_PIN_CONFIG_D 0x4C
89#define ADP5589_GENERAL_CFG 0x4D
90#define ADP5589_INT_EN 0x4E
91
92#define ADP5589_DEVICE_ID_MASK 0xF
93
94/* Put one of these structures in i2c_board_info platform_data */
95
96#define ADP5589_KEYMAPSIZE 88
97
98#define ADP5589_GPI_PIN_ROW0 97
99#define ADP5589_GPI_PIN_ROW1 98
100#define ADP5589_GPI_PIN_ROW2 99
101#define ADP5589_GPI_PIN_ROW3 100
102#define ADP5589_GPI_PIN_ROW4 101
103#define ADP5589_GPI_PIN_ROW5 102
104#define ADP5589_GPI_PIN_ROW6 103
105#define ADP5589_GPI_PIN_ROW7 104
106#define ADP5589_GPI_PIN_COL0 105
107#define ADP5589_GPI_PIN_COL1 106
108#define ADP5589_GPI_PIN_COL2 107
109#define ADP5589_GPI_PIN_COL3 108
110#define ADP5589_GPI_PIN_COL4 109
111#define ADP5589_GPI_PIN_COL5 110
112#define ADP5589_GPI_PIN_COL6 111
113#define ADP5589_GPI_PIN_COL7 112
114#define ADP5589_GPI_PIN_COL8 113
115#define ADP5589_GPI_PIN_COL9 114
116#define ADP5589_GPI_PIN_COL10 115
117#define GPI_LOGIC1 116
118#define GPI_LOGIC2 117
119
120#define ADP5589_GPI_PIN_ROW_BASE ADP5589_GPI_PIN_ROW0
121#define ADP5589_GPI_PIN_ROW_END ADP5589_GPI_PIN_ROW7
122#define ADP5589_GPI_PIN_COL_BASE ADP5589_GPI_PIN_COL0
123#define ADP5589_GPI_PIN_COL_END ADP5589_GPI_PIN_COL10
124
125#define ADP5589_GPI_PIN_BASE ADP5589_GPI_PIN_ROW_BASE
126#define ADP5589_GPI_PIN_END ADP5589_GPI_PIN_COL_END
127
128#define ADP5589_GPIMAPSIZE_MAX (ADP5589_GPI_PIN_END - ADP5589_GPI_PIN_BASE + 1)
129
130struct adp5589_gpi_map {
131 unsigned short pin;
132 unsigned short sw_evt;
133};
134
135/* scan_cycle_time */
136#define ADP5589_SCAN_CYCLE_10ms 0
137#define ADP5589_SCAN_CYCLE_20ms 1
138#define ADP5589_SCAN_CYCLE_30ms 2
139#define ADP5589_SCAN_CYCLE_40ms 3
140
141/* RESET_CFG */
142#define RESET_PULSE_WIDTH_500us 0
143#define RESET_PULSE_WIDTH_1ms 1
144#define RESET_PULSE_WIDTH_2ms 2
145#define RESET_PULSE_WIDTH_10ms 3
146
147#define RESET_TRIG_TIME_0ms (0 << 2)
148#define RESET_TRIG_TIME_1000ms (1 << 2)
149#define RESET_TRIG_TIME_1500ms (2 << 2)
150#define RESET_TRIG_TIME_2000ms (3 << 2)
151#define RESET_TRIG_TIME_2500ms (4 << 2)
152#define RESET_TRIG_TIME_3000ms (5 << 2)
153#define RESET_TRIG_TIME_3500ms (6 << 2)
154#define RESET_TRIG_TIME_4000ms (7 << 2)
155
156#define RESET_PASSTHRU_EN (1 << 5)
157#define RESET1_POL_HIGH (1 << 6)
158#define RESET1_POL_LOW (0 << 6)
159#define RESET2_POL_HIGH (1 << 7)
160#define RESET2_POL_LOW (0 << 7)
161
162/* Mask Bits:
163 * C C C C C C C C C C C | R R R R R R R R
164 * 1 9 8 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0
165 * 0
166 * ---------------- BIT ------------------
167 * 1 1 1 1 1 1 1 1 1 0 0 | 0 0 0 0 0 0 0 0
168 * 8 7 6 5 4 3 2 1 0 9 8 | 7 6 5 4 3 2 1 0
169 */
170
171#define ADP_ROW(x) (1 << (x))
172#define ADP_COL(x) (1 << (x + 8))
173
174struct adp5589_kpad_platform_data {
175 unsigned keypad_en_mask; /* Keypad (Rows/Columns) enable mask */
176 const unsigned short *keymap; /* Pointer to keymap */
177 unsigned short keymapsize; /* Keymap size */
178 bool repeat; /* Enable key repeat */
179 bool en_keylock; /* Enable key lock feature */
180 unsigned char unlock_key1; /* Unlock Key 1 */
181 unsigned char unlock_key2; /* Unlock Key 2 */
182 unsigned char unlock_timer; /* Time in seconds [0..7] between the two unlock keys 0=disable */
183 unsigned char scan_cycle_time; /* Time between consecutive scan cycles */
184 unsigned char reset_cfg; /* Reset config */
185 unsigned short reset1_key_1; /* Reset Key 1 */
186 unsigned short reset1_key_2; /* Reset Key 2 */
187 unsigned short reset1_key_3; /* Reset Key 3 */
188 unsigned short reset2_key_1; /* Reset Key 1 */
189 unsigned short reset2_key_2; /* Reset Key 2 */
190 unsigned debounce_dis_mask; /* Disable debounce mask */
191 unsigned pull_dis_mask; /* Disable all pull resistors mask */
192 unsigned pullup_en_100k; /* Pull-Up 100k Enable Mask */
193 unsigned pullup_en_300k; /* Pull-Up 300k Enable Mask */
194 unsigned pulldown_en_300k; /* Pull-Down 300k Enable Mask */
195 const struct adp5589_gpi_map *gpimap;
196 unsigned short gpimapsize;
197 const struct adp5589_gpio_platform_data *gpio_data;
198};
199
200struct i2c_client; /* forward declaration */
201
202struct adp5589_gpio_platform_data {
203 int gpio_start; /* GPIO Chip base # */
204 int (*setup)(struct i2c_client *client,
205 int gpio, unsigned ngpio,
206 void *context);
207 int (*teardown)(struct i2c_client *client,
208 int gpio, unsigned ngpio,
209 void *context);
210 void *context;
211};
212
213#endif