twl4030_keypad.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * twl4030_keypad.c - driver for 8x8 keypad controller in twl4030 chips
  4. *
  5. * Copyright (C) 2007 Texas Instruments, Inc.
  6. * Copyright (C) 2008 Nokia Corporation
  7. *
  8. * Code re-written for 2430SDP by:
  9. * Syed Mohammed Khasim <x0khasim@ti.com>
  10. *
  11. * Initial Code:
  12. * Manjunatha G K <manjugk@ti.com>
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/input.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/mfd/twl.h>
  20. #include <linux/slab.h>
  21. #include <linux/of.h>
  22. /*
  23. * The TWL4030 family chips include a keypad controller that supports
  24. * up to an 8x8 switch matrix. The controller can issue system wakeup
  25. * events, since it uses only the always-on 32KiHz oscillator, and has
  26. * an internal state machine that decodes pressed keys, including
  27. * multi-key combinations.
  28. *
  29. * This driver lets boards define what keycodes they wish to report for
  30. * which scancodes, as part of the "struct twl4030_keypad_data" used in
  31. * the probe() routine.
  32. *
  33. * See the TPS65950 documentation; that's the general availability
  34. * version of the TWL5030 second generation part.
  35. */
  36. #define TWL4030_MAX_ROWS 8 /* TWL4030 hard limit */
  37. #define TWL4030_MAX_COLS 8
  38. /*
  39. * Note that we add space for an extra column so that we can handle
  40. * row lines connected to the gnd (see twl4030_col_xlate()).
  41. */
  42. #define TWL4030_ROW_SHIFT 4
  43. #define TWL4030_KEYMAP_SIZE (TWL4030_MAX_ROWS << TWL4030_ROW_SHIFT)
  44. struct twl4030_keypad {
  45. unsigned short keymap[TWL4030_KEYMAP_SIZE];
  46. u16 kp_state[TWL4030_MAX_ROWS];
  47. bool autorepeat;
  48. unsigned int n_rows;
  49. unsigned int n_cols;
  50. int irq;
  51. struct device *dbg_dev;
  52. struct input_dev *input;
  53. };
  54. /*----------------------------------------------------------------------*/
  55. /* arbitrary prescaler value 0..7 */
  56. #define PTV_PRESCALER 4
  57. /* Register Offsets */
  58. #define KEYP_CTRL 0x00
  59. #define KEYP_DEB 0x01
  60. #define KEYP_LONG_KEY 0x02
  61. #define KEYP_LK_PTV 0x03
  62. #define KEYP_TIMEOUT_L 0x04
  63. #define KEYP_TIMEOUT_H 0x05
  64. #define KEYP_KBC 0x06
  65. #define KEYP_KBR 0x07
  66. #define KEYP_SMS 0x08
  67. #define KEYP_FULL_CODE_7_0 0x09 /* row 0 column status */
  68. #define KEYP_FULL_CODE_15_8 0x0a /* ... row 1 ... */
  69. #define KEYP_FULL_CODE_23_16 0x0b
  70. #define KEYP_FULL_CODE_31_24 0x0c
  71. #define KEYP_FULL_CODE_39_32 0x0d
  72. #define KEYP_FULL_CODE_47_40 0x0e
  73. #define KEYP_FULL_CODE_55_48 0x0f
  74. #define KEYP_FULL_CODE_63_56 0x10
  75. #define KEYP_ISR1 0x11
  76. #define KEYP_IMR1 0x12
  77. #define KEYP_ISR2 0x13
  78. #define KEYP_IMR2 0x14
  79. #define KEYP_SIR 0x15
  80. #define KEYP_EDR 0x16 /* edge triggers */
  81. #define KEYP_SIH_CTRL 0x17
  82. /* KEYP_CTRL_REG Fields */
  83. #define KEYP_CTRL_SOFT_NRST BIT(0)
  84. #define KEYP_CTRL_SOFTMODEN BIT(1)
  85. #define KEYP_CTRL_LK_EN BIT(2)
  86. #define KEYP_CTRL_TOE_EN BIT(3)
  87. #define KEYP_CTRL_TOLE_EN BIT(4)
  88. #define KEYP_CTRL_RP_EN BIT(5)
  89. #define KEYP_CTRL_KBD_ON BIT(6)
  90. /* KEYP_DEB, KEYP_LONG_KEY, KEYP_TIMEOUT_x*/
  91. #define KEYP_PERIOD_US(t, prescale) ((t) / (31 << ((prescale) + 1)) - 1)
  92. /* KEYP_LK_PTV_REG Fields */
  93. #define KEYP_LK_PTV_PTV_SHIFT 5
  94. /* KEYP_{IMR,ISR,SIR} Fields */
  95. #define KEYP_IMR1_MIS BIT(3)
  96. #define KEYP_IMR1_TO BIT(2)
  97. #define KEYP_IMR1_LK BIT(1)
  98. #define KEYP_IMR1_KP BIT(0)
  99. /* KEYP_EDR Fields */
  100. #define KEYP_EDR_KP_FALLING 0x01
  101. #define KEYP_EDR_KP_RISING 0x02
  102. #define KEYP_EDR_KP_BOTH 0x03
  103. #define KEYP_EDR_LK_FALLING 0x04
  104. #define KEYP_EDR_LK_RISING 0x08
  105. #define KEYP_EDR_TO_FALLING 0x10
  106. #define KEYP_EDR_TO_RISING 0x20
  107. #define KEYP_EDR_MIS_FALLING 0x40
  108. #define KEYP_EDR_MIS_RISING 0x80
  109. /*----------------------------------------------------------------------*/
  110. static int twl4030_kpread(struct twl4030_keypad *kp,
  111. u8 *data, u32 reg, u8 num_bytes)
  112. {
  113. int ret = twl_i2c_read(TWL4030_MODULE_KEYPAD, data, reg, num_bytes);
  114. if (ret < 0)
  115. dev_warn(kp->dbg_dev,
  116. "Couldn't read TWL4030: %X - ret %d[%x]\n",
  117. reg, ret, ret);
  118. return ret;
  119. }
  120. static int twl4030_kpwrite_u8(struct twl4030_keypad *kp, u8 data, u32 reg)
  121. {
  122. int ret = twl_i2c_write_u8(TWL4030_MODULE_KEYPAD, data, reg);
  123. if (ret < 0)
  124. dev_warn(kp->dbg_dev,
  125. "Could not write TWL4030: %X - ret %d[%x]\n",
  126. reg, ret, ret);
  127. return ret;
  128. }
  129. static inline u16 twl4030_col_xlate(struct twl4030_keypad *kp, u8 col)
  130. {
  131. /*
  132. * If all bits in a row are active for all columns then
  133. * we have that row line connected to gnd. Mark this
  134. * key on as if it was on matrix position n_cols (i.e.
  135. * one higher than the size of the matrix).
  136. */
  137. if (col == 0xFF)
  138. return 1 << kp->n_cols;
  139. else
  140. return col & ((1 << kp->n_cols) - 1);
  141. }
  142. static int twl4030_read_kp_matrix_state(struct twl4030_keypad *kp, u16 *state)
  143. {
  144. u8 new_state[TWL4030_MAX_ROWS];
  145. int row;
  146. int ret = twl4030_kpread(kp, new_state,
  147. KEYP_FULL_CODE_7_0, kp->n_rows);
  148. if (ret >= 0)
  149. for (row = 0; row < kp->n_rows; row++)
  150. state[row] = twl4030_col_xlate(kp, new_state[row]);
  151. return ret;
  152. }
  153. static bool twl4030_is_in_ghost_state(struct twl4030_keypad *kp, u16 *key_state)
  154. {
  155. int i;
  156. u16 check = 0;
  157. for (i = 0; i < kp->n_rows; i++) {
  158. u16 col = key_state[i];
  159. if ((col & check) && hweight16(col) > 1)
  160. return true;
  161. check |= col;
  162. }
  163. return false;
  164. }
  165. static void twl4030_kp_scan(struct twl4030_keypad *kp, bool release_all)
  166. {
  167. struct input_dev *input = kp->input;
  168. u16 new_state[TWL4030_MAX_ROWS];
  169. int col, row;
  170. if (release_all) {
  171. memset(new_state, 0, sizeof(new_state));
  172. } else {
  173. /* check for any changes */
  174. int ret = twl4030_read_kp_matrix_state(kp, new_state);
  175. if (ret < 0) /* panic ... */
  176. return;
  177. if (twl4030_is_in_ghost_state(kp, new_state))
  178. return;
  179. }
  180. /* check for changes and print those */
  181. for (row = 0; row < kp->n_rows; row++) {
  182. int changed = new_state[row] ^ kp->kp_state[row];
  183. if (!changed)
  184. continue;
  185. /* Extra column handles "all gnd" rows */
  186. for (col = 0; col < kp->n_cols + 1; col++) {
  187. int code;
  188. if (!(changed & (1 << col)))
  189. continue;
  190. dev_dbg(kp->dbg_dev, "key [%d:%d] %s\n", row, col,
  191. (new_state[row] & (1 << col)) ?
  192. "press" : "release");
  193. code = MATRIX_SCAN_CODE(row, col, TWL4030_ROW_SHIFT);
  194. input_event(input, EV_MSC, MSC_SCAN, code);
  195. input_report_key(input, kp->keymap[code],
  196. new_state[row] & (1 << col));
  197. }
  198. kp->kp_state[row] = new_state[row];
  199. }
  200. input_sync(input);
  201. }
  202. /*
  203. * Keypad interrupt handler
  204. */
  205. static irqreturn_t do_kp_irq(int irq, void *_kp)
  206. {
  207. struct twl4030_keypad *kp = _kp;
  208. u8 reg;
  209. int ret;
  210. /* Read & Clear TWL4030 pending interrupt */
  211. ret = twl4030_kpread(kp, &reg, KEYP_ISR1, 1);
  212. /*
  213. * Release all keys if I2C has gone bad or
  214. * the KEYP has gone to idle state.
  215. */
  216. if (ret >= 0 && (reg & KEYP_IMR1_KP))
  217. twl4030_kp_scan(kp, false);
  218. else
  219. twl4030_kp_scan(kp, true);
  220. return IRQ_HANDLED;
  221. }
  222. static int twl4030_kp_program(struct twl4030_keypad *kp)
  223. {
  224. u8 reg;
  225. int i;
  226. /* Enable controller, with hardware decoding but not autorepeat */
  227. reg = KEYP_CTRL_SOFT_NRST | KEYP_CTRL_SOFTMODEN
  228. | KEYP_CTRL_TOE_EN | KEYP_CTRL_KBD_ON;
  229. if (twl4030_kpwrite_u8(kp, reg, KEYP_CTRL) < 0)
  230. return -EIO;
  231. /*
  232. * NOTE: we could use sih_setup() here to package keypad
  233. * event sources as four different IRQs ... but we don't.
  234. */
  235. /* Enable TO rising and KP rising and falling edge detection */
  236. reg = KEYP_EDR_KP_BOTH | KEYP_EDR_TO_RISING;
  237. if (twl4030_kpwrite_u8(kp, reg, KEYP_EDR) < 0)
  238. return -EIO;
  239. /* Set PTV prescaler Field */
  240. reg = (PTV_PRESCALER << KEYP_LK_PTV_PTV_SHIFT);
  241. if (twl4030_kpwrite_u8(kp, reg, KEYP_LK_PTV) < 0)
  242. return -EIO;
  243. /* Set key debounce time to 20 ms */
  244. i = KEYP_PERIOD_US(20000, PTV_PRESCALER);
  245. if (twl4030_kpwrite_u8(kp, i, KEYP_DEB) < 0)
  246. return -EIO;
  247. /* Set timeout period to 200 ms */
  248. i = KEYP_PERIOD_US(200000, PTV_PRESCALER);
  249. if (twl4030_kpwrite_u8(kp, (i & 0xFF), KEYP_TIMEOUT_L) < 0)
  250. return -EIO;
  251. if (twl4030_kpwrite_u8(kp, (i >> 8), KEYP_TIMEOUT_H) < 0)
  252. return -EIO;
  253. /*
  254. * Enable Clear-on-Read; disable remembering events that fire
  255. * after the IRQ but before our handler acks (reads) them.
  256. */
  257. reg = TWL4030_SIH_CTRL_COR_MASK | TWL4030_SIH_CTRL_PENDDIS_MASK;
  258. if (twl4030_kpwrite_u8(kp, reg, KEYP_SIH_CTRL) < 0)
  259. return -EIO;
  260. /* initialize key state; irqs update it from here on */
  261. if (twl4030_read_kp_matrix_state(kp, kp->kp_state) < 0)
  262. return -EIO;
  263. return 0;
  264. }
  265. /*
  266. * Registers keypad device with input subsystem
  267. * and configures TWL4030 keypad registers
  268. */
  269. static int twl4030_kp_probe(struct platform_device *pdev)
  270. {
  271. struct twl4030_keypad_data *pdata = dev_get_platdata(&pdev->dev);
  272. const struct matrix_keymap_data *keymap_data = NULL;
  273. struct twl4030_keypad *kp;
  274. struct input_dev *input;
  275. u8 reg;
  276. int error;
  277. kp = devm_kzalloc(&pdev->dev, sizeof(*kp), GFP_KERNEL);
  278. if (!kp)
  279. return -ENOMEM;
  280. input = devm_input_allocate_device(&pdev->dev);
  281. if (!input)
  282. return -ENOMEM;
  283. /* get the debug device */
  284. kp->dbg_dev = &pdev->dev;
  285. kp->input = input;
  286. /* setup input device */
  287. input->name = "TWL4030 Keypad";
  288. input->phys = "twl4030_keypad/input0";
  289. input->id.bustype = BUS_HOST;
  290. input->id.vendor = 0x0001;
  291. input->id.product = 0x0001;
  292. input->id.version = 0x0003;
  293. if (pdata) {
  294. if (!pdata->rows || !pdata->cols || !pdata->keymap_data) {
  295. dev_err(&pdev->dev, "Missing platform_data\n");
  296. return -EINVAL;
  297. }
  298. kp->n_rows = pdata->rows;
  299. kp->n_cols = pdata->cols;
  300. kp->autorepeat = pdata->rep;
  301. keymap_data = pdata->keymap_data;
  302. } else {
  303. error = matrix_keypad_parse_properties(&pdev->dev, &kp->n_rows,
  304. &kp->n_cols);
  305. if (error)
  306. return error;
  307. kp->autorepeat = true;
  308. }
  309. if (kp->n_rows > TWL4030_MAX_ROWS || kp->n_cols > TWL4030_MAX_COLS) {
  310. dev_err(&pdev->dev,
  311. "Invalid rows/cols amount specified in platform/devicetree data\n");
  312. return -EINVAL;
  313. }
  314. kp->irq = platform_get_irq(pdev, 0);
  315. if (kp->irq < 0)
  316. return kp->irq;
  317. error = matrix_keypad_build_keymap(keymap_data, NULL,
  318. TWL4030_MAX_ROWS,
  319. 1 << TWL4030_ROW_SHIFT,
  320. kp->keymap, input);
  321. if (error) {
  322. dev_err(kp->dbg_dev, "Failed to build keymap\n");
  323. return error;
  324. }
  325. input_set_capability(input, EV_MSC, MSC_SCAN);
  326. /* Enable auto repeat feature of Linux input subsystem */
  327. if (kp->autorepeat)
  328. __set_bit(EV_REP, input->evbit);
  329. error = input_register_device(input);
  330. if (error) {
  331. dev_err(kp->dbg_dev,
  332. "Unable to register twl4030 keypad device\n");
  333. return error;
  334. }
  335. error = twl4030_kp_program(kp);
  336. if (error)
  337. return error;
  338. /*
  339. * This ISR will always execute in kernel thread context because of
  340. * the need to access the TWL4030 over the I2C bus.
  341. *
  342. * NOTE: we assume this host is wired to TWL4040 INT1, not INT2 ...
  343. */
  344. error = devm_request_threaded_irq(&pdev->dev, kp->irq, NULL, do_kp_irq,
  345. 0, pdev->name, kp);
  346. if (error) {
  347. dev_info(kp->dbg_dev, "request_irq failed for irq no=%d: %d\n",
  348. kp->irq, error);
  349. return error;
  350. }
  351. /* Enable KP and TO interrupts now. */
  352. reg = (u8) ~(KEYP_IMR1_KP | KEYP_IMR1_TO);
  353. if (twl4030_kpwrite_u8(kp, reg, KEYP_IMR1)) {
  354. /* mask all events - we don't care about the result */
  355. (void) twl4030_kpwrite_u8(kp, 0xff, KEYP_IMR1);
  356. return -EIO;
  357. }
  358. return 0;
  359. }
  360. #ifdef CONFIG_OF
  361. static const struct of_device_id twl4030_keypad_dt_match_table[] = {
  362. { .compatible = "ti,twl4030-keypad" },
  363. {},
  364. };
  365. MODULE_DEVICE_TABLE(of, twl4030_keypad_dt_match_table);
  366. #endif
  367. /*
  368. * NOTE: twl4030 are multi-function devices connected via I2C.
  369. * So this device is a child of an I2C parent, thus it needs to
  370. * support unplug/replug (which most platform devices don't).
  371. */
  372. static struct platform_driver twl4030_kp_driver = {
  373. .probe = twl4030_kp_probe,
  374. .driver = {
  375. .name = "twl4030_keypad",
  376. .of_match_table = of_match_ptr(twl4030_keypad_dt_match_table),
  377. },
  378. };
  379. module_platform_driver(twl4030_kp_driver);
  380. MODULE_AUTHOR("Texas Instruments");
  381. MODULE_DESCRIPTION("TWL4030 Keypad Driver");
  382. MODULE_LICENSE("GPL");
  383. MODULE_ALIAS("platform:twl4030_keypad");