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>
23 #include <linux/i2c/adp5588.h>
25 /* Configuration Register1 */
26 #define AUTO_INC (1 << 7)
27 #define GPIEM_CFG (1 << 6)
28 #define OVR_FLOW_M (1 << 5)
29 #define INT_CFG (1 << 4)
30 #define OVR_FLOW_IEN (1 << 3)
31 #define K_LCK_IM (1 << 2)
32 #define GPI_IEN (1 << 1)
33 #define KE_IEN (1 << 0)
35 /* Interrupt Status Register */
36 #define CMP2_INT (1 << 5)
37 #define CMP1_INT (1 << 4)
38 #define OVR_FLOW_INT (1 << 3)
39 #define K_LCK_INT (1 << 2)
40 #define GPI_INT (1 << 1)
41 #define KE_INT (1 << 0)
43 /* Key Lock and Event Counter Register */
44 #define K_LCK_EN (1 << 6)
48 /* Key Event Register xy */
49 #define KEY_EV_PRESSED (1 << 7)
50 #define KEY_EV_MASK (0x7F)
52 #define KP_SEL(x) (0xFFFF >> (16 - x)) /* 2^x-1 */
54 #define KEYP_MAX_EVENT 10
57 * Early pre 4.0 Silicon required to delay readout by at least 25ms,
58 * since the Event Counter Register updated 25ms after the interrupt
61 #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
64 struct i2c_client
*client
;
65 struct input_dev
*input
;
66 struct delayed_work work
;
68 unsigned short keycode
[ADP5588_KEYMAPSIZE
];
71 static int adp5588_read(struct i2c_client
*client
, u8 reg
)
73 int ret
= i2c_smbus_read_byte_data(client
, reg
);
76 dev_err(&client
->dev
, "Read Error\n");
81 static int adp5588_write(struct i2c_client
*client
, u8 reg
, u8 val
)
83 return i2c_smbus_write_byte_data(client
, reg
, val
);
86 static void adp5588_work(struct work_struct
*work
)
88 struct adp5588_kpad
*kpad
= container_of(work
,
89 struct adp5588_kpad
, work
.work
);
90 struct i2c_client
*client
= kpad
->client
;
91 int i
, key
, status
, ev_cnt
;
93 status
= adp5588_read(client
, INT_STAT
);
95 if (status
& OVR_FLOW_INT
) /* Unlikely and should never happen */
96 dev_err(&client
->dev
, "Event Overflow Error\n");
98 if (status
& KE_INT
) {
99 ev_cnt
= adp5588_read(client
, KEY_LCK_EC_STAT
) & KEC
;
101 for (i
= 0; i
< ev_cnt
; i
++) {
102 key
= adp5588_read(client
, Key_EVENTA
+ i
);
103 input_report_key(kpad
->input
,
104 kpad
->keycode
[(key
& KEY_EV_MASK
) - 1],
105 key
& KEY_EV_PRESSED
);
107 input_sync(kpad
->input
);
110 adp5588_write(client
, INT_STAT
, status
); /* Status is W1C */
113 static irqreturn_t
adp5588_irq(int irq
, void *handle
)
115 struct adp5588_kpad
*kpad
= handle
;
118 * use keventd context to read the event fifo registers
119 * Schedule readout at least 25ms after notification for
123 schedule_delayed_work(&kpad
->work
, kpad
->delay
);
128 static int __devinit
adp5588_setup(struct i2c_client
*client
)
130 struct adp5588_kpad_platform_data
*pdata
= client
->dev
.platform_data
;
133 ret
= adp5588_write(client
, KP_GPIO1
, KP_SEL(pdata
->rows
));
134 ret
|= adp5588_write(client
, KP_GPIO2
, KP_SEL(pdata
->cols
) & 0xFF);
135 ret
|= adp5588_write(client
, KP_GPIO3
, KP_SEL(pdata
->cols
) >> 8);
137 if (pdata
->en_keylock
) {
138 ret
|= adp5588_write(client
, UNLOCK1
, pdata
->unlock_key1
);
139 ret
|= adp5588_write(client
, UNLOCK2
, pdata
->unlock_key2
);
140 ret
|= adp5588_write(client
, KEY_LCK_EC_STAT
, K_LCK_EN
);
143 for (i
= 0; i
< KEYP_MAX_EVENT
; i
++)
144 ret
|= adp5588_read(client
, Key_EVENTA
);
146 ret
|= adp5588_write(client
, INT_STAT
, CMP2_INT
| CMP1_INT
|
147 OVR_FLOW_INT
| K_LCK_INT
|
148 GPI_INT
| KE_INT
); /* Status is W1C */
150 ret
|= adp5588_write(client
, CFG
, INT_CFG
| OVR_FLOW_IEN
| KE_IEN
);
153 dev_err(&client
->dev
, "Write Error\n");
160 static int __devinit
adp5588_probe(struct i2c_client
*client
,
161 const struct i2c_device_id
*id
)
163 struct adp5588_kpad
*kpad
;
164 struct adp5588_kpad_platform_data
*pdata
= client
->dev
.platform_data
;
165 struct input_dev
*input
;
170 if (!i2c_check_functionality(client
->adapter
,
171 I2C_FUNC_SMBUS_BYTE_DATA
)) {
172 dev_err(&client
->dev
, "SMBUS Byte Data not Supported\n");
177 dev_err(&client
->dev
, "no platform data?\n");
181 if (!pdata
->rows
|| !pdata
->cols
|| !pdata
->keymap
) {
182 dev_err(&client
->dev
, "no rows, cols or keymap from pdata\n");
186 if (pdata
->keymapsize
!= ADP5588_KEYMAPSIZE
) {
187 dev_err(&client
->dev
, "invalid keymapsize\n");
192 dev_err(&client
->dev
, "no IRQ?\n");
196 kpad
= kzalloc(sizeof(*kpad
), GFP_KERNEL
);
197 input
= input_allocate_device();
198 if (!kpad
|| !input
) {
203 kpad
->client
= client
;
205 INIT_DELAYED_WORK(&kpad
->work
, adp5588_work
);
207 ret
= adp5588_read(client
, DEV_ID
);
213 revid
= (u8
) ret
& ADP5588_DEVICE_ID_MASK
;
214 if (WA_DELAYED_READOUT_REVID(revid
))
215 kpad
->delay
= msecs_to_jiffies(30);
217 input
->name
= client
->name
;
218 input
->phys
= "adp5588-keys/input0";
219 input
->dev
.parent
= &client
->dev
;
221 input_set_drvdata(input
, kpad
);
223 input
->id
.bustype
= BUS_I2C
;
224 input
->id
.vendor
= 0x0001;
225 input
->id
.product
= 0x0001;
226 input
->id
.version
= revid
;
228 input
->keycodesize
= sizeof(kpad
->keycode
[0]);
229 input
->keycodemax
= pdata
->keymapsize
;
230 input
->keycode
= kpad
->keycode
;
232 memcpy(kpad
->keycode
, pdata
->keymap
,
233 pdata
->keymapsize
* input
->keycodesize
);
235 /* setup input device */
236 __set_bit(EV_KEY
, input
->evbit
);
239 __set_bit(EV_REP
, input
->evbit
);
241 for (i
= 0; i
< input
->keycodemax
; i
++)
242 __set_bit(kpad
->keycode
[i
] & KEY_MAX
, input
->keybit
);
243 __clear_bit(KEY_RESERVED
, input
->keybit
);
245 error
= input_register_device(input
);
247 dev_err(&client
->dev
, "unable to register input device\n");
251 error
= request_irq(client
->irq
, adp5588_irq
,
252 IRQF_TRIGGER_FALLING
| IRQF_DISABLED
,
253 client
->dev
.driver
->name
, kpad
);
255 dev_err(&client
->dev
, "irq %d busy?\n", client
->irq
);
259 error
= adp5588_setup(client
);
263 device_init_wakeup(&client
->dev
, 1);
264 i2c_set_clientdata(client
, kpad
);
266 dev_info(&client
->dev
, "Rev.%d keypad, irq %d\n", revid
, client
->irq
);
270 free_irq(client
->irq
, kpad
);
272 input_unregister_device(input
);
275 input_free_device(input
);
281 static int __devexit
adp5588_remove(struct i2c_client
*client
)
283 struct adp5588_kpad
*kpad
= i2c_get_clientdata(client
);
285 adp5588_write(client
, CFG
, 0);
286 free_irq(client
->irq
, kpad
);
287 cancel_delayed_work_sync(&kpad
->work
);
288 input_unregister_device(kpad
->input
);
289 i2c_set_clientdata(client
, NULL
);
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");