gpio-poweroff.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /*
  22. * Hold configuration here, cannot be more than one instance of the driver
  23. * since pm_power_off itself is global.
  24. */
  25. static struct gpio_desc *reset_gpio;
  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(100);
  32. /* drive inactive, also active->inactive edge */
  33. gpiod_set_value(reset_gpio, 0);
  34. mdelay(100);
  35. /* drive it active, also inactive->active edge */
  36. gpiod_set_value(reset_gpio, 1);
  37. /* give it some time */
  38. mdelay(3000);
  39. WARN_ON(1);
  40. }
  41. static int gpio_poweroff_probe(struct platform_device *pdev)
  42. {
  43. bool input = false;
  44. /* If a pm_power_off function has already been added, leave it alone */
  45. if (pm_power_off != NULL) {
  46. dev_err(&pdev->dev,
  47. "%s: pm_power_off function already registered",
  48. __func__);
  49. return -EBUSY;
  50. }
  51. reset_gpio = devm_gpiod_get(&pdev->dev, NULL);
  52. if (IS_ERR(reset_gpio))
  53. return PTR_ERR(reset_gpio);
  54. input = of_property_read_bool(pdev->dev.of_node, "input");
  55. if (input) {
  56. if (gpiod_direction_input(reset_gpio)) {
  57. dev_err(&pdev->dev,
  58. "Could not set direction of reset GPIO to input\n");
  59. return -ENODEV;
  60. }
  61. } else {
  62. if (gpiod_direction_output(reset_gpio, 0)) {
  63. dev_err(&pdev->dev,
  64. "Could not set direction of reset GPIO\n");
  65. return -ENODEV;
  66. }
  67. }
  68. pm_power_off = &gpio_poweroff_do_poweroff;
  69. return 0;
  70. }
  71. static int gpio_poweroff_remove(struct platform_device *pdev)
  72. {
  73. if (pm_power_off == &gpio_poweroff_do_poweroff)
  74. pm_power_off = NULL;
  75. return 0;
  76. }
  77. static const struct of_device_id of_gpio_poweroff_match[] = {
  78. { .compatible = "gpio-poweroff", },
  79. {},
  80. };
  81. static struct platform_driver gpio_poweroff_driver = {
  82. .probe = gpio_poweroff_probe,
  83. .remove = gpio_poweroff_remove,
  84. .driver = {
  85. .name = "poweroff-gpio",
  86. .of_match_table = of_gpio_poweroff_match,
  87. },
  88. };
  89. module_platform_driver(gpio_poweroff_driver);
  90. MODULE_AUTHOR("Jamie Lentin <jm@lentin.co.uk>");
  91. MODULE_DESCRIPTION("GPIO poweroff driver");
  92. MODULE_LICENSE("GPL v2");
  93. MODULE_ALIAS("platform:poweroff-gpio");