tca8418_keypad.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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/delay.h>
  27. #include <linux/i2c.h>
  28. #include <linux/init.h>
  29. #include <linux/input.h>
  30. #include <linux/input/matrix_keypad.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/module.h>
  33. #include <linux/of.h>
  34. #include <linux/property.h>
  35. #include <linux/slab.h>
  36. #include <linux/types.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. struct tca8418_keypad {
  106. struct i2c_client *client;
  107. struct input_dev *input;
  108. unsigned int row_shift;
  109. };
  110. /*
  111. * Write a byte to the TCA8418
  112. */
  113. static int tca8418_write_byte(struct tca8418_keypad *keypad_data,
  114. int reg, u8 val)
  115. {
  116. int error;
  117. error = i2c_smbus_write_byte_data(keypad_data->client, reg, val);
  118. if (error < 0) {
  119. dev_err(&keypad_data->client->dev,
  120. "%s failed, reg: %d, val: %d, error: %d\n",
  121. __func__, reg, val, error);
  122. return error;
  123. }
  124. return 0;
  125. }
  126. /*
  127. * Read a byte from the TCA8418
  128. */
  129. static int tca8418_read_byte(struct tca8418_keypad *keypad_data,
  130. int reg, u8 *val)
  131. {
  132. int error;
  133. error = i2c_smbus_read_byte_data(keypad_data->client, reg);
  134. if (error < 0) {
  135. dev_err(&keypad_data->client->dev,
  136. "%s failed, reg: %d, error: %d\n",
  137. __func__, reg, error);
  138. return error;
  139. }
  140. *val = (u8)error;
  141. return 0;
  142. }
  143. static void tca8418_read_keypad(struct tca8418_keypad *keypad_data)
  144. {
  145. struct input_dev *input = keypad_data->input;
  146. unsigned short *keymap = input->keycode;
  147. int error, col, row;
  148. u8 reg, state, code;
  149. do {
  150. error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
  151. if (error < 0) {
  152. dev_err(&keypad_data->client->dev,
  153. "unable to read REG_KEY_EVENT_A\n");
  154. break;
  155. }
  156. /* Assume that key code 0 signifies empty FIFO */
  157. if (reg <= 0)
  158. break;
  159. state = reg & KEY_EVENT_VALUE;
  160. code = reg & KEY_EVENT_CODE;
  161. row = code / TCA8418_MAX_COLS;
  162. col = code % TCA8418_MAX_COLS;
  163. row = (col) ? row : row - 1;
  164. col = (col) ? col - 1 : TCA8418_MAX_COLS - 1;
  165. code = MATRIX_SCAN_CODE(row, col, keypad_data->row_shift);
  166. input_event(input, EV_MSC, MSC_SCAN, code);
  167. input_report_key(input, keymap[code], state);
  168. /* Read for next loop */
  169. error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
  170. } while (1);
  171. input_sync(input);
  172. }
  173. /*
  174. * Threaded IRQ handler and this can (and will) sleep.
  175. */
  176. static irqreturn_t tca8418_irq_handler(int irq, void *dev_id)
  177. {
  178. struct tca8418_keypad *keypad_data = dev_id;
  179. u8 reg;
  180. int error;
  181. error = tca8418_read_byte(keypad_data, REG_INT_STAT, &reg);
  182. if (error) {
  183. dev_err(&keypad_data->client->dev,
  184. "unable to read REG_INT_STAT\n");
  185. return IRQ_NONE;
  186. }
  187. if (!reg)
  188. return IRQ_NONE;
  189. if (reg & INT_STAT_OVR_FLOW_INT)
  190. dev_warn(&keypad_data->client->dev, "overflow occurred\n");
  191. if (reg & INT_STAT_K_INT)
  192. tca8418_read_keypad(keypad_data);
  193. /* Clear all interrupts, even IRQs we didn't check (GPI, CAD, LCK) */
  194. reg = 0xff;
  195. error = tca8418_write_byte(keypad_data, REG_INT_STAT, reg);
  196. if (error)
  197. dev_err(&keypad_data->client->dev,
  198. "unable to clear REG_INT_STAT\n");
  199. return IRQ_HANDLED;
  200. }
  201. /*
  202. * Configure the TCA8418 for keypad operation
  203. */
  204. static int tca8418_configure(struct tca8418_keypad *keypad_data,
  205. u32 rows, u32 cols)
  206. {
  207. int reg, error;
  208. /* Write config register, if this fails assume device not present */
  209. error = tca8418_write_byte(keypad_data, REG_CFG,
  210. CFG_INT_CFG | CFG_OVR_FLOW_IEN | CFG_KE_IEN);
  211. if (error < 0)
  212. return -ENODEV;
  213. /* Assemble a mask for row and column registers */
  214. reg = ~(~0 << rows);
  215. reg += (~(~0 << cols)) << 8;
  216. /* Set registers to keypad mode */
  217. error |= tca8418_write_byte(keypad_data, REG_KP_GPIO1, reg);
  218. error |= tca8418_write_byte(keypad_data, REG_KP_GPIO2, reg >> 8);
  219. error |= tca8418_write_byte(keypad_data, REG_KP_GPIO3, reg >> 16);
  220. /* Enable column debouncing */
  221. error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS1, reg);
  222. error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS2, reg >> 8);
  223. error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS3, reg >> 16);
  224. return error;
  225. }
  226. static int tca8418_keypad_probe(struct i2c_client *client,
  227. const struct i2c_device_id *id)
  228. {
  229. struct device *dev = &client->dev;
  230. struct tca8418_keypad *keypad_data;
  231. struct input_dev *input;
  232. u32 rows = 0, cols = 0;
  233. int error, row_shift, max_keys;
  234. /* Check i2c driver capabilities */
  235. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
  236. dev_err(dev, "%s adapter not supported\n",
  237. dev_driver_string(&client->adapter->dev));
  238. return -ENODEV;
  239. }
  240. error = matrix_keypad_parse_properties(dev, &rows, &cols);
  241. if (error)
  242. return error;
  243. if (!rows || rows > TCA8418_MAX_ROWS) {
  244. dev_err(dev, "invalid rows\n");
  245. return -EINVAL;
  246. }
  247. if (!cols || cols > TCA8418_MAX_COLS) {
  248. dev_err(dev, "invalid columns\n");
  249. return -EINVAL;
  250. }
  251. row_shift = get_count_order(cols);
  252. max_keys = rows << row_shift;
  253. /* Allocate memory for keypad_data and input device */
  254. keypad_data = devm_kzalloc(dev, sizeof(*keypad_data), GFP_KERNEL);
  255. if (!keypad_data)
  256. return -ENOMEM;
  257. keypad_data->client = client;
  258. keypad_data->row_shift = row_shift;
  259. /* Initialize the chip or fail if chip isn't present */
  260. error = tca8418_configure(keypad_data, rows, cols);
  261. if (error < 0)
  262. return error;
  263. /* Configure input device */
  264. input = devm_input_allocate_device(dev);
  265. if (!input)
  266. return -ENOMEM;
  267. keypad_data->input = input;
  268. input->name = client->name;
  269. input->id.bustype = BUS_I2C;
  270. input->id.vendor = 0x0001;
  271. input->id.product = 0x001;
  272. input->id.version = 0x0001;
  273. error = matrix_keypad_build_keymap(NULL, NULL, rows, cols, NULL, input);
  274. if (error) {
  275. dev_err(dev, "Failed to build keymap\n");
  276. return error;
  277. }
  278. if (device_property_read_bool(dev, "keypad,autorepeat"))
  279. __set_bit(EV_REP, input->evbit);
  280. input_set_capability(input, EV_MSC, MSC_SCAN);
  281. error = devm_request_threaded_irq(dev, client->irq,
  282. NULL, tca8418_irq_handler,
  283. IRQF_SHARED | IRQF_ONESHOT,
  284. client->name, keypad_data);
  285. if (error) {
  286. dev_err(dev, "Unable to claim irq %d; error %d\n",
  287. client->irq, error);
  288. return error;
  289. }
  290. error = input_register_device(input);
  291. if (error) {
  292. dev_err(dev, "Unable to register input device, error: %d\n",
  293. error);
  294. return error;
  295. }
  296. return 0;
  297. }
  298. static const struct i2c_device_id tca8418_id[] = {
  299. { "tca8418", 8418, },
  300. { }
  301. };
  302. MODULE_DEVICE_TABLE(i2c, tca8418_id);
  303. static const struct of_device_id tca8418_dt_ids[] = {
  304. { .compatible = "ti,tca8418", },
  305. { }
  306. };
  307. MODULE_DEVICE_TABLE(of, tca8418_dt_ids);
  308. static struct i2c_driver tca8418_keypad_driver = {
  309. .driver = {
  310. .name = "tca8418_keypad",
  311. .of_match_table = tca8418_dt_ids,
  312. },
  313. .probe = tca8418_keypad_probe,
  314. .id_table = tca8418_id,
  315. };
  316. static int __init tca8418_keypad_init(void)
  317. {
  318. return i2c_add_driver(&tca8418_keypad_driver);
  319. }
  320. subsys_initcall(tca8418_keypad_init);
  321. static void __exit tca8418_keypad_exit(void)
  322. {
  323. i2c_del_driver(&tca8418_keypad_driver);
  324. }
  325. module_exit(tca8418_keypad_exit);
  326. MODULE_AUTHOR("Kyle Manna <kyle.manna@fuel7.com>");
  327. MODULE_DESCRIPTION("Keypad driver for TCA8418");
  328. MODULE_LICENSE("GPL");