|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2023, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2023-02-25 GuEe-GUI the first version |
| 9 | + */ |
| 10 | + |
| 11 | +#include <rtthread.h> |
| 12 | +#include <rtdevice.h> |
| 13 | + |
| 14 | +#define DBG_TAG "reset.gpio.poweroff" |
| 15 | +#define DBG_LVL DBG_INFO |
| 16 | +#include <rtdbg.h> |
| 17 | + |
| 18 | +struct gpio_poweroff |
| 19 | +{ |
| 20 | + rt_ubase_t pin; |
| 21 | + rt_uint32_t active_value; |
| 22 | + |
| 23 | + rt_uint32_t timeout_ms; |
| 24 | + rt_uint32_t active_delay_ms; |
| 25 | + rt_uint32_t inactive_delay_ms; |
| 26 | +}; |
| 27 | + |
| 28 | +static rt_err_t gpio_poweroff_do_poweroff(struct rt_device *dev) |
| 29 | +{ |
| 30 | + struct gpio_poweroff *gp = dev->user_data; |
| 31 | + |
| 32 | + rt_pin_mode(gp->pin, PIN_MODE_OUTPUT); |
| 33 | + rt_thread_mdelay(gp->active_delay_ms); |
| 34 | + |
| 35 | + rt_pin_write(gp->pin, !gp->active_value); |
| 36 | + rt_thread_mdelay(gp->inactive_delay_ms); |
| 37 | + rt_pin_write(gp->pin, gp->active_value); |
| 38 | + |
| 39 | + rt_thread_mdelay(gp->timeout_ms); |
| 40 | + |
| 41 | + return RT_EOK; |
| 42 | +} |
| 43 | + |
| 44 | +static rt_err_t gpio_poweroff_probe(struct rt_platform_device *pdev) |
| 45 | +{ |
| 46 | + rt_err_t err; |
| 47 | + struct rt_device *dev = &pdev->parent; |
| 48 | + struct gpio_poweroff *gp = rt_calloc(1, sizeof(*gp)); |
| 49 | + |
| 50 | + if (!gp) |
| 51 | + { |
| 52 | + return -RT_ENOMEM; |
| 53 | + } |
| 54 | + |
| 55 | + gp->pin = rt_pin_get_named_pin(dev, RT_NULL, 0, RT_NULL, &gp->active_value); |
| 56 | + |
| 57 | + if (gp->pin < 0) |
| 58 | + { |
| 59 | + err = gp->pin; |
| 60 | + goto _fail; |
| 61 | + } |
| 62 | + |
| 63 | + gp->active_delay_ms = 100; |
| 64 | + gp->inactive_delay_ms = 100; |
| 65 | + gp->timeout_ms = 3000; |
| 66 | + |
| 67 | + rt_dm_dev_prop_read_u32(dev, "active-delay-ms", &gp->active_delay_ms); |
| 68 | + rt_dm_dev_prop_read_u32(dev, "inactive-delay-ms", &gp->inactive_delay_ms); |
| 69 | + rt_dm_dev_prop_read_u32(dev, "timeout-ms", &gp->timeout_ms); |
| 70 | + |
| 71 | + dev->user_data = gp; |
| 72 | + |
| 73 | + if ((err = rt_dm_power_off_handler(dev, RT_DM_POWER_OFF_MODE_SHUTDOWN, |
| 74 | + RT_DM_POWER_OFF_PRIO_DEFAULT, gpio_poweroff_do_poweroff))) |
| 75 | + { |
| 76 | + goto _fail; |
| 77 | + } |
| 78 | + |
| 79 | + return RT_EOK; |
| 80 | + |
| 81 | +_fail: |
| 82 | + rt_free(gp); |
| 83 | + |
| 84 | + return err; |
| 85 | +} |
| 86 | + |
| 87 | +static const struct rt_ofw_node_id gpio_poweroff_ofw_ids[] = |
| 88 | +{ |
| 89 | + { .compatible = "gpio-poweroff" }, |
| 90 | + { /* sentinel */ } |
| 91 | +}; |
| 92 | + |
| 93 | +static struct rt_platform_driver gpio_poweroff_driver = |
| 94 | +{ |
| 95 | + .name = "reset-gpio-poweroff", |
| 96 | + .ids = gpio_poweroff_ofw_ids, |
| 97 | + |
| 98 | + .probe = gpio_poweroff_probe, |
| 99 | +}; |
| 100 | +RT_PLATFORM_DRIVER_EXPORT(gpio_poweroff_driver); |
0 commit comments