gpio-poweroff.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. 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",
  49. __func__);
  50. return -EBUSY;
  51. }
  52. input = of_property_read_bool(pdev->dev.of_node, "input");
  53. if (input)
  54. flags = GPIOD_IN;
  55. else
  56. flags = GPIOD_OUT_LOW;
  57. reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags);
  58. if (IS_ERR(reset_gpio))
  59. return PTR_ERR(reset_gpio);
  60. pm_power_off = &gpio_poweroff_do_poweroff;
  61. return 0;
  62. }
  63. static int gpio_poweroff_remove(struct platform_device *pdev)
  64. {
  65. if (pm_power_off == &gpio_poweroff_do_poweroff)
  66. pm_power_off = NULL;
  67. return 0;
  68. }
  69. static const struct of_device_id of_gpio_poweroff_match[] = {
  70. { .compatible = "gpio-poweroff", },
  71. {},
  72. };
  73. static struct platform_driver gpio_poweroff_driver = {
  74. .probe = gpio_poweroff_probe,
  75. .remove = gpio_poweroff_remove,
  76. .driver = {
  77. .name = "poweroff-gpio",
  78. .of_match_table = of_gpio_poweroff_match,
  79. },
  80. };
  81. module_platform_driver(gpio_poweroff_driver);
  82. MODULE_AUTHOR("Jamie Lentin <jm@lentin.co.uk>");
  83. MODULE_DESCRIPTION("GPIO poweroff driver");
  84. MODULE_LICENSE("GPL v2");
  85. MODULE_ALIAS("platform:poweroff-gpio");