tc3589x-keypad.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #define TC3589x_KBD_KEYMAP_SIZE 64
  59. /**
  60. * struct tc_keypad - data structure used by keypad driver
  61. * @tc3589x: pointer to tc35893
  62. * @input: pointer to input device object
  63. * @board: keypad platform device
  64. * @krow: number of rows
  65. * @kcol: number of coloumns
  66. * @keymap: matrix scan code table for keycodes
  67. * @keypad_stopped: holds keypad status
  68. */
  69. struct tc_keypad {
  70. struct tc3589x *tc3589x;
  71. struct input_dev *input;
  72. const struct tc3589x_keypad_platform_data *board;
  73. unsigned int krow;
  74. unsigned int kcol;
  75. unsigned short keymap[TC3589x_KBD_KEYMAP_SIZE];
  76. bool keypad_stopped;
  77. };
  78. static int tc3589x_keypad_init_key_hardware(struct tc_keypad *keypad)
  79. {
  80. int ret;
  81. struct tc3589x *tc3589x = keypad->tc3589x;
  82. const struct tc3589x_keypad_platform_data *board = keypad->board;
  83. /* validate platform configuration */
  84. if (board->kcol > TC3589x_MAX_KPCOL || board->krow > TC3589x_MAX_KPROW)
  85. return -EINVAL;
  86. /* configure KBDSIZE 4 LSbits for cols and 4 MSbits for rows */
  87. ret = tc3589x_reg_write(tc3589x, TC3589x_KBDSIZE,
  88. (board->krow << KP_ROW_SHIFT) | board->kcol);
  89. if (ret < 0)
  90. return ret;
  91. /* configure dedicated key config, no dedicated key selected */
  92. ret = tc3589x_reg_write(tc3589x, TC3589x_KBCFG_LSB, DEDICATED_KEY_VAL);
  93. if (ret < 0)
  94. return ret;
  95. ret = tc3589x_reg_write(tc3589x, TC3589x_KBCFG_MSB, DEDICATED_KEY_VAL);
  96. if (ret < 0)
  97. return ret;
  98. /* Configure settle time */
  99. ret = tc3589x_reg_write(tc3589x, TC3589x_KBDSETTLE_REG,
  100. board->settle_time);
  101. if (ret < 0)
  102. return ret;
  103. /* Configure debounce time */
  104. ret = tc3589x_reg_write(tc3589x, TC3589x_KBDBOUNCE,
  105. board->debounce_period);
  106. if (ret < 0)
  107. return ret;
  108. /* Start of initialise keypad GPIOs */
  109. ret = tc3589x_set_bits(tc3589x, TC3589x_IOCFG, 0x0, IOCFG_IG);
  110. if (ret < 0)
  111. return ret;
  112. /* Configure pull-up resistors for all row GPIOs */
  113. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG0_LSB,
  114. TC3589x_PULLUP_ALL_MASK);
  115. if (ret < 0)
  116. return ret;
  117. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG0_MSB,
  118. TC3589x_PULLUP_ALL_MASK);
  119. if (ret < 0)
  120. return ret;
  121. /* Configure pull-up resistors for all column GPIOs */
  122. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG1_LSB,
  123. TC3589x_PULLUP_ALL_MASK);
  124. if (ret < 0)
  125. return ret;
  126. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG1_MSB,
  127. TC3589x_PULLUP_ALL_MASK);
  128. if (ret < 0)
  129. return ret;
  130. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG2_LSB,
  131. TC3589x_PULLUP_ALL_MASK);
  132. return ret;
  133. }
  134. #define TC35893_DATA_REGS 4
  135. #define TC35893_KEYCODE_FIFO_EMPTY 0x7f
  136. #define TC35893_KEYCODE_FIFO_CLEAR 0xff
  137. #define TC35893_KEYPAD_ROW_SHIFT 0x3
  138. static irqreturn_t tc3589x_keypad_irq(int irq, void *dev)
  139. {
  140. struct tc_keypad *keypad = dev;
  141. struct tc3589x *tc3589x = keypad->tc3589x;
  142. u8 i, row_index, col_index, kbd_code, up;
  143. u8 code;
  144. for (i = 0; i < TC35893_DATA_REGS * 2; i++) {
  145. kbd_code = tc3589x_reg_read(tc3589x, TC3589x_EVTCODE_FIFO);
  146. /* loop till fifo is empty and no more keys are pressed */
  147. if (kbd_code == TC35893_KEYCODE_FIFO_EMPTY ||
  148. kbd_code == TC35893_KEYCODE_FIFO_CLEAR)
  149. continue;
  150. /* valid key is found */
  151. col_index = kbd_code & KP_EVCODE_COL_MASK;
  152. row_index = (kbd_code & KP_EVCODE_ROW_MASK) >> KP_ROW_SHIFT;
  153. code = MATRIX_SCAN_CODE(row_index, col_index,
  154. TC35893_KEYPAD_ROW_SHIFT);
  155. up = kbd_code & KP_RELEASE_EVT_MASK;
  156. input_event(keypad->input, EV_MSC, MSC_SCAN, code);
  157. input_report_key(keypad->input, keypad->keymap[code], !up);
  158. input_sync(keypad->input);
  159. }
  160. /* clear IRQ */
  161. tc3589x_set_bits(tc3589x, TC3589x_KBDIC,
  162. 0x0, TC3589x_EVT_INT_CLR | TC3589x_KBD_INT_CLR);
  163. /* enable IRQ */
  164. tc3589x_set_bits(tc3589x, TC3589x_KBDMSK,
  165. 0x0, TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT);
  166. return IRQ_HANDLED;
  167. }
  168. static int tc3589x_keypad_enable(struct tc_keypad *keypad)
  169. {
  170. struct tc3589x *tc3589x = keypad->tc3589x;
  171. int ret;
  172. /* pull the keypad module out of reset */
  173. ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL, TC3589x_KBDRST, 0x0);
  174. if (ret < 0)
  175. return ret;
  176. /* configure KBDMFS */
  177. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMFS, 0x0, TC3589x_KBDMFS_EN);
  178. if (ret < 0)
  179. return ret;
  180. /* enable the keypad clock */
  181. ret = tc3589x_set_bits(tc3589x, TC3589x_CLKEN, 0x0, KPD_CLK_EN);
  182. if (ret < 0)
  183. return ret;
  184. /* clear pending IRQs */
  185. ret = tc3589x_set_bits(tc3589x, TC3589x_RSTINTCLR, 0x0, 0x1);
  186. if (ret < 0)
  187. return ret;
  188. /* enable the IRQs */
  189. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMSK, 0x0,
  190. TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT);
  191. if (ret < 0)
  192. return ret;
  193. keypad->keypad_stopped = false;
  194. return ret;
  195. }
  196. static int tc3589x_keypad_disable(struct tc_keypad *keypad)
  197. {
  198. struct tc3589x *tc3589x = keypad->tc3589x;
  199. int ret;
  200. /* clear IRQ */
  201. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDIC,
  202. 0x0, TC3589x_EVT_INT_CLR | TC3589x_KBD_INT_CLR);
  203. if (ret < 0)
  204. return ret;
  205. /* disable all interrupts */
  206. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMSK,
  207. ~(TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT), 0x0);
  208. if (ret < 0)
  209. return ret;
  210. /* disable the keypad module */
  211. ret = tc3589x_set_bits(tc3589x, TC3589x_CLKEN, 0x1, 0x0);
  212. if (ret < 0)
  213. return ret;
  214. /* put the keypad module into reset */
  215. ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL, TC3589x_KBDRST, 0x1);
  216. keypad->keypad_stopped = true;
  217. return ret;
  218. }
  219. static int tc3589x_keypad_open(struct input_dev *input)
  220. {
  221. int error;
  222. struct tc_keypad *keypad = input_get_drvdata(input);
  223. /* enable the keypad module */
  224. error = tc3589x_keypad_enable(keypad);
  225. if (error < 0) {
  226. dev_err(&input->dev, "failed to enable keypad module\n");
  227. return error;
  228. }
  229. error = tc3589x_keypad_init_key_hardware(keypad);
  230. if (error < 0) {
  231. dev_err(&input->dev, "failed to configure keypad module\n");
  232. return error;
  233. }
  234. return 0;
  235. }
  236. static void tc3589x_keypad_close(struct input_dev *input)
  237. {
  238. struct tc_keypad *keypad = input_get_drvdata(input);
  239. /* disable the keypad module */
  240. tc3589x_keypad_disable(keypad);
  241. }
  242. static int __devinit tc3589x_keypad_probe(struct platform_device *pdev)
  243. {
  244. struct tc3589x *tc3589x = dev_get_drvdata(pdev->dev.parent);
  245. struct tc_keypad *keypad;
  246. struct input_dev *input;
  247. const struct tc3589x_keypad_platform_data *plat;
  248. int error, irq;
  249. plat = tc3589x->pdata->keypad;
  250. if (!plat) {
  251. dev_err(&pdev->dev, "invalid keypad platform data\n");
  252. return -EINVAL;
  253. }
  254. irq = platform_get_irq(pdev, 0);
  255. if (irq < 0)
  256. return irq;
  257. keypad = kzalloc(sizeof(struct tc_keypad), GFP_KERNEL);
  258. input = input_allocate_device();
  259. if (!keypad || !input) {
  260. dev_err(&pdev->dev, "failed to allocate keypad memory\n");
  261. error = -ENOMEM;
  262. goto err_free_mem;
  263. }
  264. keypad->board = plat;
  265. keypad->input = input;
  266. keypad->tc3589x = tc3589x;
  267. input->id.bustype = BUS_I2C;
  268. input->name = pdev->name;
  269. input->dev.parent = &pdev->dev;
  270. input->keycode = keypad->keymap;
  271. input->keycodesize = sizeof(keypad->keymap[0]);
  272. input->keycodemax = ARRAY_SIZE(keypad->keymap);
  273. input->open = tc3589x_keypad_open;
  274. input->close = tc3589x_keypad_close;
  275. input_set_drvdata(input, keypad);
  276. input_set_capability(input, EV_MSC, MSC_SCAN);
  277. __set_bit(EV_KEY, input->evbit);
  278. if (!plat->no_autorepeat)
  279. __set_bit(EV_REP, input->evbit);
  280. matrix_keypad_build_keymap(plat->keymap_data, 0x3,
  281. input->keycode, input->keybit);
  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 __devexit 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 = __devexit_p(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");