omap-keypad.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * linux/drivers/input/keyboard/omap-keypad.c
  3. *
  4. * OMAP Keypad Driver
  5. *
  6. * Copyright (C) 2003 Nokia Corporation
  7. * Written by Timo Teräs <ext-timo.teras@nokia.com>
  8. *
  9. * Added support for H2 & H3 Keypad
  10. * Copyright (C) 2004 Texas Instruments
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/types.h>
  30. #include <linux/input.h>
  31. #include <linux/kernel.h>
  32. #include <linux/delay.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/mutex.h>
  35. #include <linux/errno.h>
  36. #include <linux/slab.h>
  37. #include <mach/gpio.h>
  38. #include <plat/keypad.h>
  39. #include <plat/menelaus.h>
  40. #include <asm/irq.h>
  41. #include <mach/hardware.h>
  42. #include <asm/io.h>
  43. #include <plat/mux.h>
  44. #undef NEW_BOARD_LEARNING_MODE
  45. static void omap_kp_tasklet(unsigned long);
  46. static void omap_kp_timer(unsigned long);
  47. static unsigned char keypad_state[8];
  48. static DEFINE_MUTEX(kp_enable_mutex);
  49. static int kp_enable = 1;
  50. static int kp_cur_group = -1;
  51. struct omap_kp {
  52. struct input_dev *input;
  53. struct timer_list timer;
  54. int irq;
  55. unsigned int rows;
  56. unsigned int cols;
  57. unsigned long delay;
  58. unsigned int debounce;
  59. };
  60. static DECLARE_TASKLET_DISABLED(kp_tasklet, omap_kp_tasklet, 0);
  61. static int *keymap;
  62. static unsigned int *row_gpios;
  63. static unsigned int *col_gpios;
  64. #ifdef CONFIG_ARCH_OMAP2
  65. static void set_col_gpio_val(struct omap_kp *omap_kp, u8 value)
  66. {
  67. int col;
  68. for (col = 0; col < omap_kp->cols; col++)
  69. gpio_set_value(col_gpios[col], value & (1 << col));
  70. }
  71. static u8 get_row_gpio_val(struct omap_kp *omap_kp)
  72. {
  73. int row;
  74. u8 value = 0;
  75. for (row = 0; row < omap_kp->rows; row++) {
  76. if (gpio_get_value(row_gpios[row]))
  77. value |= (1 << row);
  78. }
  79. return value;
  80. }
  81. #else
  82. #define set_col_gpio_val(x, y) do {} while (0)
  83. #define get_row_gpio_val(x) 0
  84. #endif
  85. static irqreturn_t omap_kp_interrupt(int irq, void *dev_id)
  86. {
  87. struct omap_kp *omap_kp = dev_id;
  88. /* disable keyboard interrupt and schedule for handling */
  89. if (cpu_is_omap24xx()) {
  90. int i;
  91. for (i = 0; i < omap_kp->rows; i++) {
  92. int gpio_irq = gpio_to_irq(row_gpios[i]);
  93. /*
  94. * The interrupt which we're currently handling should
  95. * be disabled _nosync() to avoid deadlocks waiting
  96. * for this handler to complete. All others should
  97. * be disabled the regular way for SMP safety.
  98. */
  99. if (gpio_irq == irq)
  100. disable_irq_nosync(gpio_irq);
  101. else
  102. disable_irq(gpio_irq);
  103. }
  104. } else
  105. /* disable keyboard interrupt and schedule for handling */
  106. omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  107. tasklet_schedule(&kp_tasklet);
  108. return IRQ_HANDLED;
  109. }
  110. static void omap_kp_timer(unsigned long data)
  111. {
  112. tasklet_schedule(&kp_tasklet);
  113. }
  114. static void omap_kp_scan_keypad(struct omap_kp *omap_kp, unsigned char *state)
  115. {
  116. int col = 0;
  117. /* read the keypad status */
  118. if (cpu_is_omap24xx()) {
  119. /* read the keypad status */
  120. for (col = 0; col < omap_kp->cols; col++) {
  121. set_col_gpio_val(omap_kp, ~(1 << col));
  122. state[col] = ~(get_row_gpio_val(omap_kp)) & 0xff;
  123. }
  124. set_col_gpio_val(omap_kp, 0);
  125. } else {
  126. /* disable keyboard interrupt and schedule for handling */
  127. omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  128. /* read the keypad status */
  129. omap_writew(0xff, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBC);
  130. for (col = 0; col < omap_kp->cols; col++) {
  131. omap_writew(~(1 << col) & 0xff,
  132. OMAP1_MPUIO_BASE + OMAP_MPUIO_KBC);
  133. udelay(omap_kp->delay);
  134. state[col] = ~omap_readw(OMAP1_MPUIO_BASE +
  135. OMAP_MPUIO_KBR_LATCH) & 0xff;
  136. }
  137. omap_writew(0x00, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBC);
  138. udelay(2);
  139. }
  140. }
  141. static inline int omap_kp_find_key(int col, int row)
  142. {
  143. int i, key;
  144. key = KEY(col, row, 0);
  145. for (i = 0; keymap[i] != 0; i++)
  146. if ((keymap[i] & 0xff000000) == key)
  147. return keymap[i] & 0x00ffffff;
  148. return -1;
  149. }
  150. static void omap_kp_tasklet(unsigned long data)
  151. {
  152. struct omap_kp *omap_kp_data = (struct omap_kp *) data;
  153. unsigned char new_state[8], changed, key_down = 0;
  154. int col, row;
  155. int spurious = 0;
  156. /* check for any changes */
  157. omap_kp_scan_keypad(omap_kp_data, new_state);
  158. /* check for changes and print those */
  159. for (col = 0; col < omap_kp_data->cols; col++) {
  160. changed = new_state[col] ^ keypad_state[col];
  161. key_down |= new_state[col];
  162. if (changed == 0)
  163. continue;
  164. for (row = 0; row < omap_kp_data->rows; row++) {
  165. int key;
  166. if (!(changed & (1 << row)))
  167. continue;
  168. #ifdef NEW_BOARD_LEARNING_MODE
  169. printk(KERN_INFO "omap-keypad: key %d-%d %s\n", col,
  170. row, (new_state[col] & (1 << row)) ?
  171. "pressed" : "released");
  172. #else
  173. key = omap_kp_find_key(col, row);
  174. if (key < 0) {
  175. printk(KERN_WARNING
  176. "omap-keypad: Spurious key event %d-%d\n",
  177. col, row);
  178. /* We scan again after a couple of seconds */
  179. spurious = 1;
  180. continue;
  181. }
  182. if (!(kp_cur_group == (key & GROUP_MASK) ||
  183. kp_cur_group == -1))
  184. continue;
  185. kp_cur_group = key & GROUP_MASK;
  186. input_report_key(omap_kp_data->input, key & ~GROUP_MASK,
  187. new_state[col] & (1 << row));
  188. #endif
  189. }
  190. }
  191. memcpy(keypad_state, new_state, sizeof(keypad_state));
  192. if (key_down) {
  193. int delay = HZ / 20;
  194. /* some key is pressed - keep irq disabled and use timer
  195. * to poll the keypad */
  196. if (spurious)
  197. delay = 2 * HZ;
  198. mod_timer(&omap_kp_data->timer, jiffies + delay);
  199. } else {
  200. /* enable interrupts */
  201. if (cpu_is_omap24xx()) {
  202. int i;
  203. for (i = 0; i < omap_kp_data->rows; i++)
  204. enable_irq(gpio_to_irq(row_gpios[i]));
  205. } else {
  206. omap_writew(0, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  207. kp_cur_group = -1;
  208. }
  209. }
  210. }
  211. static ssize_t omap_kp_enable_show(struct device *dev,
  212. struct device_attribute *attr, char *buf)
  213. {
  214. return sprintf(buf, "%u\n", kp_enable);
  215. }
  216. static ssize_t omap_kp_enable_store(struct device *dev, struct device_attribute *attr,
  217. const char *buf, size_t count)
  218. {
  219. int state;
  220. if (sscanf(buf, "%u", &state) != 1)
  221. return -EINVAL;
  222. if ((state != 1) && (state != 0))
  223. return -EINVAL;
  224. mutex_lock(&kp_enable_mutex);
  225. if (state != kp_enable) {
  226. if (state)
  227. enable_irq(INT_KEYBOARD);
  228. else
  229. disable_irq(INT_KEYBOARD);
  230. kp_enable = state;
  231. }
  232. mutex_unlock(&kp_enable_mutex);
  233. return strnlen(buf, count);
  234. }
  235. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, omap_kp_enable_show, omap_kp_enable_store);
  236. #ifdef CONFIG_PM
  237. static int omap_kp_suspend(struct platform_device *dev, pm_message_t state)
  238. {
  239. /* Nothing yet */
  240. return 0;
  241. }
  242. static int omap_kp_resume(struct platform_device *dev)
  243. {
  244. /* Nothing yet */
  245. return 0;
  246. }
  247. #else
  248. #define omap_kp_suspend NULL
  249. #define omap_kp_resume NULL
  250. #endif
  251. static int __devinit omap_kp_probe(struct platform_device *pdev)
  252. {
  253. struct omap_kp *omap_kp;
  254. struct input_dev *input_dev;
  255. struct omap_kp_platform_data *pdata = pdev->dev.platform_data;
  256. int i, col_idx, row_idx, irq_idx, ret;
  257. if (!pdata->rows || !pdata->cols || !pdata->keymap) {
  258. printk(KERN_ERR "No rows, cols or keymap from pdata\n");
  259. return -EINVAL;
  260. }
  261. omap_kp = kzalloc(sizeof(struct omap_kp), GFP_KERNEL);
  262. input_dev = input_allocate_device();
  263. if (!omap_kp || !input_dev) {
  264. kfree(omap_kp);
  265. input_free_device(input_dev);
  266. return -ENOMEM;
  267. }
  268. platform_set_drvdata(pdev, omap_kp);
  269. omap_kp->input = input_dev;
  270. /* Disable the interrupt for the MPUIO keyboard */
  271. if (!cpu_is_omap24xx())
  272. omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  273. keymap = pdata->keymap;
  274. if (pdata->rep)
  275. __set_bit(EV_REP, input_dev->evbit);
  276. if (pdata->delay)
  277. omap_kp->delay = pdata->delay;
  278. if (pdata->row_gpios && pdata->col_gpios) {
  279. row_gpios = pdata->row_gpios;
  280. col_gpios = pdata->col_gpios;
  281. }
  282. omap_kp->rows = pdata->rows;
  283. omap_kp->cols = pdata->cols;
  284. if (cpu_is_omap24xx()) {
  285. /* Cols: outputs */
  286. for (col_idx = 0; col_idx < omap_kp->cols; col_idx++) {
  287. if (gpio_request(col_gpios[col_idx], "omap_kp_col") < 0) {
  288. printk(KERN_ERR "Failed to request"
  289. "GPIO%d for keypad\n",
  290. col_gpios[col_idx]);
  291. goto err1;
  292. }
  293. gpio_direction_output(col_gpios[col_idx], 0);
  294. }
  295. /* Rows: inputs */
  296. for (row_idx = 0; row_idx < omap_kp->rows; row_idx++) {
  297. if (gpio_request(row_gpios[row_idx], "omap_kp_row") < 0) {
  298. printk(KERN_ERR "Failed to request"
  299. "GPIO%d for keypad\n",
  300. row_gpios[row_idx]);
  301. goto err2;
  302. }
  303. gpio_direction_input(row_gpios[row_idx]);
  304. }
  305. } else {
  306. col_idx = 0;
  307. row_idx = 0;
  308. }
  309. setup_timer(&omap_kp->timer, omap_kp_timer, (unsigned long)omap_kp);
  310. /* get the irq and init timer*/
  311. tasklet_enable(&kp_tasklet);
  312. kp_tasklet.data = (unsigned long) omap_kp;
  313. ret = device_create_file(&pdev->dev, &dev_attr_enable);
  314. if (ret < 0)
  315. goto err2;
  316. /* setup input device */
  317. __set_bit(EV_KEY, input_dev->evbit);
  318. for (i = 0; keymap[i] != 0; i++)
  319. __set_bit(keymap[i] & KEY_MAX, input_dev->keybit);
  320. input_dev->name = "omap-keypad";
  321. input_dev->phys = "omap-keypad/input0";
  322. input_dev->dev.parent = &pdev->dev;
  323. input_dev->id.bustype = BUS_HOST;
  324. input_dev->id.vendor = 0x0001;
  325. input_dev->id.product = 0x0001;
  326. input_dev->id.version = 0x0100;
  327. ret = input_register_device(omap_kp->input);
  328. if (ret < 0) {
  329. printk(KERN_ERR "Unable to register omap-keypad input device\n");
  330. goto err3;
  331. }
  332. if (pdata->dbounce)
  333. omap_writew(0xff, OMAP1_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
  334. /* scan current status and enable interrupt */
  335. omap_kp_scan_keypad(omap_kp, keypad_state);
  336. if (!cpu_is_omap24xx()) {
  337. omap_kp->irq = platform_get_irq(pdev, 0);
  338. if (omap_kp->irq >= 0) {
  339. if (request_irq(omap_kp->irq, omap_kp_interrupt, 0,
  340. "omap-keypad", omap_kp) < 0)
  341. goto err4;
  342. }
  343. omap_writew(0, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  344. } else {
  345. for (irq_idx = 0; irq_idx < omap_kp->rows; irq_idx++) {
  346. if (request_irq(gpio_to_irq(row_gpios[irq_idx]),
  347. omap_kp_interrupt,
  348. IRQF_TRIGGER_FALLING,
  349. "omap-keypad", omap_kp) < 0)
  350. goto err5;
  351. }
  352. }
  353. return 0;
  354. err5:
  355. for (i = irq_idx - 1; i >=0; i--)
  356. free_irq(row_gpios[i], 0);
  357. err4:
  358. input_unregister_device(omap_kp->input);
  359. input_dev = NULL;
  360. err3:
  361. device_remove_file(&pdev->dev, &dev_attr_enable);
  362. err2:
  363. for (i = row_idx - 1; i >=0; i--)
  364. gpio_free(row_gpios[i]);
  365. err1:
  366. for (i = col_idx - 1; i >=0; i--)
  367. gpio_free(col_gpios[i]);
  368. kfree(omap_kp);
  369. input_free_device(input_dev);
  370. return -EINVAL;
  371. }
  372. static int __devexit omap_kp_remove(struct platform_device *pdev)
  373. {
  374. struct omap_kp *omap_kp = platform_get_drvdata(pdev);
  375. /* disable keypad interrupt handling */
  376. tasklet_disable(&kp_tasklet);
  377. if (cpu_is_omap24xx()) {
  378. int i;
  379. for (i = 0; i < omap_kp->cols; i++)
  380. gpio_free(col_gpios[i]);
  381. for (i = 0; i < omap_kp->rows; i++) {
  382. gpio_free(row_gpios[i]);
  383. free_irq(gpio_to_irq(row_gpios[i]), 0);
  384. }
  385. } else {
  386. omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  387. free_irq(omap_kp->irq, 0);
  388. }
  389. del_timer_sync(&omap_kp->timer);
  390. tasklet_kill(&kp_tasklet);
  391. /* unregister everything */
  392. input_unregister_device(omap_kp->input);
  393. kfree(omap_kp);
  394. return 0;
  395. }
  396. static struct platform_driver omap_kp_driver = {
  397. .probe = omap_kp_probe,
  398. .remove = __devexit_p(omap_kp_remove),
  399. .suspend = omap_kp_suspend,
  400. .resume = omap_kp_resume,
  401. .driver = {
  402. .name = "omap-keypad",
  403. .owner = THIS_MODULE,
  404. },
  405. };
  406. static int __init omap_kp_init(void)
  407. {
  408. printk(KERN_INFO "OMAP Keypad Driver\n");
  409. return platform_driver_register(&omap_kp_driver);
  410. }
  411. static void __exit omap_kp_exit(void)
  412. {
  413. platform_driver_unregister(&omap_kp_driver);
  414. }
  415. module_init(omap_kp_init);
  416. module_exit(omap_kp_exit);
  417. MODULE_AUTHOR("Timo Teräs");
  418. MODULE_DESCRIPTION("OMAP Keypad Driver");
  419. MODULE_LICENSE("GPL");
  420. MODULE_ALIAS("platform:omap-keypad");