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

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

commit 32ffeb8a2a59d024e573f344e07fbfb001d9ace5
Author: raiden00pl <[email protected]>
AuthorDate: Sat Jun 13 08:53:28 2026 +0200

    drivers/power: Add the nPM1100 battery charger driver.
    
    Lower half of the battery charger driver for the Nordic nPM1100 PMIC.
    The chip has no register interface, so the charge and error state come
    from its CHG and ERR pins through board callbacks.
    
    Assisted-by: Claude:claude-fable-5-1
    Signed-off-by: raiden00pl <[email protected]>
---
 .../os/drivers/special/power/battery/npm1100.rst   |  29 +++
 Documentation/os/drivers/special/power/index.rst   |   1 +
 drivers/power/battery/CMakeLists.txt               |   6 +
 drivers/power/battery/Kconfig                      |   8 +
 drivers/power/battery/Make.defs                    |   6 +
 drivers/power/battery/npm1100.c                    | 236 +++++++++++++++++++++
 include/nuttx/power/npm1100.h                      |  86 ++++++++
 7 files changed, 372 insertions(+)

diff --git a/Documentation/os/drivers/special/power/battery/npm1100.rst 
b/Documentation/os/drivers/special/power/battery/npm1100.rst
new file mode 100644
index 00000000000..5ed9053fbe8
--- /dev/null
+++ b/Documentation/os/drivers/special/power/battery/npm1100.rst
@@ -0,0 +1,29 @@
+==============================
+Nordic nPM1100 battery charger
+==============================
+
+Nordic nPM1100 PMIC charger status, enabled with ``CONFIG_NPM1100`` (needs
+``CONFIG_BATTERY_CHARGER``).
+
+The chip has no register interface. Charge and error state come from its
+open-drain, active-low ``CHG`` and ``ERR`` pins, which the board reads
+through two callbacks:
+
+.. code-block:: c
+
+   #include <nuttx/power/npm1100.h>
+
+   struct npm1100_config_s
+   {
+     CODE bool (*read_chg)(void);  /* CHG pin level, low = charging */
+     CODE bool (*read_err)(void);  /* ERR pin level, low = error */
+   };
+
+   FAR struct battery_charger_dev_s *
+     npm1100_initialize(FAR const struct npm1100_config_s *config);
+
+The returned instance is registered with ``battery_charger_register()``.
+``BATIOC_STATE`` reports ``BATTERY_CHARGING``, ``BATTERY_IDLE`` or
+``BATTERY_FAULT``; ``BATIOC_HEALTH`` reports ``BATTERY_HEALTH_GOOD`` or
+``BATTERY_HEALTH_UNSPEC_FAIL``. Voltage, current and operate requests are
+not supported.
diff --git a/Documentation/os/drivers/special/power/index.rst 
b/Documentation/os/drivers/special/power/index.rst
index e984d8675c4..5df568b53c5 100644
--- a/Documentation/os/drivers/special/power/index.rst
+++ b/Documentation/os/drivers/special/power/index.rst
@@ -11,6 +11,7 @@ Power-related Drivers
   smps.rst
   lm51772.rst
   battery/ltc4013.rst
+  battery/npm1100.rst
 
 .. toctree::
    :maxdepth: 1
diff --git a/drivers/power/battery/CMakeLists.txt 
b/drivers/power/battery/CMakeLists.txt
index ce8d9a0e50b..f7ce619bb59 100644
--- a/drivers/power/battery/CMakeLists.txt
+++ b/drivers/power/battery/CMakeLists.txt
@@ -42,6 +42,12 @@ if(CONFIG_BATTERY_CHARGER)
     list(APPEND SRCS ltc4013.c)
   endif()
 
+  # Add the Nordic nPM1100 PMIC battery charger driver
+
+  if(CONFIG_NPM1100)
+    list(APPEND SRCS npm1100.c)
+  endif()
+
   # Add the BQ2425x I2C-based battery charger driver
 
   if(CONFIG_I2C_BQ2425X)
diff --git a/drivers/power/battery/Kconfig b/drivers/power/battery/Kconfig
index a77b3c0cad9..586cf48ed7d 100644
--- a/drivers/power/battery/Kconfig
+++ b/drivers/power/battery/Kconfig
@@ -135,6 +135,14 @@ config LTC4013
                callbacks.  The charge voltage is set by a board callback that
                programs the feedback network, or it is fixed by the hardware.
 
