aboutsummaryrefslogtreecommitdiff
path: root/drivers/sysreset/poweroff_gpio.c
blob: a5c24fd85bca9734261bc5a065dc22ad52ba2df8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// SPDX-License-Identifier: GPL-2.0+
/*
 * Toggles a GPIO pin to power down a device
 *
 * Created using the Linux driver as reference, which
 * has been written by:
 *
 * Jamie Lentin <jm@lentin.co.uk>
 * Andrew Lunn <andrew@lunn.ch>
 *
 * Copyright (C) 2012 Jamie Lentin
 */

#include <common.h>
#include <dm.h>
#include <errno.h>
#include <log.h>
#include <sysreset.h>

#include <asm/gpio.h>
#include <linux/delay.h>

struct poweroff_gpio_info {
	struct gpio_desc gpio;
	u32 active_delay_ms;
	u32 inactive_delay_ms;
	u32 timeout_ms;
};

static int poweroff_gpio_request(struct udevice *dev, enum sysreset_t type)
{
	struct poweroff_gpio_info *priv = dev_get_priv(dev);
	int r;

	if (type != SYSRESET_POWER_OFF)
		return -ENOSYS;

	debug("GPIO poweroff\n");

	/* drive it active, also inactive->active edge */
	r = dm_gpio_set_value(&priv->gpio, 1);
	if (r < 0)
		return r;
	mdelay(priv->active_delay_ms);

	/* drive inactive, also active->inactive edge */
	r = dm_gpio_set_value(&priv->gpio, 0);
	if (r < 0)
		return r;
	mdelay(priv->inactive_delay_ms);

	/* drive it active, also inactive->active edge */
	r = dm_gpio_set_value(&priv->gpio, 1);
	if (r < 0)
		return r;

	/* give it some time */
	mdelay(priv->timeout_ms);

	return -EINPROGRESS;
}

static int poweroff_gpio_probe(struct udevice *dev)
{
	struct poweroff_gpio_info *priv = dev_get_priv(dev);
	int flags;

	flags = dev_read_bool(dev, "input") ? GPIOD_IS_IN : GPIOD_IS_OUT;
	priv->active_delay_ms = dev_read_u32_default(dev, "active-delay-ms", 100);
	priv->inactive_delay_ms = dev_read_u32_default(dev, "inactive-delay-ms", 100);
	priv->timeout_ms = dev_read_u32_default(dev, "timeout-ms", 3000);

	return gpio_request_by_name(dev, "gpios", 0, &priv->gpio, flags);
}

static struct sysreset_ops poweroff_gpio_ops = {
	.request = poweroff_gpio_request,
};

static const struct udevice_id poweroff_gpio_ids[] = {
	{ .compatible = "gpio-poweroff", },
	{},
};

U_BOOT_DRIVER(poweroff_gpio) = {
	.name		= "poweroff-gpio",
	.id		= UCLASS_SYSRESET,
	.ops		= &poweroff_gpio_ops,
	.probe		= poweroff_gpio_probe,
	.priv_auto	= sizeof(struct poweroff_gpio_info),
	.of_match	= poweroff_gpio_ids,
};