gpio-poweroff.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. WARN_ON(1);
  40. return NOTIFY_DONE;
  41. }
  42. static int gpio_poweroff_probe(struct platform_device *pdev)
  43. {
  44. struct gpio_poweroff *gpio_poweroff;
  45. bool input = false;
  46. enum gpiod_flags flags;
  47. int priority = SYS_OFF_PRIO_DEFAULT;
  48. int ret;
  49. gpio_poweroff = devm_kzalloc(&pdev->dev, sizeof(*gpio_poweroff), GFP_KERNEL);
  50. if (!gpio_poweroff)
  51. return -ENOMEM;
  52. input = device_property_read_bool(&pdev->dev, "input");
  53. if (input)
  54. flags = GPIOD_IN;
  55. else
  56. flags = GPIOD_OUT_LOW;
  57. gpio_poweroff->active_delay_ms = 100;
  58. gpio_poweroff->inactive_delay_ms = 100;
  59. gpio_poweroff->timeout_ms = DEFAULT_TIMEOUT_MS;
  60. device_property_read_u32(&pdev->dev, "active-delay-ms", &gpio_poweroff->active_delay_ms);
  61. device_property_read_u32(&pdev->dev, "inactive-delay-ms",
  62. &gpio_poweroff->inactive_delay_ms);
  63. device_property_read_u32(&pdev->dev, "timeout-ms", &gpio_poweroff->timeout_ms);
  64. device_property_read_u32(&pdev->dev, "priority", &priority);
  65. if (priority > 255) {
  66. dev_err(&pdev->dev, "Invalid priority property: %u\n", priority);
  67. return -EINVAL;
  68. }
  69. gpio_poweroff->reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags);
  70. if (IS_ERR(gpio_poweroff->reset_gpio))
  71. return PTR_ERR(gpio_poweroff->reset_gpio);
  72. ret = devm_register_sys_off_handler(&pdev->dev, SYS_OFF_MODE_POWER_OFF,
  73. priority, gpio_poweroff_do_poweroff, gpio_poweroff);
  74. if (ret)
  75. return dev_err_probe(&pdev->dev, ret, "Cannot register poweroff handler\n");
  76. return 0;
  77. }
  78. static const struct of_device_id of_gpio_poweroff_match[] = {
  79. { .compatible = "gpio-poweroff", },
  80. {},
  81. };
  82. MODULE_DEVICE_TABLE(of, of_gpio_poweroff_match);
  83. static struct platform_driver gpio_poweroff_driver = {
  84. .probe = gpio_poweroff_probe,
  85. .driver = {
  86. .name = "poweroff-gpio",
  87. .of_match_table = of_gpio_poweroff_match,
  88. },
  89. };
  90. module_platform_driver(gpio_poweroff_driver);
  91. MODULE_AUTHOR("Jamie Lentin <jm@lentin.co.uk>");
  92. MODULE_DESCRIPTION("GPIO poweroff driver");
  93. MODULE_ALIAS("platform:poweroff-gpio");