omap-keypad.c 7.9 KB

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