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/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>
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>
24
#include <linux/input/adp5589.h>
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)
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)
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)
52
#define LOGIC2_STAT (1 << 7)
53
#define LOGIC1_STAT (1 << 6)
54
#define LOCK_STAT (1 << 5)
57
/* PIN_CONFIG_D Register */
58
#define C4_EXTEND_CFG (1 << 6) /* RESET2 */
59
#define R4_EXTEND_CFG (1 << 5) /* RESET1 */
62
#define LOCK_EN (1 << 0)
64
#define PTIME_MASK 0x3
65
#define LTIME_MASK 0x3
67
/* Key Event Register xy */
68
#define KEY_EV_PRESSED (1 << 7)
69
#define KEY_EV_MASK (0x7F)
71
#define KEYP_MAX_EVENT 16
74
#define ADP_BANK(offs) ((offs) >> 3)
75
#define ADP_BIT(offs) (1u << ((offs) & 0x7))
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;
85
unsigned char gpiomap[MAXGPIO];
88
struct mutex gpio_lock; /* Protect cached dir, dat_out */
94
static int adp5589_read(struct i2c_client *client, u8 reg)
96
int ret = i2c_smbus_read_byte_data(client, reg);
99
dev_err(&client->dev, "Read Error\n");
104
static int adp5589_write(struct i2c_client *client, u8 reg, u8 val)
106
return i2c_smbus_write_byte_data(client, reg, val);
109
#ifdef CONFIG_GPIOLIB
110
static int adp5589_gpio_get_value(struct gpio_chip *chip, unsigned off)
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]);
116
return !!(adp5589_read(kpad->client, ADP5589_GPI_STATUS_A + bank) &
120
static void adp5589_gpio_set_value(struct gpio_chip *chip,
121
unsigned off, int val)
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]);
127
mutex_lock(&kpad->gpio_lock);
130
kpad->dat_out[bank] |= bit;
132
kpad->dat_out[bank] &= ~bit;
134
adp5589_write(kpad->client, ADP5589_GPO_DATA_OUT_A + bank,
135
kpad->dat_out[bank]);
137
mutex_unlock(&kpad->gpio_lock);
140
static int adp5589_gpio_direction_input(struct gpio_chip *chip, unsigned off)
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]);
147
mutex_lock(&kpad->gpio_lock);
149
kpad->dir[bank] &= ~bit;
150
ret = adp5589_write(kpad->client, ADP5589_GPIO_DIRECTION_A + bank,
153
mutex_unlock(&kpad->gpio_lock);
158
static int adp5589_gpio_direction_output(struct gpio_chip *chip,
159
unsigned off, int val)
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]);
166
mutex_lock(&kpad->gpio_lock);
168
kpad->dir[bank] |= bit;
171
kpad->dat_out[bank] |= bit;
173
kpad->dat_out[bank] &= ~bit;
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,
180
mutex_unlock(&kpad->gpio_lock);
185
static int __devinit adp5589_build_gpiomap(struct adp5589_kpad *kpad,
186
const struct adp5589_kpad_platform_data *pdata)
188
bool pin_used[MAXGPIO];
192
memset(pin_used, false, sizeof(pin_used));
194
for (i = 0; i < MAXGPIO; i++)
195
if (pdata->keypad_en_mask & (1 << i))
198
for (i = 0; i < kpad->gpimapsize; i++)
199
pin_used[kpad->gpimap[i].pin - ADP5589_GPI_PIN_BASE] = true;
201
if (kpad->extend_cfg & R4_EXTEND_CFG)
204
if (kpad->extend_cfg & C4_EXTEND_CFG)
207
for (i = 0; i < MAXGPIO; i++)
209
kpad->gpiomap[n_unused++] = i;
214
static int __devinit adp5589_gpio_add(struct adp5589_kpad *kpad)
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;
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");
230
kpad->export_gpio = true;
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;
238
kpad->gc.base = gpio_data->gpio_start;
239
kpad->gc.label = kpad->client->name;
240
kpad->gc.owner = THIS_MODULE;
242
mutex_init(&kpad->gpio_lock);
244
error = gpiochip_add(&kpad->gc);
246
dev_err(dev, "gpiochip_add failed, err: %d\n", error);
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);
257
if (gpio_data->setup) {
258
error = gpio_data->setup(kpad->client,
259
kpad->gc.base, kpad->gc.ngpio,
262
dev_warn(dev, "setup failed, %d\n", error);
268
static void __devexit adp5589_gpio_remove(struct adp5589_kpad *kpad)
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;
275
if (!kpad->export_gpio)
278
if (gpio_data->teardown) {
279
error = gpio_data->teardown(kpad->client,
280
kpad->gc.base, kpad->gc.ngpio,
283
dev_warn(dev, "teardown failed %d\n", error);
286
error = gpiochip_remove(&kpad->gc);
288
dev_warn(dev, "gpiochip_remove failed %d\n", error);
291
static inline int adp5589_gpio_add(struct adp5589_kpad *kpad)
296
static inline void adp5589_gpio_remove(struct adp5589_kpad *kpad)
301
static void adp5589_report_switches(struct adp5589_kpad *kpad,
302
int key, int key_val)
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);
316
static void adp5589_report_events(struct adp5589_kpad *kpad, int ev_cnt)
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;
324
if (key_val >= ADP5589_GPI_PIN_BASE &&
325
key_val <= ADP5589_GPI_PIN_END) {
326
adp5589_report_switches(kpad, key, key_val);
328
input_report_key(kpad->input,
329
kpad->keycode[key_val - 1],
330
key & KEY_EV_PRESSED);
335
static irqreturn_t adp5589_irq(int irq, void *handle)
337
struct adp5589_kpad *kpad = handle;
338
struct i2c_client *client = kpad->client;
341
status = adp5589_read(client, ADP5589_INT_STATUS);
343
if (status & OVRFLOW_INT) /* Unlikely and should never happen */
344
dev_err(&client->dev, "Event Overflow Error\n");
346
if (status & EVENT_INT) {
347
ev_cnt = adp5589_read(client, ADP5589_STATUS) & KEC;
349
adp5589_report_events(kpad, ev_cnt);
350
input_sync(kpad->input);
354
adp5589_write(client, ADP5589_INT_STATUS, status); /* Status is W1C */
359
static int __devinit adp5589_get_evcode(struct adp5589_kpad *kpad, unsigned short key)
363
for (i = 0; i < ADP5589_KEYMAPSIZE; i++)
364
if (key == kpad->keycode[i])
365
return (i + 1) | KEY_EV_PRESSED;
367
dev_err(&kpad->client->dev, "RESET/UNLOCK key not in keycode map\n");
372
static int __devinit adp5589_setup(struct adp5589_kpad *kpad)
374
struct i2c_client *client = kpad->client;
375
const struct adp5589_kpad_platform_data *pdata =
376
client->dev.platform_data;
378
unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
379
unsigned char pull_mask = 0;
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);
388
if (pdata->en_keylock) {
389
ret |= adp5589_write(client, ADP5589_UNLOCK1,
391
ret |= adp5589_write(client, ADP5589_UNLOCK2,
393
ret |= adp5589_write(client, ADP5589_UNLOCK_TIMERS,
394
pdata->unlock_timer & LTIME_MASK);
395
ret |= adp5589_write(client, ADP5589_LOCK_CFG, LOCK_EN);
398
for (i = 0; i < KEYP_MAX_EVENT; i++)
399
ret |= adp5589_read(client, ADP5589_FIFO_1 + i);
401
for (i = 0; i < pdata->gpimapsize; i++) {
402
unsigned short pin = pdata->gpimap[i].pin;
404
if (pin <= ADP5589_GPI_PIN_ROW_END) {
405
evt_mode1 |= (1 << (pin - ADP5589_GPI_PIN_ROW_BASE));
408
((1 << (pin - ADP5589_GPI_PIN_COL_BASE)) & 0xFF);
410
((1 << (pin - ADP5589_GPI_PIN_COL_BASE)) >> 8);
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);
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");
424
for (i = 0; i < MAXGPIO; i++) {
427
if (pdata->pullup_en_300k & (1 << i))
429
else if (pdata->pulldown_en_300k & (1 << i))
431
else if (pdata->pullup_en_100k & (1 << i))
433
else if (pdata->pull_dis_mask & (1 << i))
436
pull_mask |= val << (2 * (i & 0x3));
438
if ((i & 0x3) == 0x3 || i == MAXGPIO - 1) {
439
ret |= adp5589_write(client,
440
ADP5589_RPULL_CONFIG_A + (i >> 2),
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;
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;
469
if (kpad->extend_cfg) {
470
ret |= adp5589_write(client, ADP5589_RESET_CFG,
472
ret |= adp5589_write(client, ADP5589_PIN_CONFIG_D,
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));
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 */
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);
492
dev_err(&client->dev, "Write Error\n");
499
static void __devinit adp5589_report_switch_state(struct adp5589_kpad *kpad)
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;
507
for (i = 0; i < kpad->gpimapsize; i++) {
508
unsigned short pin = kpad->gpimap[i].pin;
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;
517
gpi_stat_tmp = gpi_stat3;
518
pin_loc = pin - ADP5589_GPI_PIN_COL_BASE - 8;
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);
528
input_report_switch(kpad->input,
529
kpad->gpimap[i].sw_evt,
530
!(gpi_stat_tmp & (1 << pin_loc)));
533
input_sync(kpad->input);
536
static int __devinit adp5589_probe(struct i2c_client *client,
537
const struct i2c_device_id *id)
539
struct adp5589_kpad *kpad;
540
const struct adp5589_kpad_platform_data *pdata;
541
struct input_dev *input;
546
if (!i2c_check_functionality(client->adapter,
547
I2C_FUNC_SMBUS_BYTE_DATA)) {
548
dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
552
pdata = client->dev.platform_data;
554
dev_err(&client->dev, "no platform data?\n");
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");
564
if (pdata->keymapsize != ADP5589_KEYMAPSIZE) {
565
dev_err(&client->dev, "invalid keymapsize\n");
569
if (!pdata->gpimap && pdata->gpimapsize) {
570
dev_err(&client->dev, "invalid gpimap from pdata\n");
574
if (pdata->gpimapsize > ADP5589_GPIMAPSIZE_MAX) {
575
dev_err(&client->dev, "invalid gpimapsize\n");
579
for (i = 0; i < pdata->gpimapsize; i++) {
580
unsigned short pin = pdata->gpimap[i].pin;
582
if (pin < ADP5589_GPI_PIN_BASE || pin > ADP5589_GPI_PIN_END) {
583
dev_err(&client->dev, "invalid gpi pin data\n");
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");
595
dev_err(&client->dev, "no IRQ?\n");
599
kpad = kzalloc(sizeof(*kpad), GFP_KERNEL);
600
input = input_allocate_device();
601
if (!kpad || !input) {
606
kpad->client = client;
609
ret = adp5589_read(client, ADP5589_ID);
615
revid = (u8) ret & ADP5589_DEVICE_ID_MASK;
617
input->name = client->name;
618
input->phys = "adp5589-keys/input0";
619
input->dev.parent = &client->dev;
621
input_set_drvdata(input, kpad);
623
input->id.bustype = BUS_I2C;
624
input->id.vendor = 0x0001;
625
input->id.product = 0x0001;
626
input->id.version = revid;
628
input->keycodesize = sizeof(kpad->keycode[0]);
629
input->keycodemax = pdata->keymapsize;
630
input->keycode = kpad->keycode;
632
memcpy(kpad->keycode, pdata->keymap,
633
pdata->keymapsize * input->keycodesize);
635
kpad->gpimap = pdata->gpimap;
636
kpad->gpimapsize = pdata->gpimapsize;
638
/* setup input device */
639
__set_bit(EV_KEY, input->evbit);
642
__set_bit(EV_REP, input->evbit);
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);
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);
653
error = input_register_device(input);
655
dev_err(&client->dev, "unable to register input device\n");
659
error = request_threaded_irq(client->irq, NULL, adp5589_irq,
660
IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
661
client->dev.driver->name, kpad);
663
dev_err(&client->dev, "irq %d busy?\n", client->irq);
667
error = adp5589_setup(kpad);
671
if (kpad->gpimapsize)
672
adp5589_report_switch_state(kpad);
674
error = adp5589_gpio_add(kpad);
678
device_init_wakeup(&client->dev, 1);
679
i2c_set_clientdata(client, kpad);
681
dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
685
free_irq(client->irq, kpad);
687
input_unregister_device(input);
690
input_free_device(input);
696
static int __devexit adp5589_remove(struct i2c_client *client)
698
struct adp5589_kpad *kpad = i2c_get_clientdata(client);
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);
709
#ifdef CONFIG_PM_SLEEP
710
static int adp5589_suspend(struct device *dev)
712
struct adp5589_kpad *kpad = dev_get_drvdata(dev);
713
struct i2c_client *client = kpad->client;
715
disable_irq(client->irq);
717
if (device_may_wakeup(&client->dev))
718
enable_irq_wake(client->irq);
723
static int adp5589_resume(struct device *dev)
725
struct adp5589_kpad *kpad = dev_get_drvdata(dev);
726
struct i2c_client *client = kpad->client;
728
if (device_may_wakeup(&client->dev))
729
disable_irq_wake(client->irq);
731
enable_irq(client->irq);
737
static SIMPLE_DEV_PM_OPS(adp5589_dev_pm_ops, adp5589_suspend, adp5589_resume);
739
static const struct i2c_device_id adp5589_id[] = {
744
MODULE_DEVICE_TABLE(i2c, adp5589_id);
746
static struct i2c_driver adp5589_driver = {
748
.name = KBUILD_MODNAME,
749
.owner = THIS_MODULE,
750
.pm = &adp5589_dev_pm_ops,
752
.probe = adp5589_probe,
753
.remove = __devexit_p(adp5589_remove),
754
.id_table = adp5589_id,
757
static int __init adp5589_init(void)
759
return i2c_add_driver(&adp5589_driver);
761
module_init(adp5589_init);
763
static void __exit adp5589_exit(void)
765
i2c_del_driver(&adp5589_driver);
767
module_exit(adp5589_exit);
769
MODULE_LICENSE("GPL");
770
MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
771
MODULE_DESCRIPTION("ADP5589 Keypad driver");