Kyle Manna | fb6c721 | 2011-10-29 12:31:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for TCA8418 I2C keyboard |
| 3 | * |
| 4 | * Copyright (C) 2011 Fuel7, Inc. All rights reserved. |
| 5 | * |
| 6 | * Author: Kyle Manna <kyle.manna@fuel7.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public |
| 10 | * License v2 as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public |
| 18 | * License along with this program; if not, write to the |
| 19 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 20 | * Boston, MA 021110-1307, USA. |
| 21 | * |
| 22 | * If you can't comply with GPLv2, alternative licensing terms may be |
| 23 | * arranged. Please contact Fuel7, Inc. (http://fuel7.com/) for proprietary |
| 24 | * alternative licensing inquiries. |
| 25 | */ |
| 26 | |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/delay.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/interrupt.h> |
| 33 | #include <linux/workqueue.h> |
| 34 | #include <linux/gpio.h> |
| 35 | #include <linux/i2c.h> |
| 36 | #include <linux/input.h> |
| 37 | #include <linux/input/tca8418_keypad.h> |
| 38 | |
| 39 | /* TCA8418 hardware limits */ |
| 40 | #define TCA8418_MAX_ROWS 8 |
| 41 | #define TCA8418_MAX_COLS 10 |
| 42 | |
| 43 | /* TCA8418 register offsets */ |
| 44 | #define REG_CFG 0x01 |
| 45 | #define REG_INT_STAT 0x02 |
| 46 | #define REG_KEY_LCK_EC 0x03 |
| 47 | #define REG_KEY_EVENT_A 0x04 |
| 48 | #define REG_KEY_EVENT_B 0x05 |
| 49 | #define REG_KEY_EVENT_C 0x06 |
| 50 | #define REG_KEY_EVENT_D 0x07 |
| 51 | #define REG_KEY_EVENT_E 0x08 |
| 52 | #define REG_KEY_EVENT_F 0x09 |
| 53 | #define REG_KEY_EVENT_G 0x0A |
| 54 | #define REG_KEY_EVENT_H 0x0B |
| 55 | #define REG_KEY_EVENT_I 0x0C |
| 56 | #define REG_KEY_EVENT_J 0x0D |
| 57 | #define REG_KP_LCK_TIMER 0x0E |
| 58 | #define REG_UNLOCK1 0x0F |
| 59 | #define REG_UNLOCK2 0x10 |
| 60 | #define REG_GPIO_INT_STAT1 0x11 |
| 61 | #define REG_GPIO_INT_STAT2 0x12 |
| 62 | #define REG_GPIO_INT_STAT3 0x13 |
| 63 | #define REG_GPIO_DAT_STAT1 0x14 |
| 64 | #define REG_GPIO_DAT_STAT2 0x15 |
| 65 | #define REG_GPIO_DAT_STAT3 0x16 |
| 66 | #define REG_GPIO_DAT_OUT1 0x17 |
| 67 | #define REG_GPIO_DAT_OUT2 0x18 |
| 68 | #define REG_GPIO_DAT_OUT3 0x19 |
| 69 | #define REG_GPIO_INT_EN1 0x1A |
| 70 | #define REG_GPIO_INT_EN2 0x1B |
| 71 | #define REG_GPIO_INT_EN3 0x1C |
| 72 | #define REG_KP_GPIO1 0x1D |
| 73 | #define REG_KP_GPIO2 0x1E |
| 74 | #define REG_KP_GPIO3 0x1F |
| 75 | #define REG_GPI_EM1 0x20 |
| 76 | #define REG_GPI_EM2 0x21 |
| 77 | #define REG_GPI_EM3 0x22 |
| 78 | #define REG_GPIO_DIR1 0x23 |
| 79 | #define REG_GPIO_DIR2 0x24 |
| 80 | #define REG_GPIO_DIR3 0x25 |
| 81 | #define REG_GPIO_INT_LVL1 0x26 |
| 82 | #define REG_GPIO_INT_LVL2 0x27 |
| 83 | #define REG_GPIO_INT_LVL3 0x28 |
| 84 | #define REG_DEBOUNCE_DIS1 0x29 |
| 85 | #define REG_DEBOUNCE_DIS2 0x2A |
| 86 | #define REG_DEBOUNCE_DIS3 0x2B |
| 87 | #define REG_GPIO_PULL1 0x2C |
| 88 | #define REG_GPIO_PULL2 0x2D |
| 89 | #define REG_GPIO_PULL3 0x2E |
| 90 | |
| 91 | /* TCA8418 bit definitions */ |
| 92 | #define CFG_AI BIT(7) |
| 93 | #define CFG_GPI_E_CFG BIT(6) |
| 94 | #define CFG_OVR_FLOW_M BIT(5) |
| 95 | #define CFG_INT_CFG BIT(4) |
| 96 | #define CFG_OVR_FLOW_IEN BIT(3) |
| 97 | #define CFG_K_LCK_IEN BIT(2) |
| 98 | #define CFG_GPI_IEN BIT(1) |
| 99 | #define CFG_KE_IEN BIT(0) |
| 100 | |
| 101 | #define INT_STAT_CAD_INT BIT(4) |
| 102 | #define INT_STAT_OVR_FLOW_INT BIT(3) |
| 103 | #define INT_STAT_K_LCK_INT BIT(2) |
| 104 | #define INT_STAT_GPI_INT BIT(1) |
| 105 | #define INT_STAT_K_INT BIT(0) |
| 106 | |
| 107 | /* TCA8418 register masks */ |
| 108 | #define KEY_LCK_EC_KEC 0x7 |
| 109 | #define KEY_EVENT_CODE 0x7f |
| 110 | #define KEY_EVENT_VALUE 0x80 |
| 111 | |
| 112 | |
| 113 | static const struct i2c_device_id tca8418_id[] = { |
| 114 | { TCA8418_NAME, 8418, }, |
| 115 | { } |
| 116 | }; |
| 117 | MODULE_DEVICE_TABLE(i2c, tca8418_id); |
| 118 | |
| 119 | struct tca8418_keypad { |
| 120 | unsigned int rows; |
| 121 | unsigned int cols; |
| 122 | unsigned int keypad_mask; /* Mask for keypad col/rol regs */ |
| 123 | unsigned int irq; |
| 124 | unsigned int row_shift; |
| 125 | |
| 126 | struct i2c_client *client; |
| 127 | struct input_dev *input; |
| 128 | |
| 129 | /* Flexible array member, must be at end of struct */ |
| 130 | unsigned short keymap[]; |
| 131 | }; |
| 132 | |
| 133 | /* |
| 134 | * Write a byte to the TCA8418 |
| 135 | */ |
| 136 | static int tca8418_write_byte(struct tca8418_keypad *keypad_data, |
| 137 | int reg, u8 val) |
| 138 | { |
| 139 | int error; |
| 140 | |
| 141 | error = i2c_smbus_write_byte_data(keypad_data->client, reg, val); |
| 142 | if (error < 0) { |
| 143 | dev_err(&keypad_data->client->dev, |
| 144 | "%s failed, reg: %d, val: %d, error: %d\n", |
| 145 | __func__, reg, val, error); |
| 146 | return error; |
| 147 | } |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Read a byte from the TCA8418 |
| 154 | */ |
| 155 | static int tca8418_read_byte(struct tca8418_keypad *keypad_data, |
| 156 | int reg, u8 *val) |
| 157 | { |
| 158 | int error; |
| 159 | |
| 160 | error = i2c_smbus_read_byte_data(keypad_data->client, reg); |
| 161 | if (error < 0) { |
| 162 | dev_err(&keypad_data->client->dev, |
| 163 | "%s failed, reg: %d, error: %d\n", |
| 164 | __func__, reg, error); |
| 165 | return error; |
| 166 | } |
| 167 | |
| 168 | *val = (u8)error; |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static void tca8418_read_keypad(struct tca8418_keypad *keypad_data) |
| 174 | { |
| 175 | int error, col, row; |
| 176 | u8 reg, state, code; |
| 177 | |
| 178 | /* Initial read of the key event FIFO */ |
| 179 | error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, ®); |
| 180 | |
| 181 | /* Assume that key code 0 signifies empty FIFO */ |
| 182 | while (error >= 0 && reg > 0) { |
| 183 | state = reg & KEY_EVENT_VALUE; |
| 184 | code = reg & KEY_EVENT_CODE; |
| 185 | |
| 186 | row = code / TCA8418_MAX_COLS; |
| 187 | col = code % TCA8418_MAX_COLS; |
| 188 | |
| 189 | row = (col) ? row : row - 1; |
| 190 | col = (col) ? col - 1 : TCA8418_MAX_COLS - 1; |
| 191 | |
| 192 | code = MATRIX_SCAN_CODE(row, col, keypad_data->row_shift); |
| 193 | input_event(keypad_data->input, EV_MSC, MSC_SCAN, code); |
| 194 | input_report_key(keypad_data->input, |
| 195 | keypad_data->keymap[code], state); |
| 196 | |
| 197 | /* Read for next loop */ |
| 198 | error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, ®); |
| 199 | } |
| 200 | |
| 201 | if (error < 0) |
| 202 | dev_err(&keypad_data->client->dev, |
| 203 | "unable to read REG_KEY_EVENT_A\n"); |
| 204 | |
| 205 | input_sync(keypad_data->input); |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Threaded IRQ handler and this can (and will) sleep. |
| 210 | */ |
| 211 | static irqreturn_t tca8418_irq_handler(int irq, void *dev_id) |
| 212 | { |
| 213 | struct tca8418_keypad *keypad_data = dev_id; |
| 214 | u8 reg; |
| 215 | int error; |
| 216 | |
| 217 | error = tca8418_read_byte(keypad_data, REG_INT_STAT, ®); |
| 218 | if (error) { |
| 219 | dev_err(&keypad_data->client->dev, |
| 220 | "unable to read REG_INT_STAT\n"); |
| 221 | goto exit; |
| 222 | } |
| 223 | |
| 224 | if (reg & INT_STAT_OVR_FLOW_INT) |
| 225 | dev_warn(&keypad_data->client->dev, "overflow occurred\n"); |
| 226 | |
| 227 | if (reg & INT_STAT_K_INT) |
| 228 | tca8418_read_keypad(keypad_data); |
| 229 | |
| 230 | exit: |
| 231 | /* Clear all interrupts, even IRQs we didn't check (GPI, CAD, LCK) */ |
| 232 | reg = 0xff; |
| 233 | error = tca8418_write_byte(keypad_data, REG_INT_STAT, reg); |
| 234 | if (error) |
| 235 | dev_err(&keypad_data->client->dev, |
| 236 | "unable to clear REG_INT_STAT\n"); |
| 237 | |
| 238 | return IRQ_HANDLED; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Configure the TCA8418 for keypad operation |
| 243 | */ |
| 244 | static int __devinit tca8418_configure(struct tca8418_keypad *keypad_data) |
| 245 | { |
| 246 | int reg, error; |
| 247 | |
| 248 | /* Write config register, if this fails assume device not present */ |
| 249 | error = tca8418_write_byte(keypad_data, REG_CFG, |
| 250 | CFG_INT_CFG | CFG_OVR_FLOW_IEN | CFG_KE_IEN); |
| 251 | if (error < 0) |
| 252 | return -ENODEV; |
| 253 | |
| 254 | |
| 255 | /* Assemble a mask for row and column registers */ |
| 256 | reg = ~(~0 << keypad_data->rows); |
| 257 | reg += (~(~0 << keypad_data->cols)) << 8; |
| 258 | keypad_data->keypad_mask = reg; |
| 259 | |
| 260 | /* Set registers to keypad mode */ |
| 261 | error |= tca8418_write_byte(keypad_data, REG_KP_GPIO1, reg); |
| 262 | error |= tca8418_write_byte(keypad_data, REG_KP_GPIO2, reg >> 8); |
| 263 | error |= tca8418_write_byte(keypad_data, REG_KP_GPIO3, reg >> 16); |
| 264 | |
| 265 | /* Enable column debouncing */ |
| 266 | error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS1, reg); |
| 267 | error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS2, reg >> 8); |
| 268 | error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS3, reg >> 16); |
| 269 | |
| 270 | return error; |
| 271 | } |
| 272 | |
| 273 | static int __devinit tca8418_keypad_probe(struct i2c_client *client, |
| 274 | const struct i2c_device_id *id) |
| 275 | { |
| 276 | const struct tca8418_keypad_platform_data *pdata = |
| 277 | client->dev.platform_data; |
| 278 | struct tca8418_keypad *keypad_data; |
| 279 | struct input_dev *input; |
| 280 | int error, row_shift, max_keys; |
| 281 | |
| 282 | /* Copy the platform data */ |
| 283 | if (!pdata) { |
| 284 | dev_dbg(&client->dev, "no platform data\n"); |
| 285 | return -EINVAL; |
| 286 | } |
| 287 | |
| 288 | if (!pdata->keymap_data) { |
| 289 | dev_err(&client->dev, "no keymap data defined\n"); |
| 290 | return -EINVAL; |
| 291 | } |
| 292 | |
| 293 | if (!pdata->rows || pdata->rows > TCA8418_MAX_ROWS) { |
| 294 | dev_err(&client->dev, "invalid rows\n"); |
| 295 | return -EINVAL; |
| 296 | } |
| 297 | |
| 298 | if (!pdata->cols || pdata->cols > TCA8418_MAX_COLS) { |
| 299 | dev_err(&client->dev, "invalid columns\n"); |
| 300 | return -EINVAL; |
| 301 | } |
| 302 | |
| 303 | /* Check i2c driver capabilities */ |
| 304 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) { |
| 305 | dev_err(&client->dev, "%s adapter not supported\n", |
| 306 | dev_driver_string(&client->adapter->dev)); |
| 307 | return -ENODEV; |
| 308 | } |
| 309 | |
| 310 | row_shift = get_count_order(pdata->cols); |
| 311 | max_keys = pdata->rows << row_shift; |
| 312 | |
| 313 | /* Allocate memory for keypad_data, keymap and input device */ |
| 314 | keypad_data = kzalloc(sizeof(*keypad_data) + |
| 315 | max_keys * sizeof(keypad_data->keymap[0]), GFP_KERNEL); |
| 316 | if (!keypad_data) |
| 317 | return -ENOMEM; |
| 318 | |
| 319 | keypad_data->rows = pdata->rows; |
| 320 | keypad_data->cols = pdata->cols; |
| 321 | keypad_data->client = client; |
| 322 | keypad_data->row_shift = row_shift; |
| 323 | |
| 324 | /* Initialize the chip or fail if chip isn't present */ |
| 325 | error = tca8418_configure(keypad_data); |
| 326 | if (error < 0) |
| 327 | goto fail1; |
| 328 | |
| 329 | /* Configure input device */ |
| 330 | input = input_allocate_device(); |
| 331 | if (!input) { |
| 332 | error = -ENOMEM; |
| 333 | goto fail1; |
| 334 | } |
| 335 | keypad_data->input = input; |
| 336 | |
| 337 | input->name = client->name; |
| 338 | input->dev.parent = &client->dev; |
| 339 | |
| 340 | input->id.bustype = BUS_I2C; |
| 341 | input->id.vendor = 0x0001; |
| 342 | input->id.product = 0x001; |
| 343 | input->id.version = 0x0001; |
| 344 | |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame^] | 345 | error = matrix_keypad_build_keymap(pdata->keymap_data, NULL, |
| 346 | pdata->rows, pdata->cols, |
| 347 | keypad_data->keymap, input); |
| 348 | if (error) { |
| 349 | dev_dbg(&client->dev, "Failed to build keymap\n"); |
| 350 | goto fail2; |
| 351 | } |
Kyle Manna | fb6c721 | 2011-10-29 12:31:35 -0700 | [diff] [blame] | 352 | |
Kyle Manna | fb6c721 | 2011-10-29 12:31:35 -0700 | [diff] [blame] | 353 | if (pdata->rep) |
| 354 | __set_bit(EV_REP, input->evbit); |
Kyle Manna | fb6c721 | 2011-10-29 12:31:35 -0700 | [diff] [blame] | 355 | input_set_capability(input, EV_MSC, MSC_SCAN); |
| 356 | |
| 357 | input_set_drvdata(input, keypad_data); |
| 358 | |
Kyle Manna | fb6c721 | 2011-10-29 12:31:35 -0700 | [diff] [blame] | 359 | if (pdata->irq_is_gpio) |
| 360 | client->irq = gpio_to_irq(client->irq); |
| 361 | |
| 362 | error = request_threaded_irq(client->irq, NULL, tca8418_irq_handler, |
| 363 | IRQF_TRIGGER_FALLING, |
| 364 | client->name, keypad_data); |
| 365 | if (error) { |
| 366 | dev_dbg(&client->dev, |
| 367 | "Unable to claim irq %d; error %d\n", |
| 368 | client->irq, error); |
| 369 | goto fail2; |
| 370 | } |
| 371 | |
| 372 | error = input_register_device(input); |
| 373 | if (error) { |
| 374 | dev_dbg(&client->dev, |
| 375 | "Unable to register input device, error: %d\n", error); |
| 376 | goto fail3; |
| 377 | } |
| 378 | |
| 379 | i2c_set_clientdata(client, keypad_data); |
| 380 | return 0; |
| 381 | |
| 382 | fail3: |
| 383 | free_irq(client->irq, keypad_data); |
| 384 | fail2: |
| 385 | input_free_device(input); |
| 386 | fail1: |
| 387 | kfree(keypad_data); |
| 388 | return error; |
| 389 | } |
| 390 | |
| 391 | static int __devexit tca8418_keypad_remove(struct i2c_client *client) |
| 392 | { |
| 393 | struct tca8418_keypad *keypad_data = i2c_get_clientdata(client); |
| 394 | |
| 395 | free_irq(keypad_data->client->irq, keypad_data); |
| 396 | |
| 397 | input_unregister_device(keypad_data->input); |
| 398 | |
| 399 | kfree(keypad_data); |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | |
| 405 | static struct i2c_driver tca8418_keypad_driver = { |
| 406 | .driver = { |
| 407 | .name = TCA8418_NAME, |
| 408 | .owner = THIS_MODULE, |
| 409 | }, |
| 410 | .probe = tca8418_keypad_probe, |
| 411 | .remove = __devexit_p(tca8418_keypad_remove), |
| 412 | .id_table = tca8418_id, |
| 413 | }; |
| 414 | |
| 415 | static int __init tca8418_keypad_init(void) |
| 416 | { |
| 417 | return i2c_add_driver(&tca8418_keypad_driver); |
| 418 | } |
| 419 | subsys_initcall(tca8418_keypad_init); |
| 420 | |
| 421 | static void __exit tca8418_keypad_exit(void) |
| 422 | { |
| 423 | i2c_del_driver(&tca8418_keypad_driver); |
| 424 | } |
| 425 | module_exit(tca8418_keypad_exit); |
| 426 | |
| 427 | MODULE_AUTHOR("Kyle Manna <kyle.manna@fuel7.com>"); |
| 428 | MODULE_DESCRIPTION("Keypad driver for TCA8418"); |
| 429 | MODULE_LICENSE("GPL"); |