tc3589x-keypad.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. #ifdef CONFIG_OF
  241. static const struct tc3589x_keypad_platform_data *
  242. tc3589x_keypad_of_probe(struct device *dev)
  243. {
  244. struct device_node *np = dev->of_node;
  245. struct tc3589x_keypad_platform_data *plat;
  246. u32 cols, rows;
  247. u32 debounce_ms;
  248. int proplen;
  249. if (!np)
  250. return ERR_PTR(-ENODEV);
  251. plat = devm_kzalloc(dev, sizeof(*plat), GFP_KERNEL);
  252. if (!plat)
  253. return ERR_PTR(-ENOMEM);
  254. of_property_read_u32(np, "keypad,num-columns", &cols);
  255. of_property_read_u32(np, "keypad,num-rows", &rows);
  256. plat->kcol = (u8) cols;
  257. plat->krow = (u8) rows;
  258. if (!plat->krow || !plat->kcol ||
  259. plat->krow > TC_KPD_ROWS || plat->kcol > TC_KPD_COLUMNS) {
  260. dev_err(dev,
  261. "keypad columns/rows not properly specified (%ux%u)\n",
  262. plat->kcol, plat->krow);
  263. return ERR_PTR(-EINVAL);
  264. }
  265. if (!of_get_property(np, "linux,keymap", &proplen)) {
  266. dev_err(dev, "property linux,keymap not found\n");
  267. return ERR_PTR(-ENOENT);
  268. }
  269. plat->no_autorepeat = of_property_read_bool(np, "linux,no-autorepeat");
  270. plat->enable_wakeup = of_property_read_bool(np, "linux,wakeup");
  271. /* The custom delay format is ms/16 */
  272. of_property_read_u32(np, "debounce-delay-ms", &debounce_ms);
  273. if (debounce_ms)
  274. plat->debounce_period = debounce_ms * 16;
  275. else
  276. plat->debounce_period = TC_KPD_DEBOUNCE_PERIOD;
  277. plat->settle_time = TC_KPD_SETTLE_TIME;
  278. /* FIXME: should be property of the IRQ resource? */
  279. plat->irqtype = IRQF_TRIGGER_FALLING;
  280. return plat;
  281. }
  282. #else
  283. static inline const struct tc3589x_keypad_platform_data *
  284. tc3589x_keypad_of_probe(struct device *dev)
  285. {
  286. return ERR_PTR(-ENODEV);
  287. }
  288. #endif
  289. static int tc3589x_keypad_probe(struct platform_device *pdev)
  290. {
  291. struct tc3589x *tc3589x = dev_get_drvdata(pdev->dev.parent);
  292. struct tc_keypad *keypad;
  293. struct input_dev *input;
  294. const struct tc3589x_keypad_platform_data *plat;
  295. int error, irq;
  296. plat = tc3589x->pdata->keypad;
  297. if (!plat) {
  298. plat = tc3589x_keypad_of_probe(&pdev->dev);
  299. if (IS_ERR(plat)) {
  300. dev_err(&pdev->dev, "invalid keypad platform data\n");
  301. return PTR_ERR(plat);
  302. }
  303. }
  304. irq = platform_get_irq(pdev, 0);
  305. if (irq < 0)
  306. return irq;
  307. keypad = kzalloc(sizeof(struct tc_keypad), GFP_KERNEL);
  308. input = input_allocate_device();
  309. if (!keypad || !input) {
  310. dev_err(&pdev->dev, "failed to allocate keypad memory\n");
  311. error = -ENOMEM;
  312. goto err_free_mem;
  313. }
  314. keypad->board = plat;
  315. keypad->input = input;
  316. keypad->tc3589x = tc3589x;
  317. input->id.bustype = BUS_I2C;
  318. input->name = pdev->name;
  319. input->dev.parent = &pdev->dev;
  320. input->open = tc3589x_keypad_open;
  321. input->close = tc3589x_keypad_close;
  322. error = matrix_keypad_build_keymap(plat->keymap_data, NULL,
  323. TC3589x_MAX_KPROW, TC3589x_MAX_KPCOL,
  324. NULL, input);
  325. if (error) {
  326. dev_err(&pdev->dev, "Failed to build keymap\n");
  327. goto err_free_mem;
  328. }
  329. keypad->keymap = input->keycode;
  330. input_set_capability(input, EV_MSC, MSC_SCAN);
  331. if (!plat->no_autorepeat)
  332. __set_bit(EV_REP, input->evbit);
  333. input_set_drvdata(input, keypad);
  334. error = request_threaded_irq(irq, NULL,
  335. tc3589x_keypad_irq, plat->irqtype,
  336. "tc3589x-keypad", keypad);
  337. if (error < 0) {
  338. dev_err(&pdev->dev,
  339. "Could not allocate irq %d,error %d\n",
  340. irq, error);
  341. goto err_free_mem;
  342. }
  343. error = input_register_device(input);
  344. if (error) {
  345. dev_err(&pdev->dev, "Could not register input device\n");
  346. goto err_free_irq;
  347. }
  348. /* let platform decide if keypad is a wakeup source or not */
  349. device_init_wakeup(&pdev->dev, plat->enable_wakeup);
  350. device_set_wakeup_capable(&pdev->dev, plat->enable_wakeup);
  351. platform_set_drvdata(pdev, keypad);
  352. return 0;
  353. err_free_irq:
  354. free_irq(irq, keypad);
  355. err_free_mem:
  356. input_free_device(input);
  357. kfree(keypad);
  358. return error;
  359. }
  360. static int tc3589x_keypad_remove(struct platform_device *pdev)
  361. {
  362. struct tc_keypad *keypad = platform_get_drvdata(pdev);
  363. int irq = platform_get_irq(pdev, 0);
  364. if (!keypad->keypad_stopped)
  365. tc3589x_keypad_disable(keypad);
  366. free_irq(irq, keypad);
  367. input_unregister_device(keypad->input);
  368. kfree(keypad);
  369. return 0;
  370. }
  371. #ifdef CONFIG_PM_SLEEP
  372. static int tc3589x_keypad_suspend(struct device *dev)
  373. {
  374. struct platform_device *pdev = to_platform_device(dev);
  375. struct tc_keypad *keypad = platform_get_drvdata(pdev);
  376. int irq = platform_get_irq(pdev, 0);
  377. /* keypad is already off; we do nothing */
  378. if (keypad->keypad_stopped)
  379. return 0;
  380. /* if device is not a wakeup source, disable it for powersave */
  381. if (!device_may_wakeup(&pdev->dev))
  382. tc3589x_keypad_disable(keypad);
  383. else
  384. enable_irq_wake(irq);
  385. return 0;
  386. }
  387. static int tc3589x_keypad_resume(struct device *dev)
  388. {
  389. struct platform_device *pdev = to_platform_device(dev);
  390. struct tc_keypad *keypad = platform_get_drvdata(pdev);
  391. int irq = platform_get_irq(pdev, 0);
  392. if (!keypad->keypad_stopped)
  393. return 0;
  394. /* enable the device to resume normal operations */
  395. if (!device_may_wakeup(&pdev->dev))
  396. tc3589x_keypad_enable(keypad);
  397. else
  398. disable_irq_wake(irq);
  399. return 0;
  400. }
  401. #endif
  402. static SIMPLE_DEV_PM_OPS(tc3589x_keypad_dev_pm_ops,
  403. tc3589x_keypad_suspend, tc3589x_keypad_resume);
  404. static struct platform_driver tc3589x_keypad_driver = {
  405. .driver = {
  406. .name = "tc3589x-keypad",
  407. .owner = THIS_MODULE,
  408. .pm = &tc3589x_keypad_dev_pm_ops,
  409. },
  410. .probe = tc3589x_keypad_probe,
  411. .remove = tc3589x_keypad_remove,
  412. };
  413. module_platform_driver(tc3589x_keypad_driver);
  414. MODULE_LICENSE("GPL v2");
  415. MODULE_AUTHOR("Jayeeta Banerjee/Sundar Iyer");
  416. MODULE_DESCRIPTION("TC35893 Keypad Driver");
  417. MODULE_ALIAS("platform:tc3589x-keypad");