2 * Description: keypad driver for ADP5589
3 * I2C QWERTY Keypad and IO Expander
4 * Bugs: Enter bugs at http://blackfin.uclinux.org/
6 * Copyright (C) 2010-2011 Analog Devices Inc.
7 * Licensed under the GPL-2.
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/workqueue.h>
15 #include <linux/errno.h>
17 #include <linux/platform_device.h>
18 #include <linux/input.h>
19 #include <linux/i2c.h>
20 #include <linux/gpio.h>
21 #include <linux/slab.h>
23 #include <linux/input/adp5589.h>
25 /* GENERAL_CFG Register */
26 #define OSC_EN (1 << 7)
27 #define CORE_CLK(x) (((x) & 0x3) << 5)
28 #define LCK_TRK_LOGIC (1 << 4)
29 #define LCK_TRK_GPI (1 << 3)
30 #define INT_CFG (1 << 1)
31 #define RST_CFG (1 << 0)
34 #define LOGIC2_IEN (1 << 5)
35 #define LOGIC1_IEN (1 << 4)
36 #define LOCK_IEN (1 << 3)
37 #define OVRFLOW_IEN (1 << 2)
38 #define GPI_IEN (1 << 1)
39 #define EVENT_IEN (1 << 0)
41 /* Interrupt Status Register */
42 #define LOGIC2_INT (1 << 5)
43 #define LOGIC1_INT (1 << 4)
44 #define LOCK_INT (1 << 3)
45 #define OVRFLOW_INT (1 << 2)
46 #define GPI_INT (1 << 1)
47 #define EVENT_INT (1 << 0)
51 #define LOGIC2_STAT (1 << 7)
52 #define LOGIC1_STAT (1 << 6)
53 #define LOCK_STAT (1 << 5)
56 /* PIN_CONFIG_D Register */
57 #define C4_EXTEND_CFG (1 << 6) /* RESET2 */
58 #define R4_EXTEND_CFG (1 << 5) /* RESET1 */
61 #define LOCK_EN (1 << 0)
63 #define PTIME_MASK 0x3
64 #define LTIME_MASK 0x3
66 /* Key Event Register xy */
67 #define KEY_EV_PRESSED (1 << 7)
68 #define KEY_EV_MASK (0x7F)
70 #define KEYP_MAX_EVENT 16
73 #define ADP_BANK(offs) ((offs) >> 3)
74 #define ADP_BIT(offs) (1u << ((offs) & 0x7))
77 struct i2c_client *client;
78 struct input_dev *input;
79 unsigned short keycode[ADP5589_KEYMAPSIZE];
80 const struct adp5589_gpi_map *gpimap;
81 unsigned short gpimapsize;
84 unsigned char gpiomap[MAXGPIO];
87 struct mutex gpio_lock; /* Protect cached dir, dat_out */
93 static int adp5589_read(struct i2c_client *client, u8 reg)
95 int ret = i2c_smbus_read_byte_data(client, reg);
98 dev_err(&client->dev, "Read Error\n");
103 static int adp5589_write(struct i2c_client *client, u8 reg, u8 val)
105 return i2c_smbus_write_byte_data(client, reg, val);
108 #ifdef CONFIG_GPIOLIB
109 static int adp5589_gpio_get_value(struct gpio_chip *chip, unsigned off)
111 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
112 unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
113 unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
115 return !!(adp5589_read(kpad->client, ADP5589_GPI_STATUS_A + bank) &
119 static void adp5589_gpio_set_value(struct gpio_chip *chip,
120 unsigned off, int val)
122 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
123 unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
124 unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
126 mutex_lock(&kpad->gpio_lock);
129 kpad->dat_out[bank] |= bit;
131 kpad->dat_out[bank] &= ~bit;
133 adp5589_write(kpad->client, ADP5589_GPO_DATA_OUT_A + bank,
134 kpad->dat_out[bank]);
136 mutex_unlock(&kpad->gpio_lock);
139 static int adp5589_gpio_direction_input(struct gpio_chip *chip, unsigned off)
141 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
142 unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
143 unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
146 mutex_lock(&kpad->gpio_lock);
148 kpad->dir[bank] &= ~bit;
149 ret = adp5589_write(kpad->client, ADP5589_GPIO_DIRECTION_A + bank,
152 mutex_unlock(&kpad->gpio_lock);
157 static int adp5589_gpio_direction_output(struct gpio_chip *chip,
158 unsigned off, int val)
160 struct adp5589_kpad *kpad = container_of(chip, struct adp5589_kpad, gc);
161 unsigned int bank = ADP_BANK(kpad->gpiomap[off]);
162 unsigned int bit = ADP_BIT(kpad->gpiomap[off]);
165 mutex_lock(&kpad->gpio_lock);
167 kpad->dir[bank] |= bit;
170 kpad->dat_out[bank] |= bit;
172 kpad->dat_out[bank] &= ~bit;
174 ret = adp5589_write(kpad->client, ADP5589_GPO_DATA_OUT_A + bank,
175 kpad->dat_out[bank]);
176 ret |= adp5589_write(kpad->client, ADP5589_GPIO_DIRECTION_A + bank,
179 mutex_unlock(&kpad->gpio_lock);
184 static int __devinit adp5589_build_gpiomap(struct adp5589_kpad *kpad,
185 const struct adp5589_kpad_platform_data *pdata)
187 bool pin_used[MAXGPIO];
191 memset(pin_used, false, sizeof(pin_used));
193 for (i = 0; i < MAXGPIO; i++)
194 if (pdata->keypad_en_mask & (1 << i))
197 for (i = 0; i < kpad->gpimapsize; i++)
198 pin_used[kpad->gpimap[i].pin - ADP5589_GPI_PIN_BASE] = true;
200 if (kpad->extend_cfg & R4_EXTEND_CFG)
203 if (kpad->extend_cfg & C4_EXTEND_CFG)
206 for (i = 0; i < MAXGPIO; i++)
208 kpad->gpiomap[n_unused++] = i;
213 static int __devinit adp5589_gpio_add(struct adp5589_kpad *kpad)
215 struct device *dev = &kpad->client->dev;
216 const struct adp5589_kpad_platform_data *pdata = dev->platform_data;
217 const struct adp5589_gpio_platform_data *gpio_data = pdata->gpio_data;
223 kpad->gc.ngpio = adp5589_build_gpiomap(kpad, pdata);
224 if (kpad->gc.ngpio == 0) {
225 dev_info(dev, "No unused gpios left to export\n");
229 kpad->export_gpio = true;
231 kpad->gc.direction_input = adp5589_gpio_direction_input;
232 kpad->gc.direction_output = adp5589_gpio_direction_output;
233 kpad->gc.get = adp5589_gpio_get_value;
234 kpad->gc.set = adp5589_gpio_set_value;
235 kpad->gc.can_sleep = 1;
237 kpad->gc.base = gpio_data->gpio_start;
238 kpad->gc.label = kpad->client->name;
239 kpad->gc.owner = THIS_MODULE;
241 mutex_init(&kpad->gpio_lock);
243 error = gpiochip_add(&kpad->gc);
245 dev_err(dev, "gpiochip_add failed, err: %d\n", error);
249 for (i = 0; i <= ADP_BANK(MAXGPIO); i++) {
250 kpad->dat_out[i] = adp5589_read(kpad->client,
251 ADP5589_GPO_DATA_OUT_A + i);
252 kpad->dir[i] = adp5589_read(kpad->client,
253 ADP5589_GPIO_DIRECTION_A + i);
256 if (gpio_data->setup) {
257 error = gpio_data->setup(kpad->client,
258 kpad->gc.base, kpad->gc.ngpio,
261 dev_warn(dev, "setup failed, %d\n", error);
267 static void __devexit adp5589_gpio_remove(struct adp5589_kpad *kpad)
269 struct device *dev = &kpad->client->dev;
270 const struct adp5589_kpad_platform_data *pdata = dev->platform_data;
271 const struct adp5589_gpio_platform_data *gpio_data = pdata->gpio_data;
274 if (!kpad->export_gpio)
277 if (gpio_data->teardown) {
278 error = gpio_data->teardown(kpad->client,
279 kpad->gc.base, kpad->gc.ngpio,
282 dev_warn(dev, "teardown failed %d\n", error);
285 error = gpiochip_remove(&kpad->gc);
287 dev_warn(dev, "gpiochip_remove failed %d\n", error);
290 static inline int adp5589_gpio_add(struct adp5589_kpad *kpad)
295 static inline void adp5589_gpio_remove(struct adp5589_kpad *kpad)
300 static void adp5589_report_switches(struct adp5589_kpad *kpad,
301 int key, int key_val)
305 for (i = 0; i < kpad->gpimapsize; i++) {
306 if (key_val == kpad->gpimap[i].pin) {
307 input_report_switch(kpad->input,
308 kpad->gpimap[i].sw_evt,
309 key & KEY_EV_PRESSED);
315 static void adp5589_report_events(struct adp5589_kpad *kpad, int ev_cnt)
319 for (i = 0; i < ev_cnt; i++) {
320 int key = adp5589_read(kpad->client, ADP5589_FIFO_1 + i);
321 int key_val = key & KEY_EV_MASK;
323 if (key_val >= ADP5589_GPI_PIN_BASE &&
324 key_val <= ADP5589_GPI_PIN_END) {
325 adp5589_report_switches(kpad, key, key_val);
327 input_report_key(kpad->input,
328 kpad->keycode[key_val - 1],
329 key & KEY_EV_PRESSED);
334 static irqreturn_t adp5589_irq(int irq, void *handle)
336 struct adp5589_kpad *kpad = handle;
337 struct i2c_client *client = kpad->client;
340 status = adp5589_read(client, ADP5589_INT_STATUS);
342 if (status & OVRFLOW_INT) /* Unlikely and should never happen */
343 dev_err(&client->dev, "Event Overflow Error\n");
345 if (status & EVENT_INT) {
346 ev_cnt = adp5589_read(client, ADP5589_STATUS) & KEC;
348 adp5589_report_events(kpad, ev_cnt);
349 input_sync(kpad->input);
353 adp5589_write(client, ADP5589_INT_STATUS, status); /* Status is W1C */
358 static int __devinit adp5589_get_evcode(struct adp5589_kpad *kpad, unsigned short key)
362 for (i = 0; i < ADP5589_KEYMAPSIZE; i++)
363 if (key == kpad->keycode[i])
364 return (i + 1) | KEY_EV_PRESSED;
366 dev_err(&kpad->client->dev, "RESET/UNLOCK key not in keycode map\n");
371 static int __devinit adp5589_setup(struct adp5589_kpad *kpad)
373 struct i2c_client *client = kpad->client;
374 const struct adp5589_kpad_platform_data *pdata =
375 client->dev.platform_data;
377 unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
378 unsigned char pull_mask = 0;
380 ret = adp5589_write(client, ADP5589_PIN_CONFIG_A,
381 pdata->keypad_en_mask & 0xFF);
382 ret |= adp5589_write(client, ADP5589_PIN_CONFIG_B,
383 (pdata->keypad_en_mask >> 8) & 0xFF);
384 ret |= adp5589_write(client, ADP5589_PIN_CONFIG_C,
385 (pdata->keypad_en_mask >> 16) & 0xFF);
387 if (pdata->en_keylock) {
388 ret |= adp5589_write(client, ADP5589_UNLOCK1,
390 ret |= adp5589_write(client, ADP5589_UNLOCK2,
392 ret |= adp5589_write(client, ADP5589_UNLOCK_TIMERS,
393 pdata->unlock_timer & LTIME_MASK);
394 ret |= adp5589_write(client, ADP5589_LOCK_CFG, LOCK_EN);
397 for (i = 0; i < KEYP_MAX_EVENT; i++)
398 ret |= adp5589_read(client, ADP5589_FIFO_1 + i);
400 for (i = 0; i < pdata->gpimapsize; i++) {
401 unsigned short pin = pdata->gpimap[i].pin;
403 if (pin <= ADP5589_GPI_PIN_ROW_END) {
404 evt_mode1 |= (1 << (pin - ADP5589_GPI_PIN_ROW_BASE));
407 ((1 << (pin - ADP5589_GPI_PIN_COL_BASE)) & 0xFF);
409 ((1 << (pin - ADP5589_GPI_PIN_COL_BASE)) >> 8);
413 if (pdata->gpimapsize) {
414 ret |= adp5589_write(client, ADP5589_GPI_EVENT_EN_A, evt_mode1);
415 ret |= adp5589_write(client, ADP5589_GPI_EVENT_EN_B, evt_mode2);
416 ret |= adp5589_write(client, ADP5589_GPI_EVENT_EN_C, evt_mode3);
419 if (pdata->pull_dis_mask & pdata->pullup_en_100k &
420 pdata->pullup_en_300k & pdata->pulldown_en_300k)
421 dev_warn(&client->dev, "Conflicting pull resistor config\n");
423 for (i = 0; i < MAXGPIO; i++) {
426 if (pdata->pullup_en_300k & (1 << i))
428 else if (pdata->pulldown_en_300k & (1 << i))
430 else if (pdata->pullup_en_100k & (1 << i))
432 else if (pdata->pull_dis_mask & (1 << i))
435 pull_mask |= val << (2 * (i & 0x3));
437 if ((i & 0x3) == 0x3 || i == MAXGPIO - 1) {
438 ret |= adp5589_write(client,
439 ADP5589_RPULL_CONFIG_A + (i >> 2),
445 if (pdata->reset1_key_1 && pdata->reset1_key_2 && pdata->reset1_key_3) {
446 ret |= adp5589_write(client, ADP5589_RESET1_EVENT_A,
447 adp5589_get_evcode(kpad,
448 pdata->reset1_key_1));
449 ret |= adp5589_write(client, ADP5589_RESET1_EVENT_B,
450 adp5589_get_evcode(kpad,
451 pdata->reset1_key_2));
452 ret |= adp5589_write(client, ADP5589_RESET1_EVENT_C,
453 adp5589_get_evcode(kpad,
454 pdata->reset1_key_3));
455 kpad->extend_cfg |= R4_EXTEND_CFG;
458 if (pdata->reset2_key_1 && pdata->reset2_key_2) {
459 ret |= adp5589_write(client, ADP5589_RESET2_EVENT_A,
460 adp5589_get_evcode(kpad,
461 pdata->reset2_key_1));
462 ret |= adp5589_write(client, ADP5589_RESET2_EVENT_B,
463 adp5589_get_evcode(kpad,
464 pdata->reset2_key_2));
465 kpad->extend_cfg |= C4_EXTEND_CFG;
468 if (kpad->extend_cfg) {
469 ret |= adp5589_write(client, ADP5589_RESET_CFG,
471 ret |= adp5589_write(client, ADP5589_PIN_CONFIG_D,
475 for (i = 0; i <= ADP_BANK(MAXGPIO); i++)
476 ret |= adp5589_write(client, ADP5589_DEBOUNCE_DIS_A + i,
477 pdata->debounce_dis_mask >> (i * 8));
479 ret |= adp5589_write(client, ADP5589_POLL_PTIME_CFG,
480 pdata->scan_cycle_time & PTIME_MASK);
481 ret |= adp5589_write(client, ADP5589_INT_STATUS, LOGIC2_INT |
482 LOGIC1_INT | OVRFLOW_INT | LOCK_INT |
483 GPI_INT | EVENT_INT); /* Status is W1C */
485 ret |= adp5589_write(client, ADP5589_GENERAL_CFG,
486 INT_CFG | OSC_EN | CORE_CLK(3));
487 ret |= adp5589_write(client, ADP5589_INT_EN,
488 OVRFLOW_IEN | GPI_IEN | EVENT_IEN);
491 dev_err(&client->dev, "Write Error\n");
498 static void __devinit adp5589_report_switch_state(struct adp5589_kpad *kpad)
500 int gpi_stat1 = adp5589_read(kpad->client, ADP5589_GPI_STATUS_A);
501 int gpi_stat2 = adp5589_read(kpad->client, ADP5589_GPI_STATUS_B);
502 int gpi_stat3 = adp5589_read(kpad->client, ADP5589_GPI_STATUS_C);
503 int gpi_stat_tmp, pin_loc;
506 for (i = 0; i < kpad->gpimapsize; i++) {
507 unsigned short pin = kpad->gpimap[i].pin;
509 if (pin <= ADP5589_GPI_PIN_ROW_END) {
510 gpi_stat_tmp = gpi_stat1;
511 pin_loc = pin - ADP5589_GPI_PIN_ROW_BASE;
512 } else if ((pin - ADP5589_GPI_PIN_COL_BASE) < 8) {
513 gpi_stat_tmp = gpi_stat2;
514 pin_loc = pin - ADP5589_GPI_PIN_COL_BASE;
516 gpi_stat_tmp = gpi_stat3;
517 pin_loc = pin - ADP5589_GPI_PIN_COL_BASE - 8;
520 if (gpi_stat_tmp < 0) {
521 dev_err(&kpad->client->dev,
522 "Can't read GPIO_DAT_STAT switch"
523 " %d default to OFF\n", pin);
527 input_report_switch(kpad->input,
528 kpad->gpimap[i].sw_evt,
529 !(gpi_stat_tmp & (1 << pin_loc)));
532 input_sync(kpad->input);
535 static int __devinit adp5589_probe(struct i2c_client *client,
536 const struct i2c_device_id *id)
538 struct adp5589_kpad *kpad;
539 const struct adp5589_kpad_platform_data *pdata;
540 struct input_dev *input;
545 if (!i2c_check_functionality(client->adapter,
546 I2C_FUNC_SMBUS_BYTE_DATA)) {
547 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
551 pdata = client->dev.platform_data;
553 dev_err(&client->dev, "no platform data?\n");
557 if (!((pdata->keypad_en_mask & 0xFF) &&
558 (pdata->keypad_en_mask >> 8)) || !pdata->keymap) {
559 dev_err(&client->dev, "no rows, cols or keymap from pdata\n");
563 if (pdata->keymapsize != ADP5589_KEYMAPSIZE) {
564 dev_err(&client->dev, "invalid keymapsize\n");
568 if (!pdata->gpimap && pdata->gpimapsize) {
569 dev_err(&client->dev, "invalid gpimap from pdata\n");
573 if (pdata->gpimapsize > ADP5589_GPIMAPSIZE_MAX) {
574 dev_err(&client->dev, "invalid gpimapsize\n");
578 for (i = 0; i < pdata->gpimapsize; i++) {
579 unsigned short pin = pdata->gpimap[i].pin;
581 if (pin < ADP5589_GPI_PIN_BASE || pin > ADP5589_GPI_PIN_END) {
582 dev_err(&client->dev, "invalid gpi pin data\n");
586 if ((1 << (pin - ADP5589_GPI_PIN_ROW_BASE)) &
587 pdata->keypad_en_mask) {
588 dev_err(&client->dev, "invalid gpi row/col data\n");
594 dev_err(&client->dev, "no IRQ?\n");
598 kpad = kzalloc(sizeof(*kpad), GFP_KERNEL);
599 input = input_allocate_device();
600 if (!kpad || !input) {
605 kpad->client = client;
608 ret = adp5589_read(client, ADP5589_ID);
614 revid = (u8) ret & ADP5589_DEVICE_ID_MASK;
616 input->name = client->name;
617 input->phys = "adp5589-keys/input0";
618 input->dev.parent = &client->dev;
620 input_set_drvdata(input, kpad);
622 input->id.bustype = BUS_I2C;
623 input->id.vendor = 0x0001;
624 input->id.product = 0x0001;
625 input->id.version = revid;
627 input->keycodesize = sizeof(kpad->keycode[0]);
628 input->keycodemax = pdata->keymapsize;
629 input->keycode = kpad->keycode;
631 memcpy(kpad->keycode, pdata->keymap,
632 pdata->keymapsize * input->keycodesize);
634 kpad->gpimap = pdata->gpimap;
635 kpad->gpimapsize = pdata->gpimapsize;
637 /* setup input device */
638 __set_bit(EV_KEY, input->evbit);
641 __set_bit(EV_REP, input->evbit);
643 for (i = 0; i < input->keycodemax; i++)
644 __set_bit(kpad->keycode[i] & KEY_MAX, input->keybit);
645 __clear_bit(KEY_RESERVED, input->keybit);
647 if (kpad->gpimapsize)
648 __set_bit(EV_SW, input->evbit);
649 for (i = 0; i < kpad->gpimapsize; i++)
650 __set_bit(kpad->gpimap[i].sw_evt, input->swbit);
652 error = input_register_device(input);
654 dev_err(&client->dev, "unable to register input device\n");
658 error = request_threaded_irq(client->irq, NULL, adp5589_irq,
659 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
660 client->dev.driver->name, kpad);
662 dev_err(&client->dev, "irq %d busy?\n", client->irq);
666 error = adp5589_setup(kpad);
670 if (kpad->gpimapsize)
671 adp5589_report_switch_state(kpad);
673 error = adp5589_gpio_add(kpad);
677 device_init_wakeup(&client->dev, 1);
678 i2c_set_clientdata(client, kpad);
680 dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
684 free_irq(client->irq, kpad);
686 input_unregister_device(input);
689 input_free_device(input);
695 static int __devexit adp5589_remove(struct i2c_client *client)
697 struct adp5589_kpad *kpad = i2c_get_clientdata(client);
699 adp5589_write(client, ADP5589_GENERAL_CFG, 0);
700 free_irq(client->irq, kpad);
701 input_unregister_device(kpad->input);
702 adp5589_gpio_remove(kpad);
708 #ifdef CONFIG_PM_SLEEP
709 static int adp5589_suspend(struct device *dev)
711 struct adp5589_kpad *kpad = dev_get_drvdata(dev);
712 struct i2c_client *client = kpad->client;
714 disable_irq(client->irq);
716 if (device_may_wakeup(&client->dev))
717 enable_irq_wake(client->irq);
722 static int adp5589_resume(struct device *dev)
724 struct adp5589_kpad *kpad = dev_get_drvdata(dev);
725 struct i2c_client *client = kpad->client;
727 if (device_may_wakeup(&client->dev))
728 disable_irq_wake(client->irq);
730 enable_irq(client->irq);
736 static SIMPLE_DEV_PM_OPS(adp5589_dev_pm_ops, adp5589_suspend, adp5589_resume);
738 static const struct i2c_device_id adp5589_id[] = {
743 MODULE_DEVICE_TABLE(i2c, adp5589_id);
745 static struct i2c_driver adp5589_driver = {
747 .name = KBUILD_MODNAME,
748 .owner = THIS_MODULE,
749 .pm = &adp5589_dev_pm_ops,
751 .probe = adp5589_probe,
752 .remove = __devexit_p(adp5589_remove),
753 .id_table = adp5589_id,
756 static int __init adp5589_init(void)
758 return i2c_add_driver(&adp5589_driver);
760 module_init(adp5589_init);
762 static void __exit adp5589_exit(void)
764 i2c_del_driver(&adp5589_driver);
766 module_exit(adp5589_exit);
768 MODULE_LICENSE("GPL");
769 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
770 MODULE_DESCRIPTION("ADP5589 Keypad driver");