cobalt_btns.c 4.3 KB

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