cobalt_btns.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Cobalt button interface driver.
  3. *
  4. * Copyright (C) 2007-2008 Yoichi Yuasa <yuasa@linux-mips.org>
  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/input-polldev.h>
  21. #include <linux/ioport.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #define BUTTONS_POLL_INTERVAL 30 /* msec */
  26. #define BUTTONS_COUNT_THRESHOLD 3
  27. #define BUTTONS_STATUS_MASK 0xfe000000
  28. static const unsigned short cobalt_map[] = {
  29. KEY_RESERVED,
  30. KEY_RESTART,
  31. KEY_LEFT,
  32. KEY_UP,
  33. KEY_DOWN,
  34. KEY_RIGHT,
  35. KEY_ENTER,
  36. KEY_SELECT
  37. };
  38. struct buttons_dev {
  39. struct input_polled_dev *poll_dev;
  40. unsigned short keymap[ARRAY_SIZE(cobalt_map)];
  41. int count[ARRAY_SIZE(cobalt_map)];
  42. void __iomem *reg;
  43. };
  44. static void handle_buttons(struct input_polled_dev *dev)
  45. {
  46. struct buttons_dev *bdev = dev->private;
  47. struct input_dev *input = dev->input;
  48. uint32_t status;
  49. int i;
  50. status = ~readl(bdev->reg) >> 24;
  51. for (i = 0; i < ARRAY_SIZE(bdev->keymap); i++) {
  52. if (status & (1U << i)) {
  53. if (++bdev->count[i] == BUTTONS_COUNT_THRESHOLD) {
  54. input_event(input, EV_MSC, MSC_SCAN, i);
  55. input_report_key(input, bdev->keymap[i], 1);
  56. input_sync(input);
  57. }
  58. } else {
  59. if (bdev->count[i] >= BUTTONS_COUNT_THRESHOLD) {
  60. input_event(input, EV_MSC, MSC_SCAN, i);
  61. input_report_key(input, bdev->keymap[i], 0);
  62. input_sync(input);
  63. }
  64. bdev->count[i] = 0;
  65. }
  66. }
  67. }
  68. static int cobalt_buttons_probe(struct platform_device *pdev)
  69. {
  70. struct buttons_dev *bdev;
  71. struct input_polled_dev *poll_dev;
  72. struct input_dev *input;
  73. struct resource *res;
  74. int error, i;
  75. bdev = kzalloc(sizeof(struct buttons_dev), GFP_KERNEL);
  76. poll_dev = input_allocate_polled_device();
  77. if (!bdev || !poll_dev) {
  78. error = -ENOMEM;
  79. goto err_free_mem;
  80. }
  81. memcpy(bdev->keymap, cobalt_map, sizeof(bdev->keymap));
  82. poll_dev->private = bdev;
  83. poll_dev->poll = handle_buttons;
  84. poll_dev->poll_interval = BUTTONS_POLL_INTERVAL;
  85. input = poll_dev->input;
  86. input->name = "Cobalt buttons";
  87. input->phys = "cobalt/input0";
  88. input->id.bustype = BUS_HOST;
  89. input->dev.parent = &pdev->dev;
  90. input->keycode = bdev->keymap;
  91. input->keycodemax = ARRAY_SIZE(bdev->keymap);
  92. input->keycodesize = sizeof(unsigned short);
  93. input_set_capability(input, EV_MSC, MSC_SCAN);
  94. __set_bit(EV_KEY, input->evbit);
  95. for (i = 0; i < ARRAY_SIZE(cobalt_map); i++)
  96. __set_bit(bdev->keymap[i], input->keybit);
  97. __clear_bit(KEY_RESERVED, input->keybit);
  98. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  99. if (!res) {
  100. error = -EBUSY;
  101. goto err_free_mem;
  102. }
  103. bdev->poll_dev = poll_dev;
  104. bdev->reg = ioremap(res->start, resource_size(res));
  105. dev_set_drvdata(&pdev->dev, bdev);
  106. error = input_register_polled_device(poll_dev);
  107. if (error)
  108. goto err_iounmap;
  109. return 0;
  110. err_iounmap:
  111. iounmap(bdev->reg);
  112. err_free_mem:
  113. input_free_polled_device(poll_dev);
  114. kfree(bdev);
  115. return error;
  116. }
  117. static int cobalt_buttons_remove(struct platform_device *pdev)
  118. {
  119. struct device *dev = &pdev->dev;
  120. struct buttons_dev *bdev = dev_get_drvdata(dev);
  121. input_unregister_polled_device(bdev->poll_dev);
  122. input_free_polled_device(bdev->poll_dev);
  123. iounmap(bdev->reg);
  124. kfree(bdev);
  125. return 0;
  126. }
  127. MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
  128. MODULE_DESCRIPTION("Cobalt button interface driver");
  129. MODULE_LICENSE("GPL");
  130. /* work with hotplug and coldplug */
  131. MODULE_ALIAS("platform:Cobalt buttons");
  132. static struct platform_driver cobalt_buttons_driver = {
  133. .probe = cobalt_buttons_probe,
  134. .remove = cobalt_buttons_remove,
  135. .driver = {
  136. .name = "Cobalt buttons",
  137. },
  138. };
  139. module_platform_driver(cobalt_buttons_driver);