rb532_button.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Support for the S1 button on Routerboard 532
  3. *
  4. * Copyright (C) 2009 Phil Sutter <n0-1@freewrt.org>
  5. */
  6. #include <linux/input-polldev.h>
  7. #include <linux/module.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/gpio.h>
  10. #include <asm/mach-rc32434/gpio.h>
  11. #include <asm/mach-rc32434/rb.h>
  12. #define DRV_NAME "rb532-button"
  13. #define RB532_BTN_RATE 100 /* msec */
  14. #define RB532_BTN_KSYM BTN_0
  15. /* The S1 button state is provided by GPIO pin 1. But as this
  16. * pin is also used for uart input as alternate function, the
  17. * operational modes must be switched first:
  18. * 1) disable uart using set_latch_u5()
  19. * 2) turn off alternate function implicitly through
  20. * gpio_direction_input()
  21. * 3) read the GPIO's current value
  22. * 4) undo step 2 by enabling alternate function (in this
  23. * mode the GPIO direction is fixed, so no change needed)
  24. * 5) turn on uart again
  25. * The GPIO value occurs to be inverted, so pin high means
  26. * button is not pressed.
  27. */
  28. static bool rb532_button_pressed(void)
  29. {
  30. int val;
  31. set_latch_u5(0, LO_FOFF);
  32. gpio_direction_input(GPIO_BTN_S1);
  33. val = gpio_get_value(GPIO_BTN_S1);
  34. rb532_gpio_set_func(GPIO_BTN_S1);
  35. set_latch_u5(LO_FOFF, 0);
  36. return !val;
  37. }
  38. static void rb532_button_poll(struct input_polled_dev *poll_dev)
  39. {
  40. input_report_key(poll_dev->input, RB532_BTN_KSYM,
  41. rb532_button_pressed());
  42. input_sync(poll_dev->input);
  43. }
  44. static int rb532_button_probe(struct platform_device *pdev)
  45. {
  46. struct input_polled_dev *poll_dev;
  47. int error;
  48. poll_dev = input_allocate_polled_device();
  49. if (!poll_dev)
  50. return -ENOMEM;
  51. poll_dev->poll = rb532_button_poll;
  52. poll_dev->poll_interval = RB532_BTN_RATE;
  53. poll_dev->input->name = "rb532 button";
  54. poll_dev->input->phys = "rb532/button0";
  55. poll_dev->input->id.bustype = BUS_HOST;
  56. poll_dev->input->dev.parent = &pdev->dev;
  57. dev_set_drvdata(&pdev->dev, poll_dev);
  58. input_set_capability(poll_dev->input, EV_KEY, RB532_BTN_KSYM);
  59. error = input_register_polled_device(poll_dev);
  60. if (error) {
  61. input_free_polled_device(poll_dev);
  62. return error;
  63. }
  64. return 0;
  65. }
  66. static int rb532_button_remove(struct platform_device *pdev)
  67. {
  68. struct input_polled_dev *poll_dev = dev_get_drvdata(&pdev->dev);
  69. input_unregister_polled_device(poll_dev);
  70. input_free_polled_device(poll_dev);
  71. return 0;
  72. }
  73. static struct platform_driver rb532_button_driver = {
  74. .probe = rb532_button_probe,
  75. .remove = rb532_button_remove,
  76. .driver = {
  77. .name = DRV_NAME,
  78. },
  79. };
  80. module_platform_driver(rb532_button_driver);
  81. MODULE_AUTHOR("Phil Sutter <n0-1@freewrt.org>");
  82. MODULE_LICENSE("GPL");
  83. MODULE_DESCRIPTION("Support for S1 button on Routerboard 532");
  84. MODULE_ALIAS("platform:" DRV_NAME);