gpio-restart.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Toggles a GPIO pin to restart a device
  4. *
  5. * Copyright (C) 2014 Google, Inc.
  6. *
  7. * Based on the gpio-poweroff driver.
  8. */
  9. #include <linux/reboot.h>
  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. struct gpio_restart {
  18. struct gpio_desc *reset_gpio;
  19. struct notifier_block restart_handler;
  20. u32 active_delay_ms;
  21. u32 inactive_delay_ms;
  22. u32 wait_delay_ms;
  23. };
  24. static int gpio_restart_notify(struct notifier_block *this,
  25. unsigned long mode, void *cmd)
  26. {
  27. struct gpio_restart *gpio_restart =
  28. container_of(this, struct gpio_restart, restart_handler);
  29. /* drive it active, also inactive->active edge */
  30. gpiod_direction_output(gpio_restart->reset_gpio, 1);
  31. mdelay(gpio_restart->active_delay_ms);
  32. /* drive inactive, also active->inactive edge */
  33. gpiod_set_value(gpio_restart->reset_gpio, 0);
  34. mdelay(gpio_restart->inactive_delay_ms);
  35. /* drive it active, also inactive->active edge */
  36. gpiod_set_value(gpio_restart->reset_gpio, 1);
  37. /* give it some time */
  38. mdelay(gpio_restart->wait_delay_ms);
  39. WARN_ON(1);
  40. return NOTIFY_DONE;
  41. }
  42. static int gpio_restart_probe(struct platform_device *pdev)
  43. {
  44. struct gpio_restart *gpio_restart;
  45. bool open_source = false;
  46. u32 property;
  47. int ret;
  48. gpio_restart = devm_kzalloc(&pdev->dev, sizeof(*gpio_restart),
  49. GFP_KERNEL);
  50. if (!gpio_restart)
  51. return -ENOMEM;
  52. open_source = of_property_read_bool(pdev->dev.of_node, "open-source");
  53. gpio_restart->reset_gpio = devm_gpiod_get(&pdev->dev, NULL,
  54. open_source ? GPIOD_IN : GPIOD_OUT_LOW);
  55. ret = PTR_ERR_OR_ZERO(gpio_restart->reset_gpio);
  56. if (ret) {
  57. if (ret != -EPROBE_DEFER)
  58. dev_err(&pdev->dev, "Could not get reset GPIO\n");
  59. return ret;
  60. }
  61. gpio_restart->restart_handler.notifier_call = gpio_restart_notify;
  62. gpio_restart->restart_handler.priority = 129;
  63. gpio_restart->active_delay_ms = 100;
  64. gpio_restart->inactive_delay_ms = 100;
  65. gpio_restart->wait_delay_ms = 3000;
  66. ret = of_property_read_u32(pdev->dev.of_node, "priority", &property);
  67. if (!ret) {
  68. if (property > 255)
  69. dev_err(&pdev->dev, "Invalid priority property: %u\n",
  70. property);
  71. else
  72. gpio_restart->restart_handler.priority = property;
  73. }
  74. of_property_read_u32(pdev->dev.of_node, "active-delay",
  75. &gpio_restart->active_delay_ms);
  76. of_property_read_u32(pdev->dev.of_node, "inactive-delay",
  77. &gpio_restart->inactive_delay_ms);
  78. of_property_read_u32(pdev->dev.of_node, "wait-delay",
  79. &gpio_restart->wait_delay_ms);
  80. platform_set_drvdata(pdev, gpio_restart);
  81. ret = register_restart_handler(&gpio_restart->restart_handler);
  82. if (ret) {
  83. dev_err(&pdev->dev, "%s: cannot register restart handler, %d\n",
  84. __func__, ret);
  85. return -ENODEV;
  86. }
  87. return 0;
  88. }
  89. static int gpio_restart_remove(struct platform_device *pdev)
  90. {
  91. struct gpio_restart *gpio_restart = platform_get_drvdata(pdev);
  92. int ret;
  93. ret = unregister_restart_handler(&gpio_restart->restart_handler);
  94. if (ret) {
  95. dev_err(&pdev->dev,
  96. "%s: cannot unregister restart handler, %d\n",
  97. __func__, ret);
  98. return -ENODEV;
  99. }
  100. return 0;
  101. }
  102. static const struct of_device_id of_gpio_restart_match[] = {
  103. { .compatible = "gpio-restart", },
  104. {},
  105. };
  106. static struct platform_driver gpio_restart_driver = {
  107. .probe = gpio_restart_probe,
  108. .remove = gpio_restart_remove,
  109. .driver = {
  110. .name = "restart-gpio",
  111. .of_match_table = of_gpio_restart_match,
  112. },
  113. };
  114. module_platform_driver(gpio_restart_driver);
  115. MODULE_AUTHOR("David Riley <davidriley@chromium.org>");
  116. MODULE_DESCRIPTION("GPIO restart driver");
  117. MODULE_LICENSE("GPL");