gpio-poweroff.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/gpio/consumer.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/module.h>
  17. #define DEFAULT_TIMEOUT_MS 3000
  18. /*
  19. * Hold configuration here, cannot be more than one instance of the driver
  20. * since pm_power_off itself is global.
  21. */
  22. static struct gpio_desc *reset_gpio;
  23. static u32 timeout = DEFAULT_TIMEOUT_MS;
  24. static u32 active_delay = 100;
  25. static u32 inactive_delay = 100;
  26. static void gpio_poweroff_do_poweroff(void)
  27. {
  28. BUG_ON(!reset_gpio);
  29. /* drive it active, also inactive->active edge */
  30. gpiod_direction_output(reset_gpio, 1);
  31. mdelay(active_delay);
  32. /* drive inactive, also active->inactive edge */
  33. gpiod_set_value_cansleep(reset_gpio, 0);
  34. mdelay(inactive_delay);
  35. /* drive it active, also inactive->active edge */
  36. gpiod_set_value_cansleep(reset_gpio, 1);
  37. /* give it some time */
  38. mdelay(timeout);
  39. WARN_ON(1);
  40. }
  41. static int gpio_poweroff_probe(struct platform_device *pdev)
  42. {
  43. bool input = false;
  44. enum gpiod_flags flags;
  45. /* If a pm_power_off function has already been added, leave it alone */
  46. if (pm_power_off != NULL) {
  47. dev_err(&pdev->dev,
  48. "%s: pm_power_off function already registered\n",
  49. __func__);
  50. return -EBUSY;
  51. }
  52. input = device_property_read_bool(&pdev->dev, "input");
  53. if (input)
  54. flags = GPIOD_IN;
  55. else
  56. flags = GPIOD_OUT_LOW;
  57. device_property_read_u32(&pdev->dev, "active-delay-ms", &active_delay);
  58. device_property_read_u32(&pdev->dev, "inactive-delay-ms",
  59. &inactive_delay);
  60. device_property_read_u32(&pdev->dev, "timeout-ms", &timeout);
  61. reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags);
  62. if (IS_ERR(reset_gpio))
  63. return PTR_ERR(reset_gpio);
  64. pm_power_off = &gpio_poweroff_do_poweroff;
  65. return 0;
  66. }
  67. static int gpio_poweroff_remove(struct platform_device *pdev)
  68. {
  69. if (pm_power_off == &gpio_poweroff_do_poweroff)
  70. pm_power_off = NULL;
  71. return 0;
  72. }
  73. static const struct of_device_id of_gpio_poweroff_match[] = {
  74. { .compatible = "gpio-poweroff", },
  75. {},
  76. };
  77. MODULE_DEVICE_TABLE(of, of_gpio_poweroff_match);
  78. static struct platform_driver gpio_poweroff_driver = {
  79. .probe = gpio_poweroff_probe,
  80. .remove = gpio_poweroff_remove,
  81. .driver = {
  82. .name = "poweroff-gpio",
  83. .of_match_table = of_gpio_poweroff_match,
  84. },
  85. };
  86. module_platform_driver(gpio_poweroff_driver);
  87. MODULE_AUTHOR("Jamie Lentin <jm@lentin.co.uk>");
  88. MODULE_DESCRIPTION("GPIO poweroff driver");
  89. MODULE_LICENSE("GPL v2");
  90. MODULE_ALIAS("platform:poweroff-gpio");