tca8418_keypad.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. #include <linux/types.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/delay.h>
  30. #include <linux/slab.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/gpio.h>
  34. #include <linux/i2c.h>
  35. #include <linux/input.h>
  36. #include <linux/input/tca8418_keypad.h>
  37. /* TCA8418 hardware limits */
  38. #define TCA8418_MAX_ROWS 8
  39. #define TCA8418_MAX_COLS 10
  40. /* TCA8418 register offsets */
  41. #define REG_CFG 0x01
  42. #define REG_INT_STAT 0x02
  43. #define REG_KEY_LCK_EC 0x03
  44. #define REG_KEY_EVENT_A 0x04
  45. #define REG_KEY_EVENT_B 0x05
  46. #define REG_KEY_EVENT_C 0x06
  47. #define REG_KEY_EVENT_D 0x07
  48. #define REG_KEY_EVENT_E 0x08
  49. #define REG_KEY_EVENT_F 0x09
  50. #define REG_KEY_EVENT_G 0x0A
  51. #define REG_KEY_EVENT_H 0x0B
  52. #define REG_KEY_EVENT_I 0x0C
  53. #define REG_KEY_EVENT_J 0x0D
  54. #define REG_KP_LCK_TIMER 0x0E
  55. #define REG_UNLOCK1 0x0F
  56. #define REG_UNLOCK2 0x10
  57. #define REG_GPIO_INT_STAT1 0x11
  58. #define REG_GPIO_INT_STAT2 0x12
  59. #define REG_GPIO_INT_STAT3 0x13
  60. #define REG_GPIO_DAT_STAT1 0x14
  61. #define REG_GPIO_DAT_STAT2 0x15
  62. #define REG_GPIO_DAT_STAT3 0x16
  63. #define REG_GPIO_DAT_OUT1 0x17
  64. #define REG_GPIO_DAT_OUT2 0x18
  65. #define REG_GPIO_DAT_OUT3 0x19
  66. #define REG_GPIO_INT_EN1 0x1A
  67. #define REG_GPIO_INT_EN2 0x1B
  68. #define REG_GPIO_INT_EN3 0x1C
  69. #define REG_KP_GPIO1 0x1D
  70. #define REG_KP_GPIO2 0x1E
  71. #define REG_KP_GPIO3 0x1F
  72. #define REG_GPI_EM1 0x20
  73. #define REG_GPI_EM2 0x21
  74. #define REG_GPI_EM3 0x22
  75. #define REG_GPIO_DIR1 0x23
  76. #define REG_GPIO_DIR2 0x24
  77. #define REG_GPIO_DIR3 0x25
  78. #define REG_GPIO_INT_LVL1 0x26
  79. #define REG_GPIO_INT_LVL2 0x27
  80. #define REG_GPIO_INT_LVL3 0x28
  81. #define REG_DEBOUNCE_DIS1 0x29
  82. #define REG_DEBOUNCE_DIS2 0x2A
  83. #define REG_DEBOUNCE_DIS3 0x2B
  84. #define REG_GPIO_PULL1 0x2C
  85. #define REG_GPIO_PULL2 0x2D
  86. #define REG_GPIO_PULL3 0x2E
  87. /* TCA8418 bit definitions */
  88. #define CFG_AI BIT(7)
  89. #define CFG_GPI_E_CFG BIT(6)
  90. #define CFG_OVR_FLOW_M BIT(5)
  91. #define CFG_INT_CFG BIT(4)
  92. #define CFG_OVR_FLOW_IEN BIT(3)
  93. #define CFG_K_LCK_IEN BIT(2)
  94. #define CFG_GPI_IEN BIT(1)
  95. #define CFG_KE_IEN BIT(0)
  96. #define INT_STAT_CAD_INT BIT(4)
  97. #define INT_STAT_OVR_FLOW_INT BIT(3)
  98. #define INT_STAT_K_LCK_INT BIT(2)
  99. #define INT_STAT_GPI_INT BIT(1)
  100. #define INT_STAT_K_INT BIT(0)
  101. /* TCA8418 register masks */
  102. #define KEY_LCK_EC_KEC 0x7
  103. #define KEY_EVENT_CODE 0x7f
  104. #define KEY_EVENT_VALUE 0x80
  105. static const struct i2c_device_id tca8418_id[] = {
  106. { TCA8418_NAME, 8418, },
  107. { }
  108. };
  109. MODULE_DEVICE_TABLE(i2c, tca8418_id);
  110. struct tca8418_keypad {
  111. unsigned int rows;
  112. unsigned int cols;
  113. unsigned int keypad_mask; /* Mask for keypad col/rol regs */
  114. unsigned int irq;
  115. unsigned int row_shift;
  116. struct i2c_client *client;
  117. struct input_dev *input;
  118. /* Flexible array member, must be at end of struct */
  119. unsigned short keymap[];
  120. };
  121. /*
  122. * Write a byte to the TCA8418
  123. */
  124. static int tca8418_write_byte(struct tca8418_keypad *keypad_data,
  125. int reg, u8 val)
  126. {
  127. int error;
  128. error = i2c_smbus_write_byte_data(keypad_data->client, reg, val);
  129. if (error < 0) {
  130. dev_err(&keypad_data->client->dev,
  131. "%s failed, reg: %d, val: %d, error: %d\n",
  132. __func__, reg, val, error);
  133. return error;
  134. }
  135. return 0;
  136. }
  137. /*
  138. * Read a byte from the TCA8418
  139. */
  140. static int tca8418_read_byte(struct tca8418_keypad *keypad_data,
  141. int reg, u8 *val)
  142. {
  143. int error;
  144. error = i2c_smbus_read_byte_data(keypad_data->client, reg);
  145. if (error < 0) {
  146. dev_err(&keypad_data->client->dev,
  147. "%s failed, reg: %d, error: %d\n",
  148. __func__, reg, error);
  149. return error;
  150. }
  151. *val = (u8)error;
  152. return 0;
  153. }
  154. static void tca8418_read_keypad(struct tca8418_keypad *keypad_data)
  155. {
  156. int error, col, row;
  157. u8 reg, state, code;
  158. /* Initial read of the key event FIFO */
  159. error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
  160. /* Assume that key code 0 signifies empty FIFO */
  161. while (error >= 0 && reg > 0) {
  162. state = reg & KEY_EVENT_VALUE;
  163. code = reg & KEY_EVENT_CODE;
  164. row = code / TCA8418_MAX_COLS;
  165. col = code % TCA8418_MAX_COLS;
  166. row = (col) ? row : row - 1;
  167. col = (col) ? col - 1 : TCA8418_MAX_COLS - 1;
  168. code = MATRIX_SCAN_CODE(row, col, keypad_data->row_shift);
  169. input_event(keypad_data->input, EV_MSC, MSC_SCAN, code);
  170. input_report_key(keypad_data->input,
  171. keypad_data->keymap[code], state);
  172. /* Read for next loop */
  173. error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
  174. }
  175. if (error < 0)
  176. dev_err(&keypad_data->client->dev,
  177. "unable to read REG_KEY_EVENT_A\n");
  178. input_sync(keypad_data->input);
  179. }
  180. /*
  181. * Threaded IRQ handler and this can (and will) sleep.
  182. */
  183. static irqreturn_t tca8418_irq_handler(int irq, void *dev_id)
  184. {
  185. struct tca8418_keypad *keypad_data = dev_id;
  186. u8 reg;
  187. int error;
  188. error = tca8418_read_byte(keypad_data, REG_INT_STAT, &reg);
  189. if (error) {
  190. dev_err(&keypad_data->client->dev,
  191. "unable to read REG_INT_STAT\n");
  192. goto exit;
  193. }
  194. if (reg & INT_STAT_OVR_FLOW_INT)
  195. dev_warn(&keypad_data->client->dev, "overflow occurred\n");
  196. if (reg & INT_STAT_K_INT)
  197. tca8418_read_keypad(keypad_data);
  198. exit:
  199. /* Clear all interrupts, even IRQs we didn't check (GPI, CAD, LCK) */
  200. reg = 0xff;
  201. error = tca8418_write_byte(keypad_data, REG_INT_STAT, reg);
  202. if (error)
  203. dev_err(&keypad_data->client->dev,
  204. "unable to clear REG_INT_STAT\n");
  205. return IRQ_HANDLED;
  206. }
  207. /*
  208. * Configure the TCA8418 for keypad operation
  209. */
  210. static int __devinit tca8418_configure(struct tca8418_keypad *keypad_data)
  211. {
  212. int reg, error;
  213. /* Write config register, if this fails assume device not present */
  214. error = tca8418_write_byte(keypad_data, REG_CFG,
  215. CFG_INT_CFG | CFG_OVR_FLOW_IEN | CFG_KE_IEN);
  216. if (error < 0)
  217. return -ENODEV;
  218. /* Assemble a mask for row and column registers */
  219. reg = ~(~0 << keypad_data->rows);
  220. reg += (~(~0 << keypad_data->cols)) << 8;
  221. keypad_data->keypad_mask = reg;
  222. /* Set registers to keypad mode */
  223. error |= tca8418_write_byte(keypad_data, REG_KP_GPIO1, reg);
  224. error |= tca8418_write_byte(keypad_data, REG_KP_GPIO2, reg >> 8);
  225. error |= tca8418_write_byte(keypad_data, REG_KP_GPIO3, reg >> 16);
  226. /* Enable column debouncing */
  227. error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS1, reg);
  228. error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS2, reg >> 8);
  229. error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS3, reg >> 16);
  230. return error;
  231. }
  232. static int __devinit tca8418_keypad_probe(struct i2c_client *client,
  233. const struct i2c_device_id *id)
  234. {
  235. const struct tca8418_keypad_platform_data *pdata =
  236. client->dev.platform_data;
  237. struct tca8418_keypad *keypad_data;
  238. struct input_dev *input;
  239. int error, row_shift, max_keys;
  240. /* Copy the platform data */
  241. if (!pdata) {
  242. dev_dbg(&client->dev, "no platform data\n");
  243. return -EINVAL;
  244. }
  245. if (!pdata->keymap_data) {
  246. dev_err(&client->dev, "no keymap data defined\n");
  247. return -EINVAL;
  248. }
  249. if (!pdata->rows || pdata->rows > TCA8418_MAX_ROWS) {
  250. dev_err(&client->dev, "invalid rows\n");
  251. return -EINVAL;
  252. }
  253. if (!pdata->cols || pdata->cols > TCA8418_MAX_COLS) {
  254. dev_err(&client->dev, "invalid columns\n");
  255. return -EINVAL;
  256. }
  257. /* Check i2c driver capabilities */
  258. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
  259. dev_err(&client->dev, "%s adapter not supported\n",
  260. dev_driver_string(&client->adapter->dev));
  261. return -ENODEV;
  262. }
  263. row_shift = get_count_order(pdata->cols);
  264. max_keys = pdata->rows << row_shift;
  265. /* Allocate memory for keypad_data, keymap and input device */
  266. keypad_data = kzalloc(sizeof(*keypad_data) +
  267. max_keys * sizeof(keypad_data->keymap[0]), GFP_KERNEL);
  268. if (!keypad_data)
  269. return -ENOMEM;
  270. keypad_data->rows = pdata->rows;
  271. keypad_data->cols = pdata->cols;
  272. keypad_data->client = client;
  273. keypad_data->row_shift = row_shift;
  274. /* Initialize the chip or fail if chip isn't present */
  275. error = tca8418_configure(keypad_data);
  276. if (error < 0)
  277. goto fail1;
  278. /* Configure input device */
  279. input = input_allocate_device();
  280. if (!input) {
  281. error = -ENOMEM;
  282. goto fail1;
  283. }
  284. keypad_data->input = input;
  285. input->name = client->name;
  286. input->dev.parent = &client->dev;
  287. input->id.bustype = BUS_I2C;
  288. input->id.vendor = 0x0001;
  289. input->id.product = 0x001;
  290. input->id.version = 0x0001;
  291. input->keycode = keypad_data->keymap;
  292. input->keycodesize = sizeof(keypad_data->keymap[0]);
  293. input->keycodemax = max_keys;
  294. __set_bit(EV_KEY, input->evbit);
  295. if (pdata->rep)
  296. __set_bit(EV_REP, input->evbit);
  297. input_set_capability(input, EV_MSC, MSC_SCAN);
  298. input_set_drvdata(input, keypad_data);
  299. matrix_keypad_build_keymap(pdata->keymap_data, row_shift,
  300. input->keycode, input->keybit);
  301. if (pdata->irq_is_gpio)
  302. client->irq = gpio_to_irq(client->irq);
  303. error = request_threaded_irq(client->irq, NULL, tca8418_irq_handler,
  304. IRQF_TRIGGER_FALLING,
  305. client->name, keypad_data);
  306. if (error) {
  307. dev_dbg(&client->dev,
  308. "Unable to claim irq %d; error %d\n",
  309. client->irq, error);
  310. goto fail2;
  311. }
  312. error = input_register_device(input);
  313. if (error) {
  314. dev_dbg(&client->dev,
  315. "Unable to register input device, error: %d\n", error);
  316. goto fail3;
  317. }
  318. i2c_set_clientdata(client, keypad_data);
  319. return 0;
  320. fail3:
  321. free_irq(client->irq, keypad_data);
  322. fail2:
  323. input_free_device(input);
  324. fail1:
  325. kfree(keypad_data);
  326. return error;
  327. }
  328. static int __devexit tca8418_keypad_remove(struct i2c_client *client)
  329. {
  330. struct tca8418_keypad *keypad_data = i2c_get_clientdata(client);
  331. free_irq(keypad_data->client->irq, keypad_data);
  332. input_unregister_device(keypad_data->input);
  333. kfree(keypad_data);
  334. return 0;
  335. }
  336. static struct i2c_driver tca8418_keypad_driver = {
  337. .driver = {
  338. .name = TCA8418_NAME,
  339. .owner = THIS_MODULE,
  340. },
  341. .probe = tca8418_keypad_probe,
  342. .remove = __devexit_p(tca8418_keypad_remove),
  343. .id_table = tca8418_id,
  344. };
  345. static int __init tca8418_keypad_init(void)
  346. {
  347. return i2c_add_driver(&tca8418_keypad_driver);
  348. }
  349. subsys_initcall(tca8418_keypad_init);
  350. static void __exit tca8418_keypad_exit(void)
  351. {
  352. i2c_del_driver(&tca8418_keypad_driver);
  353. }
  354. module_exit(tca8418_keypad_exit);
  355. MODULE_AUTHOR("Kyle Manna <kyle.manna@fuel7.com>");
  356. MODULE_DESCRIPTION("Keypad driver for TCA8418");
  357. MODULE_LICENSE("GPL");