rb532_button.c 2.2 KB

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