Skip to content

Commit c0cf880

Browse files
committed
[dm][power] add new drivers for power framework
1. GPIO poweroff/restart 2. Generic SYSCON regmap poweroff/reboot mode/reboot 3. Emulator battery(thermal)/charger 4. GPIO charger Signed-off-by: GuEe-GUI <[email protected]>
1 parent 559d614 commit c0cf880

File tree

15 files changed

+1464
-0
lines changed

15 files changed

+1464
-0
lines changed

components/drivers/power/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rsource "reset/Kconfig"
2+
rsource "supply/Kconfig"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from building import *
2+
3+
cwd = GetCurrentDir()
4+
objs = []
5+
list = os.listdir(cwd)
6+
7+
for d in list:
8+
path = os.path.join(cwd, d)
9+
if os.path.isfile(os.path.join(path, 'SConscript')):
10+
objs = objs + SConscript(os.path.join(d, 'SConscript'))
11+
Return('objs')

components/drivers/power/reset/Kconfig

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,38 @@ menuconfig RT_USING_POWER_RESET
22
bool "Using Board level reset or poweroff"
33
depends on RT_USING_DM
44

5+
config RT_POWER_RESET_GPIO_POWEROFF
6+
bool "GPIO poweroff"
7+
depends on RT_USING_POWER_RESET
8+
depends on RT_USING_PIN
9+
depends on RT_USING_PINCTRL
10+
11+
config RT_POWER_RESET_GPIO_RESTART
12+
bool "GPIO restart"
13+
depends on RT_USING_POWER_RESET
14+
depends on RT_USING_PIN
15+
depends on RT_USING_PINCTRL
16+
17+
config RT_POWER_RESET_SYSCON_POWEROFF
18+
bool "Generic SYSCON regmap poweroff driver"
19+
depends on RT_USING_POWER_RESET
20+
depends on RT_MFD_SYSCON
21+
22+
config RT_POWER_RESET_SYSCON_REBOOT_MODE
23+
bool "Generic SYSCON regmap reboot mode driver"
24+
depends on RT_USING_POWER_RESET
25+
depends on RT_MFD_SYSCON
26+
select RT_POWER_RESET_REBOOT_MODE
27+
28+
config RT_POWER_RESET_SYSCON_REBOOT
29+
bool "Generic SYSCON regmap reboot driver"
30+
depends on RT_USING_POWER_RESET
31+
depends on RT_MFD_SYSCON
32+
533
if RT_USING_POWER_RESET
634
osource "$(SOC_DM_POWER_RESET_DIR)/Kconfig"
735
endif
36+
37+
config RT_POWER_RESET_REBOOT_MODE
38+
bool
39+
depends on RT_USING_OFW

components/drivers/power/reset/SConscript

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ CPPPATH = [cwd + '/../../include']
1010

1111
src = []
1212

13+
if GetDepend(['RT_POWER_RESET_GPIO_POWEROFF']):
14+
src += ['gpio-poweroff.c']
15+
16+
if GetDepend(['RT_POWER_RESET_GPIO_RESTART']):
17+
src += ['gpio-restart.c']
18+
19+
if GetDepend(['RT_POWER_RESET_REBOOT_MODE']):
20+
src += ['reboot-mode.c']
21+
22+
if GetDepend(['RT_POWER_RESET_SYSCON_POWEROFF']):
23+
src += ['syscon-poweroff.c']
24+
25+
if GetDepend(['RT_POWER_RESET_SYSCON_REBOOT_MODE']):
26+
src += ['syscon-reboot-mode.c']
27+
28+
if GetDepend(['RT_POWER_RESET_SYSCON_REBOOT']):
29+
src += ['syscon-reboot.c']
30+
1331
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
1432

