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