tc3589x-keypad.c 13 KB

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