omap-keypad.c 9.4 KB

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