Loading...
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
18struct gpio_restart {
19 struct gpio_desc *reset_gpio;
20 u32 active_delay_ms;
21 u32 inactive_delay_ms;
22 u32 wait_delay_ms;
23};
24
25static int gpio_restart_notify(struct sys_off_data *data)
26{
27 struct gpio_restart *gpio_restart = data->cb_data;
28
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
33 /* drive inactive, also active->inactive edge */
34 gpiod_set_value(gpio_restart->reset_gpio, 0);
35 mdelay(gpio_restart->inactive_delay_ms);
36
37 /* drive it active, also inactive->active edge */
38 gpiod_set_value(gpio_restart->reset_gpio, 1);
39
40 /* give it some time */
41 mdelay(gpio_restart->wait_delay_ms);
42
43 WARN_ON(1);
44
45 return NOTIFY_DONE;
46}
47
48static int gpio_restart_probe(struct platform_device *pdev)
49{
50 struct gpio_restart *gpio_restart;
51 bool open_source = false;
52 int priority = 129;
53 u32 property;
54 int ret;
55
56 gpio_restart = devm_kzalloc(&pdev->dev, sizeof(*gpio_restart),
57 GFP_KERNEL);
58 if (!gpio_restart)
59 return -ENOMEM;
60
61 open_source = of_property_read_bool(pdev->dev.of_node, "open-source");
62
63 gpio_restart->reset_gpio = devm_gpiod_get(&pdev->dev, NULL,
64 open_source ? GPIOD_IN : GPIOD_OUT_LOW);
65 ret = PTR_ERR_OR_ZERO(gpio_restart->reset_gpio);
66 if (ret) {
67 if (ret != -EPROBE_DEFER)
68 dev_err(&pdev->dev, "Could not get reset GPIO\n");
69 return ret;
70 }
71
72 gpio_restart->active_delay_ms = 100;
73 gpio_restart->inactive_delay_ms = 100;
74 gpio_restart->wait_delay_ms = 3000;
75
76 ret = of_property_read_u32(pdev->dev.of_node, "priority", &property);
77 if (!ret) {
78 if (property > 255)
79 dev_err(&pdev->dev, "Invalid priority property: %u\n",
80 property);
81 else
82 priority = property;
83 }
84
85 of_property_read_u32(pdev->dev.of_node, "active-delay",
86 &gpio_restart->active_delay_ms);
87 of_property_read_u32(pdev->dev.of_node, "inactive-delay",
88 &gpio_restart->inactive_delay_ms);
89 of_property_read_u32(pdev->dev.of_node, "wait-delay",
90 &gpio_restart->wait_delay_ms);
91
92 ret = devm_register_sys_off_handler(&pdev->dev,
93 SYS_OFF_MODE_RESTART,
94 priority,
95 gpio_restart_notify,
96 gpio_restart);
97 if (ret) {
98 dev_err(&pdev->dev, "%s: cannot register restart handler, %d\n",
99 __func__, ret);
100 return -ENODEV;
101 }
102
103 return 0;
104}
105
106static const struct of_device_id of_gpio_restart_match[] = {
107 { .compatible = "gpio-restart", },
108 {},
109};
110
111static struct platform_driver gpio_restart_driver = {
112 .probe = gpio_restart_probe,
113 .driver = {
114 .name = "restart-gpio",
115 .of_match_table = of_gpio_restart_match,
116 },
117};
118
119module_platform_driver(gpio_restart_driver);
120
121MODULE_AUTHOR("David Riley <davidriley@chromium.org>");
122MODULE_DESCRIPTION("GPIO restart driver");
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
26struct gpio_restart {
27 struct gpio_desc *reset_gpio;
28 struct notifier_block restart_handler;
29 u32 active_delay_ms;
30 u32 inactive_delay_ms;
31 u32 wait_delay_ms;
32};
33
34static int gpio_restart_notify(struct notifier_block *this,
35 unsigned long mode, void *cmd)
36{
37 struct gpio_restart *gpio_restart =
38 container_of(this, struct gpio_restart, restart_handler);
39
40 /* drive it active, also inactive->active edge */
41 gpiod_direction_output(gpio_restart->reset_gpio, 1);
42 mdelay(gpio_restart->active_delay_ms);
43
44 /* drive inactive, also active->inactive edge */
45 gpiod_set_value(gpio_restart->reset_gpio, 0);
46 mdelay(gpio_restart->inactive_delay_ms);
47
48 /* drive it active, also inactive->active edge */
49 gpiod_set_value(gpio_restart->reset_gpio, 1);
50
51 /* give it some time */
52 mdelay(gpio_restart->wait_delay_ms);
53
54 WARN_ON(1);
55
56 return NOTIFY_DONE;
57}
58
59static int gpio_restart_probe(struct platform_device *pdev)
60{
61 struct gpio_restart *gpio_restart;
62 bool open_source = false;
63 u32 property;
64 int ret;
65
66 gpio_restart = devm_kzalloc(&pdev->dev, sizeof(*gpio_restart),
67 GFP_KERNEL);
68 if (!gpio_restart)
69 return -ENOMEM;
70
71 open_source = of_property_read_bool(pdev->dev.of_node, "open-source");
72
73 gpio_restart->reset_gpio = devm_gpiod_get(&pdev->dev, NULL,
74 open_source ? GPIOD_IN : GPIOD_OUT_LOW);
75 if (IS_ERR(gpio_restart->reset_gpio)) {
76 dev_err(&pdev->dev, "Could net get reset GPIO\n");
77 return PTR_ERR(gpio_restart->reset_gpio);
78 }
79
80 gpio_restart->restart_handler.notifier_call = gpio_restart_notify;
81 gpio_restart->restart_handler.priority = 129;
82 gpio_restart->active_delay_ms = 100;
83 gpio_restart->inactive_delay_ms = 100;
84 gpio_restart->wait_delay_ms = 3000;
85
86 ret = of_property_read_u32(pdev->dev.of_node, "priority", &property);
87 if (!ret) {
88 if (property > 255)
89 dev_err(&pdev->dev, "Invalid priority property: %u\n",
90 property);
91 else
92 gpio_restart->restart_handler.priority = property;
93 }
94
95 of_property_read_u32(pdev->dev.of_node, "active-delay",
96 &gpio_restart->active_delay_ms);
97 of_property_read_u32(pdev->dev.of_node, "inactive-delay",
98 &gpio_restart->inactive_delay_ms);
99 of_property_read_u32(pdev->dev.of_node, "wait-delay",
100 &gpio_restart->wait_delay_ms);
101
102 platform_set_drvdata(pdev, gpio_restart);
103
104 ret = register_restart_handler(&gpio_restart->restart_handler);
105 if (ret) {
106 dev_err(&pdev->dev, "%s: cannot register restart handler, %d\n",
107 __func__, ret);
108 return -ENODEV;
109 }
110
111 return 0;
112}
113
114static int gpio_restart_remove(struct platform_device *pdev)
115{
116 struct gpio_restart *gpio_restart = platform_get_drvdata(pdev);
117 int ret;
118
119 ret = unregister_restart_handler(&gpio_restart->restart_handler);
120 if (ret) {
121 dev_err(&pdev->dev,
122 "%s: cannot unregister restart handler, %d\n",
123 __func__, ret);
124 return -ENODEV;
125 }
126
127 return 0;
128}
129
130static const struct of_device_id of_gpio_restart_match[] = {
131 { .compatible = "gpio-restart", },
132 {},
133};
134
135static struct platform_driver gpio_restart_driver = {
136 .probe = gpio_restart_probe,
137 .remove = gpio_restart_remove,
138 .driver = {
139 .name = "restart-gpio",
140 .of_match_table = of_gpio_restart_match,
141 },
142};
143
144module_platform_driver(gpio_restart_driver);
145
146MODULE_AUTHOR("David Riley <davidriley@chromium.org>");
147MODULE_DESCRIPTION("GPIO restart driver");
148MODULE_LICENSE("GPL");