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
The following commit(s) were added to refs/heads/master by this push:
new d6c5cbcca28 drivers/power: Add AXP2101 PMIC battery charger driver.
d6c5cbcca28 is described below
commit d6c5cbcca2833cf236ebbce7df9dec8e3bfd5360
Author: ViezeVingertjes <[email protected]>
AuthorDate: Wed Nov 25 17:58:22 2026 +0100
drivers/power: Add AXP2101 PMIC battery charger driver.
Add a driver for the X-Powers AXP2101 PMIC (I2C, address 0x34), found
on several ESP32-S3 boards. It follows the AXP202 driver and provides
the lower half of the battery charger driver: charge state, health,
VBUS presence, charge voltage, charge current, input current limit,
charge termination and power off. With CONFIG_BATTERY_GAUGE it also
provides a battery gauge lower half: battery voltage, state of charge
from the gauge of the chip and die temperature.
The driver checks the chip ID and enables the VBAT, VBUS, VSYS and die
temperature ADC channels. It does not change the DCDC and LDO outputs,
which keep the values of the OTP, and it does not use the IRQ pin.
CONFIG_AXP2101_VOFF_VOLTAGE optionally sets the power-off voltage, below
which the chip turns the system off to protect the battery from deep
discharge (the OTP value may be as low as 2.6 V).
axp2101_poweroff() turns off the outputs, for board_power_off().
The fault flags used for the health are latched by the chip; they are
cleared after reading, so each fault is reported once.
Tested on Waveshare ESP32-S3-Touch-AMOLED-2.16 with USB power and a full
battery: chip ID, charge state, VBUS, health, battery voltage, state of
charge and die temperature match the raw registers, power off works,
and CONFIG_AXP2101_VOFF_VOLTAGE=3000 sets the power-off voltage register
from 2.6 V (OTP) to 3.0 V. Built with and without CONFIG_BATTERY_GAUGE.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: ViezeVingertjes <[email protected]>
---
.../os/drivers/special/power/battery/axp2101.rst | 106 ++
Documentation/os/drivers/special/power/index.rst | 1 +
drivers/power/battery/CMakeLists.txt | 6 +
drivers/power/battery/Kconfig | 32 +
drivers/power/battery/Make.defs | 6 +
drivers/power/battery/axp2101.c | 1013 ++++++++++++++++++++
include/nuttx/power/axp2101.h | 128 +++
7 files changed, 1292 insertions(+)
diff --git a/Documentation/os/drivers/special/power/battery/axp2101.rst
b/Documentation/os/drivers/special/power/battery/axp2101.rst
new file mode 100644
index 00000000000..bfa59c3526e
--- /dev/null
+++ b/Documentation/os/drivers/special/power/battery/axp2101.rst
@@ -0,0 +1,106 @@
+=============================
+X-Powers AXP2101 PMIC charger
+=============================
+
+The AXP2101 driver (``drivers/power/battery/axp2101.c``) supports the
+battery charger and the fuel gauge of the X-Powers AXP2101 PMIC, an I2C
+device at address ``0x34``. It is enabled with ``CONFIG_AXP2101``, which
+needs ``CONFIG_BATTERY_CHARGER``. The gauge part, ``CONFIG_AXP2101_GAUGE``,
+is enabled by default when ``CONFIG_BATTERY_GAUGE`` is set.
+
+The driver does not change the DCDC and LDO outputs: they keep the values
+programmed in the OTP of the chip. At initialization it checks the chip ID
+and enables the VBAT, VBUS, VSYS and die temperature ADC channels. The IRQ
+pin is not used.
+
+The power-off voltage, below which the AXP2101 turns the system off to
+protect the battery from deep discharge, is also left as programmed in the
+OTP (which may be as low as 2.6 V), unless ``CONFIG_AXP2101_VOFF_VOLTAGE``
+is set (2600 to 3300 mV in steps of 100 mV). For Li-ion and LiPo batteries
+3000 mV or more is recommended.
+
+Board interface
+===============
+
+.. code-block:: c
+
+ #include <nuttx/power/axp2101.h>
+
+ FAR struct battery_charger_dev_s *
+ axp2101_initialize(FAR struct i2c_master_s *i2c, uint8_t addr,
+ uint32_t frequency);
+
+ FAR struct battery_gauge_dev_s *
+ axp2101_gauge_initialize(FAR struct i2c_master_s *i2c, uint8_t addr,
+ uint32_t frequency);
+
+ int axp2101_poweroff(FAR struct battery_charger_dev_s *dev);
+
+The two lower halves are independent and are registered separately, for
+example:
+
+.. code-block:: c
+
+ charger = axp2101_initialize(i2c, AXP2101_I2C_ADDR, 400000);
+ battery_charger_register("/dev/batt0", charger);
+
+ gauge = axp2101_gauge_initialize(i2c, AXP2101_I2C_ADDR, 400000);
+ battery_gauge_register("/dev/gauge0", gauge);
+
+``axp2101_poweroff()`` turns off all the outputs. It may be called from
+``board_power_off()``. The system starts again on a power-on event, such as
+the PWRON key or the insertion of VBUS.
+
+Battery charger
+===============
+
+========================== ==================================================
+IOCTL Behavior
+========================== ==================================================
+``BATIOC_STATE`` ``BATTERY_CHARGING``, ``BATTERY_DISCHARGING``,
+ ``BATTERY_FULL`` (charge done) or
+ ``BATTERY_IDLE`` (also without battery).
+``BATIOC_HEALTH`` ``BATTERY_HEALTH_DISCONNECTED`` without battery,
+ else ``GOOD``, ``OVERVOLTAGE``, ``OVERHEAT``,
+ ``COLD`` or ``SAFE_TMR_EXP``.
+``BATIOC_ONLINE`` True if a good VBUS input is present.
+``BATIOC_VOLTAGE`` Charge voltage in mV: 4000, 4100, 4200, 4350 or
+ 4400.
+``BATIOC_CURRENT`` Constant charge current, 0 to 1000 mA. The value
+ is rounded down to a step of 25 mA up to 200 mA
+ and of 100 mA above.
+``BATIOC_INPUT_CURRENT`` VBUS input current limit, rounded down to 100,
+ 500, 900, 1000, 1500 or 2000 mA.
+``BATIOC_OPERATE`` ``BATIO_OPRTN_EN_TERM`` (``u32`` not zero enables
+ the charge termination) and
+ ``BATIO_OPRTN_SYSOFF`` (power off).
+``BATIOC_CHIPID`` Content of the chip ID register, ``0x4a``.
+``BATIOC_GET_VOLTAGE`` Measured battery voltage in mV.
+``BATIOC_VOLTAGE_INFO`` Measured VBUS voltage in mV.
+========================== ==================================================
+
+The health faults come from the IRQ status registers. The chip sets them
+only for the IRQs that are enabled in REG 40h to 42h, which the driver does
+not change.
+
+Battery gauge
+=============
+
+========================== ==================================================
+IOCTL Behavior
+========================== ==================================================
+``BATIOC_STATE`` As for the charger.
+``BATIOC_ONLINE`` True if a battery is present.
+``BATIOC_VOLTAGE`` Battery voltage in mV, 0 without battery.
+``BATIOC_CAPACITY`` State of charge from the gauge of the chip, in
+ percent, 0 without battery.
+``BATIOC_TEMPERATURE`` Die temperature of the PMIC in 0.1 degrees
+ Celsius. It is not the temperature of the cell.
+``BATIOC_CHIPID`` Content of the chip ID register, ``0x4a``.
+========================== ==================================================
+
+The chip does not measure the battery current, so ``BATIOC_CURRENT`` is not
+supported.
+
+The ``batterydump`` tool (``CONFIG_SYSTEM_BATTERYDUMP``) shows both devices:
+``batterydump -t 0 /dev/batt0`` and ``batterydump -t 1 /dev/gauge0``.
diff --git a/Documentation/os/drivers/special/power/index.rst
b/Documentation/os/drivers/special/power/index.rst
index 05e09765fa1..7736fce5fd5 100644
--- a/Documentation/os/drivers/special/power/index.rst
+++ b/Documentation/os/drivers/special/power/index.rst
@@ -13,6 +13,7 @@ Power-related Drivers
npzero.rst
battery/ltc4013.rst
battery/npm1100.rst
+ battery/axp2101.rst
.. toctree::
:maxdepth: 1
diff --git a/drivers/power/battery/CMakeLists.txt
b/drivers/power/battery/CMakeLists.txt
index f7ce619bb59..b5e3b463d09 100644
--- a/drivers/power/battery/CMakeLists.txt
+++ b/drivers/power/battery/CMakeLists.txt
@@ -72,6 +72,12 @@ if(CONFIG_BATTERY_CHARGER)
list(APPEND SRCS axp202.c)
endif()
+ # Add the AXP2101 I2C-based PMIC battery charger driver
+
+ if(CONFIG_AXP2101)
+ list(APPEND SRCS axp2101.c)
+ endif()
+
endif()
# Add battery gauge drivers
diff --git a/drivers/power/battery/Kconfig b/drivers/power/battery/Kconfig
index a13be33d58a..b855348039a 100644
--- a/drivers/power/battery/Kconfig
+++ b/drivers/power/battery/Kconfig
@@ -114,6 +114,38 @@ config AXP202_LDO4_VOLTAGE
1250 1300 1400 1500 1600 1700 1800 1900
2000 2500 2700 2800 3000 3100 3200 3300 mV.
+config AXP2101
+ bool "AXP2101 PMIC battery charger support"
+ default n
+ select I2C
+ depends on BATTERY_CHARGER
+ ---help---
+ Battery charger driver for the X-Powers AXP2101 PMIC (I2C).
+ It reports the charge state and sets the charge voltage, the
+ charge current and the input current limit. The power rails
+ are left as programmed by the OTP of the chip.
+
+config AXP2101_GAUGE
+ bool "AXP2101 fuel gauge support"
+ default y
+ depends on AXP2101 && BATTERY_GAUGE
+ ---help---
+ Also provide a battery gauge lower half for the AXP2101: battery
+ voltage, state of charge and die temperature.
+
+config AXP2101_VOFF_VOLTAGE
+ int "AXP2101 power-off voltage (mV)"
+ default 0
+ range 0 3300
+ depends on AXP2101
+ ---help---
+ The AXP2101 turns the system off when the battery voltage falls
+ below this level, which protects the battery from deep
+ discharge. 2600 to 3300 mV in steps of 100 mV. 0 keeps the
+ value programmed in the OTP of the chip, which may be as low as
+ 2600 mV. For a Li-ion or LiPo battery, 3000 mV or more is
+ recommended.
+
config GD30BC2416
bool "GD30BC2416 Battery charger support"
default n
diff --git a/drivers/power/battery/Make.defs b/drivers/power/battery/Make.defs
index 4bd0fd8acdc..77fd5a4b7ca 100644
--- a/drivers/power/battery/Make.defs
+++ b/drivers/power/battery/Make.defs
@@ -68,6 +68,12 @@ ifeq ($(CONFIG_I2C_AXP202),y)
CSRCS += axp202.c
endif
+# Add the AXP2101 I2C-based PMIC battery charger driver
+
+ifeq ($(CONFIG_AXP2101),y)
+CSRCS += axp2101.c
+endif
+
endif
# Add battery gauge drivers
diff --git a/drivers/power/battery/axp2101.c b/drivers/power/battery/axp2101.c
new file mode 100644
index 00000000000..0054a9956a9
--- /dev/null
+++ b/drivers/power/battery/axp2101.c
@@ -0,0 +1,1013 @@
+/****************************************************************************
+ * drivers/power/battery/axp2101.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.
+ *
+ ****************************************************************************/
+
+/* Lower halves of the battery charger and battery gauge drivers for the
+ * X-Powers AXP2101 PMIC. The power rails are left as programmed by the
+ * OTP of the chip.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/param.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <errno.h>
+#include <assert.h>
+
+#include <nuttx/debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/power/battery_charger.h>
+#include <nuttx/power/battery_ioctl.h>
+#include <nuttx/power/axp2101.h>
+
+#ifdef CONFIG_AXP2101_GAUGE
+# include <nuttx/power/battery_gauge.h>
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Registers */
+
+#define AXP2101_STATUS1 0x00 /* PMU status 1 */
+#define AXP2101_STATUS2 0x01 /* PMU status 2 */
+#define AXP2101_CHIPID 0x03 /* Chip ID */
+#define AXP2101_COMMON_CFG 0x10 /* PMU common configuration */
+#define AXP2101_ICL 0x16 /* Input current limit */
+#define AXP2101_VOFF 0x24 /* Power-off voltage */
+#define AXP2101_MODULE_EN 0x18 /* Charger, gauge, watchdog enable */
+#define AXP2101_ADC_EN 0x30 /* ADC channel enable */
+#define AXP2101_VBAT_H 0x34 /* Battery voltage, bits 13:8 */
+#define AXP2101_VBAT_L 0x35 /* Battery voltage, bits 7:0 */
+#define AXP2101_VBUS_H 0x38 /* VBUS voltage, bits 13:8 */
+#define AXP2101_VBUS_L 0x39 /* VBUS voltage, bits 7:0 */
+#define AXP2101_TDIE_H 0x3c /* Die temperature, bits 13:8 */
+#define AXP2101_TDIE_L 0x3d /* Die temperature, bits 7:0 */
+#define AXP2101_IRQ_STATUS0 0x48 /* IRQ status 0 */
+#define AXP2101_IRQ_STATUS2 0x4a /* IRQ status 2 */
+#define AXP2101_ICC 0x62 /* Constant charge current */
+#define AXP2101_ITERM 0x63 /* Termination current and control */
+#define AXP2101_CV 0x64 /* Charge voltage */
+#define AXP2101_SOC 0xa4 /* Battery percentage */
+
+/* AXP2101_STATUS1 */
+
+#define STATUS1_VBUS_GOOD (1 << 5)
+#define STATUS1_BAT_PRESENT (1 << 3)
+
+/* AXP2101_STATUS2 */
+
+#define STATUS2_DIR_SHIFT 5
+#define STATUS2_DIR_MASK (3 << STATUS2_DIR_SHIFT)
+#define STATUS2_DIR_STANDBY (0 << STATUS2_DIR_SHIFT)
+#define STATUS2_DIR_CHARGE (1 << STATUS2_DIR_SHIFT)
+#define STATUS2_DIR_DISCHARGE (2 << STATUS2_DIR_SHIFT)
+#define STATUS2_CHG_MASK 0x07
+#define STATUS2_CHG_CV 0x03 /* 0 to 3: trickle, pre, CC, CV */
+#define STATUS2_CHG_DONE 0x04
+
+/* AXP2101_COMMON_CFG */
+
+#define COMMON_CFG_PWROFF (1 << 0)
+
+/* AXP2101_MODULE_EN */
+
+#define MODULE_EN_GAUGE (1 << 3)
+
+/* AXP2101_ADC_EN */
+
+#define ADC_EN_VBAT (1 << 0)
+#define ADC_EN_VBUS (1 << 2)
+#define ADC_EN_VSYS (1 << 3)
+#define ADC_EN_TDIE (1 << 4)
+
+/* AXP2101_IRQ_STATUS0 */
+
+#define IRQ0_BAT_OT_CHG (1 << 3)
+#define IRQ0_BAT_UT_CHG (1 << 2)
+#define IRQ0_BAT_OT_WORK (1 << 1)
+#define IRQ0_BAT_UT_WORK (1 << 0)
+
+/* AXP2101_IRQ_STATUS2 */
+
+#define IRQ2_SAFETY_TIMER (1 << 1)
+#define IRQ2_BAT_OVP (1 << 0)
+
+/* The fault flags used for the battery health */
+
+#define IRQ0_HEALTH (IRQ0_BAT_OT_CHG | IRQ0_BAT_UT_CHG | \
+ IRQ0_BAT_OT_WORK | IRQ0_BAT_UT_WORK)
+#define IRQ2_HEALTH (IRQ2_SAFETY_TIMER | IRQ2_BAT_OVP)
+
+/* AXP2101_ITERM */
+
+#define ITERM_EN (1 << 4)
+
+/* Field masks */
+
+#define ICL_MASK 0x07
+#define VOFF_MASK 0x07
+#define ICC_MASK 0x1f
+#define CV_MASK 0x07
+
+/* ADC results are 14 bits, 1 mV per LSB for the voltage channels */
+
+#define ADC_H_MASK 0x3f
+
+#if CONFIG_AXP2101_VOFF_VOLTAGE != 0 && \
+ (CONFIG_AXP2101_VOFF_VOLTAGE < 2600 || \
+ CONFIG_AXP2101_VOFF_VOLTAGE % 100 != 0)
+# error "CONFIG_AXP2101_VOFF_VOLTAGE must be 0 or 2600..3300 in 100 mV steps"
+#endif
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct axp2101_bus_s
+{
+ FAR struct i2c_master_s *i2c; /* I2C interface */
+ uint8_t addr; /* I2C address */
+ uint32_t frequency; /* I2C frequency */
+};
+
+struct axp2101_dev_s
+{
+ struct battery_charger_dev_s dev; /* Must be first */
+ struct axp2101_bus_s bus;
+};
+
+#ifdef CONFIG_AXP2101_GAUGE
+struct axp2101_gauge_s
+{
+ struct battery_gauge_dev_s dev; /* Must be first */
+ struct axp2101_bus_s bus;
+};
+#endif
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int axp2101_state(FAR struct battery_charger_dev_s *dev,
+ FAR int *status);
+static int axp2101_health(FAR struct battery_charger_dev_s *dev,
+ FAR int *health);
+static int axp2101_online(FAR struct battery_charger_dev_s *dev,
+ FAR bool *status);
+static int axp2101_voltage(FAR struct battery_charger_dev_s *dev,
+ int value);
+static int axp2101_current(FAR struct battery_charger_dev_s *dev,
+ int value);
+static int axp2101_input_current(FAR struct battery_charger_dev_s *dev,
+ int value);
+static int axp2101_operate(FAR struct battery_charger_dev_s *dev,
+ uintptr_t param);
+static int axp2101_chipid(FAR struct battery_charger_dev_s *dev,
+ FAR unsigned int *value);
+static int axp2101_get_voltage(FAR struct battery_charger_dev_s *dev,
+ FAR int *value);
+static int axp2101_voltage_info(FAR struct battery_charger_dev_s *dev,
+ FAR int *value);
+
+#ifdef CONFIG_AXP2101_GAUGE
+static int axp2101_gauge_state(FAR struct battery_gauge_dev_s *dev,
+ FAR int *status);
+static int axp2101_gauge_online(FAR struct battery_gauge_dev_s *dev,
+ FAR bool *status);
+static int axp2101_gauge_voltage(FAR struct battery_gauge_dev_s *dev,
+ FAR int *value);
+static int axp2101_gauge_capacity(FAR struct battery_gauge_dev_s *dev,
+ FAR int *value);
+static int axp2101_gauge_temp(FAR struct battery_gauge_dev_s *dev,
+ FAR int *value);
+static int axp2101_gauge_chipid(FAR struct battery_gauge_dev_s *dev,
+ FAR unsigned int *value);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct battery_charger_operations_s g_axp2101ops =
+{
+ axp2101_state,
+ axp2101_health,
+ axp2101_online,
+ axp2101_voltage,
+ axp2101_current,
+ axp2101_input_current,
+ axp2101_operate,
+ axp2101_chipid,
+ axp2101_get_voltage,
+ axp2101_voltage_info,
+ NULL /* get_protocol */
+};
+
+#ifdef CONFIG_AXP2101_GAUGE
+static const struct battery_gauge_operations_s g_axp2101_gaugeops =
+{
+ axp2101_gauge_state,
+ axp2101_gauge_online,
+ axp2101_gauge_voltage,
+ axp2101_gauge_capacity,
+ NULL, /* current: no current ADC */
+ axp2101_gauge_temp,
+ axp2101_gauge_chipid,
+ NULL /* operate */
+};
+#endif
+
+/* Input current limit steps in mA, index is the register value */
+
+static const uint16_t g_axp2101_icl[] =
+{
+ 100, 500, 900, 1000, 1500, 2000
+};
+
+/* Charge voltage steps in mV, index is the register value (0 is 5.0 V and
+ * is never selected).
+ */
+
+static const uint16_t g_axp2101_cv[] =
+{
+ 0, 4000, 4100, 4200, 4350, 4400
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int axp2101_getreg(FAR struct axp2101_bus_s *bus, uint8_t regaddr,
+ FAR uint8_t *regval)
+{
+ struct i2c_config_s config;
+ int ret;
+
+ config.frequency = bus->frequency;
+ config.address = bus->addr;
+ config.addrlen = 7;
+
+ ret = i2c_writeread(bus->i2c, &config, ®addr, 1, regval, 1);
+ if (ret < 0)
+ {
+ baterr("ERROR: read of reg 0x%02x failed: %d\n", regaddr, ret);
+ return ret;
+ }
+
+ return OK;
+}
+
+static int axp2101_putreg(FAR struct axp2101_bus_s *bus, uint8_t regaddr,
+ uint8_t regval)
+{
+ struct i2c_config_s config;
+ uint8_t buffer[2];
+ int ret;
+
+ config.frequency = bus->frequency;
+ config.address = bus->addr;
+ config.addrlen = 7;
+
+ buffer[0] = regaddr;
+ buffer[1] = regval;
+
+ ret = i2c_write(bus->i2c, &config, buffer, sizeof(buffer));
+ if (ret < 0)
+ {
+ baterr("ERROR: write of reg 0x%02x failed: %d\n", regaddr, ret);
+ return ret;
+ }
+
+ return OK;
+}
+
+static int axp2101_modifyreg(FAR struct axp2101_bus_s *bus, uint8_t regaddr,
+ uint8_t clearbits, uint8_t setbits)
+{
+ uint8_t regval;
+ int ret;
+
+ ret = axp2101_getreg(bus, regaddr, ®val);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ regval &= ~clearbits;
+ regval |= setbits;
+
+ return axp2101_putreg(bus, regaddr, regval);
+}
+
+/* Read a 14-bit ADC result. The high byte must be read first. */
+
+static int axp2101_getadc(FAR struct axp2101_bus_s *bus, uint8_t regh,
+ FAR int *value)
+{
+ uint8_t high;
+ uint8_t low;
+ int ret;
+
+ ret = axp2101_getreg(bus, regh, &high);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = axp2101_getreg(bus, regh + 1, &low);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ *value = ((high & ADC_H_MASK) << 8) | low;
+ return OK;
+}
+
+/* Check the chip ID and enable the ADC channels used by the driver. */
+
+static int axp2101_probe(FAR struct axp2101_bus_s *bus)
+{
+ uint8_t chipid;
+ int ret;
+
+ ret = axp2101_getreg(bus, AXP2101_CHIPID, &chipid);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ if (chipid != AXP2101_CHIP_ID)
+ {
+ baterr("ERROR: unexpected chip ID 0x%02x\n", chipid);
+ return -ENODEV;
+ }
+
+ ret = axp2101_modifyreg(bus, AXP2101_ADC_EN, 0,
+ ADC_EN_VBAT | ADC_EN_VBUS | ADC_EN_VSYS |
+ ADC_EN_TDIE);
+
+#if CONFIG_AXP2101_VOFF_VOLTAGE > 0
+ /* Power off below this battery voltage, to protect the battery from deep
+ * discharge (2.6 V + 0.1 V per step).
+ */
+
+ if (ret >= 0)
+ {
+ ret = axp2101_modifyreg(bus, AXP2101_VOFF, VOFF_MASK,
+ (CONFIG_AXP2101_VOFF_VOLTAGE - 2600) / 100);
+ }
+#endif
+
+ return ret;
+}
+
+static int axp2101_bat_state(FAR struct axp2101_bus_s *bus,
+ FAR int *status)
+{
+ uint8_t status1;
+ uint8_t status2;
+ uint8_t chg;
+ int ret;
+
+ *status = BATTERY_UNKNOWN;
+
+ ret = axp2101_getreg(bus, AXP2101_STATUS1, &status1);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = axp2101_getreg(bus, AXP2101_STATUS2, &status2);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ chg = status2 & STATUS2_CHG_MASK;
+
+ if ((status1 & STATUS1_BAT_PRESENT) == 0)
+ {
+ /* No battery: BATIOC_HEALTH reports it as disconnected */
+
+ *status = BATTERY_IDLE;
+ }
+ else if (chg == STATUS2_CHG_DONE)
+ {
+ *status = BATTERY_FULL;
+ }
+ else if ((status2 & STATUS2_DIR_MASK) == STATUS2_DIR_CHARGE ||
+ chg <= STATUS2_CHG_CV)
+ {
+ *status = BATTERY_CHARGING;
+ }
+ else if ((status2 & STATUS2_DIR_MASK) == STATUS2_DIR_DISCHARGE)
+ {
+ *status = BATTERY_DISCHARGING;
+ }
+ else
+ {
+ *status = BATTERY_IDLE;
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: axp2101_state
+ *
+ * Description:
+ * Return the battery state (enum battery_status_e).
+ *
+ ****************************************************************************/
+
+static int axp2101_state(FAR struct battery_charger_dev_s *dev,
+ FAR int *status)
+{
+ FAR struct axp2101_dev_s *priv = (FAR struct axp2101_dev_s *)dev;
+
+ return axp2101_bat_state(&priv->bus, status);
+}
+
+/****************************************************************************
+ * Name: axp2101_health
+ *
+ * Description:
+ * Return the battery health (enum battery_health_e). The fault flags
+ * are IRQ status bits, which the chip sets only for the IRQs enabled in
+ * REG 40h-42h. The driver does not change these enables. The flags
+ * are latched until cleared, so each fault event is reported once and
+ * then cleared.
+ *
+ ****************************************************************************/
+
+static int axp2101_health(FAR struct battery_charger_dev_s *dev,
+ FAR int *health)
+{
+ FAR struct axp2101_dev_s *priv = (FAR struct axp2101_dev_s *)dev;
+ uint8_t status1;
+ uint8_t irq0;
+ uint8_t irq2;
+ int ret;
+
+ *health = BATTERY_HEALTH_UNKNOWN;
+
+ ret = axp2101_getreg(&priv->bus, AXP2101_STATUS1, &status1);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = axp2101_getreg(&priv->bus, AXP2101_IRQ_STATUS0, &irq0);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = axp2101_getreg(&priv->bus, AXP2101_IRQ_STATUS2, &irq2);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ /* Clear the latched fault flags that were seen (write 1 to clear) */
+
+ irq0 &= IRQ0_HEALTH;
+ irq2 &= IRQ2_HEALTH;
+
+ if (irq0 != 0)
+ {
+ ret = axp2101_putreg(&priv->bus, AXP2101_IRQ_STATUS0, irq0);
+ if (ret < 0)
+ {
+ return ret;
+ }
+ }
+
+ if (irq2 != 0)
+ {
+ ret = axp2101_putreg(&priv->bus, AXP2101_IRQ_STATUS2, irq2);
+ if (ret < 0)
+ {
+ return ret;
+ }
+ }
+
+ if ((status1 & STATUS1_BAT_PRESENT) == 0)
+ {
+ *health = BATTERY_HEALTH_DISCONNECTED;
+ }
+ else if (irq2 & IRQ2_BAT_OVP)
+ {
+ *health = BATTERY_HEALTH_OVERVOLTAGE;
+ }
+ else if (irq0 & (IRQ0_BAT_OT_CHG | IRQ0_BAT_OT_WORK))
+ {
+ *health = BATTERY_HEALTH_OVERHEAT;
+ }
+ else if (irq0 & (IRQ0_BAT_UT_CHG | IRQ0_BAT_UT_WORK))
+ {
+ *health = BATTERY_HEALTH_COLD;
+ }
+ else if (irq2 & IRQ2_SAFETY_TIMER)
+ {
+ *health = BATTERY_HEALTH_SAFE_TMR_EXP;
+ }
+ else
+ {
+ *health = BATTERY_HEALTH_GOOD;
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: axp2101_online
+ *
+ * Description:
+ * Return true if a good VBUS input is present.
+ *
+ ****************************************************************************/
+
+static int axp2101_online(FAR struct battery_charger_dev_s *dev,
+ FAR bool *status)
+{
+ FAR struct axp2101_dev_s *priv = (FAR struct axp2101_dev_s *)dev;
+ uint8_t regval;
+ int ret;
+
+ ret = axp2101_getreg(&priv->bus, AXP2101_STATUS1, ®val);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ *status = (regval & STATUS1_VBUS_GOOD) != 0;
+ return OK;
+}
+
+/****************************************************************************
+ * Name: axp2101_voltage
+ *
+ * Description:
+ * Set the charge voltage in mV: 4000, 4100, 4200, 4350 or 4400.
+ *
+ ****************************************************************************/
+
+static int axp2101_voltage(FAR struct battery_charger_dev_s *dev,
+ int value)
+{
+ FAR struct axp2101_dev_s *priv = (FAR struct axp2101_dev_s *)dev;
+ int i;
+
+ for (i = 1; i < nitems(g_axp2101_cv); i++)
+ {
+ if (g_axp2101_cv[i] == value)
+ {
+ return axp2101_modifyreg(&priv->bus, AXP2101_CV, CV_MASK, i);
+ }
+ }
+
+ return -EINVAL;
+}
+
+/****************************************************************************
+ * Name: axp2101_current
+ *
+ * Description:
+ * Set the constant charge current in mA, from 0 to 1000. The value is
+ * rounded down to a step of 25 mA up to 200 mA and of 100 mA above.
+ *
+ ****************************************************************************/
+
+static int axp2101_current(FAR struct battery_charger_dev_s *dev,
+ int value)
+{
+ FAR struct axp2101_dev_s *priv = (FAR struct axp2101_dev_s *)dev;
+ uint8_t regval;
+
+ if (value < 0 || value > 1000)
+ {
+ return -EINVAL;
+ }
+
+ if (value <= 200)
+ {
+ regval = value / 25;
+ }
+ else
+ {
+ regval = 8 + (value - 200) / 100;
+ }
+
+ return axp2101_modifyreg(&priv->bus, AXP2101_ICC, ICC_MASK, regval);
+}
+
+/****************************************************************************
+ * Name: axp2101_input_current
+ *
+ * Description:
+ * Set the VBUS input current limit in mA, rounded down to one of 100,
+ * 500, 900, 1000, 1500 or 2000.
+ *
+ ****************************************************************************/
+
+static int axp2101_input_current(FAR struct battery_charger_dev_s *dev,
+ int value)
+{
+ FAR struct axp2101_dev_s *priv = (FAR struct axp2101_dev_s *)dev;
+ int i;
+
+ if (value < g_axp2101_icl[0])
+ {
+ return -EINVAL;
+ }
+
+ for (i = nitems(g_axp2101_icl) - 1; i > 0; i--)
+ {
+ if (value >= g_axp2101_icl[i])
+ {
+ break;
+ }
+ }
+
+ return axp2101_modifyreg(&priv->bus, AXP2101_ICL, ICL_MASK, i);
+}
+
+/****************************************************************************
+ * Name: axp2101_operate
+ *
+ * Description:
+ * Supported operations:
+ * BATIO_OPRTN_EN_TERM - u32 != 0 enables the charge termination
+ * BATIO_OPRTN_SYSOFF - Turn off the PMIC outputs
+ *
+ ****************************************************************************/
+
+static int axp2101_operate(FAR struct battery_charger_dev_s *dev,
+ uintptr_t param)
+{
+ FAR struct axp2101_dev_s *priv = (FAR struct axp2101_dev_s *)dev;
+ FAR struct batio_operate_msg_s *msg =
+ (FAR struct batio_operate_msg_s *)param;
+
+ switch (msg->operate_type)
+ {
+ case BATIO_OPRTN_EN_TERM:
+ return axp2101_modifyreg(&priv->bus, AXP2101_ITERM, ITERM_EN,
+ msg->u32 != 0 ? ITERM_EN : 0);
+
+ case BATIO_OPRTN_SYSOFF:
+ return axp2101_poweroff(dev);
+
+ default:
+ return -ENOSYS;
+ }
+}
+
+/****************************************************************************
+ * Name: axp2101_chipid
+ ****************************************************************************/
+
+static int axp2101_chipid(FAR struct battery_charger_dev_s *dev,
+ FAR unsigned int *value)
+{
+ FAR struct axp2101_dev_s *priv = (FAR struct axp2101_dev_s *)dev;
+ uint8_t regval;
+ int ret;
+
+ ret = axp2101_getreg(&priv->bus, AXP2101_CHIPID, ®val);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ *value = regval;
+ return OK;
+}
+
+/****************************************************************************
+ * Name: axp2101_get_voltage
+ *
+ * Description:
+ * Return the measured battery voltage in mV.
+ *
+ ****************************************************************************/
+
+static int axp2101_get_voltage(FAR struct battery_charger_dev_s *dev,
+ FAR int *value)
+{
+ FAR struct axp2101_dev_s *priv = (FAR struct axp2101_dev_s *)dev;
+
+ return axp2101_getadc(&priv->bus, AXP2101_VBAT_H, value);
+}
+
+/****************************************************************************
+ * Name: axp2101_voltage_info
+ *
+ * Description:
+ * Return the measured VBUS input voltage in mV.
+ *
+ ****************************************************************************/
+
+static int axp2101_voltage_info(FAR struct battery_charger_dev_s *dev,
+ FAR int *value)
+{
+ FAR struct axp2101_dev_s *priv = (FAR struct axp2101_dev_s *)dev;
+
+ return axp2101_getadc(&priv->bus, AXP2101_VBUS_H, value);
+}
+
+#ifdef CONFIG_AXP2101_GAUGE
+
+static int axp2101_bat_present(FAR struct axp2101_bus_s *bus,
+ FAR bool *present)
+{
+ uint8_t regval;
+ int ret;
+
+ ret = axp2101_getreg(bus, AXP2101_STATUS1, ®val);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ *present = (regval & STATUS1_BAT_PRESENT) != 0;
+ return OK;
+}
+
+/****************************************************************************
+ * Name: axp2101_gauge_state
+ ****************************************************************************/
+
+static int axp2101_gauge_state(FAR struct battery_gauge_dev_s *dev,
+ FAR int *status)
+{
+ FAR struct axp2101_gauge_s *priv = (FAR struct axp2101_gauge_s *)dev;
+
+ return axp2101_bat_state(&priv->bus, status);
+}
+
+/****************************************************************************
+ * Name: axp2101_gauge_online
+ *
+ * Description:
+ * Return true if a battery is present.
+ *
+ ****************************************************************************/
+
+static int axp2101_gauge_online(FAR struct battery_gauge_dev_s *dev,
+ FAR bool *status)
+{
+ FAR struct axp2101_gauge_s *priv = (FAR struct axp2101_gauge_s *)dev;
+
+ return axp2101_bat_present(&priv->bus, status);
+}
+
+/****************************************************************************
+ * Name: axp2101_gauge_voltage
+ *
+ * Description:
+ * Return the battery voltage in mV, 0 without battery.
+ *
+ ****************************************************************************/
+
+static int axp2101_gauge_voltage(FAR struct battery_gauge_dev_s *dev,
+ FAR int *value)
+{
+ FAR struct axp2101_gauge_s *priv = (FAR struct axp2101_gauge_s *)dev;
+ bool present;
+ int ret;
+
+ ret = axp2101_bat_present(&priv->bus, &present);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ if (!present)
+ {
+ *value = 0;
+ return OK;
+ }
+
+ return axp2101_getadc(&priv->bus, AXP2101_VBAT_H, value);
+}
+
+/****************************************************************************
+ * Name: axp2101_gauge_capacity
+ *
+ * Description:
+ * Return the state of charge in percent, 0 without battery.
+ *
+ ****************************************************************************/
+
+static int axp2101_gauge_capacity(FAR struct battery_gauge_dev_s *dev,
+ FAR int *value)
+{
+ FAR struct axp2101_gauge_s *priv = (FAR struct axp2101_gauge_s *)dev;
+ uint8_t regval;
+ bool present;
+ int ret;
+
+ ret = axp2101_bat_present(&priv->bus, &present);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ if (!present)
+ {
+ *value = 0;
+ return OK;
+ }
+
+ ret = axp2101_getreg(&priv->bus, AXP2101_SOC, ®val);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ *value = regval > 100 ? 100 : regval;
+ return OK;
+}
+
+/****************************************************************************
+ * Name: axp2101_gauge_temp
+ *
+ * Description:
+ * Return the die temperature of the PMIC in 0.1 degrees Celsius. The
+ * chip has no battery temperature sensor of its own.
+ *
+ ****************************************************************************/
+
+static int axp2101_gauge_temp(FAR struct battery_gauge_dev_s *dev,
+ FAR int *value)
+{
+ FAR struct axp2101_gauge_s *priv = (FAR struct axp2101_gauge_s *)dev;
+ int raw;
+ int ret;
+
+ ret = axp2101_getadc(&priv->bus, AXP2101_TDIE_H, &raw);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ /* T = 22 + (7274 - raw) / 20 degrees Celsius */
+
+ *value = 220 + (7274 - raw) / 2;
+ return OK;
+}
+
+/****************************************************************************
+ * Name: axp2101_gauge_chipid
+ ****************************************************************************/
+
+static int axp2101_gauge_chipid(FAR struct battery_gauge_dev_s *dev,
+ FAR unsigned int *value)
+{
+ FAR struct axp2101_gauge_s *priv = (FAR struct axp2101_gauge_s *)dev;
+ uint8_t regval;
+ int ret;
+
+ ret = axp2101_getreg(&priv->bus, AXP2101_CHIPID, ®val);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ *value = regval;
+ return OK;
+}
+
+#endif /* CONFIG_AXP2101_GAUGE */
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: axp2101_initialize
+ *
+ * Description:
+ * Initialize the AXP2101 battery charger lower half.
+ *
+ ****************************************************************************/
+
+FAR struct battery_charger_dev_s *
+axp2101_initialize(FAR struct i2c_master_s *i2c, uint8_t addr,
+ uint32_t frequency)
+{
+ FAR struct axp2101_dev_s *priv;
+
+ DEBUGASSERT(i2c != NULL);
+
+ priv = kmm_zalloc(sizeof(struct axp2101_dev_s));
+ if (priv == NULL)
+ {
+ return NULL;
+ }
+
+ priv->dev.ops = &g_axp2101ops;
+ priv->bus.i2c = i2c;
+ priv->bus.addr = addr;
+ priv->bus.frequency = frequency;
+
+ if (axp2101_probe(&priv->bus) < 0)
+ {
+ kmm_free(priv);
+ return NULL;
+ }
+
+ return &priv->dev;
+}
+
+/****************************************************************************
+ * Name: axp2101_poweroff
+ *
+ * Description:
+ * Turn off all the outputs of the AXP2101.
+ *
+ ****************************************************************************/
+
+int axp2101_poweroff(FAR struct battery_charger_dev_s *dev)
+{
+ FAR struct axp2101_dev_s *priv = (FAR struct axp2101_dev_s *)dev;
+
+ DEBUGASSERT(priv != NULL);
+
+ return axp2101_modifyreg(&priv->bus, AXP2101_COMMON_CFG, 0,
+ COMMON_CFG_PWROFF);
+}
+
+#ifdef CONFIG_AXP2101_GAUGE
+
+/****************************************************************************
+ * Name: axp2101_gauge_initialize
+ *
+ * Description:
+ * Initialize the AXP2101 fuel gauge lower half.
+ *
+ ****************************************************************************/
+
+FAR struct battery_gauge_dev_s *
+axp2101_gauge_initialize(FAR struct i2c_master_s *i2c, uint8_t addr,
+ uint32_t frequency)
+{
+ FAR struct axp2101_gauge_s *priv;
+
+ DEBUGASSERT(i2c != NULL);
+
+ priv = kmm_zalloc(sizeof(struct axp2101_gauge_s));
+ if (priv == NULL)
+ {
+ return NULL;
+ }
+
+ priv->dev.ops = &g_axp2101_gaugeops;
+ priv->bus.i2c = i2c;
+ priv->bus.addr = addr;
+ priv->bus.frequency = frequency;
+
+ /* The gauge is enabled by default, make sure that it still is */
+
+ if (axp2101_probe(&priv->bus) < 0 ||
+ axp2101_modifyreg(&priv->bus, AXP2101_MODULE_EN, 0,
+ MODULE_EN_GAUGE) < 0)
+ {
+ kmm_free(priv);
+ return NULL;
+ }
+
+ return &priv->dev;
+}
+
+#endif /* CONFIG_AXP2101_GAUGE */
diff --git a/include/nuttx/power/axp2101.h b/include/nuttx/power/axp2101.h
new file mode 100644
index 00000000000..1724b290d2e
--- /dev/null
+++ b/include/nuttx/power/axp2101.h
@@ -0,0 +1,128 @@
+/****************************************************************************
+ * include/nuttx/power/axp2101.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_AXP2101_H
+#define __INCLUDE_NUTTX_POWER_AXP2101_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+
+#ifdef CONFIG_AXP2101
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define AXP2101_I2C_ADDR 0x34 /* 7-bit I2C address */
+#define AXP2101_CHIP_ID 0x4a /* Content of the chip ID register */
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+struct i2c_master_s;
+struct battery_charger_dev_s;
+struct battery_gauge_dev_s;
+
+/****************************************************************************
+ * Name: axp2101_initialize
+ *
+ * Description:
+ * Initialize the AXP2101 battery charger lower half. The chip ID is
+ * checked and the VBAT, VBUS, VSYS and die temperature ADC channels are
+ * enabled. The power rails are not changed.
+ *
+ * Input Parameters:
+ * i2c - The I2C bus of the AXP2101
+ * addr - The I2C address (AXP2101_I2C_ADDR)
+ * frequency - The I2C frequency
+ *
+ * Returned Value:
+ * An instance to register with battery_charger_register(), or NULL on
+ * failure.
+ *
+ ****************************************************************************/
+
+FAR struct battery_charger_dev_s *
+axp2101_initialize(FAR struct i2c_master_s *i2c, uint8_t addr,
+ uint32_t frequency);
+
+/****************************************************************************
+ * Name: axp2101_poweroff
+ *
+ * Description:
+ * Turn off all the outputs of the AXP2101. The system starts again only
+ * on a power-on event (PWRON key, VBUS insertion, ...).
+ *
+ * Input Parameters:
+ * dev - The instance returned by axp2101_initialize()
+ *
+ * Returned Value:
+ * Zero on success, the outputs turn off right after. A negated errno
+ * value on failure.
+ *
+ ****************************************************************************/
+
+int axp2101_poweroff(FAR struct battery_charger_dev_s *dev);
+
+#ifdef CONFIG_AXP2101_GAUGE
+
+/****************************************************************************
+ * Name: axp2101_gauge_initialize
+ *
+ * Description:
+ * Initialize the AXP2101 fuel gauge lower half. It may be used with or
+ * without the charger lower half on the same chip.
+ *
+ * Input Parameters:
+ * i2c - The I2C bus of the AXP2101
+ * addr - The I2C address (AXP2101_I2C_ADDR)
+ * frequency - The I2C frequency
+ *
+ * Returned Value:
+ * An instance to register with battery_gauge_register(), or NULL on
+ * failure.
+ *
+ ****************************************************************************/
+
+FAR struct battery_gauge_dev_s *
+axp2101_gauge_initialize(FAR struct i2c_master_s *i2c, uint8_t addr,
+ uint32_t frequency);
+
+#endif /* CONFIG_AXP2101_GAUGE */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CONFIG_AXP2101 */
+#endif /* __INCLUDE_NUTTX_POWER_AXP2101_H */