2 * Toggles a GPIO pin to restart a device
4 * Copyright (C) 2014 Google, Inc.
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.
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.
15 * Based on the gpio-poweroff driver.
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>
27 struct gpio_desc
*reset_gpio
;
28 struct notifier_block restart_handler
;
30 u32 inactive_delay_ms
;
34 static int gpio_restart_notify(struct notifier_block
*this,
35 unsigned long mode
, void *cmd
)
37 struct gpio_restart
*gpio_restart
=
38 container_of(this, struct gpio_restart
, restart_handler
);
40 /* drive it active, also inactive->active edge */
41 gpiod_direction_output(gpio_restart
->reset_gpio
, 1);
42 mdelay(gpio_restart
->active_delay_ms
);
44 /* drive inactive, also active->inactive edge */
45 gpiod_set_value(gpio_restart
->reset_gpio
, 0);
46 mdelay(gpio_restart
->inactive_delay_ms
);
48 /* drive it active, also inactive->active edge */
49 gpiod_set_value(gpio_restart
->reset_gpio
, 1);
51 /* give it some time */
52 mdelay(gpio_restart
->wait_delay_ms
);
59 static int gpio_restart_probe(struct platform_device
*pdev
)
61 struct gpio_restart
*gpio_restart
;
62 bool open_source
= false;
66 gpio_restart
= devm_kzalloc(&pdev
->dev
, sizeof(*gpio_restart
),
71 open_source
= of_property_read_bool(pdev
->dev
.of_node
, "open-source");
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
);
80 gpio_restart
->restart_handler
.notifier_call
= gpio_restart_notify
;
81 gpio_restart
->restart_handler
.priority
= 128;
82 gpio_restart
->active_delay_ms
= 100;
83 gpio_restart
->inactive_delay_ms
= 100;
84 gpio_restart
->wait_delay_ms
= 3000;
86 ret
= of_property_read_u32(pdev
->dev
.of_node
, "priority", &property
);
89 dev_err(&pdev
->dev
, "Invalid priority property: %u\n",
92 gpio_restart
->restart_handler
.priority
= property
;
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
);
102 platform_set_drvdata(pdev
, gpio_restart
);
104 ret
= register_restart_handler(&gpio_restart
->restart_handler
);
106 dev_err(&pdev
->dev
, "%s: cannot register restart handler, %d\n",
114 static int gpio_restart_remove(struct platform_device
*pdev
)
116 struct gpio_restart
*gpio_restart
= platform_get_drvdata(pdev
);
119 ret
= unregister_restart_handler(&gpio_restart
->restart_handler
);
122 "%s: cannot unregister restart handler, %d\n",
130 static const struct of_device_id of_gpio_restart_match
[] = {
131 { .compatible
= "gpio-restart", },
135 static struct platform_driver gpio_restart_driver
= {
136 .probe
= gpio_restart_probe
,
137 .remove
= gpio_restart_remove
,
139 .name
= "restart-gpio",
140 .of_match_table
= of_gpio_restart_match
,
144 module_platform_driver(gpio_restart_driver
);
146 MODULE_AUTHOR("David Riley <davidriley@chromium.org>");
147 MODULE_DESCRIPTION("GPIO restart driver");
148 MODULE_LICENSE("GPL");