gpio-restart.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Toggles a GPIO pin to restart a device
  3. *
  4. * Copyright (C) 2014 Google, Inc.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * Based on the gpio-poweroff driver.
  16. */
  17. #include <linux/reboot.h>
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/delay.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/gpio/consumer.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/module.h>
  25. struct gpio_restart {
  26. struct gpio_desc *reset_gpio;
  27. struct notifier_block restart_handler;
  28. u32 active_delay_ms;
  29. u32 inactive_delay_ms;
  30. u32 wait_delay_ms;
  31. };
  32. static int gpio_restart_notify(struct notifier_block *this,
  33. unsigned long mode, void *cmd)
  34. {
  35. struct gpio_restart *gpio_restart =
  36. container_of(this, struct gpio_restart, restart_handler);
  37. /* drive it active, also inactive->active edge */
  38. gpiod_direction_output(gpio_restart->reset_gpio, 1);
  39. mdelay(gpio_restart->active_delay_ms);
  40. /* drive inactive, also active->inactive edge */
  41. gpiod_set_value(gpio_restart->reset_gpio, 0);
  42. mdelay(gpio_restart->inactive_delay_ms);
  43. /* drive it active, also inactive->active edge */
  44. gpiod_set_value(gpio_restart->reset_gpio, 1);
  45. /* give it some time */
  46. mdelay(gpio_restart->wait_delay_ms);
  47. WARN_ON(1);
  48. return NOTIFY_DONE;
  49. }
  50. static int gpio_restart_probe(struct platform_device *pdev)
  51. {
  52. struct gpio_restart *gpio_restart;
  53. bool open_source = false;
  54. u32 property;
  55. int ret;
  56. gpio_restart = devm_kzalloc(&pdev->dev, sizeof(*gpio_restart),
  57. GFP_KERNEL);
  58. if (!gpio_restart)
  59. return -ENOMEM;
  60. open_source = of_property_read_bool(pdev->dev.of_node, "open-source");
  61. gpio_restart->reset_gpio = devm_gpiod_get(&pdev->dev, NULL,
  62. open_source ? GPIOD_IN : GPIOD_OUT_LOW);
  63. if (IS_ERR(gpio_restart->reset_gpio)) {
  64. dev_err(&pdev->dev, "Could net get reset GPIO\n");
  65. return PTR_ERR(gpio_restart->reset_gpio);
  66. }
  67. gpio_restart->restart_handler.notifier_call = gpio_restart_notify;
  68. gpio_restart->restart_handler.priority = 129;
  69. gpio_restart->active_delay_ms = 100;
  70. gpio_restart->inactive_delay_ms = 100;
  71. gpio_restart->wait_delay_ms = 3000;
  72. ret = of_property_read_u32(pdev->dev.of_node, "priority", &property);
  73. if (!ret) {
  74. if (property > 255)
  75. dev_err(&pdev->dev, "Invalid priority property: %u\n",
  76. property);
  77. else
  78. gpio_restart->restart_handler.priority = property;
  79. }
  80. of_property_read_u32(pdev->dev.of_node, "active-delay",
  81. &gpio_restart->active_delay_ms);
  82. of_property_read_u32(pdev->dev.of_node, "inactive-delay",
  83. &gpio_restart->inactive_delay_ms);
  84. of_property_read_u32(pdev->dev.of_node, "wait-delay",
  85. &gpio_restart->wait_delay_ms);
  86. platform_set_drvdata(pdev, gpio_restart);
  87. ret = register_restart_handler(&gpio_restart->restart_handler);
  88. if (ret) {
  89. dev_err(&pdev->dev, "%s: cannot register restart handler, %d\n",
  90. __func__, ret);
  91. return -ENODEV;
  92. }
  93. return 0;
  94. }
  95. static int gpio_restart_remove(struct platform_device *pdev)
  96. {
  97. struct gpio_restart *gpio_restart = platform_get_drvdata(pdev);
  98. int ret;
  99. ret = unregister_restart_handler(&gpio_restart->restart_handler);
  100. if (ret) {
  101. dev_err(&pdev->dev,
  102. "%s: cannot unregister restart handler, %d\n",
  103. __func__, ret);
  104. return -ENODEV;
  105. }
  106. return 0;
  107. }
  108. static const struct of_device_id of_gpio_restart_match[] = {
  109. { .compatible = "gpio-restart", },
  110. {},
  111. };
  112. static struct platform_driver gpio_restart_driver = {
  113. .probe = gpio_restart_probe,
  114. .remove = gpio_restart_remove,
  115. .driver = {
  116. .name = "restart-gpio",
  117. .of_match_table = of_gpio_restart_match,
  118. },
  119. };
  120. module_platform_driver(gpio_restart_driver);
  121. MODULE_AUTHOR("David Riley <davidriley@chromium.org>");
  122. MODULE_DESCRIPTION("GPIO restart driver");
  123. MODULE_LICENSE("GPL");