omap-keypad.c 7.5 KB

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