tc3589x-keypad.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. u8 settle_time = keypad->board->settle_time;
  83. u8 dbounce_period = keypad->board->debounce_period;
  84. u8 rows = keypad->board->krow & 0xf; /* mask out the nibble */
  85. u8 column = keypad->board->kcol & 0xf; /* mask out the nibble */
  86. /* validate platform configurations */
  87. if (keypad->board->kcol > TC3589x_MAX_KPCOL ||
  88. keypad->board->krow > TC3589x_MAX_KPROW ||
  89. keypad->board->debounce_period > TC3589x_MAX_DEBOUNCE_SETTLE ||
  90. keypad->board->settle_time > TC3589x_MAX_DEBOUNCE_SETTLE)
  91. return -EINVAL;
  92. /* configure KBDSIZE 4 LSbits for cols and 4 MSbits for rows */
  93. ret = tc3589x_reg_write(tc3589x, TC3589x_KBDSIZE,
  94. (rows << KP_ROW_SHIFT) | column);
  95. if (ret < 0)
  96. return ret;
  97. /* configure dedicated key config, no dedicated key selected */
  98. ret = tc3589x_reg_write(tc3589x, TC3589x_KBCFG_LSB, DEDICATED_KEY_VAL);
  99. if (ret < 0)
  100. return ret;
  101. ret = tc3589x_reg_write(tc3589x, TC3589x_KBCFG_MSB, DEDICATED_KEY_VAL);
  102. if (ret < 0)
  103. return ret;
  104. /* Configure settle time */
  105. ret = tc3589x_reg_write(tc3589x, TC3589x_KBDSETTLE_REG, settle_time);
  106. if (ret < 0)
  107. return ret;
  108. /* Configure debounce time */
  109. ret = tc3589x_reg_write(tc3589x, TC3589x_KBDBOUNCE, dbounce_period);
  110. if (ret < 0)
  111. return ret;
  112. /* Start of initialise keypad GPIOs */
  113. ret = tc3589x_set_bits(tc3589x, TC3589x_IOCFG, 0x0, IOCFG_IG);
  114. if (ret < 0)
  115. return ret;
  116. /* Configure pull-up resistors for all row GPIOs */
  117. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG0_LSB,
  118. TC3589x_PULLUP_ALL_MASK);
  119. if (ret < 0)
  120. return ret;
  121. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG0_MSB,
  122. TC3589x_PULLUP_ALL_MASK);
  123. if (ret < 0)
  124. return ret;
  125. /* Configure pull-up resistors for all column GPIOs */
  126. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG1_LSB,
  127. TC3589x_PULLUP_ALL_MASK);
  128. if (ret < 0)
  129. return ret;
  130. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG1_MSB,
  131. TC3589x_PULLUP_ALL_MASK);
  132. if (ret < 0)
  133. return ret;
  134. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG2_LSB,
  135. TC3589x_PULLUP_ALL_MASK);
  136. return ret;
  137. }
  138. #define TC35893_DATA_REGS 4
  139. #define TC35893_KEYCODE_FIFO_EMPTY 0x7f
  140. #define TC35893_KEYCODE_FIFO_CLEAR 0xff
  141. #define TC35893_KEYPAD_ROW_SHIFT 0x3
  142. static irqreturn_t tc3589x_keypad_irq(int irq, void *dev)
  143. {
  144. struct tc_keypad *keypad = dev;
  145. struct tc3589x *tc3589x = keypad->tc3589x;
  146. u8 i, row_index, col_index, kbd_code, up;
  147. u8 code;
  148. for (i = 0; i < TC35893_DATA_REGS * 2; i++) {
  149. kbd_code = tc3589x_reg_read(tc3589x, TC3589x_EVTCODE_FIFO);
  150. /* loop till fifo is empty and no more keys are pressed */
  151. if (kbd_code == TC35893_KEYCODE_FIFO_EMPTY ||
  152. kbd_code == TC35893_KEYCODE_FIFO_CLEAR)
  153. continue;
  154. /* valid key is found */
  155. col_index = kbd_code & KP_EVCODE_COL_MASK;
  156. row_index = (kbd_code & KP_EVCODE_ROW_MASK) >> KP_ROW_SHIFT;
  157. code = MATRIX_SCAN_CODE(row_index, col_index,
  158. TC35893_KEYPAD_ROW_SHIFT);
  159. up = kbd_code & KP_RELEASE_EVT_MASK;
  160. input_event(keypad->input, EV_MSC, MSC_SCAN, code);
  161. input_report_key(keypad->input, keypad->keymap[code], !up);
  162. input_sync(keypad->input);
  163. }
  164. /* clear IRQ */
  165. tc3589x_set_bits(tc3589x, TC3589x_KBDIC,
  166. 0x0, TC3589x_EVT_INT_CLR | TC3589x_KBD_INT_CLR);
  167. /* enable IRQ */
  168. tc3589x_set_bits(tc3589x, TC3589x_KBDMSK,
  169. 0x0, TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT);
  170. return IRQ_HANDLED;
  171. }
  172. static int tc3589x_keypad_enable(struct tc_keypad *keypad)
  173. {
  174. struct tc3589x *tc3589x = keypad->tc3589x;
  175. int ret;
  176. /* pull the keypad module out of reset */
  177. ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL, TC3589x_KBDRST, 0x0);
  178. if (ret < 0)
  179. return ret;
  180. /* configure KBDMFS */
  181. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMFS, 0x0, TC3589x_KBDMFS_EN);
  182. if (ret < 0)
  183. return ret;
  184. /* enable the keypad clock */
  185. ret = tc3589x_set_bits(tc3589x, TC3589x_CLKEN, 0x0, KPD_CLK_EN);
  186. if (ret < 0)
  187. return ret;
  188. /* clear pending IRQs */
  189. ret = tc3589x_set_bits(tc3589x, TC3589x_RSTINTCLR, 0x0, 0x1);
  190. if (ret < 0)
  191. return ret;
  192. /* enable the IRQs */
  193. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMSK, 0x0,
  194. TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT);
  195. if (ret < 0)
  196. return ret;
  197. keypad->keypad_stopped = false;
  198. return ret;
  199. }
  200. static int tc3589x_keypad_disable(struct tc_keypad *keypad)
  201. {
  202. struct tc3589x *tc3589x = keypad->tc3589x;
  203. int ret;
  204. /* clear IRQ */
  205. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDIC,
  206. 0x0, TC3589x_EVT_INT_CLR | TC3589x_KBD_INT_CLR);
  207. if (ret < 0)
  208. return ret;
  209. /* disable all interrupts */
  210. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMSK,
  211. ~(TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT), 0x0);
  212. if (ret < 0)
  213. return ret;
  214. /* disable the keypad module */
  215. ret = tc3589x_set_bits(tc3589x, TC3589x_CLKEN, 0x1, 0x0);
  216. if (ret < 0)
  217. return ret;
  218. /* put the keypad module into reset */
  219. ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL, TC3589x_KBDRST, 0x1);
  220. keypad->keypad_stopped = true;
  221. return ret;
  222. }
  223. static int tc3589x_keypad_open(struct input_dev *input)
  224. {
  225. int error;
  226. struct tc_keypad *keypad = input_get_drvdata(input);
  227. /* enable the keypad module */
  228. error = tc3589x_keypad_enable(keypad);
  229. if (error < 0) {
  230. dev_err(&input->dev, "failed to enable keypad module\n");
  231. return error;
  232. }
  233. error = tc3589x_keypad_init_key_hardware(keypad);
  234. if (error < 0) {
  235. dev_err(&input->dev, "failed to configure keypad module\n");
  236. return error;
  237. }
  238. return 0;
  239. }
  240. static void tc3589x_keypad_close(struct input_dev *input)
  241. {
  242. struct tc_keypad *keypad = input_get_drvdata(input);
  243. /* disable the keypad module */
  244. tc3589x_keypad_disable(keypad);
  245. }
  246. static int __devinit tc3589x_keypad_probe(struct platform_device *pdev)
  247. {
  248. struct tc3589x *tc3589x = dev_get_drvdata(pdev->dev.parent);
  249. struct tc_keypad *keypad;
  250. struct input_dev *input;
  251. const struct tc3589x_keypad_platform_data *plat;
  252. int error, irq;
  253. plat = tc3589x->pdata->keypad;
  254. if (!plat) {
  255. dev_err(&pdev->dev, "invalid keypad platform data\n");
  256. return -EINVAL;
  257. }
  258. irq = platform_get_irq(pdev, 0);
  259. if (irq < 0)
  260. return irq;
  261. keypad = kzalloc(sizeof(struct tc_keypad), GFP_KERNEL);
  262. input = input_allocate_device();
  263. if (!keypad || !input) {
  264. dev_err(&pdev->dev, "failed to allocate keypad memory\n");
  265. error = -ENOMEM;
  266. goto err_free_mem;
  267. }
  268. keypad->board = plat;
  269. keypad->input = input;
  270. keypad->tc3589x = tc3589x;
  271. input->id.bustype = BUS_I2C;
  272. input->name = pdev->name;
  273. input->dev.parent = &pdev->dev;
  274. input->keycode = keypad->keymap;
  275. input->keycodesize = sizeof(keypad->keymap[0]);
  276. input->keycodemax = ARRAY_SIZE(keypad->keymap);
  277. input->open = tc3589x_keypad_open;
  278. input->close = tc3589x_keypad_close;
  279. input_set_drvdata(input, keypad);
  280. input_set_capability(input, EV_MSC, MSC_SCAN);
  281. __set_bit(EV_KEY, input->evbit);
  282. if (!plat->no_autorepeat)
  283. __set_bit(EV_REP, input->evbit);
  284. matrix_keypad_build_keymap(plat->keymap_data, 0x3,
  285. input->keycode, input->keybit);
  286. error = request_threaded_irq(irq, NULL,
  287. tc3589x_keypad_irq, plat->irqtype,
  288. "tc3589x-keypad", keypad);
  289. if (error < 0) {
  290. dev_err(&pdev->dev,
  291. "Could not allocate irq %d,error %d\n",
  292. irq, error);
  293. goto err_free_mem;
  294. }
  295. error = input_register_device(input);
  296. if (error) {
  297. dev_err(&pdev->dev, "Could not register input device\n");
  298. goto err_free_irq;
  299. }
  300. /* let platform decide if keypad is a wakeup source or not */
  301. device_init_wakeup(&pdev->dev, plat->enable_wakeup);
  302. device_set_wakeup_capable(&pdev->dev, plat->enable_wakeup);
  303. platform_set_drvdata(pdev, keypad);
  304. return 0;
  305. err_free_irq:
  306. free_irq(irq, keypad);
  307. err_free_mem:
  308. input_free_device(input);
  309. kfree(keypad);
  310. return error;
  311. }
  312. static int __devexit tc3589x_keypad_remove(struct platform_device *pdev)
  313. {
  314. struct tc_keypad *keypad = platform_get_drvdata(pdev);
  315. int irq = platform_get_irq(pdev, 0);
  316. if (!keypad->keypad_stopped)
  317. tc3589x_keypad_disable(keypad);
  318. free_irq(irq, keypad);
  319. input_unregister_device(keypad->input);
  320. kfree(keypad);
  321. return 0;
  322. }
  323. #ifdef CONFIG_PM_SLEEP
  324. static int tc3589x_keypad_suspend(struct device *dev)
  325. {
  326. struct platform_device *pdev = to_platform_device(dev);
  327. struct tc_keypad *keypad = platform_get_drvdata(pdev);
  328. int irq = platform_get_irq(pdev, 0);
  329. /* keypad is already off; we do nothing */
  330. if (keypad->keypad_stopped)
  331. return 0;
  332. /* if device is not a wakeup source, disable it for powersave */
  333. if (!device_may_wakeup(&pdev->dev))
  334. tc3589x_keypad_disable(keypad);
  335. else
  336. enable_irq_wake(irq);
  337. return 0;
  338. }
  339. static int tc3589x_keypad_resume(struct device *dev)
  340. {
  341. struct platform_device *pdev = to_platform_device(dev);
  342. struct tc_keypad *keypad = platform_get_drvdata(pdev);
  343. int irq = platform_get_irq(pdev, 0);
  344. if (!keypad->keypad_stopped)
  345. return 0;
  346. /* enable the device to resume normal operations */
  347. if (!device_may_wakeup(&pdev->dev))
  348. tc3589x_keypad_enable(keypad);
  349. else
  350. disable_irq_wake(irq);
  351. return 0;
  352. }
  353. #endif
  354. static SIMPLE_DEV_PM_OPS(tc3589x_keypad_dev_pm_ops,
  355. tc3589x_keypad_suspend, tc3589x_keypad_resume);
  356. static struct platform_driver tc3589x_keypad_driver = {
  357. .driver = {
  358. .name = "tc3589x-keypad",
  359. .owner = THIS_MODULE,
  360. .pm = &tc3589x_keypad_dev_pm_ops,
  361. },
  362. .probe = tc3589x_keypad_probe,
  363. .remove = __devexit_p(tc3589x_keypad_remove),
  364. };
  365. module_platform_driver(tc3589x_keypad_driver);
  366. MODULE_LICENSE("GPL v2");
  367. MODULE_AUTHOR("Jayeeta Banerjee/Sundar Iyer");
  368. MODULE_DESCRIPTION("TC35893 Keypad Driver");
  369. MODULE_ALIAS("platform:tc3589x-keypad");