xref: /OK3568_Linux_fs/kernel/drivers/power/reset/gpio-poweroff.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Toggles a GPIO pin to power down a device
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Jamie Lentin <jm@lentin.co.uk>
6*4882a593Smuzhiyun  * Andrew Lunn <andrew@lunn.ch>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Copyright (C) 2012 Jamie Lentin
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/init.h>
12*4882a593Smuzhiyun #include <linux/delay.h>
13*4882a593Smuzhiyun #include <linux/platform_device.h>
14*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
15*4882a593Smuzhiyun #include <linux/of_platform.h>
16*4882a593Smuzhiyun #include <linux/module.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define DEFAULT_TIMEOUT_MS 3000
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun  * Hold configuration here, cannot be more than one instance of the driver
21*4882a593Smuzhiyun  * since pm_power_off itself is global.
22*4882a593Smuzhiyun  */
23*4882a593Smuzhiyun static struct gpio_desc *reset_gpio;
24*4882a593Smuzhiyun static u32 timeout = DEFAULT_TIMEOUT_MS;
25*4882a593Smuzhiyun static u32 active_delay = 100;
26*4882a593Smuzhiyun static u32 inactive_delay = 100;
27*4882a593Smuzhiyun 
gpio_poweroff_do_poweroff(void)28*4882a593Smuzhiyun static void gpio_poweroff_do_poweroff(void)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	BUG_ON(!reset_gpio);
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	/* drive it active, also inactive->active edge */
33*4882a593Smuzhiyun 	gpiod_direction_output(reset_gpio, 1);
34*4882a593Smuzhiyun 	mdelay(active_delay);
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	/* drive inactive, also active->inactive edge */
37*4882a593Smuzhiyun 	gpiod_set_value_cansleep(reset_gpio, 0);
38*4882a593Smuzhiyun 	mdelay(inactive_delay);
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	/* drive it active, also inactive->active edge */
41*4882a593Smuzhiyun 	gpiod_set_value_cansleep(reset_gpio, 1);
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	/* give it some time */
44*4882a593Smuzhiyun 	mdelay(timeout);
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	WARN_ON(1);
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun 
gpio_poweroff_probe(struct platform_device * pdev)49*4882a593Smuzhiyun static int gpio_poweroff_probe(struct platform_device *pdev)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	bool input = false;
52*4882a593Smuzhiyun 	enum gpiod_flags flags;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	/* If a pm_power_off function has already been added, leave it alone */
55*4882a593Smuzhiyun 	if (pm_power_off != NULL) {
56*4882a593Smuzhiyun 		dev_err(&pdev->dev,
57*4882a593Smuzhiyun 			"%s: pm_power_off function already registered\n",
58*4882a593Smuzhiyun 		       __func__);
59*4882a593Smuzhiyun 		return -EBUSY;
60*4882a593Smuzhiyun 	}
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	input = device_property_read_bool(&pdev->dev, "input");
63*4882a593Smuzhiyun 	if (input)
64*4882a593Smuzhiyun 		flags = GPIOD_IN;
65*4882a593Smuzhiyun 	else
66*4882a593Smuzhiyun 		flags = GPIOD_OUT_LOW;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	device_property_read_u32(&pdev->dev, "active-delay-ms", &active_delay);
69*4882a593Smuzhiyun 	device_property_read_u32(&pdev->dev, "inactive-delay-ms",
70*4882a593Smuzhiyun 				 &inactive_delay);
71*4882a593Smuzhiyun 	device_property_read_u32(&pdev->dev, "timeout-ms", &timeout);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags);
74*4882a593Smuzhiyun 	if (IS_ERR(reset_gpio))
75*4882a593Smuzhiyun 		return PTR_ERR(reset_gpio);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	pm_power_off = &gpio_poweroff_do_poweroff;
78*4882a593Smuzhiyun 	return 0;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
gpio_poweroff_remove(struct platform_device * pdev)81*4882a593Smuzhiyun static int gpio_poweroff_remove(struct platform_device *pdev)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	if (pm_power_off == &gpio_poweroff_do_poweroff)
84*4882a593Smuzhiyun 		pm_power_off = NULL;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	return 0;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun static const struct of_device_id of_gpio_poweroff_match[] = {
90*4882a593Smuzhiyun 	{ .compatible = "gpio-poweroff", },
91*4882a593Smuzhiyun 	{},
92*4882a593Smuzhiyun };
93*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, of_gpio_poweroff_match);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun static struct platform_driver gpio_poweroff_driver = {
96*4882a593Smuzhiyun 	.probe = gpio_poweroff_probe,
97*4882a593Smuzhiyun 	.remove = gpio_poweroff_remove,
98*4882a593Smuzhiyun 	.driver = {
99*4882a593Smuzhiyun 		.name = "poweroff-gpio",
100*4882a593Smuzhiyun 		.of_match_table = of_gpio_poweroff_match,
101*4882a593Smuzhiyun 	},
102*4882a593Smuzhiyun };
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun module_platform_driver(gpio_poweroff_driver);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun MODULE_AUTHOR("Jamie Lentin <jm@lentin.co.uk>");
107*4882a593Smuzhiyun MODULE_DESCRIPTION("GPIO poweroff driver");
108*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
109*4882a593Smuzhiyun MODULE_ALIAS("platform:poweroff-gpio");
110