twl4030_keypad.c 12 KB

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