2 * File: drivers/input/keyboard/adp5588_keys.c
3 * Description: keypad driver for ADP5588 and ADP5587
4 * I2C QWERTY Keypad and IO Expander
5 * Bugs: Enter bugs at http://blackfin.uclinux.org/
7 * Copyright (C) 2008-2009 Analog Devices Inc.
8 * Licensed under the GPL-2 or later.
11 #include <linux/module.h>
12 #include <linux/version.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/workqueue.h>
17 #include <linux/errno.h>
19 #include <linux/platform_device.h>
20 #include <linux/input.h>
21 #include <linux/i2c.h>
22 #include <linux/slab.h>
24 #include <linux/i2c/adp5588.h>
26 /* Configuration Register1 */
27 #define AUTO_INC (1 << 7)
28 #define GPIEM_CFG (1 << 6)
29 #define OVR_FLOW_M (1 << 5)
30 #define INT_CFG (1 << 4)
31 #define OVR_FLOW_IEN (1 << 3)
32 #define K_LCK_IM (1 << 2)
33 #define GPI_IEN (1 << 1)
34 #define KE_IEN (1 << 0)
36 /* Interrupt Status Register */
37 #define CMP2_INT (1 << 5)
38 #define CMP1_INT (1 << 4)
39 #define OVR_FLOW_INT (1 << 3)
40 #define K_LCK_INT (1 << 2)
41 #define GPI_INT (1 << 1)
42 #define KE_INT (1 << 0)
44 /* Key Lock and Event Counter Register */
45 #define K_LCK_EN (1 << 6)
49 /* Key Event Register xy */
50 #define KEY_EV_PRESSED (1 << 7)
51 #define KEY_EV_MASK (0x7F)
53 #define KP_SEL(x) (0xFFFF >> (16 - x)) /* 2^x-1 */
55 #define KEYP_MAX_EVENT 10
58 * Early pre 4.0 Silicon required to delay readout by at least 25ms,
59 * since the Event Counter Register updated 25ms after the interrupt
62 #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
65 struct i2c_client
*client
;
66 struct input_dev
*input
;
67 struct delayed_work work
;
69 unsigned short keycode
[ADP5588_KEYMAPSIZE
];
72 static int adp5588_read(struct i2c_client
*client
, u8 reg
)
74 int ret
= i2c_smbus_read_byte_data(client
, reg
);
77 dev_err(&client
->dev
, "Read Error\n");
82 static int adp5588_write(struct i2c_client
*client
, u8 reg
, u8 val
)
84 return i2c_smbus_write_byte_data(client
, reg
, val
);
87 static void adp5588_work(struct work_struct
*work
)
89 struct adp5588_kpad
*kpad
= container_of(work
,
90 struct adp5588_kpad
, work
.work
);
91 struct i2c_client
*client
= kpad
->client
;
92 int i
, key
, status
, ev_cnt
;
94 status
= adp5588_read(client
, INT_STAT
);
96 if (status
& OVR_FLOW_INT
) /* Unlikely and should never happen */
97 dev_err(&client
->dev
, "Event Overflow Error\n");
99 if (status
& KE_INT
) {
100 ev_cnt
= adp5588_read(client
, KEY_LCK_EC_STAT
) & KEC
;
102 for (i
= 0; i
< ev_cnt
; i
++) {
103 key
= adp5588_read(client
, Key_EVENTA
+ i
);
104 input_report_key(kpad
->input
,
105 kpad
->keycode
[(key
& KEY_EV_MASK
) - 1],
106 key
& KEY_EV_PRESSED
);
108 input_sync(kpad
->input
);
111 adp5588_write(client
, INT_STAT
, status
); /* Status is W1C */
114 static irqreturn_t
adp5588_irq(int irq
, void *handle
)
116 struct adp5588_kpad
*kpad
= handle
;
119 * use keventd context to read the event fifo registers
120 * Schedule readout at least 25ms after notification for
124 schedule_delayed_work(&kpad
->work
, kpad
->delay
);
129 static int __devinit
adp5588_setup(struct i2c_client
*client
)
131 struct adp5588_kpad_platform_data
*pdata
= client
->dev
.platform_data
;
134 ret
= adp5588_write(client
, KP_GPIO1
, KP_SEL(pdata
->rows
));
135 ret
|= adp5588_write(client
, KP_GPIO2
, KP_SEL(pdata
->cols
) & 0xFF);
136 ret
|= adp5588_write(client
, KP_GPIO3
, KP_SEL(pdata
->cols
) >> 8);
138 if (pdata
->en_keylock
) {
139 ret
|= adp5588_write(client
, UNLOCK1
, pdata
->unlock_key1
);
140 ret
|= adp5588_write(client
, UNLOCK2
, pdata
->unlock_key2
);
141 ret
|= adp5588_write(client
, KEY_LCK_EC_STAT
, K_LCK_EN
);
144 for (i
= 0; i
< KEYP_MAX_EVENT
; i
++)
145 ret
|= adp5588_read(client
, Key_EVENTA
);
147 ret
|= adp5588_write(client
, INT_STAT
, CMP2_INT
| CMP1_INT
|
148 OVR_FLOW_INT
| K_LCK_INT
|
149 GPI_INT
| KE_INT
); /* Status is W1C */
151 ret
|= adp5588_write(client
, CFG
, INT_CFG
| OVR_FLOW_IEN
| KE_IEN
);
154 dev_err(&client
->dev
, "Write Error\n");
161 static int __devinit
adp5588_probe(struct i2c_client
*client
,
162 const struct i2c_device_id
*id
)
164 struct adp5588_kpad
*kpad
;
165 struct adp5588_kpad_platform_data
*pdata
= client
->dev
.platform_data
;
166 struct input_dev
*input
;
171 if (!i2c_check_functionality(client
->adapter
,
172 I2C_FUNC_SMBUS_BYTE_DATA
)) {
173 dev_err(&client
->dev
, "SMBUS Byte Data not Supported\n");
178 dev_err(&client
->dev
, "no platform data?\n");
182 if (!pdata
->rows
|| !pdata
->cols
|| !pdata
->keymap
) {
183 dev_err(&client
->dev
, "no rows, cols or keymap from pdata\n");
187 if (pdata
->keymapsize
!= ADP5588_KEYMAPSIZE
) {
188 dev_err(&client
->dev
, "invalid keymapsize\n");
193 dev_err(&client
->dev
, "no IRQ?\n");
197 kpad
= kzalloc(sizeof(*kpad
), GFP_KERNEL
);
198 input
= input_allocate_device();
199 if (!kpad
|| !input
) {
204 kpad
->client
= client
;
206 INIT_DELAYED_WORK(&kpad
->work
, adp5588_work
);
208 ret
= adp5588_read(client
, DEV_ID
);
214 revid
= (u8
) ret
& ADP5588_DEVICE_ID_MASK
;
215 if (WA_DELAYED_READOUT_REVID(revid
))
216 kpad
->delay
= msecs_to_jiffies(30);
218 input
->name
= client
->name
;
219 input
->phys
= "adp5588-keys/input0";
220 input
->dev
.parent
= &client
->dev
;
222 input_set_drvdata(input
, kpad
);
224 input
->id
.bustype
= BUS_I2C
;
225 input
->id
.vendor
= 0x0001;
226 input
->id
.product
= 0x0001;
227 input
->id
.version
= revid
;
229 input
->keycodesize
= sizeof(kpad
->keycode
[0]);
230 input
->keycodemax
= pdata
->keymapsize
;
231 input
->keycode
= kpad
->keycode
;
233 memcpy(kpad
->keycode
, pdata
->keymap
,
234 pdata
->keymapsize
* input
->keycodesize
);
236 /* setup input device */
237 __set_bit(EV_KEY
, input
->evbit
);
240 __set_bit(EV_REP
, input
->evbit
);
242 for (i
= 0; i
< input
->keycodemax
; i
++)
243 __set_bit(kpad
->keycode
[i
] & KEY_MAX
, input
->keybit
);
244 __clear_bit(KEY_RESERVED
, input
->keybit
);
246 error
= input_register_device(input
);
248 dev_err(&client
->dev
, "unable to register input device\n");
252 error
= request_irq(client
->irq
, adp5588_irq
,
253 IRQF_TRIGGER_FALLING
| IRQF_DISABLED
,
254 client
->dev
.driver
->name
, kpad
);
256 dev_err(&client
->dev
, "irq %d busy?\n", client
->irq
);
260 error
= adp5588_setup(client
);
264 device_init_wakeup(&client
->dev
, 1);
265 i2c_set_clientdata(client
, kpad
);
267 dev_info(&client
->dev
, "Rev.%d keypad, irq %d\n", revid
, client
->irq
);
271 free_irq(client
->irq
, kpad
);
273 input_unregister_device(input
);
276 input_free_device(input
);
282 static int __devexit
adp5588_remove(struct i2c_client
*client
)
284 struct adp5588_kpad
*kpad
= i2c_get_clientdata(client
);
286 adp5588_write(client
, CFG
, 0);
287 free_irq(client
->irq
, kpad
);
288 cancel_delayed_work_sync(&kpad
->work
);
289 input_unregister_device(kpad
->input
);
296 static int adp5588_suspend(struct device
*dev
)
298 struct adp5588_kpad
*kpad
= dev_get_drvdata(dev
);
299 struct i2c_client
*client
= kpad
->client
;
301 disable_irq(client
->irq
);
302 cancel_delayed_work_sync(&kpad
->work
);
304 if (device_may_wakeup(&client
->dev
))
305 enable_irq_wake(client
->irq
);
310 static int adp5588_resume(struct device
*dev
)
312 struct adp5588_kpad
*kpad
= dev_get_drvdata(dev
);
313 struct i2c_client
*client
= kpad
->client
;
315 if (device_may_wakeup(&client
->dev
))
316 disable_irq_wake(client
->irq
);
318 enable_irq(client
->irq
);
323 static const struct dev_pm_ops adp5588_dev_pm_ops
= {
324 .suspend
= adp5588_suspend
,
325 .resume
= adp5588_resume
,
329 static const struct i2c_device_id adp5588_id
[] = {
330 { KBUILD_MODNAME
, 0 },
331 { "adp5587-keys", 0 },
334 MODULE_DEVICE_TABLE(i2c
, adp5588_id
);
336 static struct i2c_driver adp5588_driver
= {
338 .name
= KBUILD_MODNAME
,
340 .pm
= &adp5588_dev_pm_ops
,
343 .probe
= adp5588_probe
,
344 .remove
= __devexit_p(adp5588_remove
),
345 .id_table
= adp5588_id
,
348 static int __init
adp5588_init(void)
350 return i2c_add_driver(&adp5588_driver
);
352 module_init(adp5588_init
);
354 static void __exit
adp5588_exit(void)
356 i2c_del_driver(&adp5588_driver
);
358 module_exit(adp5588_exit
);
360 MODULE_LICENSE("GPL");
361 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
362 MODULE_DESCRIPTION("ADP5588/87 Keypad driver");
363 MODULE_ALIAS("platform:adp5588-keys");