cobalt_btns.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 resource cobalt_buttons_resource __initdata = {
  49. .start = 0x1d000000,
  50. .end = 0x1d000003,
  51. .flags = IORESOURCE_MEM,
  52. };
  53. static struct platform_device *cobalt_buttons_device;
  54. static struct timer_list buttons_timer;
  55. static void handle_buttons(unsigned long data)
  56. {
  57. struct buttons_map *button = buttons_map;
  58. struct buttons_dev *bdev;
  59. uint32_t status;
  60. int i;
  61. bdev = (struct buttons_dev *)data;
  62. status = readl(bdev->reg);
  63. status = ~status & BUTTONS_STATUS_MASK;
  64. for (i = 0; i < ARRAY_SIZE(buttons_map); i++) {
  65. if (status & button->mask) {
  66. button->count++;
  67. } else {
  68. if (button->count >= BUTTONS_COUNT_THRESHOLD) {
  69. input_report_key(bdev->input, button->keycode, 0);
  70. input_sync(bdev->input);
  71. }
  72. button->count = 0;
  73. }
  74. if (button->count == BUTTONS_COUNT_THRESHOLD) {
  75. input_report_key(bdev->input, button->keycode, 1);
  76. input_sync(bdev->input);
  77. }
  78. button++;
  79. }
  80. mod_timer(&buttons_timer, jiffies + msecs_to_jiffies(BUTTONS_POLL_INTERVAL));
  81. }
  82. static int cobalt_buttons_open(struct input_dev *dev)
  83. {
  84. mod_timer(&buttons_timer, jiffies + msecs_to_jiffies(BUTTONS_POLL_INTERVAL));
  85. return 0;
  86. }
  87. static void cobalt_buttons_close(struct input_dev *dev)
  88. {
  89. del_timer_sync(&buttons_timer);
  90. }
  91. static int __devinit cobalt_buttons_probe(struct platform_device *pdev)
  92. {
  93. struct buttons_dev *bdev;
  94. struct input_dev *input;
  95. struct resource *res;
  96. int error, i;
  97. bdev = kzalloc(sizeof(struct buttons_dev), GFP_KERNEL);
  98. input = input_allocate_device();
  99. if (!bdev || !input) {
  100. error = -ENOMEM;
  101. goto err_free_mem;
  102. }
  103. input->name = "Cobalt buttons";
  104. input->phys = "cobalt/input0";
  105. input->id.bustype = BUS_HOST;
  106. input->cdev.dev = &pdev->dev;
  107. input->open = cobalt_buttons_open;
  108. input->close = cobalt_buttons_close;
  109. input->evbit[0] = BIT(EV_KEY);
  110. for (i = 0; i < ARRAY_SIZE(buttons_map); i++) {
  111. set_bit(buttons_map[i].keycode, input->keybit);
  112. buttons_map[i].count = 0;
  113. }
  114. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  115. if (!res) {
  116. error = -EBUSY;
  117. goto err_free_mem;
  118. }
  119. bdev->input = input;
  120. bdev->reg = ioremap(res->start, res->end - res->start + 1);
  121. dev_set_drvdata(&pdev->dev, bdev);
  122. setup_timer(&buttons_timer, handle_buttons, (unsigned long)bdev);
  123. error = input_register_device(input);
  124. if (error)
  125. goto err_iounmap;
  126. return 0;
  127. err_iounmap:
  128. iounmap(bdev->reg);
  129. err_free_mem:
  130. input_free_device(input);
  131. kfree(bdev);
  132. dev_set_drvdata(&pdev->dev, NULL);
  133. return error;
  134. }
  135. static int __devexit cobalt_buttons_remove(struct platform_device *pdev)
  136. {
  137. struct device *dev = &pdev->dev;
  138. struct buttons_dev *bdev = dev_get_drvdata(dev);
  139. input_unregister_device(bdev->input);
  140. iounmap(bdev->reg);
  141. kfree(bdev);
  142. dev_set_drvdata(dev, NULL);
  143. return 0;
  144. }
  145. static struct platform_driver cobalt_buttons_driver = {
  146. .probe = cobalt_buttons_probe,
  147. .remove = __devexit_p(cobalt_buttons_remove),
  148. .driver = {
  149. .name = "Cobalt buttons",
  150. .owner = THIS_MODULE,
  151. },
  152. };
  153. static int __init cobalt_buttons_init(void)
  154. {
  155. int retval;
  156. cobalt_buttons_device = platform_device_register_simple("Cobalt buttons", -1,
  157. &cobalt_buttons_resource, 1);
  158. if (IS_ERR(cobalt_buttons_device)) {
  159. retval = PTR_ERR(cobalt_buttons_device);
  160. return retval;
  161. }
  162. retval = platform_driver_register(&cobalt_buttons_driver);
  163. if (retval < 0)
  164. platform_device_unregister(cobalt_buttons_device);
  165. return retval;
  166. }
  167. static void __exit cobalt_buttons_exit(void)
  168. {
  169. platform_driver_unregister(&cobalt_buttons_driver);
  170. platform_device_unregister(cobalt_buttons_device);
  171. }
  172. module_init(cobalt_buttons_init);
  173. module_exit(cobalt_buttons_exit);