cobalt_btns.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Cobalt button interface driver.
  3. *
  4. * Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <linux/init.h>
  21. #include <linux/input.h>
  22. #include <linux/ioport.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/timer.h>
  27. #define BUTTONS_POLL_INTERVAL 30 /* msec */
  28. #define BUTTONS_COUNT_THRESHOLD 3
  29. #define BUTTONS_STATUS_MASK 0xfe000000
  30. struct buttons_dev {
  31. struct input_dev *input;
  32. void __iomem *reg;
  33. };
  34. struct buttons_map {
  35. uint32_t mask;
  36. int keycode;
  37. int count;
  38. };
  39. static struct buttons_map buttons_map[] = {
  40. { 0x02000000, KEY_RESTART, },
  41. { 0x04000000, KEY_LEFT, },
  42. { 0x08000000, KEY_UP, },
  43. { 0x10000000, KEY_DOWN, },
  44. { 0x20000000, KEY_RIGHT, },
  45. { 0x40000000, KEY_ENTER, },
  46. { 0x80000000, KEY_SELECT, },
  47. };
  48. static struct timer_list buttons_timer;
  49. static void handle_buttons(unsigned long data)
  50. {
  51. struct buttons_map *button = buttons_map;
  52. struct buttons_dev *bdev;
  53. uint32_t status;
  54. int i;
  55. bdev = (struct buttons_dev *)data;
  56. status = readl(bdev->reg);
  57. status = ~status & BUTTONS_STATUS_MASK;
  58. for (i = 0; i < ARRAY_SIZE(buttons_map); i++) {
  59. if (status & button->mask) {
  60. button->count++;
  61. } else {
  62. if (button->count >= BUTTONS_COUNT_THRESHOLD) {
  63. input_report_key(bdev->input, button->keycode, 0);
  64. input_sync(bdev->input);
  65. }
  66. button->count = 0;
  67. }
  68. if (button->count == BUTTONS_COUNT_THRESHOLD) {
  69. input_report_key(bdev->input, button->keycode, 1);
  70. input_sync(bdev->input);
  71. }
  72. button++;
  73. }
  74. mod_timer(&buttons_timer, jiffies + msecs_to_jiffies(BUTTONS_POLL_INTERVAL));
  75. }
  76. static int cobalt_buttons_open(struct input_dev *dev)
  77. {
  78. mod_timer(&buttons_timer, jiffies + msecs_to_jiffies(BUTTONS_POLL_INTERVAL));
  79. return 0;
  80. }
  81. static void cobalt_buttons_close(struct input_dev *dev)
  82. {
  83. del_timer_sync(&buttons_timer);
  84. }
  85. static int __devinit cobalt_buttons_probe(struct platform_device *pdev)
  86. {
  87. struct buttons_dev *bdev;
  88. struct input_dev *input;
  89. struct resource *res;
  90. int error, i;
  91. bdev = kzalloc(sizeof(struct buttons_dev), GFP_KERNEL);
  92. input = input_allocate_device();
  93. if (!bdev || !input) {
  94. error = -ENOMEM;
  95. goto err_free_mem;
  96. }
  97. input->name = "Cobalt buttons";
  98. input->phys = "cobalt/input0";
  99. input->id.bustype = BUS_HOST;
  100. input->cdev.dev = &pdev->dev;
  101. input->open = cobalt_buttons_open;
  102. input->close = cobalt_buttons_close;
  103. input->evbit[0] = BIT(EV_KEY);
  104. for (i = 0; i < ARRAY_SIZE(buttons_map); i++) {
  105. set_bit(buttons_map[i].keycode, input->keybit);
  106. buttons_map[i].count = 0;
  107. }
  108. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  109. if (!res) {
  110. error = -EBUSY;
  111. goto err_free_mem;
  112. }
  113. bdev->input = input;
  114. bdev->reg = ioremap(res->start, res->end - res->start + 1);
  115. dev_set_drvdata(&pdev->dev, bdev);
  116. setup_timer(&buttons_timer, handle_buttons, (unsigned long)bdev);
  117. error = input_register_device(input);
  118. if (error)
  119. goto err_iounmap;
  120. return 0;
  121. err_iounmap:
  122. iounmap(bdev->reg);
  123. err_free_mem:
  124. input_free_device(input);
  125. kfree(bdev);
  126. dev_set_drvdata(&pdev->dev, NULL);
  127. return error;
  128. }
  129. static int __devexit cobalt_buttons_remove(struct platform_device *pdev)
  130. {
  131. struct device *dev = &pdev->dev;
  132. struct buttons_dev *bdev = dev_get_drvdata(dev);
  133. input_unregister_device(bdev->input);
  134. iounmap(bdev->reg);
  135. kfree(bdev);
  136. dev_set_drvdata(dev, NULL);
  137. return 0;
  138. }
  139. static struct platform_driver cobalt_buttons_driver = {
  140. .probe = cobalt_buttons_probe,
  141. .remove = __devexit_p(cobalt_buttons_remove),
  142. .driver = {
  143. .name = "Cobalt buttons",
  144. .owner = THIS_MODULE,
  145. },
  146. };
  147. static int __init cobalt_buttons_init(void)
  148. {
  149. return platform_driver_register(&cobalt_buttons_driver);
  150. }
  151. static void __exit cobalt_buttons_exit(void)
  152. {
  153. platform_driver_unregister(&cobalt_buttons_driver);
  154. }
  155. module_init(cobalt_buttons_init);
  156. module_exit(cobalt_buttons_exit);