| Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * File: drivers/input/keyboard/adp5588_keys.c | 
| Michael Hennerich | 5a9003d | 2010-01-19 00:28:44 -0800 | [diff] [blame] | 3 | * Description:  keypad driver for ADP5588 and ADP5587 | 
|  | 4 | *		 I2C QWERTY Keypad and IO Expander | 
| Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 5 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ | 
|  | 6 | * | 
|  | 7 | * Copyright (C) 2008-2009 Analog Devices Inc. | 
|  | 8 | * Licensed under the GPL-2 or later. | 
|  | 9 | */ | 
|  | 10 |  | 
|  | 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> | 
|  | 18 | #include <linux/pm.h> | 
|  | 19 | #include <linux/platform_device.h> | 
|  | 20 | #include <linux/input.h> | 
|  | 21 | #include <linux/i2c.h> | 
|  | 22 |  | 
|  | 23 | #include <linux/i2c/adp5588.h> | 
|  | 24 |  | 
|  | 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) | 
|  | 34 |  | 
|  | 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) | 
|  | 42 |  | 
|  | 43 | /* Key Lock and Event Counter Register */ | 
|  | 44 | #define K_LCK_EN	(1 << 6) | 
|  | 45 | #define LCK21		0x30 | 
|  | 46 | #define KEC		0xF | 
|  | 47 |  | 
|  | 48 | /* Key Event Register xy */ | 
|  | 49 | #define KEY_EV_PRESSED		(1 << 7) | 
|  | 50 | #define KEY_EV_MASK		(0x7F) | 
|  | 51 |  | 
|  | 52 | #define KP_SEL(x)		(0xFFFF >> (16 - x))	/* 2^x-1 */ | 
|  | 53 |  | 
|  | 54 | #define KEYP_MAX_EVENT		10 | 
|  | 55 |  | 
|  | 56 | /* | 
|  | 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 | 
|  | 59 | * asserted. | 
|  | 60 | */ | 
|  | 61 | #define WA_DELAYED_READOUT_REVID(rev)		((rev) < 4) | 
|  | 62 |  | 
|  | 63 | struct adp5588_kpad { | 
|  | 64 | struct i2c_client *client; | 
|  | 65 | struct input_dev *input; | 
|  | 66 | struct delayed_work work; | 
|  | 67 | unsigned long delay; | 
|  | 68 | unsigned short keycode[ADP5588_KEYMAPSIZE]; | 
|  | 69 | }; | 
|  | 70 |  | 
|  | 71 | static int adp5588_read(struct i2c_client *client, u8 reg) | 
|  | 72 | { | 
|  | 73 | int ret = i2c_smbus_read_byte_data(client, reg); | 
|  | 74 |  | 
|  | 75 | if (ret < 0) | 
|  | 76 | dev_err(&client->dev, "Read Error\n"); | 
|  | 77 |  | 
|  | 78 | return ret; | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | static int adp5588_write(struct i2c_client *client, u8 reg, u8 val) | 
|  | 82 | { | 
|  | 83 | return i2c_smbus_write_byte_data(client, reg, val); | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | static void adp5588_work(struct work_struct *work) | 
|  | 87 | { | 
|  | 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; | 
|  | 92 |  | 
|  | 93 | status = adp5588_read(client, INT_STAT); | 
|  | 94 |  | 
|  | 95 | if (status & OVR_FLOW_INT)	/* Unlikely and should never happen */ | 
|  | 96 | dev_err(&client->dev, "Event Overflow Error\n"); | 
|  | 97 |  | 
|  | 98 | if (status & KE_INT) { | 
|  | 99 | ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & KEC; | 
|  | 100 | if (ev_cnt) { | 
|  | 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); | 
|  | 106 | } | 
|  | 107 | input_sync(kpad->input); | 
|  | 108 | } | 
|  | 109 | } | 
|  | 110 | adp5588_write(client, INT_STAT, status); /* Status is W1C */ | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | static irqreturn_t adp5588_irq(int irq, void *handle) | 
|  | 114 | { | 
|  | 115 | struct adp5588_kpad *kpad = handle; | 
|  | 116 |  | 
|  | 117 | /* | 
|  | 118 | * use keventd context to read the event fifo registers | 
|  | 119 | * Schedule readout at least 25ms after notification for | 
|  | 120 | * REVID < 4 | 
|  | 121 | */ | 
|  | 122 |  | 
|  | 123 | schedule_delayed_work(&kpad->work, kpad->delay); | 
|  | 124 |  | 
|  | 125 | return IRQ_HANDLED; | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | static int __devinit adp5588_setup(struct i2c_client *client) | 
|  | 129 | { | 
|  | 130 | struct adp5588_kpad_platform_data *pdata = client->dev.platform_data; | 
|  | 131 | int i, ret; | 
|  | 132 |  | 
|  | 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); | 
|  | 136 |  | 
|  | 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); | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | for (i = 0; i < KEYP_MAX_EVENT; i++) | 
|  | 144 | ret |= adp5588_read(client, Key_EVENTA); | 
|  | 145 |  | 
|  | 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 */ | 
|  | 149 |  | 
|  | 150 | ret |= adp5588_write(client, CFG, INT_CFG | OVR_FLOW_IEN | KE_IEN); | 
|  | 151 |  | 
|  | 152 | if (ret < 0) { | 
|  | 153 | dev_err(&client->dev, "Write Error\n"); | 
|  | 154 | return ret; | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | return 0; | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | static int __devinit adp5588_probe(struct i2c_client *client, | 
|  | 161 | const struct i2c_device_id *id) | 
|  | 162 | { | 
|  | 163 | struct adp5588_kpad *kpad; | 
|  | 164 | struct adp5588_kpad_platform_data *pdata = client->dev.platform_data; | 
|  | 165 | struct input_dev *input; | 
|  | 166 | unsigned int revid; | 
|  | 167 | int ret, i; | 
|  | 168 | int error; | 
|  | 169 |  | 
|  | 170 | if (!i2c_check_functionality(client->adapter, | 
|  | 171 | I2C_FUNC_SMBUS_BYTE_DATA)) { | 
|  | 172 | dev_err(&client->dev, "SMBUS Byte Data not Supported\n"); | 
|  | 173 | return -EIO; | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | if (!pdata) { | 
|  | 177 | dev_err(&client->dev, "no platform data?\n"); | 
|  | 178 | return -EINVAL; | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | if (!pdata->rows || !pdata->cols || !pdata->keymap) { | 
|  | 182 | dev_err(&client->dev, "no rows, cols or keymap from pdata\n"); | 
|  | 183 | return -EINVAL; | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | if (pdata->keymapsize != ADP5588_KEYMAPSIZE) { | 
|  | 187 | dev_err(&client->dev, "invalid keymapsize\n"); | 
|  | 188 | return -EINVAL; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | if (!client->irq) { | 
|  | 192 | dev_err(&client->dev, "no IRQ?\n"); | 
|  | 193 | return -EINVAL; | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | kpad = kzalloc(sizeof(*kpad), GFP_KERNEL); | 
|  | 197 | input = input_allocate_device(); | 
|  | 198 | if (!kpad || !input) { | 
|  | 199 | error = -ENOMEM; | 
|  | 200 | goto err_free_mem; | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | kpad->client = client; | 
|  | 204 | kpad->input = input; | 
|  | 205 | INIT_DELAYED_WORK(&kpad->work, adp5588_work); | 
|  | 206 |  | 
|  | 207 | ret = adp5588_read(client, DEV_ID); | 
|  | 208 | if (ret < 0) { | 
|  | 209 | error = ret; | 
|  | 210 | goto err_free_mem; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | revid = (u8) ret & ADP5588_DEVICE_ID_MASK; | 
|  | 214 | if (WA_DELAYED_READOUT_REVID(revid)) | 
|  | 215 | kpad->delay = msecs_to_jiffies(30); | 
|  | 216 |  | 
|  | 217 | input->name = client->name; | 
|  | 218 | input->phys = "adp5588-keys/input0"; | 
|  | 219 | input->dev.parent = &client->dev; | 
|  | 220 |  | 
|  | 221 | input_set_drvdata(input, kpad); | 
|  | 222 |  | 
|  | 223 | input->id.bustype = BUS_I2C; | 
|  | 224 | input->id.vendor = 0x0001; | 
|  | 225 | input->id.product = 0x0001; | 
|  | 226 | input->id.version = revid; | 
|  | 227 |  | 
|  | 228 | input->keycodesize = sizeof(kpad->keycode[0]); | 
|  | 229 | input->keycodemax = pdata->keymapsize; | 
|  | 230 | input->keycode = kpad->keycode; | 
|  | 231 |  | 
|  | 232 | memcpy(kpad->keycode, pdata->keymap, | 
|  | 233 | pdata->keymapsize * input->keycodesize); | 
|  | 234 |  | 
|  | 235 | /* setup input device */ | 
|  | 236 | __set_bit(EV_KEY, input->evbit); | 
|  | 237 |  | 
|  | 238 | if (pdata->repeat) | 
|  | 239 | __set_bit(EV_REP, input->evbit); | 
|  | 240 |  | 
|  | 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); | 
|  | 244 |  | 
|  | 245 | error = input_register_device(input); | 
|  | 246 | if (error) { | 
|  | 247 | dev_err(&client->dev, "unable to register input device\n"); | 
|  | 248 | goto err_free_mem; | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | error = request_irq(client->irq, adp5588_irq, | 
|  | 252 | IRQF_TRIGGER_FALLING | IRQF_DISABLED, | 
|  | 253 | client->dev.driver->name, kpad); | 
|  | 254 | if (error) { | 
|  | 255 | dev_err(&client->dev, "irq %d busy?\n", client->irq); | 
|  | 256 | goto err_unreg_dev; | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | error = adp5588_setup(client); | 
|  | 260 | if (error) | 
|  | 261 | goto err_free_irq; | 
|  | 262 |  | 
|  | 263 | device_init_wakeup(&client->dev, 1); | 
|  | 264 | i2c_set_clientdata(client, kpad); | 
|  | 265 |  | 
|  | 266 | dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq); | 
|  | 267 | return 0; | 
|  | 268 |  | 
|  | 269 | err_free_irq: | 
|  | 270 | free_irq(client->irq, kpad); | 
|  | 271 | err_unreg_dev: | 
|  | 272 | input_unregister_device(input); | 
|  | 273 | input = NULL; | 
|  | 274 | err_free_mem: | 
|  | 275 | input_free_device(input); | 
|  | 276 | kfree(kpad); | 
|  | 277 |  | 
|  | 278 | return error; | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | static int __devexit adp5588_remove(struct i2c_client *client) | 
|  | 282 | { | 
|  | 283 | struct adp5588_kpad *kpad = i2c_get_clientdata(client); | 
|  | 284 |  | 
|  | 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); | 
|  | 290 | kfree(kpad); | 
|  | 291 |  | 
|  | 292 | return 0; | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | #ifdef CONFIG_PM | 
|  | 296 | static int adp5588_suspend(struct device *dev) | 
|  | 297 | { | 
|  | 298 | struct adp5588_kpad *kpad = dev_get_drvdata(dev); | 
|  | 299 | struct i2c_client *client = kpad->client; | 
|  | 300 |  | 
|  | 301 | disable_irq(client->irq); | 
|  | 302 | cancel_delayed_work_sync(&kpad->work); | 
|  | 303 |  | 
|  | 304 | if (device_may_wakeup(&client->dev)) | 
|  | 305 | enable_irq_wake(client->irq); | 
|  | 306 |  | 
|  | 307 | return 0; | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | static int adp5588_resume(struct device *dev) | 
|  | 311 | { | 
|  | 312 | struct adp5588_kpad *kpad = dev_get_drvdata(dev); | 
|  | 313 | struct i2c_client *client = kpad->client; | 
|  | 314 |  | 
|  | 315 | if (device_may_wakeup(&client->dev)) | 
|  | 316 | disable_irq_wake(client->irq); | 
|  | 317 |  | 
|  | 318 | enable_irq(client->irq); | 
|  | 319 |  | 
|  | 320 | return 0; | 
|  | 321 | } | 
|  | 322 |  | 
| Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 323 | static const struct dev_pm_ops adp5588_dev_pm_ops = { | 
| Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 324 | .suspend = adp5588_suspend, | 
|  | 325 | .resume  = adp5588_resume, | 
|  | 326 | }; | 
|  | 327 | #endif | 
|  | 328 |  | 
|  | 329 | static const struct i2c_device_id adp5588_id[] = { | 
|  | 330 | { KBUILD_MODNAME, 0 }, | 
| Michael Hennerich | 5a9003d | 2010-01-19 00:28:44 -0800 | [diff] [blame] | 331 | { "adp5587-keys", 0 }, | 
| Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 332 | { } | 
|  | 333 | }; | 
|  | 334 | MODULE_DEVICE_TABLE(i2c, adp5588_id); | 
|  | 335 |  | 
|  | 336 | static struct i2c_driver adp5588_driver = { | 
|  | 337 | .driver = { | 
|  | 338 | .name = KBUILD_MODNAME, | 
|  | 339 | #ifdef CONFIG_PM | 
|  | 340 | .pm   = &adp5588_dev_pm_ops, | 
|  | 341 | #endif | 
|  | 342 | }, | 
|  | 343 | .probe    = adp5588_probe, | 
|  | 344 | .remove   = __devexit_p(adp5588_remove), | 
|  | 345 | .id_table = adp5588_id, | 
|  | 346 | }; | 
|  | 347 |  | 
|  | 348 | static int __init adp5588_init(void) | 
|  | 349 | { | 
|  | 350 | return i2c_add_driver(&adp5588_driver); | 
|  | 351 | } | 
|  | 352 | module_init(adp5588_init); | 
|  | 353 |  | 
|  | 354 | static void __exit adp5588_exit(void) | 
|  | 355 | { | 
|  | 356 | i2c_del_driver(&adp5588_driver); | 
|  | 357 | } | 
|  | 358 | module_exit(adp5588_exit); | 
|  | 359 |  | 
|  | 360 | MODULE_LICENSE("GPL"); | 
|  | 361 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); | 
| Michael Hennerich | 5a9003d | 2010-01-19 00:28:44 -0800 | [diff] [blame] | 362 | MODULE_DESCRIPTION("ADP5588/87 Keypad driver"); | 
| Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 363 | MODULE_ALIAS("platform:adp5588-keys"); |