tc3589x-keypad.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * Author: Jayeeta Banerjee <jayeeta.banerjee@stericsson.com>
  5. * Author: Sundar Iyer <sundar.iyer@stericsson.com>
  6. *
  7. * License Terms: GNU General Public License, version 2
  8. *
  9. * TC35893 MFD Keypad Controller driver
  10. */
  11. #include <linux/module.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/input.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/input/matrix_keypad.h>
  16. #include <linux/i2c.h>
  17. #include <linux/slab.h>
  18. #include <linux/mfd/tc3589x.h>
  19. /* Maximum supported keypad matrix row/columns size */
  20. #define TC3589x_MAX_KPROW 8
  21. #define TC3589x_MAX_KPCOL 12
  22. /* keypad related Constants */
  23. #define TC3589x_MAX_DEBOUNCE_SETTLE 0xFF
  24. #define DEDICATED_KEY_VAL 0xFF
  25. /* Pull up/down masks */
  26. #define TC3589x_NO_PULL_MASK 0x0
  27. #define TC3589x_PULL_DOWN_MASK 0x1
  28. #define TC3589x_PULL_UP_MASK 0x2
  29. #define TC3589x_PULLUP_ALL_MASK 0xAA
  30. #define TC3589x_IO_PULL_VAL(index, mask) ((mask)<<((index)%4)*2))
  31. /* Bit masks for IOCFG register */
  32. #define IOCFG_BALLCFG 0x01
  33. #define IOCFG_IG 0x08
  34. #define KP_EVCODE_COL_MASK 0x0F
  35. #define KP_EVCODE_ROW_MASK 0x70
  36. #define KP_RELEASE_EVT_MASK 0x80
  37. #define KP_ROW_SHIFT 4
  38. #define KP_NO_VALID_KEY_MASK 0x7F
  39. /* bit masks for RESTCTRL register */
  40. #define TC3589x_KBDRST 0x2
  41. #define TC3589x_IRQRST 0x10
  42. #define TC3589x_RESET_ALL 0x1B
  43. /* KBDMFS register bit mask */
  44. #define TC3589x_KBDMFS_EN 0x1
  45. /* CLKEN register bitmask */
  46. #define KPD_CLK_EN 0x1
  47. /* RSTINTCLR register bit mask */
  48. #define IRQ_CLEAR 0x1
  49. /* bit masks for keyboard interrupts*/
  50. #define TC3589x_EVT_LOSS_INT 0x8
  51. #define TC3589x_EVT_INT 0x4
  52. #define TC3589x_KBD_LOSS_INT 0x2
  53. #define TC3589x_KBD_INT 0x1
  54. /* bit masks for keyboard interrupt clear*/
  55. #define TC3589x_EVT_INT_CLR 0x2
  56. #define TC3589x_KBD_INT_CLR 0x1
  57. /**
  58. * struct tc_keypad - data structure used by keypad driver
  59. * @tc3589x: pointer to tc35893
  60. * @input: pointer to input device object
  61. * @board: keypad platform device
  62. * @krow: number of rows
  63. * @kcol: number of columns
  64. * @keymap: matrix scan code table for keycodes
  65. * @keypad_stopped: holds keypad status
  66. */
  67. struct tc_keypad {
  68. struct tc3589x *tc3589x;
  69. struct input_dev *input;
  70. const struct tc3589x_keypad_platform_data *board;
  71. unsigned int krow;
  72. unsigned int kcol;
  73. unsigned short *keymap;
  74. bool keypad_stopped;
  75. };
  76. static int tc3589x_keypad_init_key_hardware(struct tc_keypad *keypad)
  77. {
  78. int ret;
  79. struct tc3589x *tc3589x = keypad->tc3589x;
  80. const struct tc3589x_keypad_platform_data *board = keypad->board;
  81. /* validate platform configuration */
  82. if (board->kcol > TC3589x_MAX_KPCOL || board->krow > TC3589x_MAX_KPROW)
  83. return -EINVAL;
  84. /* configure KBDSIZE 4 LSbits for cols and 4 MSbits for rows */
  85. ret = tc3589x_reg_write(tc3589x, TC3589x_KBDSIZE,
  86. (board->krow << KP_ROW_SHIFT) | board->kcol);
  87. if (ret < 0)
  88. return ret;
  89. /* configure dedicated key config, no dedicated key selected */
  90. ret = tc3589x_reg_write(tc3589x, TC3589x_KBCFG_LSB, DEDICATED_KEY_VAL);
  91. if (ret < 0)
  92. return ret;
  93. ret = tc3589x_reg_write(tc3589x, TC3589x_KBCFG_MSB, DEDICATED_KEY_VAL);
  94. if (ret < 0)
  95. return ret;
  96. /* Configure settle time */
  97. ret = tc3589x_reg_write(tc3589x, TC3589x_KBDSETTLE_REG,
  98. board->settle_time);
  99. if (ret < 0)
  100. return ret;
  101. /* Configure debounce time */
  102. ret = tc3589x_reg_write(tc3589x, TC3589x_KBDBOUNCE,
  103. board->debounce_period);
  104. if (ret < 0)
  105. return ret;
  106. /* Start of initialise keypad GPIOs */
  107. ret = tc3589x_set_bits(tc3589x, TC3589x_IOCFG, 0x0, IOCFG_IG);
  108. if (ret < 0)
  109. return ret;
  110. /* Configure pull-up resistors for all row GPIOs */
  111. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG0_LSB,
  112. TC3589x_PULLUP_ALL_MASK);
  113. if (ret < 0)
  114. return ret;
  115. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG0_MSB,
  116. TC3589x_PULLUP_ALL_MASK);
  117. if (ret < 0)
  118. return ret;
  119. /* Configure pull-up resistors for all column GPIOs */
  120. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG1_LSB,
  121. TC3589x_PULLUP_ALL_MASK);
  122. if (ret < 0)
  123. return ret;
  124. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG1_MSB,
  125. TC3589x_PULLUP_ALL_MASK);
  126. if (ret < 0)
  127. return ret;
  128. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG2_LSB,
  129. TC3589x_PULLUP_ALL_MASK);
  130. return ret;
  131. }
  132. #define TC35893_DATA_REGS 4
  133. #define TC35893_KEYCODE_FIFO_EMPTY 0x7f
  134. #define TC35893_KEYCODE_FIFO_CLEAR 0xff
  135. #define TC35893_KEYPAD_ROW_SHIFT 0x3
  136. static irqreturn_t tc3589x_keypad_irq(int irq, void *dev)
  137. {
  138. struct tc_keypad *keypad = dev;
  139. struct tc3589x *tc3589x = keypad->tc3589x;
  140. u8 i, row_index, col_index, kbd_code, up;
  141. u8 code;
  142. for (i = 0; i < TC35893_DATA_REGS * 2; i++) {
  143. kbd_code = tc3589x_reg_read(tc3589x, TC3589x_EVTCODE_FIFO);
  144. /* loop till fifo is empty and no more keys are pressed */
  145. if (kbd_code == TC35893_KEYCODE_FIFO_EMPTY ||
  146. kbd_code == TC35893_KEYCODE_FIFO_CLEAR)
  147. continue;
  148. /* valid key is found */
  149. col_index = kbd_code & KP_EVCODE_COL_MASK;
  150. row_index = (kbd_code & KP_EVCODE_ROW_MASK) >> KP_ROW_SHIFT;
  151. code = MATRIX_SCAN_CODE(row_index, col_index,
  152. TC35893_KEYPAD_ROW_SHIFT);
  153. up = kbd_code & KP_RELEASE_EVT_MASK;
  154. input_event(keypad->input, EV_MSC, MSC_SCAN, code);
  155. input_report_key(keypad->input, keypad->keymap[code], !up);
  156. input_sync(keypad->input);
  157. }
  158. /* clear IRQ */
  159. tc3589x_set_bits(tc3589x, TC3589x_KBDIC,
  160. 0x0, TC3589x_EVT_INT_CLR | TC3589x_KBD_INT_CLR);
  161. /* enable IRQ */
  162. tc3589x_set_bits(tc3589x, TC3589x_KBDMSK,
  163. 0x0, TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT);
  164. return IRQ_HANDLED;
  165. }
  166. static int tc3589x_keypad_enable(struct tc_keypad *keypad)
  167. {
  168. struct tc3589x *tc3589x = keypad->tc3589x;
  169. int ret;
  170. /* pull the keypad module out of reset */
  171. ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL, TC3589x_KBDRST, 0x0);
  172. if (ret < 0)
  173. return ret;
  174. /* configure KBDMFS */
  175. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMFS, 0x0, TC3589x_KBDMFS_EN);
  176. if (ret < 0)
  177. return ret;
  178. /* enable the keypad clock */
  179. ret = tc3589x_set_bits(tc3589x, TC3589x_CLKEN, 0x0, KPD_CLK_EN);
  180. if (ret < 0)
  181. return ret;
  182. /* clear pending IRQs */
  183. ret = tc3589x_set_bits(tc3589x, TC3589x_RSTINTCLR, 0x0, 0x1);
  184. if (ret < 0)
  185. return ret;
  186. /* enable the IRQs */
  187. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMSK, 0x0,
  188. TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT);
  189. if (ret < 0)
  190. return ret;
  191. keypad->keypad_stopped = false;
  192. return ret;
  193. }
  194. static int tc3589x_keypad_disable(struct tc_keypad *keypad)
  195. {
  196. struct tc3589x *tc3589x = keypad->tc3589x;
  197. int ret;
  198. /* clear IRQ */
  199. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDIC,
  200. 0x0, TC3589x_EVT_INT_CLR | TC3589x_KBD_INT_CLR);
  201. if (ret < 0)
  202. return ret;
  203. /* disable all interrupts */
  204. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMSK,
  205. ~(TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT), 0x0);
  206. if (ret < 0)
  207. return ret;
  208. /* disable the keypad module */
  209. ret = tc3589x_set_bits(tc3589x, TC3589x_CLKEN, 0x1, 0x0);
  210. if (ret < 0)
  211. return ret;
  212. /* put the keypad module into reset */
  213. ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL, TC3589x_KBDRST, 0x1);
  214. keypad->keypad_stopped = true;
  215. return ret;
  216. }
  217. static int tc3589x_keypad_open(struct input_dev *input)
  218. {
  219. int error;
  220. struct tc_keypad *keypad = input_get_drvdata(input);
  221. /* enable the keypad module */
  222. error = tc3589x_keypad_enable(keypad);
  223. if (error < 0) {
  224. dev_err(&input->dev, "failed to enable keypad module\n");
  225. return error;
  226. }
  227. error = tc3589x_keypad_init_key_hardware(keypad);
  228. if (error < 0) {
  229. dev_err(&input->dev, "failed to configure keypad module\n");
  230. return error;
  231. }
  232. return 0;
  233. }
  234. static void tc3589x_keypad_close(struct input_dev *input)
  235. {
  236. struct tc_keypad *keypad = input_get_drvdata(input);
  237. /* disable the keypad module */
  238. tc3589x_keypad_disable(keypad);
  239. }
  240. static int tc3589x_keypad_probe(struct platform_device *pdev)
  241. {
  242. struct tc3589x *tc3589x = dev_get_drvdata(pdev->dev.parent);
  243. struct tc_keypad *keypad;
  244. struct input_dev *input;
  245. const struct tc3589x_keypad_platform_data *plat;
  246. int error, irq;
  247. plat = tc3589x->pdata->keypad;
  248. if (!plat) {
  249. dev_err(&pdev->dev, "invalid keypad platform data\n");
  250. return -EINVAL;
  251. }
  252. irq = platform_get_irq(pdev, 0);
  253. if (irq < 0)
  254. return irq;
  255. keypad = kzalloc(sizeof(struct tc_keypad), GFP_KERNEL);
  256. input = input_allocate_device();
  257. if (!keypad || !input) {
  258. dev_err(&pdev->dev, "failed to allocate keypad memory\n");
  259. error = -ENOMEM;
  260. goto err_free_mem;
  261. }
  262. keypad->board = plat;
  263. keypad->input = input;
  264. keypad->tc3589x = tc3589x;
  265. input->id.bustype = BUS_I2C;
  266. input->name = pdev->name;
  267. input->dev.parent = &pdev->dev;
  268. input->open = tc3589x_keypad_open;
  269. input->close = tc3589x_keypad_close;
  270. error = matrix_keypad_build_keymap(plat->keymap_data, NULL,
  271. TC3589x_MAX_KPROW, TC3589x_MAX_KPCOL,
  272. NULL, input);
  273. if (error) {
  274. dev_err(&pdev->dev, "Failed to build keymap\n");
  275. goto err_free_mem;
  276. }
  277. keypad->keymap = input->keycode;
  278. input_set_capability(input, EV_MSC, MSC_SCAN);
  279. if (!plat->no_autorepeat)
  280. __set_bit(EV_REP, input->evbit);
  281. input_set_drvdata(input, keypad);
  282. error = request_threaded_irq(irq, NULL,
  283. tc3589x_keypad_irq, plat->irqtype,
  284. "tc3589x-keypad", keypad);
  285. if (error < 0) {
  286. dev_err(&pdev->dev,
  287. "Could not allocate irq %d,error %d\n",
  288. irq, error);
  289. goto err_free_mem;
  290. }
  291. error = input_register_device(input);
  292. if (error) {
  293. dev_err(&pdev->dev, "Could not register input device\n");
  294. goto err_free_irq;
  295. }
  296. /* let platform decide if keypad is a wakeup source or not */
  297. device_init_wakeup(&pdev->dev, plat->enable_wakeup);
  298. device_set_wakeup_capable(&pdev->dev, plat->enable_wakeup);
  299. platform_set_drvdata(pdev, keypad);
  300. return 0;
  301. err_free_irq:
  302. free_irq(irq, keypad);
  303. err_free_mem:
  304. input_free_device(input);
  305. kfree(keypad);
  306. return error;
  307. }
  308. static int tc3589x_keypad_remove(struct platform_device *pdev)
  309. {
  310. struct tc_keypad *keypad = platform_get_drvdata(pdev);
  311. int irq = platform_get_irq(pdev, 0);
  312. if (!keypad->keypad_stopped)
  313. tc3589x_keypad_disable(keypad);
  314. free_irq(irq, keypad);
  315. input_unregister_device(keypad->input);
  316. kfree(keypad);
  317. return 0;
  318. }
  319. #ifdef CONFIG_PM_SLEEP
  320. static int tc3589x_keypad_suspend(struct device *dev)
  321. {
  322. struct platform_device *pdev = to_platform_device(dev);
  323. struct tc_keypad *keypad = platform_get_drvdata(pdev);
  324. int irq = platform_get_irq(pdev, 0);
  325. /* keypad is already off; we do nothing */
  326. if (keypad->keypad_stopped)
  327. return 0;
  328. /* if device is not a wakeup source, disable it for powersave */
  329. if (!device_may_wakeup(&pdev->dev))
  330. tc3589x_keypad_disable(keypad);
  331. else
  332. enable_irq_wake(irq);
  333. return 0;
  334. }
  335. static int tc3589x_keypad_resume(struct device *dev)
  336. {
  337. struct platform_device *pdev = to_platform_device(dev);
  338. struct tc_keypad *keypad = platform_get_drvdata(pdev);
  339. int irq = platform_get_irq(pdev, 0);
  340. if (!keypad->keypad_stopped)
  341. return 0;
  342. /* enable the device to resume normal operations */
  343. if (!device_may_wakeup(&pdev->dev))
  344. tc3589x_keypad_enable(keypad);
  345. else
  346. disable_irq_wake(irq);
  347. return 0;
  348. }
  349. #endif
  350. static SIMPLE_DEV_PM_OPS(tc3589x_keypad_dev_pm_ops,
  351. tc3589x_keypad_suspend, tc3589x_keypad_resume);
  352. static struct platform_driver tc3589x_keypad_driver = {
  353. .driver = {
  354. .name = "tc3589x-keypad",
  355. .owner = THIS_MODULE,
  356. .pm = &tc3589x_keypad_dev_pm_ops,
  357. },
  358. .probe = tc3589x_keypad_probe,
  359. .remove = tc3589x_keypad_remove,
  360. };
  361. module_platform_driver(tc3589x_keypad_driver);
  362. MODULE_LICENSE("GPL v2");
  363. MODULE_AUTHOR("Jayeeta Banerjee/Sundar Iyer");
  364. MODULE_DESCRIPTION("TC35893 Keypad Driver");
  365. MODULE_ALIAS("platform:tc3589x-keypad");