summaryrefslogtreecommitdiff
path: root/components/drivers/power/reset/gpio-restart.c
blob: 02eff0a7f42fd3a6885bf950b302fafcb287b4e1 (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
93
94
95
96
97
98
99
100
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-25     GuEe-GUI     the first version
 */

#include <rtthread.h>
#include <rtdevice.h>

#define DBG_TAG "reset.gpio.restart"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

struct gpio_restart
{
    rt_ubase_t pin;
    rt_uint8_t active_value;

    rt_uint32_t wait_delay_ms;
    rt_uint32_t active_delay_ms;
    rt_uint32_t inactive_delay_ms;
};

static rt_err_t gpio_restart_do_restart(struct rt_device *dev)
{
    struct gpio_restart *gr = dev->user_data;

    rt_pin_mode(gr->pin, PIN_MODE_OUTPUT);
    rt_thread_mdelay(gr->active_delay_ms);

    rt_pin_write(gr->pin, !gr->active_value);
    rt_thread_mdelay(gr->inactive_delay_ms);
    rt_pin_write(gr->pin, gr->active_value);

    rt_thread_mdelay(gr->wait_delay_ms);

    return RT_EOK;
}

static rt_err_t gpio_restart_probe(struct rt_platform_device *pdev)
{
    rt_err_t err;
    struct rt_device *dev = &pdev->parent;
    struct gpio_restart *gr = rt_calloc(1, sizeof(*gr));

    if (!gr)
    {
        return -RT_ENOMEM;
    }

    gr->pin = rt_pin_get_named_pin(dev, RT_NULL, 0, RT_NULL, &gr->active_value);

    if (gr->pin < 0)
    {
        err = gr->pin;
        goto _fail;
    }

    gr->active_delay_ms = 100;
    gr->inactive_delay_ms = 100;
    gr->wait_delay_ms = 3000;

    rt_dm_dev_prop_read_u32(dev, "active-delay", &gr->active_delay_ms);
    rt_dm_dev_prop_read_u32(dev, "inactive-delay", &gr->inactive_delay_ms);
    rt_dm_dev_prop_read_u32(dev, "wait-delay", &gr->wait_delay_ms);

    dev->user_data = gr;

    if ((err = rt_dm_power_off_handler(dev, RT_DM_POWER_OFF_MODE_RESET,
            RT_DM_POWER_OFF_PRIO_DEFAULT, gpio_restart_do_restart)))
    {
        goto _fail;
    }

    return RT_EOK;

_fail:
    rt_free(gr);

    return err;
}

static const struct rt_ofw_node_id gpio_restart_ofw_ids[] =
{
    { .compatible = "gpio-restart" },
    { /* sentinel */ }
};

static struct rt_platform_driver gpio_restart_driver =
{
    .name = "reset-gpio-restart",
    .ids = gpio_restart_ofw_ids,

    .probe = gpio_restart_probe,
};
RT_PLATFORM_DRIVER_EXPORT(gpio_restart_driver);