twl4030_keypad.c 12 KB

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