gpio-poweroff.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Toggles a GPIO pin to power down a device
  4. *
  5. * Jamie Lentin <jm@lentin.co.uk>
  6. * Andrew Lunn <andrew@lunn.ch>
  7. *
  8. * Copyright (C) 2012 Jamie Lentin
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/delay.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/property.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/module.h>
  18. #include <linux/reboot.h>
  19. #define DEFAULT_TIMEOUT_MS 3000
  20. struct gpio_poweroff {
  21. struct gpio_desc *reset_gpio;
  22. u32 timeout_ms;
  23. u32 active_delay_ms;
  24. u32 inactive_delay_ms;
  25. };
  26. static int gpio_poweroff_do_poweroff(struct sys_off_data *data)
  27. {
  28. struct gpio_poweroff *gpio_poweroff = data->cb_data;
  29. /* drive it active, also inactive->active edge */
  30. gpiod_direction_output(gpio_poweroff->reset_gpio, 1);
  31. mdelay(gpio_poweroff->active_delay_ms);
  32. /* drive inactive, also active->inactive edge */
  33. gpiod_set_value_cansleep(gpio_poweroff->reset_gpio, 0);
  34. mdelay(gpio_poweroff->inactive_delay_ms);
  35. /* drive it active, also inactive->active edge */
  36. gpiod_set_value_cansleep(gpio_poweroff->reset_gpio, 1);
  37. /* give it some time */
  38. mdelay(gpio_poweroff->timeout_ms);
  39. /*
  40. * If code reaches this point, it means that gpio-poweroff has failed
  41. * to actually power off the system.
  42. * Warn the user that the attempt to poweroff via gpio-poweroff
  43. * has gone wrong.
  44. */
  45. WARN(1, "Failed to poweroff via gpio-poweroff mechanism\n");
  46. return NOTIFY_DONE;
  47. }
  48. static int gpio_poweroff_probe(struct platform_device *pdev)
  49. {
  50. struct gpio_poweroff *gpio_poweroff;
  51. bool input = false;
  52. enum gpiod_flags flags;
  53. int priority = SYS_OFF_PRIO_DEFAULT;
  54. int ret;
  55. gpio_poweroff = devm_kzalloc(&pdev->dev, sizeof(*gpio_poweroff), GFP_KERNEL);
  56. if (!gpio_poweroff)
  57. return -ENOMEM;
  58. input = device_property_read_bool(&pdev->dev, "input");
  59. if (input)
  60. flags = GPIOD_IN;
  61. else
  62. flags = GPIOD_OUT_LOW;
  63. gpio_poweroff->active_delay_ms = 100;
  64. gpio_poweroff->inactive_delay_ms = 100;
  65. gpio_poweroff->timeout_ms = DEFAULT_TIMEOUT_MS;
  66. device_property_read_u32(&pdev->dev, "active-delay-ms", &gpio_poweroff->active_delay_ms);
  67. device_property_read_u32(&pdev->dev, "inactive-delay-ms",
  68. &gpio_poweroff->inactive_delay_ms);
  69. device_property_read_u32(&pdev->dev, "timeout-ms", &gpio_poweroff->timeout_ms);
  70. device_property_read_u32(&pdev->dev, "priority", &priority);
  71. if (priority > 255) {
  72. dev_err(&pdev->dev, "Invalid priority property: %u\n", priority);
  73. return -EINVAL;
  74. }
  75. gpio_poweroff->reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags);
  76. if (IS_ERR(gpio_poweroff->reset_gpio))
  77. return PTR_ERR(gpio_poweroff->reset_gpio);
  78. ret = devm_register_sys_off_handler(&pdev->dev, SYS_OFF_MODE_POWER_OFF,
  79. priority, gpio_poweroff_do_poweroff, gpio_poweroff);
  80. if (ret)
  81. return dev_err_probe(&pdev->dev, ret, "Cannot register poweroff handler\n");
  82. return 0;
  83. }
  84. static const struct of_device_id of_gpio_poweroff_match[] = {
  85. { .compatible = "gpio-poweroff", },
  86. {},
  87. };
  88. MODULE_DEVICE_TABLE(of, of_gpio_poweroff_match);
  89. static struct platform_driver gpio_poweroff_driver = {
  90. .probe = gpio_poweroff_probe,
  91. .driver = {
  92. .name = "poweroff-gpio",
  93. .of_match_table = of_gpio_poweroff_match,
  94. },
  95. };
  96. module_platform_driver(gpio_poweroff_driver);
  97. MODULE_AUTHOR("Jamie Lentin <jm@lentin.co.uk>");
  98. MODULE_DESCRIPTION("GPIO poweroff driver");
  99. MODULE_ALIAS("platform:poweroff-gpio");