OMAP3: PM: Prevented DVFS state switches when enabling off-mode
[linux-ginger.git] / drivers / input / keyboard / adp5588-keys.c
blobd48c808d59281b415505842ae5d1fb0ff55a51ac
1 /*
2 * File: drivers/input/keyboard/adp5588_keys.c
3 * Description: keypad driver for ADP5588 I2C QWERTY Keypad and IO Expander
4 * Bugs: Enter bugs at http://blackfin.uclinux.org/
6 * Copyright (C) 2008-2009 Analog Devices Inc.
7 * Licensed under the GPL-2 or later.
8 */
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>
22 #include <linux/i2c/adp5588.h>
24 /* Configuration Register1 */
25 #define AUTO_INC (1 << 7)
26 #define GPIEM_CFG (1 << 6)
27 #define OVR_FLOW_M (1 << 5)
28 #define INT_CFG (1 << 4)
29 #define OVR_FLOW_IEN (1 << 3)
30 #define K_LCK_IM (1 << 2)
31 #define GPI_IEN (1 << 1)
32 #define KE_IEN (1 << 0)
34 /* Interrupt Status Register */
35 #define CMP2_INT (1 << 5)
36 #define CMP1_INT (1 << 4)
37 #define OVR_FLOW_INT (1 << 3)
38 #define K_LCK_INT (1 << 2)
39 #define GPI_INT (1 << 1)
40 #define KE_INT (1 << 0)
42 /* Key Lock and Event Counter Register */
43 #define K_LCK_EN (1 << 6)
44 #define LCK21 0x30
45 #define KEC 0xF
47 /* Key Event Register xy */
48 #define KEY_EV_PRESSED (1 << 7)
49 #define KEY_EV_MASK (0x7F)
51 #define KP_SEL(x) (0xFFFF >> (16 - x)) /* 2^x-1 */
53 #define KEYP_MAX_EVENT 10
56 * Early pre 4.0 Silicon required to delay readout by at least 25ms,
57 * since the Event Counter Register updated 25ms after the interrupt
58 * asserted.
60 #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
62 struct adp5588_kpad {
63 struct i2c_client *client;
64 struct input_dev *input;
65 struct delayed_work work;
66 unsigned long delay;
67 unsigned short keycode[ADP5588_KEYMAPSIZE];
70 static int adp5588_read(struct i2c_client *client, u8 reg)
72 int ret = i2c_smbus_read_byte_data(client, reg);
74 if (ret < 0)
75 dev_err(&client->dev, "Read Error\n");
77 return ret;
80 static int adp5588_write(struct i2c_client *client, u8 reg, u8 val)
82 return i2c_smbus_write_byte_data(client, reg, val);
85 static void adp5588_work(struct work_struct *work)
87 struct adp5588_kpad *kpad = container_of(work,
88 struct adp5588_kpad, work.work);
89 struct i2c_client *client = kpad->client;
90 int i, key, status, ev_cnt;
92 status = adp5588_read(client, INT_STAT);
94 if (status & OVR_FLOW_INT) /* Unlikely and should never happen */
95 dev_err(&client->dev, "Event Overflow Error\n");
97 if (status & KE_INT) {
98 ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & KEC;
99 if (ev_cnt) {
100 for (i = 0; i < ev_cnt; i++) {
101 key = adp5588_read(client, Key_EVENTA + i);
102 input_report_key(kpad->input,
103 kpad->keycode[(key & KEY_EV_MASK) - 1],
104 key & KEY_EV_PRESSED);
106 input_sync(kpad->input);
109 adp5588_write(client, INT_STAT, status); /* Status is W1C */
112 static irqreturn_t adp5588_irq(int irq, void *handle)
114 struct adp5588_kpad *kpad = handle;
117 * use keventd context to read the event fifo registers
118 * Schedule readout at least 25ms after notification for
119 * REVID < 4
122 schedule_delayed_work(&kpad->work, kpad->delay);
124 return IRQ_HANDLED;
127 static int __devinit adp5588_setup(struct i2c_client *client)
129 struct adp5588_kpad_platform_data *pdata = client->dev.platform_data;
130 int i, ret;
132 ret = adp5588_write(client, KP_GPIO1, KP_SEL(pdata->rows));
133 ret |= adp5588_write(client, KP_GPIO2, KP_SEL(pdata->cols) & 0xFF);
134 ret |= adp5588_write(client, KP_GPIO3, KP_SEL(pdata->cols) >> 8);
136 if (pdata->en_keylock) {
137 ret |= adp5588_write(client, UNLOCK1, pdata->unlock_key1);
138 ret |= adp5588_write(client, UNLOCK2, pdata->unlock_key2);
139 ret |= adp5588_write(client, KEY_LCK_EC_STAT, K_LCK_EN);
142 for (i = 0; i < KEYP_MAX_EVENT; i++)
143 ret |= adp5588_read(client, Key_EVENTA);
145 ret |= adp5588_write(client, INT_STAT, CMP2_INT | CMP1_INT |
146 OVR_FLOW_INT | K_LCK_INT |
147 GPI_INT | KE_INT); /* Status is W1C */
149 ret |= adp5588_write(client, CFG, INT_CFG | OVR_FLOW_IEN | KE_IEN);
151 if (ret < 0) {
152 dev_err(&client->dev, "Write Error\n");
153 return ret;
156 return 0;
159 static int __devinit adp5588_probe(struct i2c_client *client,
160 const struct i2c_device_id *id)
162 struct adp5588_kpad *kpad;
163 struct adp5588_kpad_platform_data *pdata = client->dev.platform_data;
164 struct input_dev *input;
165 unsigned int revid;
166 int ret, i;
167 int error;
169 if (!i2c_check_functionality(client->adapter,
170 I2C_FUNC_SMBUS_BYTE_DATA)) {
171 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
172 return -EIO;
175 if (!pdata) {
176 dev_err(&client->dev, "no platform data?\n");
177 return -EINVAL;
180 if (!pdata->rows || !pdata->cols || !pdata->keymap) {
181 dev_err(&client->dev, "no rows, cols or keymap from pdata\n");
182 return -EINVAL;
185 if (pdata->keymapsize != ADP5588_KEYMAPSIZE) {
186 dev_err(&client->dev, "invalid keymapsize\n");
187 return -EINVAL;
190 if (!client->irq) {
191 dev_err(&client->dev, "no IRQ?\n");
192 return -EINVAL;
195 kpad = kzalloc(sizeof(*kpad), GFP_KERNEL);
196 input = input_allocate_device();
197 if (!kpad || !input) {
198 error = -ENOMEM;
199 goto err_free_mem;
202 kpad->client = client;
203 kpad->input = input;
204 INIT_DELAYED_WORK(&kpad->work, adp5588_work);
206 ret = adp5588_read(client, DEV_ID);
207 if (ret < 0) {
208 error = ret;
209 goto err_free_mem;
212 revid = (u8) ret & ADP5588_DEVICE_ID_MASK;
213 if (WA_DELAYED_READOUT_REVID(revid))
214 kpad->delay = msecs_to_jiffies(30);
216 input->name = client->name;
217 input->phys = "adp5588-keys/input0";
218 input->dev.parent = &client->dev;
220 input_set_drvdata(input, kpad);
222 input->id.bustype = BUS_I2C;
223 input->id.vendor = 0x0001;
224 input->id.product = 0x0001;
225 input->id.version = revid;
227 input->keycodesize = sizeof(kpad->keycode[0]);
228 input->keycodemax = pdata->keymapsize;
229 input->keycode = kpad->keycode;
231 memcpy(kpad->keycode, pdata->keymap,
232 pdata->keymapsize * input->keycodesize);
234 /* setup input device */
235 __set_bit(EV_KEY, input->evbit);
237 if (pdata->repeat)
238 __set_bit(EV_REP, input->evbit);
240 for (i = 0; i < input->keycodemax; i++)
241 __set_bit(kpad->keycode[i] & KEY_MAX, input->keybit);
242 __clear_bit(KEY_RESERVED, input->keybit);
244 error = input_register_device(input);
245 if (error) {
246 dev_err(&client->dev, "unable to register input device\n");
247 goto err_free_mem;
250 error = request_irq(client->irq, adp5588_irq,
251 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
252 client->dev.driver->name, kpad);
253 if (error) {
254 dev_err(&client->dev, "irq %d busy?\n", client->irq);
255 goto err_unreg_dev;
258 error = adp5588_setup(client);
259 if (error)
260 goto err_free_irq;
262 device_init_wakeup(&client->dev, 1);
263 i2c_set_clientdata(client, kpad);
265 dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
266 return 0;
268 err_free_irq:
269 free_irq(client->irq, kpad);
270 err_unreg_dev:
271 input_unregister_device(input);
272 input = NULL;
273 err_free_mem:
274 input_free_device(input);
275 kfree(kpad);
277 return error;
280 static int __devexit adp5588_remove(struct i2c_client *client)
282 struct adp5588_kpad *kpad = i2c_get_clientdata(client);
284 adp5588_write(client, CFG, 0);
285 free_irq(client->irq, kpad);
286 cancel_delayed_work_sync(&kpad->work);
287 input_unregister_device(kpad->input);
288 i2c_set_clientdata(client, NULL);
289 kfree(kpad);
291 return 0;
294 #ifdef CONFIG_PM
295 static int adp5588_suspend(struct device *dev)
297 struct adp5588_kpad *kpad = dev_get_drvdata(dev);
298 struct i2c_client *client = kpad->client;
300 disable_irq(client->irq);
301 cancel_delayed_work_sync(&kpad->work);
303 if (device_may_wakeup(&client->dev))
304 enable_irq_wake(client->irq);
306 return 0;
309 static int adp5588_resume(struct device *dev)
311 struct adp5588_kpad *kpad = dev_get_drvdata(dev);
312 struct i2c_client *client = kpad->client;
314 if (device_may_wakeup(&client->dev))
315 disable_irq_wake(client->irq);
317 enable_irq(client->irq);
319 return 0;
322 static struct dev_pm_ops adp5588_dev_pm_ops = {
323 .suspend = adp5588_suspend,
324 .resume = adp5588_resume,
326 #endif
328 static const struct i2c_device_id adp5588_id[] = {
329 { KBUILD_MODNAME, 0 },
332 MODULE_DEVICE_TABLE(i2c, adp5588_id);
334 static struct i2c_driver adp5588_driver = {
335 .driver = {
336 .name = KBUILD_MODNAME,
337 #ifdef CONFIG_PM
338 .pm = &adp5588_dev_pm_ops,
339 #endif
341 .probe = adp5588_probe,
342 .remove = __devexit_p(adp5588_remove),
343 .id_table = adp5588_id,
346 static int __init adp5588_init(void)
348 return i2c_add_driver(&adp5588_driver);
350 module_init(adp5588_init);
352 static void __exit adp5588_exit(void)
354 i2c_del_driver(&adp5588_driver);
356 module_exit(adp5588_exit);
358 MODULE_LICENSE("GPL");
359 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
360 MODULE_DESCRIPTION("ADP5588 Keypad driver");
361 MODULE_ALIAS("platform:adp5588-keys");