omap-keypad.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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/interrupt.h>
  28. #include <linux/types.h>
  29. #include <linux/input.h>
  30. #include <linux/kernel.h>
  31. #include <linux/delay.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/mutex.h>
  34. #include <linux/errno.h>
  35. #include <linux/slab.h>
  36. #include <linux/gpio.h>
  37. #include <linux/platform_data/gpio-omap.h>
  38. #include <linux/platform_data/keypad-omap.h>
  39. #undef NEW_BOARD_LEARNING_MODE
  40. static void omap_kp_tasklet(unsigned long);
  41. static void omap_kp_timer(unsigned long);
  42. static unsigned char keypad_state[8];
  43. static DEFINE_MUTEX(kp_enable_mutex);
  44. static int kp_enable = 1;
  45. static int kp_cur_group = -1;
  46. struct omap_kp {
  47. struct input_dev *input;
  48. struct timer_list timer;
  49. int irq;
  50. unsigned int rows;
  51. unsigned int cols;
  52. unsigned long delay;
  53. unsigned int debounce;
  54. unsigned short keymap[];
  55. };
  56. static DECLARE_TASKLET_DISABLED(kp_tasklet, omap_kp_tasklet, 0);
  57. static unsigned int *row_gpios;
  58. static unsigned int *col_gpios;
  59. static irqreturn_t omap_kp_interrupt(int irq, void *dev_id)
  60. {
  61. /* disable keyboard interrupt and schedule for handling */
  62. omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  63. tasklet_schedule(&kp_tasklet);
  64. return IRQ_HANDLED;
  65. }
  66. static void omap_kp_timer(unsigned long data)
  67. {
  68. tasklet_schedule(&kp_tasklet);
  69. }
  70. static void omap_kp_scan_keypad(struct omap_kp *omap_kp, unsigned char *state)
  71. {
  72. int col = 0;
  73. /* disable keyboard interrupt and schedule for handling */
  74. omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  75. /* read the keypad status */
  76. omap_writew(0xff, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBC);
  77. for (col = 0; col < omap_kp->cols; col++) {
  78. omap_writew(~(1 << col) & 0xff,
  79. OMAP1_MPUIO_BASE + OMAP_MPUIO_KBC);
  80. udelay(omap_kp->delay);
  81. state[col] = ~omap_readw(OMAP1_MPUIO_BASE +
  82. OMAP_MPUIO_KBR_LATCH) & 0xff;
  83. }
  84. omap_writew(0x00, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBC);
  85. udelay(2);
  86. }
  87. static void omap_kp_tasklet(unsigned long data)
  88. {
  89. struct omap_kp *omap_kp_data = (struct omap_kp *) data;
  90. unsigned short *keycodes = omap_kp_data->input->keycode;
  91. unsigned int row_shift = get_count_order(omap_kp_data->cols);
  92. unsigned char new_state[8], changed, key_down = 0;
  93. int col, row;
  94. /* check for any changes */
  95. omap_kp_scan_keypad(omap_kp_data, new_state);
  96. /* check for changes and print those */
  97. for (col = 0; col < omap_kp_data->cols; col++) {
  98. changed = new_state[col] ^ keypad_state[col];
  99. key_down |= new_state[col];
  100. if (changed == 0)
  101. continue;
  102. for (row = 0; row < omap_kp_data->rows; row++) {
  103. int key;
  104. if (!(changed & (1 << row)))
  105. continue;
  106. #ifdef NEW_BOARD_LEARNING_MODE
  107. printk(KERN_INFO "omap-keypad: key %d-%d %s\n", col,
  108. row, (new_state[col] & (1 << row)) ?
  109. "pressed" : "released");
  110. #else
  111. key = keycodes[MATRIX_SCAN_CODE(row, col, row_shift)];
  112. if (!(kp_cur_group == (key & GROUP_MASK) ||
  113. kp_cur_group == -1))
  114. continue;
  115. kp_cur_group = key & GROUP_MASK;
  116. input_report_key(omap_kp_data->input, key & ~GROUP_MASK,
  117. new_state[col] & (1 << row));
  118. #endif
  119. }
  120. }
  121. input_sync(omap_kp_data->input);
  122. memcpy(keypad_state, new_state, sizeof(keypad_state));
  123. if (key_down) {
  124. /* some key is pressed - keep irq disabled and use timer
  125. * to poll the keypad */
  126. mod_timer(&omap_kp_data->timer, jiffies + HZ / 20);
  127. } else {
  128. /* enable interrupts */
  129. omap_writew(0, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  130. kp_cur_group = -1;
  131. }
  132. }
  133. static ssize_t omap_kp_enable_show(struct device *dev,
  134. struct device_attribute *attr, char *buf)
  135. {
  136. return sprintf(buf, "%u\n", kp_enable);
  137. }
  138. static ssize_t omap_kp_enable_store(struct device *dev, struct device_attribute *attr,
  139. const char *buf, size_t count)
  140. {
  141. struct omap_kp *omap_kp = dev_get_drvdata(dev);
  142. int state;
  143. if (sscanf(buf, "%u", &state) != 1)
  144. return -EINVAL;
  145. if ((state != 1) && (state != 0))
  146. return -EINVAL;
  147. mutex_lock(&kp_enable_mutex);
  148. if (state != kp_enable) {
  149. if (state)
  150. enable_irq(omap_kp->irq);
  151. else
  152. disable_irq(omap_kp->irq);
  153. kp_enable = state;
  154. }
  155. mutex_unlock(&kp_enable_mutex);
  156. return strnlen(buf, count);
  157. }
  158. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, omap_kp_enable_show, omap_kp_enable_store);
  159. static int omap_kp_probe(struct platform_device *pdev)
  160. {
  161. struct omap_kp *omap_kp;
  162. struct input_dev *input_dev;
  163. struct omap_kp_platform_data *pdata = dev_get_platdata(&pdev->dev);
  164. int i, col_idx, row_idx, ret;
  165. unsigned int row_shift, keycodemax;
  166. if (!pdata->rows || !pdata->cols || !pdata->keymap_data) {
  167. printk(KERN_ERR "No rows, cols or keymap_data from pdata\n");
  168. return -EINVAL;
  169. }
  170. row_shift = get_count_order(pdata->cols);
  171. keycodemax = pdata->rows << row_shift;
  172. omap_kp = kzalloc(sizeof(struct omap_kp) +
  173. keycodemax * sizeof(unsigned short), GFP_KERNEL);
  174. input_dev = input_allocate_device();
  175. if (!omap_kp || !input_dev) {
  176. kfree(omap_kp);
  177. input_free_device(input_dev);
  178. return -ENOMEM;
  179. }
  180. platform_set_drvdata(pdev, omap_kp);
  181. omap_kp->input = input_dev;
  182. /* Disable the interrupt for the MPUIO keyboard */
  183. omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  184. if (pdata->delay)
  185. omap_kp->delay = pdata->delay;
  186. if (pdata->row_gpios && pdata->col_gpios) {
  187. row_gpios = pdata->row_gpios;
  188. col_gpios = pdata->col_gpios;
  189. }
  190. omap_kp->rows = pdata->rows;
  191. omap_kp->cols = pdata->cols;
  192. col_idx = 0;
  193. row_idx = 0;
  194. setup_timer(&omap_kp->timer, omap_kp_timer, (unsigned long)omap_kp);
  195. /* get the irq and init timer*/
  196. kp_tasklet.data = (unsigned long) omap_kp;
  197. tasklet_enable(&kp_tasklet);
  198. ret = device_create_file(&pdev->dev, &dev_attr_enable);
  199. if (ret < 0)
  200. goto err2;
  201. /* setup input device */
  202. input_dev->name = "omap-keypad";
  203. input_dev->phys = "omap-keypad/input0";
  204. input_dev->dev.parent = &pdev->dev;
  205. input_dev->id.bustype = BUS_HOST;
  206. input_dev->id.vendor = 0x0001;
  207. input_dev->id.product = 0x0001;
  208. input_dev->id.version = 0x0100;
  209. if (pdata->rep)
  210. __set_bit(EV_REP, input_dev->evbit);
  211. ret = matrix_keypad_build_keymap(pdata->keymap_data, NULL,
  212. pdata->rows, pdata->cols,
  213. omap_kp->keymap, input_dev);
  214. if (ret < 0)
  215. goto err3;
  216. ret = input_register_device(omap_kp->input);
  217. if (ret < 0) {
  218. printk(KERN_ERR "Unable to register omap-keypad input device\n");
  219. goto err3;
  220. }
  221. if (pdata->dbounce)
  222. omap_writew(0xff, OMAP1_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
  223. /* scan current status and enable interrupt */
  224. omap_kp_scan_keypad(omap_kp, keypad_state);
  225. omap_kp->irq = platform_get_irq(pdev, 0);
  226. if (omap_kp->irq >= 0) {
  227. if (request_irq(omap_kp->irq, omap_kp_interrupt, 0,
  228. "omap-keypad", omap_kp) < 0)
  229. goto err4;
  230. }
  231. omap_writew(0, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  232. return 0;
  233. err4:
  234. input_unregister_device(omap_kp->input);
  235. input_dev = NULL;
  236. err3:
  237. device_remove_file(&pdev->dev, &dev_attr_enable);
  238. err2:
  239. for (i = row_idx - 1; i >= 0; i--)
  240. gpio_free(row_gpios[i]);
  241. for (i = col_idx - 1; i >= 0; i--)
  242. gpio_free(col_gpios[i]);
  243. kfree(omap_kp);
  244. input_free_device(input_dev);
  245. return -EINVAL;
  246. }
  247. static int omap_kp_remove(struct platform_device *pdev)
  248. {
  249. struct omap_kp *omap_kp = platform_get_drvdata(pdev);
  250. /* disable keypad interrupt handling */
  251. tasklet_disable(&kp_tasklet);
  252. omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  253. free_irq(omap_kp->irq, omap_kp);
  254. del_timer_sync(&omap_kp->timer);
  255. tasklet_kill(&kp_tasklet);
  256. /* unregister everything */
  257. input_unregister_device(omap_kp->input);
  258. kfree(omap_kp);
  259. return 0;
  260. }
  261. static struct platform_driver omap_kp_driver = {
  262. .probe = omap_kp_probe,
  263. .remove = omap_kp_remove,
  264. .driver = {
  265. .name = "omap-keypad",
  266. },
  267. };
  268. module_platform_driver(omap_kp_driver);
  269. MODULE_AUTHOR("Timo Teräs");
  270. MODULE_DESCRIPTION("OMAP Keypad Driver");
  271. MODULE_LICENSE("GPL");
  272. MODULE_ALIAS("platform:omap-keypad");