1533
Return('group')
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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);
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.restart"
15+
#define DBG_LVL DBG_INFO
16+
#include <rtdbg.h>
17+
18+
struct gpio_restart
19+
{
20+
rt_ubase_t pin;
21+
rt_uint32_t pin_flags;
22+
23+
rt_uint32_t wait_delay_ms;
24+
rt_uint32_t active_delay_ms;
25+
rt_uint32_t inactive_delay_ms;
26+
};
27+
28+
static rt_err_t gpio_restart_do_restart(struct rt_device *dev)
29+
{
30+
struct gpio_restart *gr = 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->wait_delay_ms);
40+
41+
return RT_EOK;
42+
}
43+
44+
static rt_err_t gpio_restart_probe(struct rt_platform_device *pdev)
45+
{
46+
rt_err_t err;
47+
struct rt_device *dev = &pdev->parent;
48+
struct gpio_restart *gr = rt_calloc(1, sizeof(*gr));
49+
50+
if (!gr)
51+
{
52+
return -RT_ENOMEM;
53+
}
54+
55+
gr->pin = rt_pin_get_named_pin(dev, RT_NULL, 0, RT_NULL, &gr->active_value);
56+
57+
if (gr->pin < 0)
58+
{
59+
err = gr->pin;
60+
goto _fail;
61+
}
62+
63+
gr->active_delay_ms = 100;
64+
gr->inactive_delay_ms = 100;
65+
gr->timeout_ms = 3000;
66+
67+
rt_dm_dev_prop_read_u32(dev, "active-delay", &gr->active_delay_ms);
68+
rt_dm_dev_prop_read_u32(dev, "inactive-delay", &gr->inactive_delay_ms);
69+
rt_dm_dev_prop_read_u32(dev, "wait-delay", &gr->timeout_ms);
70+
71+
dev->user_data = gr;
72+
73+
if ((err = rt_dm_power_off_handler(dev, RT_DM_POWER_OFF_MODE_RESET,
74+
RT_DM_POWER_OFF_PRIO_DEFAULT, gpio_restart_do_restart)))
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_restart_ofw_ids[] =
88+
{
89+
{ .compatible = "gpio-restart" },
90+
{ /* sentinel */ }
91+
};
92+
93+
static struct rt_platform_driver gpio_restart_driver =
94+
{
95+
.name = "reset-gpio-restart",
96+
.ids = gpio_restart_ofw_ids,
97+
98+
.probe = gpio_restart_probe,
99+
};
100+
RT_PLATFORM_DRIVER_EXPORT(gpio_restart_driver);
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 "reboot-mode.h"
12+
13+
#define MODE_SUFFIXE "mode-"
14+
15+
struct mode_info
16+
{
17+
rt_slist_t list;
18+
19+
const char *mode;
20+
rt_uint32_t magic;
21+
};
22+
23+
static rt_err_t reboot_mode_work(struct rt_device *dev, char *cmd)
24+
{
25+
struct mode_info *info;
26+
struct reboot_mode *reboot = (void *)dev;
27+
28+
cmd = cmd ? : "normal";
29+
30+
rt_slist_for_each_entry(info, &reboot->mode_nodes, list)
31+
{
32+
if (!rt_strcmp(info->mode, cmd))
33+
{
34+
reboot->write(reboot, info->magic);
35+
break;
36+
}
37+
}
38+
39+
return RT_EOK;
40+
}
41+
42+
rt_err_t reboot_mode_register(struct reboot_mode *reboot)
43+
{
44+
rt_err_t err;
45+
struct mode_info *info;
46+
struct rt_ofw_prop *prop;
47+
struct rt_ofw_node *np = reboot->dev->ofw_node;
48+
const int mode_suffixe_len = sizeof(MODE_SUFFIXE) - 1;
49+
50+
if (!reboot || !reboot->dev)
51+
{
52+
return -RT_EINVAL;
53+
}
54+
55+
rt_slist_init(&reboot->mode_nodes);
56+
57+
rt_ofw_foreach_prop(np, prop)
58+
{
59+
if (!rt_strncmp(prop->name, MODE_SUFFIXE, mode_suffixe_len))
60+
{
61+
continue;
62+
}
63+
64+
info = rt_malloc(sizeof(*info));
65+
66+
if (!info)
67+
{
68+
err = -RT_ENOMEM;
69+
70+
goto _end;
71+
}
72+
73+
info->mode = prop->value + mode_suffixe_len;
74+
info->magic = fdt32_to_cpu(*(const fdt32_t *)prop->value);
75+
76+
rt_slist_init(&info->list);
77+
78+
rt_slist_insert(&reboot->mode_nodes, &info->list);
79+
}
80+
81+
err = rt_dm_reboot_mode_register((void *)reboot, &reboot_mode_work);
82+
83+
_end:
84+
if (err)
85+
{
86+
struct mode_info *prev_info = RT_NULL;
87+
88+
rt_slist_for_each_entry(info, &reboot->mode_nodes, list)
89+
{
90+
if (prev_info)
91+
{
92+
rt_free(prev_info);
93+
}
94+
95+
prev_info = info;
96+
}
97+
98+
if (prev_info)
99+
{
100+
rt_free(prev_info);
101+
}
102+
}
103+
104+
return RT_EOK;
105+
}

0 commit comments

Comments
 (0)