Skip to content

Commit

Permalink
drivers: watchdog: npm1300: Added watchdog driver
Browse files Browse the repository at this point in the history
Added watchdog driver for nPM1300

Signed-off-by: Andy Sinclair <[email protected]>
  • Loading branch information
aasinclair authored and carlescufi committed Jul 12, 2023
1 parent 4048348 commit 910d438
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/watchdog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions drivers/watchdog/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 19 additions & 0 deletions drivers/watchdog/Kconfig.npm1300
Original file line number Diff line number Diff line change
@@ -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.
168 changes: 168 additions & 0 deletions drivers/watchdog/wdt_npm1300.c
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions dts/bindings/watchdog/nordic,npm1300-wdt.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 910d438

Please sign in to comment.