1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Toggles a GPIO pin to power down a device
4 *
5 * Jamie Lentin <[email protected]>
6 * Andrew Lunn <[email protected]>
7 *
8 * Copyright (C) 2012 Jamie Lentin
9 */
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/platform_device.h>
14 #include <linux/property.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/module.h>
18 #include <linux/reboot.h>
19
20 #define DEFAULT_TIMEOUT_MS 3000
21
22 struct gpio_poweroff {
23 struct gpio_desc *reset_gpio;
24 u32 timeout_ms;
25 u32 active_delay_ms;
26 u32 inactive_delay_ms;
27 };
28
gpio_poweroff_do_poweroff(struct sys_off_data * data)29 static int gpio_poweroff_do_poweroff(struct sys_off_data *data)
30 {
31 struct gpio_poweroff *gpio_poweroff = data->cb_data;
32
33 /* drive it active, also inactive->active edge */
34 gpiod_direction_output(gpio_poweroff->reset_gpio, 1);
35 mdelay(gpio_poweroff->active_delay_ms);
36
37 /* drive inactive, also active->inactive edge */
38 gpiod_set_value_cansleep(gpio_poweroff->reset_gpio, 0);
39 mdelay(gpio_poweroff->inactive_delay_ms);
40
41 /* drive it active, also inactive->active edge */
42 gpiod_set_value_cansleep(gpio_poweroff->reset_gpio, 1);
43
44 /* give it some time */
45 mdelay(gpio_poweroff->timeout_ms);
46
47 /*
48 * If code reaches this point, it means that gpio-poweroff has failed
49 * to actually power off the system.
50 * Warn the user that the attempt to poweroff via gpio-poweroff
51 * has gone wrong.
52 */
53 WARN(1, "Failed to poweroff via gpio-poweroff mechanism\n");
54
55 return NOTIFY_DONE;
56 }
57
gpio_poweroff_probe(struct platform_device * pdev)58 static int gpio_poweroff_probe(struct platform_device *pdev)
59 {
60 struct gpio_poweroff *gpio_poweroff;
61 bool input = false;
62 enum gpiod_flags flags;
63 int priority = SYS_OFF_PRIO_DEFAULT;
64 int ret;
65
66 gpio_poweroff = devm_kzalloc(&pdev->dev, sizeof(*gpio_poweroff), GFP_KERNEL);
67 if (!gpio_poweroff)
68 return -ENOMEM;
69
70 input = device_property_read_bool(&pdev->dev, "input");
71 if (input)
72 flags = GPIOD_IN;
73 else
74 flags = GPIOD_OUT_LOW;
75
76
77 gpio_poweroff->active_delay_ms = 100;
78 gpio_poweroff->inactive_delay_ms = 100;
79 gpio_poweroff->timeout_ms = DEFAULT_TIMEOUT_MS;
80
81 device_property_read_u32(&pdev->dev, "active-delay-ms", &gpio_poweroff->active_delay_ms);
82 device_property_read_u32(&pdev->dev, "inactive-delay-ms",
83 &gpio_poweroff->inactive_delay_ms);
84 device_property_read_u32(&pdev->dev, "timeout-ms", &gpio_poweroff->timeout_ms);
85 device_property_read_u32(&pdev->dev, "priority", &priority);
86 if (priority > 255) {
87 dev_err(&pdev->dev, "Invalid priority property: %u\n", priority);
88 return -EINVAL;
89 }
90
91 gpio_poweroff->reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags);
92 if (IS_ERR(gpio_poweroff->reset_gpio))
93 return PTR_ERR(gpio_poweroff->reset_gpio);
94
95 ret = devm_register_sys_off_handler(&pdev->dev, SYS_OFF_MODE_POWER_OFF,
96 priority, gpio_poweroff_do_poweroff, gpio_poweroff);
97 if (ret)
98 return dev_err_probe(&pdev->dev, ret, "Cannot register poweroff handler\n");
99
100 return 0;
101 }
102
103 static const struct of_device_id of_gpio_poweroff_match[] = {
104 { .compatible = "gpio-poweroff", },
105 {},
106 };
107 MODULE_DEVICE_TABLE(of, of_gpio_poweroff_match);
108
109 static struct platform_driver gpio_poweroff_driver = {
110 .probe = gpio_poweroff_probe,
111 .driver = {
112 .name = "poweroff-gpio",
113 .of_match_table = of_gpio_poweroff_match,
114 },
115 };
116
117 module_platform_driver(gpio_poweroff_driver);
118
119 MODULE_AUTHOR("Jamie Lentin <[email protected]>");
120 MODULE_DESCRIPTION("GPIO poweroff driver");
121 MODULE_ALIAS("platform:poweroff-gpio");
122