tc3589x-keypad.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2010
  4. *
  5. * Author: Jayeeta Banerjee <jayeeta.banerjee@stericsson.com>
  6. * Author: Sundar Iyer <sundar.iyer@stericsson.com>
  7. *
  8. * TC35893 MFD Keypad Controller driver
  9. */
  10. #include <linux/module.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/input.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/input/matrix_keypad.h>
  15. #include <linux/i2c.h>
  16. #include <linux/slab.h>
  17. #include <linux/mfd/tc3589x.h>
  18. #include <linux/device.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 tc35893_keypad_platform_data - platform specific keypad data
  59. * @keymap_data: matrix scan code table for keycodes
  60. * @krow: mask for available rows, value is 0xFF
  61. * @kcol: mask for available columns, value is 0xFF
  62. * @debounce_period: platform specific debounce time
  63. * @settle_time: platform specific settle down time
  64. * @irqtype: type of interrupt, falling or rising edge
  65. * @enable_wakeup: specifies if keypad event can wake up system from sleep
  66. * @no_autorepeat: flag for auto repetition
  67. */
  68. struct tc3589x_keypad_platform_data {
  69. const struct matrix_keymap_data *keymap_data;
  70. u8 krow;
  71. u8 kcol;
  72. u8 debounce_period;
  73. u8 settle_time;
  74. unsigned long irqtype;
  75. bool enable_wakeup;
  76. bool no_autorepeat;
  77. };
  78. /**
  79. * struct tc_keypad - data structure used by keypad driver
  80. * @tc3589x: pointer to tc35893
  81. * @input: pointer to input device object
  82. * @board: keypad platform device
  83. * @krow: number of rows
  84. * @kcol: number of columns
  85. * @keymap: matrix scan code table for keycodes
  86. * @keypad_stopped: holds keypad status
  87. */
  88. struct tc_keypad {
  89. struct tc3589x *tc3589x;
  90. struct input_dev *input;
  91. const struct tc3589x_keypad_platform_data *board;
  92. unsigned int krow;
  93. unsigned int kcol;
  94. unsigned short *keymap;
  95. bool keypad_stopped;
  96. };
  97. static int tc3589x_keypad_init_key_hardware(struct tc_keypad *keypad)
  98. {
  99. int ret;
  100. struct tc3589x *tc3589x = keypad->tc3589x;
  101. const struct tc3589x_keypad_platform_data *board = keypad->board;
  102. /* validate platform configuration */
  103. if (board->kcol > TC3589x_MAX_KPCOL || board->krow > TC3589x_MAX_KPROW)
  104. return -EINVAL;
  105. /* configure KBDSIZE 4 LSbits for cols and 4 MSbits for rows */
  106. ret = tc3589x_reg_write(tc3589x, TC3589x_KBDSIZE,
  107. (board->krow << KP_ROW_SHIFT) | board->kcol);
  108. if (ret < 0)
  109. return ret;
  110. /* configure dedicated key config, no dedicated key selected */
  111. ret = tc3589x_reg_write(tc3589x, TC3589x_KBCFG_LSB, DEDICATED_KEY_VAL);
  112. if (ret < 0)
  113. return ret;
  114. ret = tc3589x_reg_write(tc3589x, TC3589x_KBCFG_MSB, DEDICATED_KEY_VAL);
  115. if (ret < 0)
  116. return ret;
  117. /* Configure settle time */
  118. ret = tc3589x_reg_write(tc3589x, TC3589x_KBDSETTLE_REG,
  119. board->settle_time);
  120. if (ret < 0)
  121. return ret;
  122. /* Configure debounce time */
  123. ret = tc3589x_reg_write(tc3589x, TC3589x_KBDBOUNCE,
  124. board->debounce_period);
  125. if (ret < 0)
  126. return ret;
  127. /* Start of initialise keypad GPIOs */
  128. ret = tc3589x_set_bits(tc3589x, TC3589x_IOCFG, 0x0, IOCFG_IG);
  129. if (ret < 0)
  130. return ret;
  131. /* Configure pull-up resistors for all row GPIOs */
  132. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG0_LSB,
  133. TC3589x_PULLUP_ALL_MASK);
  134. if (ret < 0)
  135. return ret;
  136. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG0_MSB,
  137. TC3589x_PULLUP_ALL_MASK);
  138. if (ret < 0)
  139. return ret;
  140. /* Configure pull-up resistors for all column GPIOs */
  141. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG1_LSB,
  142. TC3589x_PULLUP_ALL_MASK);
  143. if (ret < 0)
  144. return ret;
  145. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG1_MSB,
  146. TC3589x_PULLUP_ALL_MASK);
  147. if (ret < 0)
  148. return ret;
  149. ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG2_LSB,
  150. TC3589x_PULLUP_ALL_MASK);
  151. return ret;
  152. }
  153. #define TC35893_DATA_REGS 4
  154. #define TC35893_KEYCODE_FIFO_EMPTY 0x7f
  155. #define TC35893_KEYCODE_FIFO_CLEAR 0xff
  156. #define TC35893_KEYPAD_ROW_SHIFT 0x3
  157. static irqreturn_t tc3589x_keypad_irq(int irq, void *dev)
  158. {
  159. struct tc_keypad *keypad = dev;
  160. struct tc3589x *tc3589x = keypad->tc3589x;
  161. u8 i, row_index, col_index, kbd_code, up;
  162. u8 code;
  163. for (i = 0; i < TC35893_DATA_REGS * 2; i++) {
  164. kbd_code = tc3589x_reg_read(tc3589x, TC3589x_EVTCODE_FIFO);
  165. /* loop till fifo is empty and no more keys are pressed */
  166. if (kbd_code == TC35893_KEYCODE_FIFO_EMPTY ||
  167. kbd_code == TC35893_KEYCODE_FIFO_CLEAR)
  168. continue;
  169. /* valid key is found */
  170. col_index = kbd_code & KP_EVCODE_COL_MASK;
  171. row_index = (kbd_code & KP_EVCODE_ROW_MASK) >> KP_ROW_SHIFT;
  172. code = MATRIX_SCAN_CODE(row_index, col_index,
  173. TC35893_KEYPAD_ROW_SHIFT);
  174. up = kbd_code & KP_RELEASE_EVT_MASK;
  175. input_event(keypad->input, EV_MSC, MSC_SCAN, code);
  176. input_report_key(keypad->input, keypad->keymap[code], !up);
  177. input_sync(keypad->input);
  178. }
  179. /* clear IRQ */
  180. tc3589x_set_bits(tc3589x, TC3589x_KBDIC,
  181. 0x0, TC3589x_EVT_INT_CLR | TC3589x_KBD_INT_CLR);
  182. /* enable IRQ */
  183. tc3589x_set_bits(tc3589x, TC3589x_KBDMSK,
  184. 0x0, TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT);
  185. return IRQ_HANDLED;
  186. }
  187. static int tc3589x_keypad_enable(struct tc_keypad *keypad)
  188. {
  189. struct tc3589x *tc3589x = keypad->tc3589x;
  190. int ret;
  191. /* pull the keypad module out of reset */
  192. ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL, TC3589x_KBDRST, 0x0);
  193. if (ret < 0)
  194. return ret;
  195. /* configure KBDMFS */
  196. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMFS, 0x0, TC3589x_KBDMFS_EN);
  197. if (ret < 0)
  198. return ret;
  199. /* enable the keypad clock */
  200. ret = tc3589x_set_bits(tc3589x, TC3589x_CLKEN, 0x0, KPD_CLK_EN);
  201. if (ret < 0)
  202. return ret;
  203. /* clear pending IRQs */
  204. ret = tc3589x_set_bits(tc3589x, TC3589x_RSTINTCLR, 0x0, 0x1);
  205. if (ret < 0)
  206. return ret;
  207. /* enable the IRQs */
  208. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMSK, 0x0,
  209. TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT);
  210. if (ret < 0)
  211. return ret;
  212. keypad->keypad_stopped = false;
  213. return ret;
  214. }
  215. static int tc3589x_keypad_disable(struct tc_keypad *keypad)
  216. {
  217. struct tc3589x *tc3589x = keypad->tc3589x;
  218. int ret;
  219. /* clear IRQ */
  220. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDIC,
  221. 0x0, TC3589x_EVT_INT_CLR | TC3589x_KBD_INT_CLR);
  222. if (ret < 0)
  223. return ret;
  224. /* disable all interrupts */
  225. ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMSK,
  226. ~(TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT), 0x0);
  227. if (ret < 0)
  228. return ret;
  229. /* disable the keypad module */
  230. ret = tc3589x_set_bits(tc3589x, TC3589x_CLKEN, 0x1, 0x0);
  231. if (ret < 0)
  232. return ret;
  233. /* put the keypad module into reset */
  234. ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL, TC3589x_KBDRST, 0x1);
  235. keypad->keypad_stopped = true;
  236. return ret;
  237. }
  238. static int tc3589x_keypad_open(struct input_dev *input)
  239. {
  240. int error;
  241. struct tc_keypad *keypad = input_get_drvdata(input);
  242. /* enable the keypad module */
  243. error = tc3589x_keypad_enable(keypad);
  244. if (error < 0) {
  245. dev_err(&input->dev, "failed to enable keypad module\n");
  246. return error;
  247. }
  248. error = tc3589x_keypad_init_key_hardware(keypad);
  249. if (error < 0) {
  250. dev_err(&input->dev, "failed to configure keypad module\n");
  251. return error;
  252. }
  253. return 0;
  254. }
  255. static void tc3589x_keypad_close(struct input_dev *input)
  256. {
  257. struct tc_keypad *keypad = input_get_drvdata(input);
  258. /* disable the keypad module */
  259. tc3589x_keypad_disable(keypad);
  260. }
  261. static const struct tc3589x_keypad_platform_data *
  262. tc3589x_keypad_of_probe(struct device *dev)
  263. {
  264. struct device_node *np = dev->of_node;
  265. struct tc3589x_keypad_platform_data *plat;
  266. u32 cols, rows;
  267. u32 debounce_ms;
  268. int proplen;
  269. if (!np)
  270. return ERR_PTR(-ENODEV);
  271. plat = devm_kzalloc(dev, sizeof(*plat), GFP_KERNEL);
  272. if (!plat)
  273. return ERR_PTR(-ENOMEM);
  274. of_property_read_u32(np, "keypad,num-columns", &cols);
  275. of_property_read_u32(np, "keypad,num-rows", &rows);
  276. plat->kcol = (u8) cols;
  277. plat->krow = (u8) rows;
  278. if (!plat->krow || !plat->kcol ||
  279. plat->krow > TC_KPD_ROWS || plat->kcol > TC_KPD_COLUMNS) {
  280. dev_err(dev,
  281. "keypad columns/rows not properly specified (%ux%u)\n",
  282. plat->kcol, plat->krow);
  283. return ERR_PTR(-EINVAL);
  284. }
  285. if (!of_get_property(np, "linux,keymap", &proplen)) {
  286. dev_err(dev, "property linux,keymap not found\n");
  287. return ERR_PTR(-ENOENT);
  288. }
  289. plat->no_autorepeat = of_property_read_bool(np, "linux,no-autorepeat");
  290. plat->enable_wakeup = of_property_read_bool(np, "wakeup-source") ||
  291. /* legacy name */
  292. of_property_read_bool(np, "linux,wakeup");
  293. /* The custom delay format is ms/16 */
  294. of_property_read_u32(np, "debounce-delay-ms", &debounce_ms);
  295. if (debounce_ms)
  296. plat->debounce_period = debounce_ms * 16;
  297. else
  298. plat->debounce_period = TC_KPD_DEBOUNCE_PERIOD;
  299. plat->settle_time = TC_KPD_SETTLE_TIME;
  300. /* FIXME: should be property of the IRQ resource? */
  301. plat->irqtype = IRQF_TRIGGER_FALLING;
  302. return plat;
  303. }
  304. static int tc3589x_keypad_probe(struct platform_device *pdev)
  305. {
  306. struct tc3589x *tc3589x = dev_get_drvdata(pdev->dev.parent);
  307. struct tc_keypad *keypad;
  308. struct input_dev *input;
  309. const struct tc3589x_keypad_platform_data *plat;
  310. int error, irq;
  311. plat = tc3589x_keypad_of_probe(&pdev->dev);
  312. if (IS_ERR(plat)) {
  313. dev_err(&pdev->dev, "invalid keypad platform data\n");
  314. return PTR_ERR(plat);
  315. }
  316. irq = platform_get_irq(pdev, 0);
  317. if (irq < 0)
  318. return irq;
  319. keypad = devm_kzalloc(&pdev->dev, sizeof(struct tc_keypad),
  320. GFP_KERNEL);
  321. if (!keypad)
  322. return -ENOMEM;
  323. input = devm_input_allocate_device(&pdev->dev);
  324. if (!input) {
  325. dev_err(&pdev->dev, "failed to allocate input device\n");
  326. return -ENOMEM;
  327. }
  328. keypad->board = plat;
  329. keypad->input = input;
  330. keypad->tc3589x = tc3589x;
  331. input->id.bustype = BUS_I2C;
  332. input->name = pdev->name;
  333. input->dev.parent = &pdev->dev;
  334. input->open = tc3589x_keypad_open;
  335. input->close = tc3589x_keypad_close;
  336. error = matrix_keypad_build_keymap(plat->keymap_data, NULL,
  337. TC3589x_MAX_KPROW, TC3589x_MAX_KPCOL,
  338. NULL, input);
  339. if (error) {
  340. dev_err(&pdev->dev, "Failed to build keymap\n");
  341. return error;
  342. }
  343. keypad->keymap = input->keycode;
  344. input_set_capability(input, EV_MSC, MSC_SCAN);
  345. if (!plat->no_autorepeat)
  346. __set_bit(EV_REP, input->evbit);
  347. input_set_drvdata(input, keypad);
  348. tc3589x_keypad_disable(keypad);
  349. error = devm_request_threaded_irq(&pdev->dev, irq,
  350. NULL, tc3589x_keypad_irq,
  351. plat->irqtype | IRQF_ONESHOT,
  352. "tc3589x-keypad", keypad);
  353. if (error) {
  354. dev_err(&pdev->dev,
  355. "Could not allocate irq %d,error %d\n",
  356. irq, error);
  357. return error;
  358. }
  359. error = input_register_device(input);
  360. if (error) {
  361. dev_err(&pdev->dev, "Could not register input device\n");
  362. return error;
  363. }
  364. /* let platform decide if keypad is a wakeup source or not */
  365. device_init_wakeup(&pdev->dev, plat->enable_wakeup);
  366. device_set_wakeup_capable(&pdev->dev, plat->enable_wakeup);
  367. platform_set_drvdata(pdev, keypad);
  368. return 0;
  369. }
  370. #ifdef CONFIG_PM_SLEEP
  371. static int tc3589x_keypad_suspend(struct device *dev)
  372. {
  373. struct platform_device *pdev = to_platform_device(dev);
  374. struct tc_keypad *keypad = platform_get_drvdata(pdev);
  375. int irq = platform_get_irq(pdev, 0);
  376. /* keypad is already off; we do nothing */
  377. if (keypad->keypad_stopped)
  378. return 0;
  379. /* if device is not a wakeup source, disable it for powersave */
  380. if (!device_may_wakeup(&pdev->dev))
  381. tc3589x_keypad_disable(keypad);
  382. else
  383. enable_irq_wake(irq);
  384. return 0;
  385. }
  386. static int tc3589x_keypad_resume(struct device *dev)
  387. {
  388. struct platform_device *pdev = to_platform_device(dev);
  389. struct tc_keypad *keypad = platform_get_drvdata(pdev);
  390. int irq = platform_get_irq(pdev, 0);
  391. if (!keypad->keypad_stopped)
  392. return 0;
  393. /* enable the device to resume normal operations */
  394. if (!device_may_wakeup(&pdev->dev))
  395. tc3589x_keypad_enable(keypad);
  396. else
  397. disable_irq_wake(irq);
  398. return 0;
  399. }
  400. #endif
  401. static SIMPLE_DEV_PM_OPS(tc3589x_keypad_dev_pm_ops,
  402. tc3589x_keypad_suspend, tc3589x_keypad_resume);
  403. static struct platform_driver tc3589x_keypad_driver = {
  404. .driver = {
  405. .name = "tc3589x-keypad",
  406. .pm = &tc3589x_keypad_dev_pm_ops,
  407. },
  408. .probe = tc3589x_keypad_probe,
  409. };
  410. module_platform_driver(tc3589x_keypad_driver);
  411. MODULE_LICENSE("GPL v2");
  412. MODULE_AUTHOR("Jayeeta Banerjee/Sundar Iyer");
  413. MODULE_DESCRIPTION("TC35893 Keypad Driver");
  414. MODULE_ALIAS("platform:tc3589x-keypad");