gpio-restart.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/module.h>
  16. #include <linux/of.h>
  17. struct gpio_restart {
  18. struct gpio_desc *reset_gpio;
  19. u32 active_delay_ms;
  20. u32 inactive_delay_ms;
  21. u32 wait_delay_ms;
  22. };
  23. static int gpio_restart_notify(struct sys_off_data *data)
  24. {
  25. struct gpio_restart *gpio_restart = data->cb_data;
  26. /* drive it active, also inactive->active edge */
  27. gpiod_direction_output(gpio_restart->reset_gpio, 1);
  28. mdelay(gpio_restart->active_delay_ms);
  29. /* drive inactive, also active->inactive edge */
  30. gpiod_set_value(gpio_restart->reset_gpio, 0);
  31. mdelay(gpio_restart->inactive_delay_ms);
  32. /* drive it active, also inactive->active edge */
  33. gpiod_set_value(gpio_restart->reset_gpio, 1);
  34. /* give it some time */
  35. mdelay(gpio_restart->wait_delay_ms);
  36. WARN_ON(1);
  37. return NOTIFY_DONE;
  38. }
  39. static int gpio_restart_probe(struct platform_device *pdev)
  40. {
  41. struct gpio_restart *gpio_restart;
  42. bool open_source = false;
  43. int priority = 129;
  44. u32 property;
  45. int ret;
  46. gpio_restart = devm_kzalloc(&pdev->dev, sizeof(*gpio_restart),
  47. GFP_KERNEL);
  48. if (!gpio_restart)
  49. return -ENOMEM;
  50. open_source = of_property_read_bool(pdev->dev.of_node, "open-source");
  51. gpio_restart->reset_gpio = devm_gpiod_get(&pdev->dev, NULL,
  52. open_source ? GPIOD_IN : GPIOD_OUT_LOW);
  53. ret = PTR_ERR_OR_ZERO(gpio_restart->reset_gpio);
  54. if (ret) {
  55. if (ret != -EPROBE_DEFER)
  56. dev_err(&pdev->dev, "Could not get reset GPIO\n");
  57. return ret;
  58. }
  59. gpio_restart->active_delay_ms = 100;
  60. gpio_restart->inactive_delay_ms = 100;
  61. gpio_restart->wait_delay_ms = 3000;
  62. ret = of_property_read_u32(pdev->dev.of_node, "priority", &property);
  63. if (!ret) {
  64. if (property > 255)
  65. dev_err(&pdev->dev, "Invalid priority property: %u\n",
  66. property);
  67. else
  68. priority = property;
  69. }
  70. of_property_read_u32(pdev->dev.of_node, "active-delay",
  71. &gpio_restart->active_delay_ms);
  72. of_property_read_u32(pdev->dev.of_node, "inactive-delay",
  73. &gpio_restart->inactive_delay_ms);
  74. of_property_read_u32(pdev->dev.of_node, "wait-delay",
  75. &gpio_restart->wait_delay_ms);
  76. ret = devm_register_sys_off_handler(&pdev->dev,
  77. SYS_OFF_MODE_RESTART,
  78. priority,
  79. gpio_restart_notify,
  80. gpio_restart);
  81. if (ret) {
  82. dev_err(&pdev->dev, "%s: cannot register restart handler, %d\n",
  83. __func__, ret);
  84. return -ENODEV;
  85. }
  86. return 0;
  87. }
  88. static const struct of_device_id of_gpio_restart_match[] = {
  89. { .compatible = "gpio-restart", },
  90. {},
  91. };
  92. static struct platform_driver gpio_restart_driver = {
  93. .probe = gpio_restart_probe,
  94. .driver = {
  95. .name = "restart-gpio",
  96. .of_match_table = of_gpio_restart_match,
  97. },
  98. };
  99. module_platform_driver(gpio_restart_driver);
  100. MODULE_AUTHOR("David Riley <davidriley@chromium.org>");
  101. MODULE_DESCRIPTION("GPIO restart driver");