feat(drivers/pmic): 添加IP5306硬件看门狗功能支持

为IP5306 PMIC驱动添加了硬件看门狗脉冲生成功能,通过RTC2、GPIOTE和PPI外设实现硬
件级的唤醒脉冲生成。当配置keepalive-hardware属性时,系统将使用硬件方式而非软件
定时器来产生看门狗脉冲。

在设备树中新增keepalive-hardware布尔属性,用于启用硬件看门狗功能,并相应更新了
驱动程序以支持软硬件两种看门狗模式的选择。

相关的DTS文件和配置也进行了相应调整,包括添加必要的nRF52硬件抽象层依赖以及
RTC2和GPIOTE相关配置选项。
This commit is contained in:
2026-04-08 13:42:04 +08:00
parent cfcefbf28a
commit e4c824d657
4 changed files with 171 additions and 3 deletions

View File

@@ -7,9 +7,14 @@
#include <zephyr/drivers/i2c.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <soc_nrf_common.h>
#include <zephyr/sys/util.h>
#include <helpers/nrfx_gppi.h>
#include <drivers/pmic/ip5306.h>
#include <gpiote_nrfx.h>
#include <nrfx_gpiote.h>
#include <nrfx_rtc.h>
#define DT_DRV_COMPAT injoinic_ip5306
@@ -19,21 +24,34 @@ LOG_MODULE_REGISTER(ip5306, LOG_LEVEL_INF);
#define IP5306_REG_READ1 0x71
#define IP5306_CHARGING_BIT BIT(3)
#define IP5306_FULL_BIT BIT(3)
#define IP5306_KEEPALIVE_RTC_FREQ_HZ 32768U
#define IP5306_KEEPALIVE_RTC_CHANNEL_END 0U
#define IP5306_KEEPALIVE_RTC_CHANNEL_START 1U
struct ip5306_config {
struct i2c_dt_spec bus;
nrfx_gpiote_t *gpiote;
struct gpio_dt_spec wakeup_gpio;
uint8_t wakeup_pin;
uint32_t keepalive_interval_ms;
uint32_t keepalive_pulse_width_ms;
bool keepalive_hardware;
};
struct ip5306_data {
const struct device *dev;
struct k_work_delayable keepalive_work;
uint8_t gpiote_channel;
nrfx_gppi_handle_t ppi_start;
nrfx_gppi_handle_t ppi_end;
nrfx_gppi_handle_t ppi_clear;
bool started;
bool pulse_active;
bool hardware_ready;
};
static const nrfx_rtc_t keepalive_rtc = NRFX_RTC_INSTANCE(2);
static uint32_t keepalive_low_delay_ms(const struct ip5306_config *config)
{
if (config->keepalive_interval_ms > config->keepalive_pulse_width_ms) {
@@ -44,6 +62,11 @@ static uint32_t keepalive_low_delay_ms(const struct ip5306_config *config)
return 0;
}
static uint32_t keepalive_ms_to_rtc_ticks(uint32_t time_ms)
{
return (uint32_t)(((uint64_t)time_ms * IP5306_KEEPALIVE_RTC_FREQ_HZ) / 1000U);
}
static void keepalive_work_handler(struct k_work *work)
{
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
@@ -79,6 +102,130 @@ static void keepalive_work_handler(struct k_work *work)
K_MSEC(keepalive_low_delay_ms(config)));
}
static void ip5306_keepalive_rtc_handler(nrfx_rtc_int_type_t int_type)
{
ARG_UNUSED(int_type);
}
static int hardware_keepalive_init(const struct device *dev)
{
const struct ip5306_config *config = dev->config;
struct ip5306_data *data = dev->data;
nrfx_gpiote_output_config_t output_config = NRFX_GPIOTE_DEFAULT_OUTPUT_CONFIG;
nrfx_gpiote_task_config_t task_config = {
.task_ch = 0,
.polarity = NRF_GPIOTE_POLARITY_TOGGLE,
.init_val = (config->wakeup_gpio.dt_flags & GPIO_ACTIVE_LOW) ?
NRF_GPIOTE_INITIAL_VALUE_HIGH :
NRF_GPIOTE_INITIAL_VALUE_LOW,
};
nrfx_rtc_config_t rtc_cfg = NRFX_RTC_DEFAULT_CONFIG;
uint32_t period_ticks = keepalive_ms_to_rtc_ticks(config->keepalive_interval_ms);
uint32_t pulse_ticks = keepalive_ms_to_rtc_ticks(config->keepalive_pulse_width_ms);
uint32_t start_ticks = period_ticks - pulse_ticks;
uint32_t eep_start;
uint32_t eep_end;
uint32_t tep_toggle;
uint32_t tep_clear;
int err;
if (data->hardware_ready) {
return 0;
}
if (!nrfx_gpiote_init_check(config->gpiote)) {
LOG_ERR("GPIOTE shared instance is not initialized");
return -ENODEV;
}
err = nrfx_gpiote_channel_alloc(config->gpiote, &data->gpiote_channel);
if (err) {
LOG_ERR("GPIOTE channel alloc failed (%d)", err);
return err;
}
task_config.task_ch = data->gpiote_channel;
err = nrfx_gpiote_output_configure(config->gpiote,
config->wakeup_pin,
&output_config,
&task_config);
if (err) {
LOG_ERR("GPIOTE output configure failed (%d)", err);
return err;
}
nrfx_gpiote_out_task_enable(config->gpiote, config->wakeup_pin);
err = nrfx_rtc_init(&keepalive_rtc, &rtc_cfg, ip5306_keepalive_rtc_handler);
if ((err != 0) && (err != -EALREADY)) {
LOG_ERR("RTC2 init failed (%d)", err);
return err;
}
err = nrfx_rtc_cc_set(&keepalive_rtc,
IP5306_KEEPALIVE_RTC_CHANNEL_END,
period_ticks,
false);
if (err) {
LOG_ERR("RTC2 CC end set failed (%d)", err);
return err;
}
err = nrfx_rtc_cc_set(&keepalive_rtc,
IP5306_KEEPALIVE_RTC_CHANNEL_START,
start_ticks,
false);
if (err) {
LOG_ERR("RTC2 CC start set failed (%d)", err);
return err;
}
eep_start = nrfx_rtc_event_address_get(&keepalive_rtc,
NRF_RTC_EVENT_COMPARE_1);
eep_end = nrfx_rtc_event_address_get(&keepalive_rtc,
NRF_RTC_EVENT_COMPARE_0);
tep_toggle = nrfx_gpiote_out_task_address_get(config->gpiote,
config->wakeup_pin);
tep_clear = nrfx_rtc_task_address_get(&keepalive_rtc, NRF_RTC_TASK_CLEAR);
err = nrfx_gppi_conn_alloc(eep_start, tep_toggle, &data->ppi_start);
if (err) {
LOG_ERR("GPPI start alloc failed (%d)", err);
return err;
}
err = nrfx_gppi_conn_alloc(eep_end, tep_toggle, &data->ppi_end);
if (err) {
LOG_ERR("GPPI end alloc failed (%d)", err);
return err;
}
err = nrfx_gppi_conn_alloc(eep_end, tep_clear, &data->ppi_clear);
if (err) {
LOG_ERR("GPPI clear alloc failed (%d)", err);
return err;
}
nrfx_gppi_conn_enable(data->ppi_start);
nrfx_gppi_conn_enable(data->ppi_end);
nrfx_gppi_conn_enable(data->ppi_clear);
nrfx_rtc_counter_clear(&keepalive_rtc);
nrfx_rtc_enable(&keepalive_rtc);
data->hardware_ready = true;
return 0;
}
static int software_keepalive_init(const struct device *dev)
{
struct ip5306_data *data = dev->data;
k_work_init_delayable(&data->keepalive_work, keepalive_work_handler);
return 0;
}
static int ip5306_init_api(const struct device *dev)
{
const struct ip5306_config *config = dev->config;
@@ -95,9 +242,15 @@ static int ip5306_init_api(const struct device *dev)
LOG_ERR("IP5306 probe failed (%d)", err);
return err;
}
ARG_UNUSED(reg_val);
data->started = true;
data->pulse_active = false;
if (config->keepalive_hardware) {
return 0;
}
k_work_reschedule(&data->keepalive_work,
K_MSEC(keepalive_low_delay_ms(config)));
@@ -165,10 +318,13 @@ static int ip5306_dev_init(const struct device *dev)
data->dev = dev;
data->started = false;
data->pulse_active = false;
data->hardware_ready = false;
k_work_init_delayable(&data->keepalive_work, keepalive_work_handler);
if (config->keepalive_hardware) {
return hardware_keepalive_init(dev);
}
return gpio_pin_configure_dt(&config->wakeup_gpio, GPIO_OUTPUT_INACTIVE);
return software_keepalive_init(dev);
}
static const struct ip5306_driver_api ip5306_driver_api = {
@@ -181,11 +337,16 @@ static const struct ip5306_driver_api ip5306_driver_api = {
\
static const struct ip5306_config ip5306_config_##inst = { \
.bus = I2C_DT_SPEC_INST_GET(inst), \
.gpiote = &GPIOTE_NRFX_INST_BY_NODE( \
NRF_DT_GPIOTE_NODE(DT_DRV_INST(inst), wakeup_gpios)), \
.wakeup_gpio = GPIO_DT_SPEC_INST_GET(inst, wakeup_gpios), \
.wakeup_pin = NRF_DT_GPIOS_TO_PSEL(DT_DRV_INST(inst), wakeup_gpios), \
.keepalive_interval_ms = \
DT_INST_PROP(inst, keepalive_interval_ms), \
.keepalive_pulse_width_ms = \
DT_INST_PROP(inst, keepalive_pulse_width_ms), \
.keepalive_hardware = \
DT_INST_NODE_HAS_PROP(inst, keepalive_hardware), \
}; \
\
DEVICE_DT_INST_DEFINE(inst, ip5306_dev_init, NULL, \