gpio-poweroff.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Toggles a GPIO pin to power down a device
  3. *
  4. * Jamie Lentin <jm@lentin.co.uk>
  5. * Andrew Lunn <andrew@lunn.ch>
  6. *
  7. * Copyright (C) 2012 Jamie Lentin
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/module.h>
  21. #define DEFAULT_TIMEOUT_MS 3000
  22. /*
  23. * Hold configuration here, cannot be more than one instance of the driver
  24. * since pm_power_off itself is global.
  25. */
  26. static struct gpio_desc *reset_gpio;
  27. static u32 timeout = DEFAULT_TIMEOUT_MS;
  28. static void gpio_poweroff_do_poweroff(void)
  29. {
  30. BUG_ON(!reset_gpio);
  31. /* drive it active, also inactive->active edge */
  32. gpiod_direction_output(reset_gpio, 1);
  33. mdelay(100);
  34. /* drive inactive, also active->inactive edge */
  35. gpiod_set_value_cansleep(reset_gpio, 0);
  36. mdelay(100);
  37. /* drive it active, also inactive->active edge */
  38. gpiod_set_value_cansleep(reset_gpio, 1);
  39. /* give it some time */
  40. mdelay(timeout);
  41. WARN_ON(1);
  42. }
  43. static int gpio_poweroff_probe(struct platform_device *pdev)
  44. {
  45. bool input = false;
  46. enum gpiod_flags flags;
  47. /* If a pm_power_off function has already been added, leave it alone */
  48. if (pm_power_off != NULL) {
  49. dev_err(&pdev->dev,
  50. "%s: pm_power_off function already registered",
  51. __func__);
  52. return -EBUSY;
  53. }
  54. input = device_property_read_bool(&pdev->dev, "input");
  55. if (input)
  56. flags = GPIOD_IN;
  57. else
  58. flags = GPIOD_OUT_LOW;
  59. device_property_read_u32(&pdev->dev, "timeout-ms", &timeout);
  60. reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags);
  61. if (IS_ERR(reset_gpio))
  62. return PTR_ERR(reset_gpio);
  63. pm_power_off = &gpio_poweroff_do_poweroff;
  64. return 0;
  65. }
  66. static int gpio_poweroff_remove(struct platform_device *pdev)
  67. {
  68. if (pm_power_off == &gpio_poweroff_do_poweroff)
  69. pm_power_off = NULL;
  70. return 0;
  71. }
  72. static const struct of_device_id of_gpio_poweroff_match[] = {
  73. { .compatible = "gpio-poweroff", },
  74. {},
  75. };
  76. static struct platform_driver gpio_poweroff_driver = {
  77. .probe = gpio_poweroff_probe,
  78. .remove = gpio_poweroff_remove,
  79. .driver = {
  80. .name = "poweroff-gpio",
  81. .of_match_table = of_gpio_poweroff_match,
  82. },
  83. };
  84. module_platform_driver(gpio_poweroff_driver);
  85. MODULE_AUTHOR("Jamie Lentin <jm@lentin.co.uk>");
  86. MODULE_DESCRIPTION("GPIO poweroff driver");
  87. MODULE_LICENSE("GPL v2");
  88. MODULE_ALIAS("platform:poweroff-gpio");