drivers: watchdog: npm1300: Added watchdog driver

Added watchdog driver for nPM1300

Signed-off-by: Andy Sinclair <andy.sinclair@nordicsemi.no>
This commit is contained in:
Andy Sinclair 2023-07-05 11:38:05 +01:00 committed by Carles Cufí
parent 4048348e3e
commit 910d43805b
5 changed files with 203 additions and 0 deletions

View file

@ -22,6 +22,7 @@ zephyr_library_sources_ifdef(CONFIG_WDT_MCUX_WDOG wdt_mcux_wdog.c)
zephyr_library_sources_ifdef(CONFIG_WDT_MCUX_WDOG32 wdt_mcux_wdog32.c)
zephyr_library_sources_ifdef(CONFIG_WDT_MCUX_WWDT wdt_mcux_wwdt.c)
zephyr_library_sources_ifdef(CONFIG_WDT_NPCX wdt_npcx.c)
zephyr_library_sources_ifdef(CONFIG_WDT_NPM1300 wdt_npm1300.c)
zephyr_library_sources_ifdef(CONFIG_WDT_NPM6001 wdt_npm6001.c)
zephyr_library_sources_ifdef(CONFIG_WDT_NRFX wdt_nrfx.c)
zephyr_library_sources_ifdef(CONFIG_WDT_RPI_PICO wdt_rpi_pico.c)

View file

@ -90,6 +90,8 @@ source "drivers/watchdog/Kconfig.rpi_pico"
source "drivers/watchdog/Kconfig.gd32"
source "drivers/watchdog/Kconfig.npm1300"
source "drivers/watchdog/Kconfig.npm6001"
source "drivers/watchdog/Kconfig.nxp_s32"

View file

@ -0,0 +1,19 @@
# Copyright (c) 2023 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
config WDT_NPM1300
bool "nPM1300 Watchdog driver"
default y
depends on DT_HAS_NORDIC_NPM1300_WDT_ENABLED
select I2C
select MFD
help
Enable nPM1300 Watchdog driver
config WDT_NPM1300_INIT_PRIORITY
int "nPM1300 Watchdog driver initialization priority"
depends on WDT_NPM1300
default 75
help
Initialization priority for the nPM1300 Watchdog driver.
It must be greater than GPIO_NPM1300_INIT_PRIORITY.

View file

@ -0,0 +1,168 @@
/*
* Copyright (c) 2023 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT nordic_npm1300_wdt
#include <errno.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/watchdog.h>
#include <zephyr/drivers/mfd/npm1300.h>
#include <zephyr/dt-bindings/gpio/nordic-npm1300-gpio.h>
/* nPM1300 TIMER base address */
#define TIME_BASE 0x07U
/* nPM1300 timer register offsets */
#define TIME_OFFSET_START 0x00U
#define TIME_OFFSET_STOP 0x01U
#define TIME_OFFSET_WDOG_KICK 0x04U
#define TIME_OFFSET_MODE 0x05U
/* nPM1300 timer modes */
#define TIME_MODE_BOOT 0x00U
#define TIME_MODE_WARN 0x01U
#define TIME_MODE_RESET 0x02U
#define TIME_MODE_GEN 0x03U
struct wdt_npm1300_config {
const struct device *mfd;
struct gpio_dt_spec reset_gpios;
};
struct wdt_npm1300_data {
bool timeout_valid;
};
static int wdt_npm1300_setup(const struct device *dev, uint8_t options)
{
const struct wdt_npm1300_config *config = dev->config;
struct wdt_npm1300_data *data = dev->data;
if (!data->timeout_valid) {
return -EINVAL;
}
return mfd_npm1300_reg_write(config->mfd, TIME_BASE, TIME_OFFSET_START, 1U);
}
static int wdt_npm1300_disable(const struct device *dev)
{
const struct wdt_npm1300_config *config = dev->config;
struct wdt_npm1300_data *data = dev->data;
int ret;
ret = mfd_npm1300_reg_write(config->mfd, TIME_BASE, TIME_OFFSET_STOP, 1U);
if (ret < 0) {
return ret;
}
data->timeout_valid = false;
return 0;
}
static int wdt_npm1300_install_timeout(const struct device *dev,
const struct wdt_timeout_cfg *timeout)
{
const struct wdt_npm1300_config *config = dev->config;
struct wdt_npm1300_data *data = dev->data;
uint8_t mode;
int ret;
if (data->timeout_valid) {
return -ENOMEM;
}
if (timeout->window.min != 0U) {
return -EINVAL;
}
ret = mfd_npm1300_set_timer(config->mfd, timeout->window.max);
if (ret < 0) {
return ret;
}
switch (timeout->flags & WDT_FLAG_RESET_MASK) {
case WDT_FLAG_RESET_NONE:
/* Watchdog expiry causes warn event only, and does not reset */
mode = TIME_MODE_GEN;
break;
case WDT_FLAG_RESET_CPU_CORE:
/* Watchdog expiry causes warn event, then asserts reset output */
mode = TIME_MODE_WARN;
break;
case WDT_FLAG_RESET_SOC:
/* Watchdog expiry causes warn event, then full power cycle */
mode = TIME_MODE_RESET;
break;
default:
return -EINVAL;
}
ret = mfd_npm1300_reg_write(config->mfd, TIME_BASE, TIME_OFFSET_MODE, mode);
if (ret < 0) {
return ret;
}
data->timeout_valid = true;
return 0;
}
static int wdt_npm1300_feed(const struct device *dev, int channel_id)
{
const struct wdt_npm1300_config *config = dev->config;
if (channel_id != 0) {
return -EINVAL;
}
return mfd_npm1300_reg_write(config->mfd, TIME_BASE, TIME_OFFSET_WDOG_KICK, 1U);
}
static const struct wdt_driver_api wdt_npm1300_api = {
.setup = wdt_npm1300_setup,
.disable = wdt_npm1300_disable,
.install_timeout = wdt_npm1300_install_timeout,
.feed = wdt_npm1300_feed,
};
static int wdt_npm1300_init(const struct device *dev)
{
const struct wdt_npm1300_config *config = dev->config;
int ret;
if (!device_is_ready(config->mfd)) {
return -ENODEV;
}
if (config->reset_gpios.port != NULL) {
if (!gpio_is_ready_dt(&config->reset_gpios)) {
return -ENODEV;
}
ret = gpio_pin_configure_dt(&config->reset_gpios, NPM1300_GPIO_WDT_RESET_ON);
if (ret != 0) {
return ret;
}
}
return 0;
}
#define WDT_NPM1300_DEFINE(n) \
static struct wdt_npm1300_data data##n; \
\
static const struct wdt_npm1300_config config##n = { \
.mfd = DEVICE_DT_GET(DT_INST_PARENT(n)), \
.reset_gpios = GPIO_DT_SPEC_INST_GET_OR(n, reset_gpios, {0}), \
}; \
\
DEVICE_DT_INST_DEFINE(n, &wdt_npm1300_init, NULL, &data##n, &config##n, POST_KERNEL, \
CONFIG_WDT_NPM1300_INIT_PRIORITY, &wdt_npm1300_api);
DT_INST_FOREACH_STATUS_OKAY(WDT_NPM1300_DEFINE)

View file

@ -0,0 +1,13 @@
# Copyright (c) 2023 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
description: nPM1300 Watchdog
compatible: "nordic,npm1300-wdt"
include: base.yaml
properties:
reset-gpios:
type: phandle-array
description: nPM1300 pin used as NRESETOUT