This is an automated email from the ASF dual-hosted git repository.

casaroli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit f1d22d58ab19af650d8549df54c0b59233fe138c
Author: Alan Carvalho de Assis <[email protected]>
AuthorDate: Wed Nov 11 19:12:10 2026 -0300

    drivers/power/supply: add the nPZero G1S power-saving IC driver
    
    The nPZero G1S (NPZG1SQ32) from Nanopower Semiconductor switches the
    host MCU off and polls up to four I2C or SPI peripherals by itself,
    waking the host up on a peripheral, ADC or time-out event.
    
    Add a character driver controlled with ioctl() commands to read the ID,
    the status (wake-up reason), the last peripheral and ADC values, to send
    the configuration and the SRAM commands, to enter Idle state, to reset
    it and to access its registers.  A new ioctl base, _NPZEROIOCBASE, is
    added to include/nuttx/fs/ioctl.h.
    
    The driver follows the datasheet NPZ-G1S-DS-V1.0 and applies the
    workarounds of the errata NPZ-G1S-ERRATA-V3.0:
    * #2: the SRAM is written one byte at a time in the workaround order,
      and NPZEROIOC_WRITEREG refuses the SRAM addresses.
    * #11: the IREF trim values are corrected at registration, after a
      reset of the nPZero.
    The peripheral power switch mode 0, which can lock up the polling, is
    refused.
    
    Tested on the nPZero G1S DevKit (STM32L053C8 host, npzero-g1s-dk:nsh):
    the ID and status are read, and the example configures the nPZero,
    enters Idle state and the host is woken up again.
    
    Signed-off-by: Alan C. Assis <[email protected]>
    Assisted-by: Claude:claude-opus-5-5
---
 drivers/power/supply/CMakeLists.txt |   4 +
 drivers/power/supply/Kconfig        |  22 +
 drivers/power/supply/Make.defs      |   4 +
 drivers/power/supply/npzero.c       | 922 ++++++++++++++++++++++++++++++++++++
 include/nuttx/fs/ioctl.h            |   8 +
 include/nuttx/power/npzero.h        | 470 ++++++++++++++++++
 6 files changed, 1430 insertions(+)

diff --git a/drivers/power/supply/CMakeLists.txt 
b/drivers/power/supply/CMakeLists.txt
index 3ff2b5f97d2..2a5c1034065 100644
--- a/drivers/power/supply/CMakeLists.txt
+++ b/drivers/power/supply/CMakeLists.txt
@@ -38,6 +38,10 @@ if(CONFIG_SMPS_LM51772)
   list(APPEND SRCS lm51772.c)
 endif()
 
+if(CONFIG_NPZERO)
+  list(APPEND SRCS npzero.c)
+endif()
+
 if(CONFIG_REGULATOR)
   list(APPEND SRCS regulator.c)
 
diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
index 4d2edaf3663..6a351e69e17 100644
--- a/drivers/power/supply/Kconfig
+++ b/drivers/power/supply/Kconfig
@@ -58,6 +58,28 @@ config SMPS_LM51772
 
 endif
 
+config NPZERO
+       bool "Nanopower nPZero G1S power-saving IC"
+       default n
+       depends on I2C
+       ---help---
+               Character driver for the Nanopower nPZero G1S power-saving IC,
+               controlled over I2C.  The nPZero switches the host off, polls up
+               to four I2C or SPI peripherals by itself and switches the host 
on
+               again when a wake-up condition happens.
+
+if NPZERO
+
+config NPZERO_I2C_FREQUENCY
+       int "nPZero I2C frequency"
+       default 100000
+       range 1 100000
+       ---help---
+               I2C frequency of the nPZero.  It supports the standard mode
+               (100 kHz) only.
+
+endif # NPZERO
+
 config REGULATOR
        bool "Regulator core driver support"
        default n
diff --git a/drivers/power/supply/Make.defs b/drivers/power/supply/Make.defs
index e618d5f05c2..63b46c61e4e 100644
--- a/drivers/power/supply/Make.defs
+++ b/drivers/power/supply/Make.defs
@@ -32,6 +32,10 @@ ifeq ($(CONFIG_SMPS_LM51772),y)
 CSRCS += lm51772.c
 endif
 
+ifeq ($(CONFIG_NPZERO),y)
+CSRCS += npzero.c
+endif
+
 ifeq ($(CONFIG_REGULATOR),y)
 CSRCS += regulator.c
 
