omap-keypad.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/types.h>
  30. #include <linux/input.h>
  31. #include <linux/kernel.h>
  32. #include <linux/delay.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/mutex.h>
  35. #include <linux/errno.h>
  36. #include <mach/gpio.h>
  37. #include <mach/keypad.h>
  38. #include <mach/menelaus.h>
  39. #include <asm/irq.h>
  40. #include <mach/hardware.h>
  41. #include <asm/io.h>
  42. #include <mach/mux.h>
  43. #undef NEW_BOARD_LEARNING_MODE
  44. static void omap_kp_tasklet(unsigned long);
  45. static void omap_kp_timer(unsigned long);
  46. static unsigned char keypad_state[8];
  47. static DEFINE_MUTEX(kp_enable_mutex);
  48. static int kp_enable = 1;
  49. static int kp_cur_group = -1;
  50. struct omap_kp {
  51. struct input_dev *input;
  52. struct timer_list timer;
  53. int irq;
  54. unsigned int rows;
  55. unsigned int cols;
  56. unsigned long delay;
  57. unsigned int debounce;
  58. };
  59. static DECLARE_TASKLET_DISABLED(kp_tasklet, omap_kp_tasklet, 0);
  60. static int *keymap;
  61. static unsigned int *row_gpios;
  62. static unsigned int *col_gpios;
  63. #ifdef CONFIG_ARCH_OMAP2
  64. static void set_col_gpio_val(struct omap_kp *omap_kp, u8 value)
  65. {
  66. int col;
  67. for (col = 0; col < omap_kp->cols; col++)
  68. gpio_set_value(col_gpios[col], value & (1 << col));
  69. }
  70. static u8 get_row_gpio_val(struct omap_kp *omap_kp)
  71. {
  72. int row;
  73. u8 value = 0;
  74. for (row = 0; row < omap_kp->rows; row++) {
  75. if (gpio_get_value(row_gpios[row]))
  76. value |= (1 << row);
  77. }
  78. return value;
  79. }
  80. #else
  81. #define set_col_gpio_val(x, y) do {} while (0)
  82. #define get_row_gpio_val(x) 0
  83. #endif
  84. static irqreturn_t omap_kp_interrupt(int irq, void *dev_id)
  85. {
  86. struct omap_kp *omap_kp = dev_id;
  87. /* disable keyboard interrupt and schedule for handling */
  88. if (cpu_is_omap24xx()) {
  89. int i;
  90. for (i = 0; i < omap_kp->rows; i++)
  91. disable_irq(gpio_to_irq(row_gpios[i]));
  92. } else
  93. /* disable keyboard interrupt and schedule for handling */
  94. omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  95. tasklet_schedule(&kp_tasklet);
  96. return IRQ_HANDLED;
  97. }
  98. static void omap_kp_timer(unsigned long data)
  99. {
  100. tasklet_schedule(&kp_tasklet);
  101. }
  102. static void omap_kp_scan_keypad(struct omap_kp *omap_kp, unsigned char *state)
  103. {
  104. int col = 0;
  105. /* read the keypad status */
  106. if (cpu_is_omap24xx()) {
  107. int i;
  108. for (i = 0; i < omap_kp->rows; i++)
  109. disable_irq(OMAP_GPIO_IRQ(row_gpios[i]));
  110. /* read the keypad status */
  111. for (col = 0; col < omap_kp->cols; col++) {
  112. set_col_gpio_val(omap_kp, ~(1 << col));
  113. state[col] = ~(get_row_gpio_val(omap_kp)) & 0x3f;
  114. }
  115. set_col_gpio_val(omap_kp, 0);
  116. } else {
  117. /* disable keyboard interrupt and schedule for handling */
  118. omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  119. /* read the keypad status */
  120. omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
  121. for (col = 0; col < omap_kp->cols; col++) {
  122. omap_writew(~(1 << col) & 0xff,
  123. OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
  124. udelay(omap_kp->delay);
  125. state[col] = ~omap_readw(OMAP_MPUIO_BASE +
  126. OMAP_MPUIO_KBR_LATCH) & 0xff;
  127. }
  128. omap_writew(0x00, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
  129. udelay(2);
  130. }
  131. }
  132. static inline int omap_kp_find_key(int col, int row)
  133. {
  134. int i, key;
  135. key = KEY(col, row, 0);
  136. for (i = 0; keymap[i] != 0; i++)
  137. if ((keymap[i] & 0xff000000) == key)
  138. return keymap[i] & 0x00ffffff;
  139. return -1;
  140. }
  141. static void omap_kp_tasklet(unsigned long data)
  142. {
  143. struct omap_kp *omap_kp_data = (struct omap_kp *) data;
  144. unsigned char new_state[8], changed, key_down = 0;
  145. int col, row;
  146. int spurious = 0;
  147. /* check for any changes */
  148. omap_kp_scan_keypad(omap_kp_data, new_state);
  149. /* check for changes and print those */
  150. for (col = 0; col < omap_kp_data->cols; col++) {
  151. changed = new_state[col] ^ keypad_state[col];
  152. key_down |= new_state[col];
  153. if (changed == 0)
  154. continue;
  155. for (row = 0; row < omap_kp_data->rows; row++) {
  156. int key;
  157. if (!(changed & (1 << row)))
  158. continue;
  159. #ifdef NEW_BOARD_LEARNING_MODE
  160. printk(KERN_INFO "omap-keypad: key %d-%d %s\n", col,
  161. row, (new_state[col] & (1 << row)) ?
  162. "pressed" : "released");
  163. #else
  164. key = omap_kp_find_key(col, row);
  165. if (key < 0) {
  166. printk(KERN_WARNING
  167. "omap-keypad: Spurious key event %d-%d\n",
  168. col, row);
  169. /* We scan again after a couple of seconds */
  170. spurious = 1;
  171. continue;
  172. }
  173. if (!(kp_cur_group == (key & GROUP_MASK) ||
  174. kp_cur_group == -1))
  175. continue;
  176. kp_cur_group = key & GROUP_MASK;
  177. input_report_key(omap_kp_data->input, key & ~GROUP_MASK,
  178. new_state[col] & (1 << row));
  179. #endif
  180. }
  181. }
  182. memcpy(keypad_state, new_state, sizeof(keypad_state));
  183. if (key_down) {
  184. int delay = HZ / 20;
  185. /* some key is pressed - keep irq disabled and use timer
  186. * to poll the keypad */
  187. if (spurious)
  188. delay = 2 * HZ;
  189. mod_timer(&omap_kp_data->timer, jiffies + delay);
  190. } else {
  191. /* enable interrupts */
  192. if (cpu_is_omap24xx()) {
  193. int i;
  194. for (i = 0; i < omap_kp_data->rows; i++)
  195. enable_irq(gpio_to_irq(row_gpios[i]));
  196. } else {
  197. omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  198. kp_cur_group = -1;
  199. }
  200. }
  201. }
  202. static ssize_t omap_kp_enable_show(struct device *dev,
  203. struct device_attribute *attr, char *buf)
  204. {
  205. return sprintf(buf, "%u\n", kp_enable);
  206. }
  207. static ssize_t omap_kp_enable_store(struct device *dev, struct device_attribute *attr,
  208. const char *buf, size_t count)
  209. {
  210. int state;
  211. if (sscanf(buf, "%u", &state) != 1)
  212. return -EINVAL;
  213. if ((state != 1) && (state != 0))
  214. return -EINVAL;
  215. mutex_lock(&kp_enable_mutex);
  216. if (state != kp_enable) {
  217. if (state)
  218. enable_irq(INT_KEYBOARD);
  219. else
  220. disable_irq(INT_KEYBOARD);
  221. kp_enable = state;
  222. }
  223. mutex_unlock(&kp_enable_mutex);
  224. return strnlen(buf, count);
  225. }
  226. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, omap_kp_enable_show, omap_kp_enable_store);
  227. #ifdef CONFIG_PM
  228. static int omap_kp_suspend(struct platform_device *dev, pm_message_t state)
  229. {
  230. /* Nothing yet */
  231. return 0;
  232. }
  233. static int omap_kp_resume(struct platform_device *dev)
  234. {
  235. /* Nothing yet */
  236. return 0;
  237. }
  238. #else
  239. #define omap_kp_suspend NULL
  240. #define omap_kp_resume NULL
  241. #endif
  242. static int __init omap_kp_probe(struct platform_device *pdev)
  243. {
  244. struct omap_kp *omap_kp;
  245. struct input_dev *input_dev;
  246. struct omap_kp_platform_data *pdata = pdev->dev.platform_data;
  247. int i, col_idx, row_idx, irq_idx, ret;
  248. if (!pdata->rows || !pdata->cols || !pdata->keymap) {
  249. printk(KERN_ERR "No rows, cols or keymap from pdata\n");
  250. return -EINVAL;
  251. }
  252. omap_kp = kzalloc(sizeof(struct omap_kp), GFP_KERNEL);
  253. input_dev = input_allocate_device();
  254. if (!omap_kp || !input_dev) {
  255. kfree(omap_kp);
  256. input_free_device(input_dev);
  257. return -ENOMEM;
  258. }
  259. platform_set_drvdata(pdev, omap_kp);
  260. omap_kp->input = input_dev;
  261. /* Disable the interrupt for the MPUIO keyboard */
  262. if (!cpu_is_omap24xx())
  263. omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  264. keymap = pdata->keymap;
  265. if (pdata->rep)
  266. __set_bit(EV_REP, input_dev->evbit);
  267. if (pdata->delay)
  268. omap_kp->delay = pdata->delay;
  269. if (pdata->row_gpios && pdata->col_gpios) {
  270. row_gpios = pdata->row_gpios;
  271. col_gpios = pdata->col_gpios;
  272. }
  273. omap_kp->rows = pdata->rows;
  274. omap_kp->cols = pdata->cols;
  275. if (cpu_is_omap24xx()) {
  276. /* Cols: outputs */
  277. for (col_idx = 0; col_idx < omap_kp->cols; col_idx++) {
  278. if (gpio_request(col_gpios[col_idx], "omap_kp_col") < 0) {
  279. printk(KERN_ERR "Failed to request"
  280. "GPIO%d for keypad\n",
  281. col_gpios[col_idx]);
  282. goto err1;
  283. }
  284. gpio_direction_output(col_gpios[col_idx], 0);
  285. }
  286. /* Rows: inputs */
  287. for (row_idx = 0; row_idx < omap_kp->rows; row_idx++) {
  288. if (gpio_request(row_gpios[row_idx], "omap_kp_row") < 0) {
  289. printk(KERN_ERR "Failed to request"
  290. "GPIO%d for keypad\n",
  291. row_gpios[row_idx]);
  292. goto err2;
  293. }
  294. gpio_direction_input(row_gpios[row_idx]);
  295. }
  296. } else {
  297. col_idx = 0;
  298. row_idx = 0;
  299. }
  300. setup_timer(&omap_kp->timer, omap_kp_timer, (unsigned long)omap_kp);
  301. /* get the irq and init timer*/
  302. tasklet_enable(&kp_tasklet);
  303. kp_tasklet.data = (unsigned long) omap_kp;
  304. ret = device_create_file(&pdev->dev, &dev_attr_enable);
  305. if (ret < 0)
  306. goto err2;
  307. /* setup input device */
  308. __set_bit(EV_KEY, input_dev->evbit);
  309. for (i = 0; keymap[i] != 0; i++)
  310. __set_bit(keymap[i] & KEY_MAX, input_dev->keybit);
  311. input_dev->name = "omap-keypad";
  312. input_dev->phys = "omap-keypad/input0";
  313. input_dev->dev.parent = &pdev->dev;
  314. input_dev->id.bustype = BUS_HOST;
  315. input_dev->id.vendor = 0x0001;
  316. input_dev->id.product = 0x0001;
  317. input_dev->id.version = 0x0100;
  318. ret = input_register_device(omap_kp->input);
  319. if (ret < 0) {
  320. printk(KERN_ERR "Unable to register omap-keypad input device\n");
  321. goto err3;
  322. }
  323. if (pdata->dbounce)
  324. omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
  325. /* scan current status and enable interrupt */
  326. omap_kp_scan_keypad(omap_kp, keypad_state);
  327. if (!cpu_is_omap24xx()) {
  328. omap_kp->irq = platform_get_irq(pdev, 0);
  329. if (omap_kp->irq >= 0) {
  330. if (request_irq(omap_kp->irq, omap_kp_interrupt, 0,
  331. "omap-keypad", omap_kp) < 0)
  332. goto err4;
  333. }
  334. omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  335. } else {
  336. for (irq_idx = 0; irq_idx < omap_kp->rows; irq_idx++) {
  337. if (request_irq(gpio_to_irq(row_gpios[irq_idx]),
  338. omap_kp_interrupt,
  339. IRQF_TRIGGER_FALLING,
  340. "omap-keypad", omap_kp) < 0)
  341. goto err5;
  342. }
  343. }
  344. return 0;
  345. err5:
  346. for (i = irq_idx - 1; i >=0; i--)
  347. free_irq(row_gpios[i], 0);
  348. err4:
  349. input_unregister_device(omap_kp->input);
  350. input_dev = NULL;
  351. err3:
  352. device_remove_file(&pdev->dev, &dev_attr_enable);
  353. err2:
  354. for (i = row_idx - 1; i >=0; i--)
  355. gpio_free(row_gpios[i]);
  356. err1:
  357. for (i = col_idx - 1; i >=0; i--)
  358. gpio_free(col_gpios[i]);
  359. kfree(omap_kp);
  360. input_free_device(input_dev);
  361. return -EINVAL;
  362. }
  363. static int omap_kp_remove(struct platform_device *pdev)
  364. {
  365. struct omap_kp *omap_kp = platform_get_drvdata(pdev);
  366. /* disable keypad interrupt handling */
  367. tasklet_disable(&kp_tasklet);
  368. if (cpu_is_omap24xx()) {
  369. int i;
  370. for (i = 0; i < omap_kp->cols; i++)
  371. gpio_free(col_gpios[i]);
  372. for (i = 0; i < omap_kp->rows; i++) {
  373. gpio_free(row_gpios[i]);
  374. free_irq(gpio_to_irq(row_gpios[i]), 0);
  375. }
  376. } else {
  377. omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
  378. free_irq(omap_kp->irq, 0);
  379. }
  380. del_timer_sync(&omap_kp->timer);
  381. tasklet_kill(&kp_tasklet);
  382. /* unregister everything */
  383. input_unregister_device(omap_kp->input);
  384. kfree(omap_kp);
  385. return 0;
  386. }
  387. static struct platform_driver omap_kp_driver = {
  388. .probe = omap_kp_probe,
  389. .remove = omap_kp_remove,
  390. .suspend = omap_kp_suspend,
  391. .resume = omap_kp_resume,
  392. .driver = {
  393. .name = "omap-keypad",
  394. .owner = THIS_MODULE,
  395. },
  396. };
  397. static int __devinit omap_kp_init(void)
  398. {
  399. printk(KERN_INFO "OMAP Keypad Driver\n");
  400. return platform_driver_register(&omap_kp_driver);
  401. }
  402. static void __exit omap_kp_exit(void)
  403. {
  404. platform_driver_unregister(&omap_kp_driver);
  405. }
  406. module_init(omap_kp_init);
  407. module_exit(omap_kp_exit);
  408. MODULE_AUTHOR("Timo Teräs");
  409. MODULE_DESCRIPTION("OMAP Keypad Driver");
  410. MODULE_LICENSE("GPL");
  411. MODULE_ALIAS("platform:omap-keypad");