tc3589x-keypad.c 11 KB

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