diff --git a/drivers/power/supply/npzero.c b/drivers/power/supply/npzero.c
new file mode 100644
index 00000000000..4c01218dd31
--- /dev/null
+++ b/drivers/power/supply/npzero.c
@@ -0,0 +1,922 @@
+/****************************************************************************
+ * drivers/power/supply/npzero.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* Character driver for the Nanopower nPZero G1S power-saving IC.
+ *
+ * Reference: NPZ-G1S-DS-V1.0 (datasheet) and NPZ-G1S-ERRATA-V3.0.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <stdint.h>
+#include <string.h>
+
+#include <nuttx/debug.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mutex.h>
+#include <nuttx/power/npzero.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* The nPZero supports the I2C standard mode only */
+
+#ifndef CONFIG_NPZERO_I2C_FREQUENCY
+#  define CONFIG_NPZERO_I2C_FREQUENCY 100000
+#endif
+
+/* Registers used by the workaround of the errata #11 (IREF trim) */
+
+#define NPZERO_TRIM_UNLOCK       0x6f
+#define NPZERO_TRIM_UNLOCK_KEY   0x60
+#define NPZERO_TRIM_REV          0x6a
+#define NPZERO_TRIM_REV_G1S      0x18
+#define NPZERO_TRIM_IREF         0x65
+#define NPZERO_TRIM_IREF_LOW     0x18
+#define NPZERO_TRIM_IREF_HIGH    0x19
+#define NPZERO_TRIM_IREF_MIN     0x1a
+#define NPZERO_TRIM_CTRL         0x67
+#define NPZERO_TRIM_LOT          0x76
+#define NPZERO_TRIM_LOT_MIN      0x69f54f00
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct npzero_dev_s
+{
+  FAR struct i2c_master_s *i2c; /* I2C bus */
+  struct i2c_config_s config;   /* I2C configuration */
+  mutex_t lock;                 /* Exclusive access */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int npzero_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct file_operations g_npzero_fops =
+{
+  NULL,            /* open */
+  NULL,            /* close */
+  NULL,            /* read */
+  NULL,            /* write */
+  NULL,            /* seek */
+  npzero_ioctl,    /* ioctl */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: npzero_read
+ ****************************************************************************/
+
+static int npzero_read(FAR struct npzero_dev_s *priv, uint8_t reg,
+                       FAR uint8_t *buf, size_t len)
+{
+  int ret;
+
+  ret = i2c_writeread(priv->i2c, &priv->config, &reg, 1, buf, len);
+  if (ret < 0)
+    {
+      pwrerr("ERROR: read of 0x%02x failed: %d\n", reg, ret);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: npzero_write
+ ****************************************************************************/
+
+static int npzero_write(FAR struct npzero_dev_s *priv, uint8_t reg,
+                        uint8_t val)
+{
+  uint8_t buf[2];
+  int ret;
+
+  buf[0] = reg;
+  buf[1] = val;
+
+  ret = i2c_write(priv->i2c, &priv->config, buf, 2);
+  if (ret < 0)
+    {
+      pwrerr("ERROR: write of 0x%02x failed: %d\n", reg, ret);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: npzero_write16
+ *
+ * Description:
+ *   Write a 16-bit value to a pair of low and high byte registers.
+ *
+ ****************************************************************************/
+
+static int npzero_write16(FAR struct npzero_dev_s *priv, uint8_t reg,
+                          uint16_t val)
+{
+  int ret;
+
+  ret = npzero_write(priv, reg, val & 0xff);
+  if (ret >= 0)
+    {
+      ret = npzero_write(priv, reg + 1, val >> 8);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: npzero_errata11
+ *
+ * Description:
+ *   Workaround of the errata #11: incorrect IREF trim values can reset the
+ *   nPZero in Idle state.  It must be done after each reset of the nPZero.
+ *
+ ****************************************************************************/
+
+static int npzero_errata11(FAR struct npzero_dev_s *priv)
+{
+  uint8_t lot[4];
+  uint8_t iref;
+  uint8_t rev;
+  int ret;
+
+  ret = npzero_write(priv, NPZERO_TRIM_UNLOCK, NPZERO_TRIM_UNLOCK_KEY);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = npzero_read(priv, NPZERO_TRIM_REV, &rev, 1);
+  if (ret >= 0 && rev == NPZERO_TRIM_REV_G1S)
+    {
+      ret = npzero_read(priv, NPZERO_TRIM_IREF, &iref, 1);
+      if (ret >= 0)
+        {
+          ret = npzero_read(priv, NPZERO_TRIM_LOT, lot, sizeof(lot));
+        }
+
+      if (ret >= 0)
+        {
+          uint32_t check = (uint32_t)lot[3] << 24 | (uint32_t)lot[2] << 16 |
+                           (uint32_t)lot[1] << 8 | lot[0];
+
+          if (check < NPZERO_TRIM_LOT_MIN || iref < NPZERO_TRIM_IREF_MIN)
+            {
+              iref = NPZERO_TRIM_IREF_LOW;
+            }
+          else
+            {
+              iref = NPZERO_TRIM_IREF_HIGH;
+            }
+
+          ret = npzero_write(priv, NPZERO_TRIM_IREF, iref);
+        }
+
+      if (ret >= 0)
+        {
+          ret = npzero_write(priv, NPZERO_TRIM_CTRL, 0);
+        }
+    }
+
+  /* Always lock the trim registers again */
+
+  if (npzero_write(priv, NPZERO_TRIM_UNLOCK, 0) < 0 && ret >= 0)
+    {
+      ret = -EIO;
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: npzero_check_periph
+ *
+ * Description:
+ *   Check the configuration of a peripheral.
+ *
+ ****************************************************************************/
+
+static int npzero_check_periph(FAR const struct npzero_periph_s *periph)
+{
+  if (periph->pwmod == NPZERO_PWMOD_DISABLED)
+    {
+      return OK;
+    }
+
+  /* The PSWMOD 0 (voltage detection) can lock up the polling */
+
+  if ((periph->pwmod != NPZERO_PWMOD_PERIODIC &&
+       periph->pwmod != NPZERO_PWMOD_ALWAYSON) ||
+      periph->tmod > NPZERO_TMOD_ASYNCINT ||
+      periph->pswmod < NPZERO_PSWMOD_SWITCH ||
+      periph->pswmod > NPZERO_PSWMOD_LOGICINV ||
+      periph->intmod > NPZERO_INTMOD_OUT_LOW ||
+      periph->dtype > NPZERO_DTYPE_UINT8 ||
+      periph->spimode > 3 || periph->retries > 3 ||
+      periph->addr > NPZERO_ADDRP_ADDR_MASK ||
+      periph->nread > NPZERO_ADDRP_ADDR_MASK ||
+      periph->ninit > 127)
+    {
+      return -EINVAL;
+    }
+
+  if (periph->tinit == 2 || periph->tinit > NPZERO_WAIT_4096 ||
+      periph->twait == 2 || periph->twait > NPZERO_WAIT_4096)
+    {
+      return -EINVAL;
+    }
+
+  /* Zero and one are not valid polling periods */
+
+  if (periph->pwmod == NPZERO_PWMOD_PERIODIC && periph->period < 2)
+    {
+      return -EINVAL;
+    }
+
+  if ((periph->ninit > 0 && periph->init == NULL) ||
+      (periph->spi && periph->nread > 0 && periph->read == NULL))
+    {
+      return -EINVAL;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: npzero_check
+ *
+ * Description:
+ *   Check the configuration of the nPZero and build the content of its
+ *   SRAM.  The initialization commands of the peripherals are stored in
+ *   order and without gaps, with the read commands of the SPI peripherals
+ *   after their initialization commands.
+ *
+ ****************************************************************************/
+
+static int npzero_check(FAR const struct npzero_config_s *config,
+                        FAR uint8_t *sram, FAR size_t *sramlen)
+{
+  FAR const struct npzero_periph_s *periph;
+  size_t len = 0;
+  size_t ninit;
+  int ret;
+  int i;
+
+  if ((config->hostmode != NPZERO_HOSTMODE_SWITCH &&
+       config->hostmode != NPZERO_HOSTMODE_LOGIC &&
+       config->hostmode != NPZERO_HOSTMODE_LOGICINV) ||
+      config->sclksel > NPZERO_SCLK_XO ||
+      config->sclkdiv > NPZERO_SCLKDIV_16 ||
+      config->adcclk > NPZERO_ADCCLK_1024HZ ||
+      config->i2cpull == 2 || config->i2cpull > NPZERO_I2CPULL_AUTO ||
+      config->clkout > NPZERO_CLKOUT_1KHZ ||
+      config->pswint > 0x0f || config->timeout == 0 ||
+      (config->wakeup & ~(NPZERO_SYSCFG1_WUP_P(1) | NPZERO_SYSCFG1_WUP_P(2) |
+                          NPZERO_SYSCFG1_WUP_P(3) | NPZERO_SYSCFG1_WUP_P(4) |
+                          NPZERO_SYSCFG1_WUP_A1 |
+                          NPZERO_SYSCFG1_WUP_A2)) != 0 ||
+      config->adcover[0] > 0x1f || config->adcunder[0] > 0x1f ||
+      config->adcover[1] > 0x3f || config->adcunder[1] > 0x3f)
+    {
+      return -EINVAL;
+    }
+
+  /* The ADC clocks derived from the XO need the XO system clock */
+
+  if (config->adcclk != NPZERO_ADCCLK_SCLK &&
+      config->sclksel != NPZERO_SCLK_XO)
+    {
+      return -EINVAL;
+    }
+
+  for (i = 0; i < NPZERO_NPERIPH; i++)
+    {
+      if (config->intpull[i] == 2 ||
+          config->intpull[i] > NPZERO_INTPULL_50K)
+        {
+          return -EINVAL;
+        }
+
+      periph = config->periph[i];
+      if (periph == NULL || periph->pwmod == NPZERO_PWMOD_DISABLED)
+        {
+          continue;
+        }
+
+      ret = npzero_check_periph(periph);
+      if (ret < 0)
+        {
+          return ret;
+        }
+
+      /* I2C commands are pairs of register address and value */
+
+      ninit = periph->spi ? periph->ninit : 2 * periph->ninit;
+      if (len + ninit > NPZERO_SRAM_SIZE)
+        {
+          return -E2BIG;
+        }
+
+      if (ninit > 0)
+        {
+          memcpy(&sram[len], periph->init, ninit);
+          len += ninit;
+        }
+
+      if (periph->spi && periph->nread > 0)
+        {
+          if (len + periph->nread > NPZERO_SRAM_SIZE)
+            {
+              return -E2BIG;
+            }
+
+          memcpy(&sram[len], periph->read, periph->nread);
+          len += periph->nread;
+        }
+    }
+
+  *sramlen = len;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: npzero_write_sram
+ *
+ * Description:
+ *   Write the whole SRAM.  Sequential writes can corrupt the SRAM (errata
+ *   #2), so the bytes are written one at a time in the order of the
+ *   workaround.
+ *
+ ****************************************************************************/
+
+static int npzero_write_sram(FAR struct npzero_dev_s *priv,
+                             FAR const uint8_t *sram)
+{
+  unsigned int i;
+  unsigned int k;
+  int ret;
+
+  for (i = 0; i < NPZERO_SRAM_SIZE; i++)
+    {
+      k = ((~i & 0x07) << 3) + ((i & 0x38) >> 3) + (i & 0x40);
+
+      ret = npzero_write(priv, NPZERO_SRAM + k, sram[k]);
+      if (ret < 0)
+        {
+          return ret;
+        }
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: npzero_config_periph
+ ****************************************************************************/
+
+static int npzero_config_periph(FAR struct npzero_dev_s *priv, int n,
+                                FAR const struct npzero_periph_s *periph)
+{
+  uint8_t regval;
+  int ret;
+
+  if (periph == NULL || periph->pwmod == NPZERO_PWMOD_DISABLED)
+    {
+      return npzero_write(priv, NPZERO_CFGP(n), 0);
+    }
+
+  regval = periph->pwmod << NPZERO_CFGP_PWMOD_SHIFT |
+           periph->tmod << NPZERO_CFGP_TMOD_SHIFT |
+           periph->pswmod << NPZERO_CFGP_PSWMOD_SHIFT |
+           periph->intmod << NPZERO_CFGP_INTMOD_SHIFT;
+
+  ret = npzero_write(priv, NPZERO_CFGP(n), regval);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  regval = periph->dtype << NPZERO_MODP_DTYPE_SHIFT;
+  if (periph->inverted)
+    {
+      regval |= NPZERO_MODP_CMOD;
+    }
+
+  if (periph->seqrw)
+    {
+      regval |= NPZERO_MODP_SEQRW;
+    }
+
+  if (periph->swap)
+    {
+      regval |= NPZERO_MODP_SWPRREG;
+    }
+
+  if (periph->spi)
+    {
+      regval |= periph->spimode << NPZERO_MODP_SPIMOD_SHIFT;
+    }
+  else if (periph->wunak)
+    {
+      regval |= NPZERO_MODP_WUNAK;
+    }
+
+  ret = npzero_write(priv, NPZERO_MODP(n), regval);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = npzero_write16(priv, NPZERO_PERP_L(n), periph->period);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = npzero_write(priv, NPZERO_NCMDP(n), periph->ninit);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* ADDR_P is the I2C address, or the number of SPI read bytes */
+
+  if (periph->spi)
+    {
+      regval = NPZERO_ADDRP_SPIEN | periph->nread;
+    }
+  else
+    {
+      regval = periph->addr;
+    }
+
+  ret = npzero_write(priv, NPZERO_ADDRP(n), regval);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = npzero_write(priv, NPZERO_RREGP(n), periph->spi ? 0 : periph->rreg);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = npzero_write16(priv, NPZERO_THROVP_L(n), periph->throver);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = npzero_write16(priv, NPZERO_THRUNP_L(n), periph->thrunder);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = npzero_write(priv, NPZERO_TWTP(n), periph->twtp);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  regval = periph->twait << NPZERO_TCFGP_TWT_SHIFT |
+           periph->tinit << NPZERO_TCFGP_TINIT_SHIFT;
+  if (!periph->spi)
+    {
+      regval |= periph->retries << NPZERO_TCFGP_I2CRET_SHIFT;
+    }
+
+  return npzero_write(priv, NPZERO_TCFGP(n), regval);
+}
+
+/****************************************************************************
+ * Name: npzero_configure
+ ****************************************************************************/
+
+static int npzero_configure(FAR struct npzero_dev_s *priv,
+                            FAR const struct npzero_config_s *config)
+{
+  uint8_t sram[NPZERO_SRAM_SIZE];
+  size_t sramlen;
+  uint8_t regval;
+  int ret;
+  int i;
+
+  if (config == NULL)
+    {
+      return -EINVAL;
+    }
+
+  memset(sram, 0, sizeof(sram));
+  ret = npzero_check(config, sram, &sramlen);
+  if (ret < 0)
+    {
+      pwrerr("ERROR: invalid configuration: %d\n", ret);
+      return ret;
+    }
+
+  /* Global settings */
+
+  ret = npzero_write16(priv, NPZERO_TOUT_L, config->timeout);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  regval = config->wakeup;
+  if (config->wakeupall)
+    {
+      regval |= NPZERO_SYSCFG1_WUPMOD;
+    }
+
+  ret = npzero_write(priv, NPZERO_SYSCFG1, regval);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  regval = config->adcclk << NPZERO_SYSCFG2_ADC_CLK_SHIFT;
+  if (config->sclkdiv != NPZERO_SCLKDIV_1)
+    {
+      regval |= NPZERO_SYSCFG2_SCLK_DIV_EN |
+                (config->sclkdiv - 1) << NPZERO_SYSCFG2_SCLK_DIV_SHIFT;
+    }
+
+  if (config->sclksel == NPZERO_SCLK_XO)
+    {
+      regval |= NPZERO_SYSCFG2_SCLK_SEL;
+    }
+
+  if (config->adcext)
+    {
+      regval |= NPZERO_SYSCFG2_ADC_EXT_ON;
+    }
+
+  ret = npzero_write(priv, NPZERO_SYSCFG2, regval);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  regval = config->i2cpull << NPZERO_SYSCFG3_I2C_PUP_SHIFT |
+           config->clkout << NPZERO_SYSCFG3_CLKOUT_SHIFT;
+  if (config->iostrength)
+    {
+      regval |= NPZERO_SYSCFG3_IO_STR;
+    }
+
+  if (config->spiauto)
+    {
+      regval |= NPZERO_SYSCFG3_SPI_AUTO;
+    }
+
+  ret = npzero_write(priv, NPZERO_SYSCFG3, regval);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  regval = config->pswint | config->hostmode << NPZERO_PSWCTL_PSWH_SHIFT;
+  if (config->gateboost)
+    {
+      regval |= NPZERO_PSWCTL_PSW_EN_VN;
+    }
+
+  ret = npzero_write(priv, NPZERO_PSWCTL, regval);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  regval = 0;
+  for (i = 0; i < NPZERO_NPERIPH; i++)
+    {
+      regval |= config->intpull[i] << (2 * i);
+    }
+
+  ret = npzero_write(priv, NPZERO_INTCFG, regval);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Initialization commands of the peripherals */
+
+  if (sramlen > 0)
+    {
+      ret = npzero_write_sram(priv, sram);
+      if (ret < 0)
+        {
+          return ret;
+        }
+    }
+
+  /* Peripherals */
+
+  for (i = 0; i < NPZERO_NPERIPH; i++)
+    {
+      ret = npzero_config_periph(priv, i + 1, config->periph[i]);
+      if (ret < 0)
+        {
+          return ret;
+        }
+    }
+
+  /* ADC thresholds */
+
+  ret = npzero_write(priv, NPZERO_THROVA1, config->adcover[0]);
+  if (ret >= 0)
+    {
+      ret = npzero_write(priv, NPZERO_THRUNA1, config->adcunder[0]);
+    }
+
+  if (ret >= 0)
+    {
+      ret = npzero_write(priv, NPZERO_THROVA2, config->adcover[1]);
+    }
+
+  if (ret >= 0)
+    {
+      ret = npzero_write(priv, NPZERO_THRUNA2, config->adcunder[1]);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: npzero_ioctl
+ ****************************************************************************/
+
+static int npzero_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
+{
+  FAR struct npzero_dev_s *priv = filep->f_inode->i_private;
+  uint8_t buf[2];
+  int ret;
+
+  ret = nxmutex_lock(&priv->lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  switch (cmd)
+    {
+      case NPZEROIOC_GETID:
+        {
+          FAR uint8_t *id = (FAR uint8_t *)((uintptr_t)arg);
+
+          DEBUGASSERT(id != NULL);
+          ret = npzero_read(priv, NPZERO_ID_REG, id, 1);
+        }
+        break;
+
+      case NPZEROIOC_GETSTATUS:
+        {
+          FAR struct npzero_status_s *status =
+            (FAR struct npzero_status_s *)((uintptr_t)arg);
+
+          DEBUGASSERT(status != NULL);
+          ret = npzero_read(priv, NPZERO_STA1, buf, 2);
+          if (ret >= 0)
+            {
+              status->sta1 = buf[0];
+              status->sta2 = buf[1];
+            }
+        }
+        break;
+
+      case NPZEROIOC_CONFIGURE:
+        ret = npzero_configure(priv,
+                 (FAR const struct npzero_config_s *)((uintptr_t)arg));
+        break;
+
+      case NPZEROIOC_GETVALUE:
+        {
+          FAR struct npzero_value_s *value =
+            (FAR struct npzero_value_s *)((uintptr_t)arg);
+
+          DEBUGASSERT(value != NULL);
+          if (value->periph < 1 || value->periph > NPZERO_NPERIPH)
+            {
+              ret = -EINVAL;
+              break;
+            }
+
+          ret = npzero_read(priv, NPZERO_VALP_L(value->periph), buf, 2);
+          if (ret >= 0)
+            {
+              value->value = (uint16_t)buf[1] << 8 | buf[0];
+            }
+        }
+        break;
+
+      case NPZEROIOC_GETADC:
+        {
+          FAR struct npzero_adc_s *adc =
+            (FAR struct npzero_adc_s *)((uintptr_t)arg);
+
+          DEBUGASSERT(adc != NULL);
+          ret = npzero_read(priv, NPZERO_ADC_CORE, buf, 2);
+          if (ret >= 0)
+            {
+              adc->core = buf[0];
+              adc->ext  = buf[1];
+            }
+        }
+        break;
+
+      case NPZEROIOC_IDLE:
+        ret = npzero_write(priv, NPZERO_IDLE_RST, NPZERO_IDLE_RST_IDLE);
+        break;
+
+      case NPZEROIOC_RESET:
+        ret = npzero_write(priv, NPZERO_IDLE_RST, NPZERO_IDLE_RST_RESET);
+        break;
+
+      case NPZEROIOC_READREG:
+        {
+          FAR struct npzero_reg_s *reg =
+            (FAR struct npzero_reg_s *)((uintptr_t)arg);
+
+          DEBUGASSERT(reg != NULL);
+          ret = npzero_read(priv, reg->reg, &reg->val, 1);
+        }
+        break;
+
+      case NPZEROIOC_WRITEREG:
+        {
+          FAR const struct npzero_reg_s *reg =
+            (FAR const struct npzero_reg_s *)((uintptr_t)arg);
+
+          DEBUGASSERT(reg != NULL);
+
+          /* Single SRAM writes can corrupt the SRAM (errata #2) */
+
+          if (reg->reg >= NPZERO_SRAM)
+            {
+              ret = -EACCES;
+              break;
+            }
+
+          ret = npzero_write(priv, reg->reg, reg->val);
+        }
+        break;
+
+      default:
+        ret = -ENOTTY;
+        break;
+    }
+
+  nxmutex_unlock(&priv->lock);
+  return ret < 0 ? ret : OK;
+}
+
+/****************************************************************************
+ * Name: npzero_probe
+ *
+ * Description:
+ *   Check the ID of the nPZero and apply the workaround of the errata #11
+ *   if the nPZero was reset.
+ *
+ ****************************************************************************/
+
+static int npzero_probe(FAR struct npzero_dev_s *priv)
+{
+  uint8_t buf[3];
+  int ret;
+
+  /* Read ID, STA1 and STA2 */
+
+  ret = npzero_read(priv, NPZERO_ID_REG, buf, 3);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  if (buf[0] != NPZERO_ID)
+    {
+      pwrerr("ERROR: unexpected ID 0x%02x\n", buf[0]);
+      return -ENODEV;
+    }
+
+  /* No wake-up flag: the nPZero was reset and its trim values need the
+   * workaround of the errata #11.
+   */
+
+  if (buf[2] == 0 && (buf[1] & (NPZERO_STA1_FTOUT | NPZERO_STA1_FA1 |
+                                NPZERO_STA1_FA2)) == 0)
+    {
+      ret = npzero_errata11(priv);
+      if (ret < 0)
+        {
+          pwrerr("ERROR: errata #11 workaround failed: %d\n", ret);
+          return ret;
+        }
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: npzero_register
+ *
+ * Description:
+ *   Check the nPZero G1S on the I2C bus, apply the errata workarounds that
+ *   are needed after each reset of the nPZero and register the character
+ *   driver.
+ *
+ * Input Parameters:
+ *   devpath - The path of the device, e.g. "/dev/npzero0"
+ *   i2c     - The I2C bus of the nPZero
+ *   addr    - The 7-bit I2C address, NPZERO_I2C_ADDR
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int npzero_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
+                    uint8_t addr)
+{
+  FAR struct npzero_dev_s *priv;
+  int ret;
+
+  DEBUGASSERT(devpath != NULL && i2c != NULL);
+
+  priv = kmm_zalloc(sizeof(struct npzero_dev_s));
+  if (priv == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  priv->i2c              = i2c;
+  priv->config.frequency = CONFIG_NPZERO_I2C_FREQUENCY;
+  priv->config.address   = addr;
+  priv->config.addrlen   = 7;
+  nxmutex_init(&priv->lock);
+
+  ret = npzero_probe(priv);
+  if (ret < 0)
+    {
+      goto errout;
+    }
+
+  ret = register_driver(devpath, &g_npzero_fops, 0666, priv);
+  if (ret < 0)
+    {
+      pwrerr("ERROR: register_driver failed: %d\n", ret);
+      goto errout;
+    }
+
+  return OK;
+
+errout:
+  nxmutex_destroy(&priv->lock);
+  kmm_free(priv);
+  return ret;
+}
diff --git a/include/nuttx/fs/ioctl.h b/include/nuttx/fs/ioctl.h
index 60bda32540d..45159734701 100644
--- a/include/nuttx/fs/ioctl.h
+++ b/include/nuttx/fs/ioctl.h
@@ -115,6 +115,7 @@
 #define _PTPBASE        (0x4700) /* PTP ioctl commands */
 #define _DSHOTIOCBASE   (0x4800) /* Dshot device ioctl commands */
 #define _PULSECOUNTBASE (0x4900) /* Pulse count driver ioctl commands */
+#define _NPZEROIOCBASE  (0x4a00) /* nPZero power-saving IC ioctl commands */
 #define _RPTUNBASE      (0x5000) /* Remote processor tunneling ioctl commands 
*/
 #define _VIRTIOBASE     (0x5100) /* VirtIO ioctl commands */
 #define _WLIOCBASE      (0x8b00) /* Wireless modules ioctl network commands */
@@ -861,6 +862,13 @@
 #define _PULSECOUNTIOCVALID(c) (_IOC_TYPE(c)==_PULSECOUNTBASE)
 #define _PULSECOUNTIOC(nr)     _IOC(_PULSECOUNTBASE,nr)
 
+/* nPZero power-saving IC driver ioctl definitions **************************/
+
+/* see include/nuttx/power/npzero.h */
+
+#define _NPZEROIOCVALID(c)     (_IOC_TYPE(c)==_NPZEROIOCBASE)
+#define _NPZEROIOC(nr)         _IOC(_NPZEROIOCBASE,nr)
+
 /* Rptun drivers ************************************************************/
 
 #define _RPTUNIOCVALID(c)     (_IOC_TYPE(c)==_RPTUNBASE)
diff --git a/include/nuttx/power/npzero.h b/include/nuttx/power/npzero.h
new file mode 100644
index 00000000000..4d4c4c1057c
--- /dev/null
+++ b/include/nuttx/power/npzero.h
@@ -0,0 +1,470 @@
+/****************************************************************************
+ * include/nuttx/power/npzero.h
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_POWER_NPZERO_H
+#define __INCLUDE_NUTTX_POWER_NPZERO_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include <nuttx/fs/ioctl.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* The Nanopower nPZero G1S is a power-saving IC.  While the host is
+ * powered (Standby state) it is an I2C slave of the host.  When the host
+ * sends the Idle command, it switches the host off and polls up to four
+ * I2C or SPI peripherals by itself, and it switches the host on again when
+ * a wake-up condition happens.
+ */
+
+#define NPZERO_I2C_ADDR          0x3d   /* 7-bit I2C address */
+#define NPZERO_ID                0x60   /* Value of the ID register */
+#define NPZERO_NPERIPH           4      /* Number of peripherals */
+#define NPZERO_SRAM_SIZE         128    /* Bytes of SRAM */
+
+/* Registers ****************************************************************/
+
+#define NPZERO_IDLE_RST          0x00   /* Idle/reset command */
+#define NPZERO_ID_REG            0x01   /* Device ID */
+#define NPZERO_STA1              0x02   /* Status 1 */
+#define NPZERO_STA2              0x03   /* Status 2 */
+#define NPZERO_PSWCTL            0x04   /* Power switch control */
+#define NPZERO_SYSCFG1           0x05   /* System configuration 1 */
+#define NPZERO_SYSCFG2           0x06   /* System configuration 2 */
+#define NPZERO_SYSCFG3           0x07   /* System configuration 3 */
+#define NPZERO_TOUT_L            0x08   /* Global time-out, low byte */
+#define NPZERO_TOUT_H            0x09   /* Global time-out, high byte */
+#define NPZERO_INTCFG            0x0a   /* Interrupt pins configuration */
+
+/* Peripheral registers, n = 1..4 */
+
+#define NPZERO_PERIPH_BASE(n)    (0x10 + 13 * ((n) - 1))
+#define NPZERO_CFGP(n)           (NPZERO_PERIPH_BASE(n) + 0)
+#define NPZERO_MODP(n)           (NPZERO_PERIPH_BASE(n) + 1)
+#define NPZERO_PERP_L(n)         (NPZERO_PERIPH_BASE(n) + 2)
+#define NPZERO_PERP_H(n)         (NPZERO_PERIPH_BASE(n) + 3)
+#define NPZERO_NCMDP(n)          (NPZERO_PERIPH_BASE(n) + 4)
+#define NPZERO_ADDRP(n)          (NPZERO_PERIPH_BASE(n) + 5)
+#define NPZERO_RREGP(n)          (NPZERO_PERIPH_BASE(n) + 6)
+#define NPZERO_THROVP_L(n)       (NPZERO_PERIPH_BASE(n) + 7)
+#define NPZERO_THROVP_H(n)       (NPZERO_PERIPH_BASE(n) + 8)
+#define NPZERO_THRUNP_L(n)       (NPZERO_PERIPH_BASE(n) + 9)
+#define NPZERO_THRUNP_H(n)       (NPZERO_PERIPH_BASE(n) + 10)
+#define NPZERO_TWTP(n)           (NPZERO_PERIPH_BASE(n) + 11)
+#define NPZERO_TCFGP(n)          (NPZERO_PERIPH_BASE(n) + 12)
+
+#define NPZERO_THROVA1           0x44   /* VBAT ADC over-threshold */
+#define NPZERO_THRUNA1           0x45   /* VBAT ADC under-threshold */
+#define NPZERO_THROVA2           0x46   /* ADC_IN ADC over-threshold */
+#define NPZERO_THRUNA2           0x47   /* ADC_IN ADC under-threshold */
+#define NPZERO_VALP_L(n)         (0x50 + 2 * ((n) - 1))
+#define NPZERO_VALP_H(n)         (0x51 + 2 * ((n) - 1))
+#define NPZERO_ADC_CORE          0x58   /* Last VBAT ADC value */
+#define NPZERO_ADC_EXT           0x59   /* Last ADC_IN ADC value */
+#define NPZERO_SRAM              0x80   /* SRAM, 0x80-0xff */
+
+/* Register bit fields ******************************************************/
+
+/* IDLE_RST */
+
+#define NPZERO_IDLE_RST_IDLE     0xff   /* Enter Idle state */
+#define NPZERO_IDLE_RST_RESET    0xa5   /* Soft reset */
+
+/* STA1 */
+
+#define NPZERO_STA1_RSTSRC_MASK  0x0f       /* Reset source (see errata #5) */
+#define NPZERO_STA1_RSTSRC_POR   0x01       /*   Power-on reset */
+#define NPZERO_STA1_RSTSRC_EXT   0x02       /*   External reset (RSTN pin) */
+#define NPZERO_STA1_RSTSRC_BOR   0x04       /*   Brown-out reset */
+#define NPZERO_STA1_RSTSRC_SOFT  0x08       /*   Soft reset */
+#define NPZERO_STA1_FA2          (1 << 5)   /* ADC_IN channel trigger */
+#define NPZERO_STA1_FA1          (1 << 6)   /* VBAT channel trigger */
+#define NPZERO_STA1_FTOUT        (1 << 7)   /* Global time-out */
+
+/* STA2, n = 1..4 */
+
+#define NPZERO_STA2_FP(n)        (1 << (2 * ((n) - 1)))     /* Trigger */
+#define NPZERO_STA2_FNAKP(n)     (1 << (2 * ((n) - 1) + 1)) /* NAK */
+
+/* PSWCTL */
+
+#define NPZERO_PSWCTL_PSWINT(n)  (1 << ((n) - 1))
+#define NPZERO_PSWCTL_PSWH_SHIFT 4
+#define NPZERO_PSWCTL_PSWH_MASK  (3 << NPZERO_PSWCTL_PSWH_SHIFT)
+#define NPZERO_PSWCTL_PSW_EN_VN  (1 << 6)
+#define NPZERO_PSWCTL_PSW_VN_ON  (1 << 7)
+
+/* SYSCFG1 */
+
+#define NPZERO_SYSCFG1_WUP_P(n)  (1 << ((n) - 1))
+#define NPZERO_SYSCFG1_WUP_A1    (1 << 4)
+#define NPZERO_SYSCFG1_WUP_A2    (1 << 5)
+#define NPZERO_SYSCFG1_WUPMOD    (1 << 6)
+
+/* SYSCFG2 */
+
+#define NPZERO_SYSCFG2_SCLK_DIV_EN      (1 << 0)
+#define NPZERO_SYSCFG2_SCLK_DIV_SHIFT   1
+#define NPZERO_SYSCFG2_SCLK_SEL         (1 << 3)
+#define NPZERO_SYSCFG2_ADC_EXT_ON       (1 << 4)
+#define NPZERO_SYSCFG2_ADC_CLK_SHIFT    5
+
+/* SYSCFG3 */
+
+#define NPZERO_SYSCFG3_IO_STR           (1 << 0)
+#define NPZERO_SYSCFG3_I2C_PUP_SHIFT    1
+#define NPZERO_SYSCFG3_SPI_AUTO         (1 << 3)
+#define NPZERO_SYSCFG3_CLKOUT_SHIFT     4
+#define NPZERO_SYSCFG3_SCLK_SEL_STATUS  (1 << 7)
+
+/* CFGP */
+
+#define NPZERO_CFGP_PWMOD_SHIFT  0
+#define NPZERO_CFGP_TMOD_SHIFT   2
+#define NPZERO_CFGP_PSWMOD_SHIFT 4
+#define NPZERO_CFGP_INTMOD_SHIFT 6
+
+/* MODP */
+
+#define NPZERO_MODP_CMOD         (1 << 0)
+#define NPZERO_MODP_DTYPE_SHIFT  1
+#define NPZERO_MODP_SEQRW        (1 << 3)
+#define NPZERO_MODP_WUNAK        (1 << 4)
+#define NPZERO_MODP_SWPRREG      (1 << 5)
+#define NPZERO_MODP_SPIMOD_SHIFT 6
+
+/* ADDRP */
+
+#define NPZERO_ADDRP_ADDR_MASK   0x7f
+#define NPZERO_ADDRP_SPIEN       (1 << 7)
+
+/* TCFGP */
+
+#define NPZERO_TCFGP_TWT_SHIFT   0      /* TWT_EN and TWT_EXT */
+#define NPZERO_TCFGP_TINIT_SHIFT 2      /* TINIT_EN and TINIT_EXT */
+#define NPZERO_TCFGP_I2CRET_SHIFT 4
+
+/* Values of the configuration fields ***************************************/
+
+/* Host power switch mode (npzero_config_s::hostmode) */
+
+#define NPZERO_HOSTMODE_SWITCH   0      /* SW_HP is a power switch */
+#define NPZERO_HOSTMODE_LOGIC    2      /* Logic output, high when on */
+#define NPZERO_HOSTMODE_LOGICINV 3      /* Logic output, low when on */
+
+/* System clock source (npzero_config_s::sclksel) */
+
+#define NPZERO_SCLK_LPOSC        0      /* 10 Hz low-power oscillator */
+#define NPZERO_SCLK_XO           1      /* 32.768 kHz XO / 2048 (16 Hz) */
+
+/* System clock divider (npzero_config_s::sclkdiv) */
+
+#define NPZERO_SCLKDIV_1         0
+#define NPZERO_SCLKDIV_2         1
+#define NPZERO_SCLKDIV_4         2
+#define NPZERO_SCLKDIV_8         3
+#define NPZERO_SCLKDIV_16        4
+
+/* ADC sampling clock (npzero_config_s::adcclk).  The XO clocks need
+ * sclksel = NPZERO_SCLK_XO.
+ */
+
+#define NPZERO_ADCCLK_SCLK       0      /* System clock */
+#define NPZERO_ADCCLK_64HZ       1      /* XO / 512 */
+#define NPZERO_ADCCLK_256HZ      2      /* XO / 128 */
+#define NPZERO_ADCCLK_1024HZ     3      /* XO / 32 */
+
+/* I2C pull-ups of the nPZero (npzero_config_s::i2cpull) */
+
+#define NPZERO_I2CPULL_OFF       0      /* Disabled */
+#define NPZERO_I2CPULL_ON        1      /* Always enabled */
+#define NPZERO_I2CPULL_AUTO      3      /* Disabled in Idle state */
+
+/* CLK_OUT pin (npzero_config_s::clkout) */
+
+#define NPZERO_CLKOUT_OFF        0
+#define NPZERO_CLKOUT_32KHZ      1
+#define NPZERO_CLKOUT_16KHZ      2
+#define NPZERO_CLKOUT_8KHZ       3
+#define NPZERO_CLKOUT_4KHZ       4
+#define NPZERO_CLKOUT_2KHZ       5
+#define NPZERO_CLKOUT_1KHZ       6
+
+/* Pull-up of the INT1-4 pins (npzero_config_s::intpull) */
+
+#define NPZERO_INTPULL_OFF       0      /* No pull-up */
+#define NPZERO_INTPULL_100K      1      /* About 100 kOhm */
+#define NPZERO_INTPULL_50K       3      /* About 50 kOhm */
+
+/* Peripheral power mode (npzero_periph_s::pwmod) */
+
+#define NPZERO_PWMOD_DISABLED    0
+#define NPZERO_PWMOD_PERIODIC    1      /* Powered every polling period */
+#define NPZERO_PWMOD_ALWAYSON    3
+
+/* Peripheral polling mode (npzero_periph_s::tmod) */
+
+#define NPZERO_TMOD_READ         0      /* Init, read and compare */
+#define NPZERO_TMOD_INTREAD      1      /* Init, wait INT, read, compare */
+#define NPZERO_TMOD_INT          2      /* Init and wait for INT */
+#define NPZERO_TMOD_ASYNCINT     3      /* Wait for INT */
+
+/* Peripheral power switch mode, SW_LP pin (npzero_periph_s::pswmod).  The
+ * mode 0 (power switch with voltage rise detection) can lock up the
+ * polling and it is refused by the driver.
+ */
+
+#define NPZERO_PSWMOD_SWITCH     1      /* Power switch */
+#define NPZERO_PSWMOD_LOGIC      2      /* Logic output, high when on */
+#define NPZERO_PSWMOD_LOGICINV   3      /* Logic output, low when on */
+
+/* Peripheral INT pin mode (npzero_periph_s::intmod) */
+
+#define NPZERO_INTMOD_IN_HIGH    0      /* Interrupt input, active high */
+#define NPZERO_INTMOD_IN_LOW     1      /* Interrupt input, active low */
+#define NPZERO_INTMOD_OUT_HIGH   2      /* Trigger output, active high */
+#define NPZERO_INTMOD_OUT_LOW    3      /* Trigger output, active low */
+
+/* Peripheral data type (npzero_periph_s::dtype) */
+
+#define NPZERO_DTYPE_UINT16      0
+#define NPZERO_DTYPE_INT16       1
+#define NPZERO_DTYPE_UINT8       2
+
+/* Wait times (npzero_periph_s::tinit and twait) in periods of the internal
+ * 400 kHz oscillator, multiplied by npzero_periph_s::twtp.
+ */
+
+#define NPZERO_WAIT_NONE         0
+#define NPZERO_WAIT_256          1      /* twtp x 256 periods (640 us) */
+#define NPZERO_WAIT_4096         3      /* twtp x 4096 periods (10.24 ms) */
+
+/* IOCTL commands ***********************************************************/
+
+/* Command:      NPZEROIOC_GETID
+ * Description:  Read the ID register (NPZERO_ID)
+ * Argument:     FAR uint8_t *
+ *
+ * Command:      NPZEROIOC_GETSTATUS
+ * Description:  Read the STA1 and STA2 registers, the reset source and
+ *               the wake-up reason
+ * Argument:     FAR struct npzero_status_s *
+ *
+ * Command:      NPZEROIOC_CONFIGURE
+ * Description:  Write the whole configuration and the SRAM.  It must be
+ *               sent in Standby state, before NPZEROIOC_IDLE.
+ * Argument:     FAR const struct npzero_config_s *
+ *
+ * Command:      NPZEROIOC_GETVALUE
+ * Description:  Read the last value read from a peripheral (VALP)
+ * Argument:     FAR struct npzero_value_s *
+ *
+ * Command:      NPZEROIOC_GETADC
+ * Description:  Read the last ADC values (ADC_CORE and ADC_EXT)
+ * Argument:     FAR struct npzero_adc_s *
+ *
+ * Command:      NPZEROIOC_IDLE
+ * Description:  Enter Idle state.  The nPZero switches the host off
+ *               according to the host power switch mode.
+ * Argument:     None
+ *
+ * Command:      NPZEROIOC_RESET
+ * Description:  Soft reset of the nPZero
+ * Argument:     None
+ *
+ * Command:      NPZEROIOC_READREG
+ * Description:  Read a register
+ * Argument:     FAR struct npzero_reg_s *
+ *
+ * Command:      NPZEROIOC_WRITEREG
+ * Description:  Write a register.  The SRAM can only be written with
+ *               NPZEROIOC_CONFIGURE (errata #2).
+ * Argument:     FAR const struct npzero_reg_s *
+ */
+
+#define NPZEROIOC_GETID          _NPZEROIOC(1)
+#define NPZEROIOC_GETSTATUS      _NPZEROIOC(2)
+#define NPZEROIOC_CONFIGURE      _NPZEROIOC(3)
+#define NPZEROIOC_GETVALUE       _NPZEROIOC(4)
+#define NPZEROIOC_GETADC         _NPZEROIOC(5)
+#define NPZEROIOC_IDLE           _NPZEROIOC(6)
+#define NPZEROIOC_RESET          _NPZEROIOC(7)
+#define NPZEROIOC_READREG        _NPZEROIOC(8)
+#define NPZEROIOC_WRITEREG       _NPZEROIOC(9)
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/* Configuration of a peripheral */
+
+struct npzero_periph_s
+{
+  uint8_t  pwmod;      /* Power mode, NPZERO_PWMOD_* */
+  uint8_t  tmod;       /* Polling mode, NPZERO_TMOD_* */
+  uint8_t  pswmod;     /* SW_LP pin mode, NPZERO_PSWMOD_* */
+  uint8_t  intmod;     /* INT pin mode, NPZERO_INTMOD_* */
+  bool     spi;        /* true: SPI peripheral, false: I2C peripheral */
+  uint8_t  spimode;    /* SPI mode, 0 to 3 */
+  uint8_t  dtype;      /* Type of the value, NPZERO_DTYPE_* */
+  bool     inverted;   /* Trigger between the thresholds instead of
+                        * above the over- or below the under-threshold */
+  bool     seqrw;      /* Multi-byte transfers for sequential addresses */
+  bool     wunak;      /* Wake up if the I2C peripheral does not ACK */
+  bool     swap;       /* Swap the bytes of the 16-bit value */
+  uint8_t  addr;       /* 7-bit address of the I2C peripheral */
+  uint8_t  rreg;       /* I2C register of the value (low byte) */
+  uint8_t  retries;    /* I2C retries on NAK, 0 to 3 */
+  uint16_t period;     /* Polling period, in system clock periods (>= 2) */
+  uint16_t throver;    /* Over-threshold */
+  uint16_t thrunder;   /* Under-threshold */
+  uint8_t  twtp;       /* Wait time unit count (TWTP) */
+  uint8_t  tinit;      /* Wait before the initialization, NPZERO_WAIT_* */
+  uint8_t  twait;      /* Wait after the initialization (before the
+                        * read or for the interrupt), NPZERO_WAIT_* */
+
+  /* Initialization commands, copied to the SRAM.  I2C: ninit pairs of
+   * register address and value (2 * ninit bytes).  SPI: ninit bytes.
+   */
+
+  uint8_t  ninit;
+  FAR const uint8_t *init;
+
+  /* SPI only: bytes shifted out to read the value */
+
+  uint8_t  nread;
+  FAR const uint8_t *read;
+};
+
+/* Configuration of the nPZero (NPZEROIOC_CONFIGURE) */
+
+struct npzero_config_s
+{
+  uint8_t  hostmode;   /* Host power switch mode, NPZERO_HOSTMODE_* */
+  uint8_t  pswint;     /* Bit n-1 set: SW_LPn is on in Standby state */
+  bool     gateboost;  /* Power switch gate boost (lower Rds(on)) */
+  uint8_t  wakeup;     /* Wake-up sources, NPZERO_SYSCFG1_WUP_* bits */
+  bool     wakeupall;  /* Wake up when all the sources trigger */
+  uint8_t  sclksel;    /* System clock, NPZERO_SCLK_* */
+  uint8_t  sclkdiv;    /* System clock divider, NPZERO_SCLKDIV_* */
+  uint8_t  adcclk;     /* ADC sampling clock, NPZERO_ADCCLK_* */
+  bool     adcext;     /* Sample the ADC_IN pin too */
+  bool     iostrength; /* High strength of the I/O pins */
+  uint8_t  i2cpull;    /* I2C pull-ups, NPZERO_I2CPULL_* */
+  bool     spiauto;    /* SPI outputs in high-Z when not used */
+  uint8_t  clkout;     /* CLK_OUT pin, NPZERO_CLKOUT_* */
+  uint16_t timeout;    /* Global time-out, in system clock periods */
+
+  /* Pull-ups of the INT1-4 pins, NPZERO_INTPULL_* */
+
+  uint8_t  intpull[NPZERO_NPERIPH];
+
+  /* ADC thresholds, [0] is the VBAT channel (5-bit), [1] the ADC_IN
+   * channel (6-bit).
+   */
+
+  uint8_t  adcover[2];
+  uint8_t  adcunder[2];
+
+  /* Peripherals 1 to 4, NULL if disabled */
+
+  FAR const struct npzero_periph_s *periph[NPZERO_NPERIPH];
+};
+
+/* Status (NPZEROIOC_GETSTATUS) */
+
+struct npzero_status_s
+{
+  uint8_t sta1;        /* NPZERO_STA1_* bits */
+  uint8_t sta2;        /* NPZERO_STA2_* bits */
+};
+
+/* Value of a peripheral (NPZEROIOC_GETVALUE) */
+
+struct npzero_value_s
+{
+  uint8_t  periph;     /* Peripheral, 1 to 4 (in) */
+  uint16_t value;      /* Last value read, of type dtype (out) */
+};
+
+/* ADC values (NPZEROIOC_GETADC), see the ADC codes in the datasheet */
+
+struct npzero_adc_s
+{
+  uint8_t core;        /* VBAT channel */
+  uint8_t ext;         /* ADC_IN channel */
+};
+
+/* Register access (NPZEROIOC_READREG and NPZEROIOC_WRITEREG) */
+
+struct npzero_reg_s
+{
+  uint8_t reg;         /* Register address */
+  uint8_t val;         /* Register value */
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/****************************************************************************
+ * Name: npzero_register
+ *
+ * Description:
+ *   Check the nPZero G1S on the I2C bus, apply the errata workarounds that
+ *   are needed after each reset of the nPZero and register the character
+ *   driver.  It should be called at each boot of the host, since the host
+ *   is powered off in Idle state.
+ *
+ * Input Parameters:
+ *   devpath - The path of the device, e.g. "/dev/npzero0"
+ *   i2c     - The I2C bus of the nPZero
+ *   addr    - The 7-bit I2C address, NPZERO_I2C_ADDR
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+struct i2c_master_s;
+int npzero_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
+                    uint8_t addr);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __INCLUDE_NUTTX_POWER_NPZERO_H */

Reply via email to