+config NPM1100
+       bool "Nordic nPM1100 PMIC battery charger support"
+       default n
+       depends on BATTERY_CHARGER
+       ---help---
+               A simple driver to report Nordic nPM1100 PMIC charge status from
+               its CHG/ERR GPIOs (the nPM1100 has no register interface).
+
 config BATTERY_GAUGE
        bool "Battery Fuel Gauge support"
        default n
diff --git a/drivers/power/battery/Make.defs b/drivers/power/battery/Make.defs
index 7be635c8448..4bd0fd8acdc 100644
--- a/drivers/power/battery/Make.defs
+++ b/drivers/power/battery/Make.defs
@@ -38,6 +38,12 @@ ifeq ($(CONFIG_LTC4013),y)
 CSRCS += ltc4013.c
 endif
 
+# Add the Nordic nPM1100 PMIC battery charger driver
+
+ifeq ($(CONFIG_NPM1100),y)
+CSRCS += npm1100.c
+endif
+
 # Add the BQ2425x I2C-based battery charger driver
 
 ifeq ($(CONFIG_I2C_BQ2425X),y)
diff --git a/drivers/power/battery/npm1100.c b/drivers/power/battery/npm1100.c
new file mode 100644
index 00000000000..7f584b9b80a
--- /dev/null
+++ b/drivers/power/battery/npm1100.c
@@ -0,0 +1,236 @@
+/****************************************************************************
+ * drivers/power/battery/npm1100.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * The nPM1100 is a pin-strapped charger PMIC with no register interface.
+ * It exposes only charge (CHG) and error (ERR) status on two open-drain,
+ * active-low GPIOs; charge current is set by the ISET resistor in hardware.
+ * This driver maps those two pins onto the standard battery_charger
+ * lower-half (state / health / online). Charge-current and voltage controls
+ * are not software-controllable and report -ENOSYS.
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/power/battery_charger.h>
+#include <nuttx/power/battery_ioctl.h>
+#include <nuttx/power/npm1100.h>
+
+#if defined(CONFIG_BATTERY_CHARGER) && defined(CONFIG_NPM1100)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct npm1100_dev_s
+{
+  /* The common part of the battery driver visible to the upper-half driver */
+
+  struct battery_charger_dev_s dev;
+
+  /* nPM1100 board configuration (status GPIO read callbacks) */
+
+  FAR const struct npm1100_config_s *config;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int npm1100_state(FAR struct battery_charger_dev_s *dev,
+                         FAR int *status);
+static int npm1100_health(FAR struct battery_charger_dev_s *dev,
+                          FAR int *health);
+static int npm1100_online(FAR struct battery_charger_dev_s *dev,
+                          FAR bool *status);
+static int npm1100_voltage(FAR struct battery_charger_dev_s *dev, int value);
+static int npm1100_current(FAR struct battery_charger_dev_s *dev, int value);
+static int npm1100_input_current(FAR struct battery_charger_dev_s *dev,
+                                 int value);
+static int npm1100_operate(FAR struct battery_charger_dev_s *dev,
+                           uintptr_t param);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct battery_charger_operations_s g_npm1100ops =
+{
+  npm1100_state,
+  npm1100_health,
+  npm1100_online,
+  npm1100_voltage,
+  npm1100_current,
+  npm1100_input_current,
+  npm1100_operate
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: npm1100_state
+ ****************************************************************************/
+
+static int npm1100_state(FAR struct battery_charger_dev_s *dev,
+                         FAR int *status)
+{
+  FAR struct npm1100_dev_s *priv = (FAR struct npm1100_dev_s *)dev;
+
+  /* CHG and ERR are active-low: a low pin level means the signal is
+   * asserted. The nPM1100 cannot distinguish "charge complete" from "no
+   * input" on these pins, so a de-asserted CHG with no error is reported as
+   * idle.
+   */
+
+  if (!priv->config->read_err())
+    {
+      *status = BATTERY_FAULT;
+    }
+  else if (!priv->config->read_chg())
+    {
+      *status = BATTERY_CHARGING;
+    }
+  else
+    {
+      *status = BATTERY_IDLE;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: npm1100_health
+ ****************************************************************************/
+
+static int npm1100_health(FAR struct battery_charger_dev_s *dev,
+                          FAR int *health)
+{
+  FAR struct npm1100_dev_s *priv = (FAR struct npm1100_dev_s *)dev;
+
+  if (!priv->config->read_err())
+    {
+      *health = BATTERY_HEALTH_UNSPEC_FAIL;
+    }
+  else
+    {
+      *health = BATTERY_HEALTH_GOOD;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: npm1100_online
+ ****************************************************************************/
+
+static int npm1100_online(FAR struct battery_charger_dev_s *dev,
+                          FAR bool *status)
+{
+  *status = true;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: npm1100_voltage
+ *
+ * Description:
+ *   Charge voltage is fixed by the nPM1100 hardware; not controllable.
+ *
+ ****************************************************************************/
+
+static int npm1100_voltage(FAR struct battery_charger_dev_s *dev, int value)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: npm1100_current
+ *
+ * Description:
+ *   Charge current is set by the ISET resistor; not software controllable.
+ *
+ ****************************************************************************/
+
+static int npm1100_current(FAR struct battery_charger_dev_s *dev, int value)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: npm1100_input_current
+ ****************************************************************************/
+
+static int npm1100_input_current(FAR struct battery_charger_dev_s *dev,
+                                 int value)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Name: npm1100_operate
+ ****************************************************************************/
+
+static int npm1100_operate(FAR struct battery_charger_dev_s *dev,
+                           uintptr_t param)
+{
+  return -ENOSYS;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: npm1100_initialize
+ ****************************************************************************/
+
+FAR struct battery_charger_dev_s *
+  npm1100_initialize(FAR const struct npm1100_config_s *config)
+{
+  FAR struct npm1100_dev_s *priv;
+
+  DEBUGASSERT(config != NULL && config->read_chg != NULL &&
+              config->read_err != NULL);
+
+  priv = (FAR struct npm1100_dev_s *)
+    kmm_zalloc(sizeof(struct npm1100_dev_s));
+
+  if (priv)
+    {
+      priv->dev.ops = &g_npm1100ops;
+      priv->config  = config;
+    }
+
+  return (FAR struct battery_charger_dev_s *)priv;
+}
+
+#endif /* CONFIG_BATTERY_CHARGER && CONFIG_NPM1100 */
diff --git a/include/nuttx/power/npm1100.h b/include/nuttx/power/npm1100.h
new file mode 100644
index 00000000000..6548f06f645
--- /dev/null
+++ b/include/nuttx/power/npm1100.h
@@ -0,0 +1,86 @@
+/****************************************************************************
+ * include/nuttx/power/npm1100.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_NPM1100_H
+#define __INCLUDE_NUTTX_POWER_NPM1100_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <stdbool.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/* The nPM1100 has no register interface; its charge/error status is exposed
+ * on two open-drain, active-low GPIOs. The board supplies callbacks that
+ * return the raw pin level (true = high/de-asserted, false = low/asserted);
+ * this driver applies the active-low semantics.
+ */
+
+struct npm1100_config_s
+{
+  CODE bool (*read_chg)(void);  /* CHG status pin level (low = charging) */
+  CODE bool (*read_err)(void);  /* ERR status pin level (low = error)    */
+};
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/****************************************************************************
+ * Name: npm1100_initialize
+ *
+ * Description:
+ *   Initialize the nPM1100 battery charger driver and return an instance of
+ *   the lower-half interface that may be used with
+ *   battery_charger_register().
+ *
+ *   This driver requires:
+ *
+ *   CONFIG_BATTERY_CHARGER - Upper-half battery charger driver support
+ *
+ * Input Parameters:
+ *   config - Board-specific GPIO read callbacks.
+ *
+ * Returned Value:
+ *   A pointer to the initialized lower-half driver instance, or NULL on
+ *   failure.
+ *
+ ****************************************************************************/
+
+FAR struct battery_charger_dev_s *
+  npm1100_initialize(FAR const struct npm1100_config_s *config);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __INCLUDE_NUTTX_POWER_NPM1100_H */

Reply via email to