tca8418_keypad.c 11 KB

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