omap-keypad.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 (key < 0) {
  135. printk(KERN_WARNING
  136. "omap-keypad: Spurious key event %d-%d\n",
  137. col, row);
  138. /* We scan again after a couple of seconds */
  139. spurious = 1;
  140. continue;
  141. }
  142. if (!(kp_cur_group == (key & GROUP_MASK) ||
  143. kp_cur_group == -1))
  144. continue;
  145. kp_cur_group = key & GROUP_MASK;
  146. input_report_key(omap_kp_data->input, key & ~GROUP_MASK,
  147. new_state[col] & (1 << row));
  148. #endif
  149. }
  150. }
  151. input_sync(omap_kp_data->input);
  152. memcpy(keypad_state, new_state, sizeof(keypad_state));
  153. if (key_down) {
  154. int delay = HZ / 20;
  155. /* some key is pressed - keep irq disabled and use timer
  156. * to poll the keypad */
  157. if (spurious)
  158. delay = 2 * HZ;
  159. mod_timer(&omap_kp_data->timer, jiffies + delay);
  160. } else {
  161. /* enable interrupts */
  162. omap_writew(0, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  163. kp_cur_group = -1;
  164. }
  165. }
  166. static ssize_t omap_kp_enable_show(struct device *dev,
  167. struct device_attribute *attr, char *buf)
  168. {
  169. return sprintf(buf, "%u\n", kp_enable);
  170. }
  171. static ssize_t omap_kp_enable_store(struct device *dev, struct device_attribute *attr,
  172. const char *buf, size_t count)
  173. {
  174. struct omap_kp *omap_kp = dev_get_drvdata(dev);
  175. int state;
  176. if (sscanf(buf, "%u", &state) != 1)
  177. return -EINVAL;
  178. if ((state != 1) && (state != 0))
  179. return -EINVAL;
  180. mutex_lock(&kp_enable_mutex);
  181. if (state != kp_enable) {
  182. if (state)
  183. enable_irq(omap_kp->irq);
  184. else
  185. disable_irq(omap_kp->irq);
  186. kp_enable = state;
  187. }
  188. mutex_unlock(&kp_enable_mutex);
  189. return strnlen(buf, count);
  190. }
  191. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, omap_kp_enable_show, omap_kp_enable_store);
  192. #ifdef CONFIG_PM
  193. static int omap_kp_suspend(struct platform_device *dev, pm_message_t state)
  194. {
  195. /* Nothing yet */
  196. return 0;
  197. }
  198. static int omap_kp_resume(struct platform_device *dev)
  199. {
  200. /* Nothing yet */
  201. return 0;
  202. }
  203. #else
  204. #define omap_kp_suspend NULL
  205. #define omap_kp_resume NULL
  206. #endif
  207. static int omap_kp_probe(struct platform_device *pdev)
  208. {
  209. struct omap_kp *omap_kp;
  210. struct input_dev *input_dev;
  211. struct omap_kp_platform_data *pdata = dev_get_platdata(&pdev->dev);
  212. int i, col_idx, row_idx, ret;
  213. unsigned int row_shift, keycodemax;
  214. if (!pdata->rows || !pdata->cols || !pdata->keymap_data) {
  215. printk(KERN_ERR "No rows, cols or keymap_data from pdata\n");
  216. return -EINVAL;
  217. }
  218. row_shift = get_count_order(pdata->cols);
  219. keycodemax = pdata->rows << row_shift;
  220. omap_kp = kzalloc(sizeof(struct omap_kp) +
  221. keycodemax * sizeof(unsigned short), GFP_KERNEL);
  222. input_dev = input_allocate_device();
  223. if (!omap_kp || !input_dev) {
  224. kfree(omap_kp);
  225. input_free_device(input_dev);
  226. return -ENOMEM;
  227. }
  228. platform_set_drvdata(pdev, omap_kp);
  229. omap_kp->input = input_dev;
  230. /* Disable the interrupt for the MPUIO keyboard */
  231. omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  232. if (pdata->delay)
  233. omap_kp->delay = pdata->delay;
  234. if (pdata->row_gpios && pdata->col_gpios) {
  235. row_gpios = pdata->row_gpios;
  236. col_gpios = pdata->col_gpios;
  237. }
  238. omap_kp->rows = pdata->rows;
  239. omap_kp->cols = pdata->cols;
  240. col_idx = 0;
  241. row_idx = 0;
  242. setup_timer(&omap_kp->timer, omap_kp_timer, (unsigned long)omap_kp);
  243. /* get the irq and init timer*/
  244. tasklet_enable(&kp_tasklet);
  245. kp_tasklet.data = (unsigned long) omap_kp;
  246. ret = device_create_file(&pdev->dev, &dev_attr_enable);
  247. if (ret < 0)
  248. goto err2;
  249. /* setup input device */
  250. input_dev->name = "omap-keypad";
  251. input_dev->phys = "omap-keypad/input0";
  252. input_dev->dev.parent = &pdev->dev;
  253. input_dev->id.bustype = BUS_HOST;
  254. input_dev->id.vendor = 0x0001;
  255. input_dev->id.product = 0x0001;
  256. input_dev->id.version = 0x0100;
  257. if (pdata->rep)
  258. __set_bit(EV_REP, input_dev->evbit);
  259. ret = matrix_keypad_build_keymap(pdata->keymap_data, NULL,
  260. pdata->rows, pdata->cols,
  261. omap_kp->keymap, input_dev);
  262. if (ret < 0)
  263. goto err3;
  264. ret = input_register_device(omap_kp->input);
  265. if (ret < 0) {
  266. printk(KERN_ERR "Unable to register omap-keypad input device\n");
  267. goto err3;
  268. }
  269. if (pdata->dbounce)
  270. omap_writew(0xff, OMAP1_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
  271. /* scan current status and enable interrupt */
  272. omap_kp_scan_keypad(omap_kp, keypad_state);
  273. omap_kp->irq = platform_get_irq(pdev, 0);
  274. if (omap_kp->irq >= 0) {
  275. if (request_irq(omap_kp->irq, omap_kp_interrupt, 0,
  276. "omap-keypad", omap_kp) < 0)
  277. goto err4;
  278. }
  279. omap_writew(0, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  280. return 0;
  281. err4:
  282. input_unregister_device(omap_kp->input);
  283. input_dev = NULL;
  284. err3:
  285. device_remove_file(&pdev->dev, &dev_attr_enable);
  286. err2:
  287. for (i = row_idx - 1; i >= 0; i--)
  288. gpio_free(row_gpios[i]);
  289. for (i = col_idx - 1; i >= 0; i--)
  290. gpio_free(col_gpios[i]);
  291. kfree(omap_kp);
  292. input_free_device(input_dev);
  293. return -EINVAL;
  294. }
  295. static int omap_kp_remove(struct platform_device *pdev)
  296. {
  297. struct omap_kp *omap_kp = platform_get_drvdata(pdev);
  298. /* disable keypad interrupt handling */
  299. tasklet_disable(&kp_tasklet);
  300. omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  301. free_irq(omap_kp->irq, omap_kp);
  302. del_timer_sync(&omap_kp->timer);
  303. tasklet_kill(&kp_tasklet);
  304. /* unregister everything */
  305. input_unregister_device(omap_kp->input);
  306. kfree(omap_kp);
  307. return 0;
  308. }
  309. static struct platform_driver omap_kp_driver = {
  310. .probe = omap_kp_probe,
  311. .remove = omap_kp_remove,
  312. .suspend = omap_kp_suspend,
  313. .resume = omap_kp_resume,
  314. .driver = {
  315. .name = "omap-keypad",
  316. },
  317. };
  318. module_platform_driver(omap_kp_driver);
  319. MODULE_AUTHOR("Timo Teräs");
  320. MODULE_DESCRIPTION("OMAP Keypad Driver");
  321. MODULE_LICENSE("GPL");
  322. MODULE_ALIAS("platform:omap-keypad");