Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions drivers/gpio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ zephyr_library_sources_ifdef(CONFIG_GPIO_NCT38XX_ALERT gpio_nct38xx_alert.c)
zephyr_library_sources_ifdef(CONFIG_GPIO_NEORV32 gpio_neorv32.c)
zephyr_library_sources_ifdef(CONFIG_GPIO_NPCX gpio_npcx.c)
zephyr_library_sources_ifdef(CONFIG_GPIO_NPM1300 gpio_npm1300.c)
zephyr_library_sources_ifdef(CONFIG_GPIO_NPM2100 gpio_npm2100.c)
zephyr_library_sources_ifdef(CONFIG_GPIO_NPM6001 gpio_npm6001.c)
zephyr_library_sources_ifdef(CONFIG_GPIO_NRFX gpio_nrfx.c)
zephyr_library_sources_ifdef(CONFIG_GPIO_NUMAKER gpio_numaker.c)
Expand Down
1 change: 1 addition & 0 deletions drivers/gpio/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ source "drivers/gpio/Kconfig.nct38xx"
source "drivers/gpio/Kconfig.neorv32"
source "drivers/gpio/Kconfig.npcx"
source "drivers/gpio/Kconfig.npm1300"
source "drivers/gpio/Kconfig.npm2100"
source "drivers/gpio/Kconfig.npm6001"
source "drivers/gpio/Kconfig.nrfx"
source "drivers/gpio/Kconfig.numaker"
Expand Down
19 changes: 19 additions & 0 deletions drivers/gpio/Kconfig.npm2100
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

config GPIO_NPM2100
bool "nPM2100 GPIO driver"
default y
depends on DT_HAS_NORDIC_NPM2100_GPIO_ENABLED
select I2C
select MFD
help
Enable the nPM2100 GPIO driver.

config GPIO_NPM2100_INIT_PRIORITY
int "nPM2100 GPIO driver initialization priority"
depends on GPIO_NPM2100
default 80
help
Initialization priority for the nPM2100 GPIO driver. It must be
greater than the I2C controller init priority.
182 changes: 182 additions & 0 deletions drivers/gpio/gpio_npm2100.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*/

#define DT_DRV_COMPAT nordic_npm2100_gpio

#include <errno.h>

#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/gpio/gpio_utils.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/dt-bindings/gpio/nordic-npm2100-gpio.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/util_macro.h>

#define NPM2100_GPIO_CONFIG 0x80U
#define NPM2100_GPIO_USAGE 0x83U
#define NPM2100_GPIO_OUTPUT 0x86U
#define NPM2100_GPIO_READ 0x89U

#define NPM2100_GPIO_PINS 2U

#define NPM2100_GPIO_CONFIG_INPUT 0x01U
#define NPM2100_GPIO_CONFIG_OUTPUT 0x02U
#define NPM2100_GPIO_CONFIG_OPENDRAIN 0x04U
#define NPM2100_GPIO_CONFIG_PULLDOWN 0x08U
#define NPM2100_GPIO_CONFIG_PULLUP 0x10U
#define NPM2100_GPIO_CONFIG_DRIVE 0x20U
#define NPM2100_GPIO_CONFIG_DEBOUNCE 0x40U

struct gpio_npm2100_config {
struct gpio_driver_config common;
struct i2c_dt_spec i2c;
};

struct gpio_npm2100_data {
struct gpio_driver_data common;
};

static int gpio_npm2100_port_get_raw(const struct device *dev, uint32_t *value)
{
const struct gpio_npm2100_config *config = dev->config;
uint8_t data;
int ret;

ret = i2c_reg_read_byte_dt(&config->i2c, NPM2100_GPIO_READ, &data);
if (ret < 0) {
return ret;
}

*value = data;

return 0;
}

static int gpio_npm2100_port_set_masked_raw(const struct device *dev, gpio_port_pins_t mask,
gpio_port_value_t value)
{
const struct gpio_npm2100_config *config = dev->config;
int ret = 0;

for (size_t idx = 0; idx < NPM2100_GPIO_PINS; idx++) {
if ((mask & BIT(idx)) != 0U) {
i2c_reg_write_byte_dt(&config->i2c, NPM2100_GPIO_OUTPUT + idx,
!!(value & BIT(idx)));
if (ret != 0U) {
return ret;
}
}
}

return ret;
}

static int gpio_npm2100_port_set_bits_raw(const struct device *dev, gpio_port_pins_t pins)
{
return gpio_npm2100_port_set_masked_raw(dev, pins, pins);
}

static int gpio_npm2100_port_clear_bits_raw(const struct device *dev, gpio_port_pins_t pins)
{
return gpio_npm2100_port_set_masked_raw(dev, pins, 0U);
}

static inline int gpio_npm2100_configure(const struct device *dev, gpio_pin_t pin,
gpio_flags_t flags)
{
const struct gpio_npm2100_config *config = dev->config;
uint8_t reg = 0U;

if (k_is_in_isr()) {
return -EWOULDBLOCK;
}

if (pin >= NPM2100_GPIO_PINS) {
return -EINVAL;
}

/* Set initial state if defined */
if ((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) != 0U) {
int ret = i2c_reg_write_byte_dt(&config->i2c, NPM2100_GPIO_OUTPUT + pin,
!!(flags & GPIO_OUTPUT_INIT_HIGH));
if (ret < 0) {
return ret;
}
}

/* Set pin configuration */
if ((flags & GPIO_INPUT) != 0U) {
reg |= NPM2100_GPIO_CONFIG_INPUT;
}
if ((flags & GPIO_OUTPUT) != 0U) {
reg |= NPM2100_GPIO_CONFIG_OUTPUT;
}
if ((flags & GPIO_SINGLE_ENDED) != 0U) {
reg |= NPM2100_GPIO_CONFIG_OPENDRAIN;
}
if ((flags & GPIO_PULL_UP) != 0U) {
reg |= NPM2100_GPIO_CONFIG_PULLUP;
}
if ((flags & GPIO_PULL_DOWN) != 0U) {
reg |= NPM2100_GPIO_CONFIG_PULLDOWN;
}
if ((flags & NPM2100_GPIO_DRIVE_HIGH) != 0U) {
reg |= NPM2100_GPIO_CONFIG_DRIVE;
}
if ((flags & NPM2100_GPIO_DEBOUNCE_ON) != 0U) {
reg |= NPM2100_GPIO_CONFIG_DEBOUNCE;
}

return i2c_reg_write_byte_dt(&config->i2c, NPM2100_GPIO_CONFIG + pin, reg);
}

static int gpio_npm2100_port_toggle_bits(const struct device *dev, gpio_port_pins_t pins)
{
int ret;
uint32_t value;

ret = gpio_npm2100_port_get_raw(dev, &value);
if (ret < 0) {
return ret;
}

return gpio_npm2100_port_set_masked_raw(dev, pins, ~value);
}

static DEVICE_API(gpio, gpio_npm2100_api) = {
.pin_configure = gpio_npm2100_configure,
.port_get_raw = gpio_npm2100_port_get_raw,
.port_set_masked_raw = gpio_npm2100_port_set_masked_raw,
.port_set_bits_raw = gpio_npm2100_port_set_bits_raw,
.port_clear_bits_raw = gpio_npm2100_port_clear_bits_raw,
.port_toggle_bits = gpio_npm2100_port_toggle_bits,
};

static int gpio_npm2100_init(const struct device *dev)
{
const struct gpio_npm2100_config *config = dev->config;

if (!i2c_is_ready_dt(&config->i2c)) {
return -ENODEV;
}

return 0;
}

#define GPIO_NPM2100_DEFINE(n) \
static const struct gpio_npm2100_config gpio_npm2100_config##n = { \
.common = \
{ \
.port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(n), \
}, \
.i2c = I2C_DT_SPEC_GET(DT_INST_PARENT(n))}; \
\
static struct gpio_npm2100_data gpio_npm2100_data##n; \
\
DEVICE_DT_INST_DEFINE(n, &gpio_npm2100_init, NULL, &gpio_npm2100_data##n, \
&gpio_npm2100_config##n, POST_KERNEL, \
CONFIG_GPIO_NPM2100_INIT_PRIORITY, &gpio_npm2100_api);

DT_INST_FOREACH_STATUS_OKAY(GPIO_NPM2100_DEFINE)
1 change: 1 addition & 0 deletions drivers/mfd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ zephyr_library_sources_ifdef(CONFIG_MFD_ADP5585 mfd_adp5585.c)
zephyr_library_sources_ifdef(CONFIG_MFD_MAX20335 mfd_max20335.c)
zephyr_library_sources_ifdef(CONFIG_MFD_NCT38XX mfd_nct38xx.c)
zephyr_library_sources_ifdef(CONFIG_MFD_NPM1300 mfd_npm1300.c)
zephyr_library_sources_ifdef(CONFIG_MFD_NPM2100 mfd_npm2100.c)
zephyr_library_sources_ifdef(CONFIG_MFD_NPM6001 mfd_npm6001.c)
zephyr_library_sources_ifdef(CONFIG_MFD_AXP192 mfd_axp192.c)
zephyr_library_sources_ifdef(CONFIG_MFD_AD559X mfd_ad559x.c)
Expand Down
1 change: 1 addition & 0 deletions drivers/mfd/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ source "drivers/mfd/Kconfig.max20335"
source "drivers/mfd/Kconfig.max31790"
source "drivers/mfd/Kconfig.nct38xx"
source "drivers/mfd/Kconfig.npm1300"
source "drivers/mfd/Kconfig.npm2100"
source "drivers/mfd/Kconfig.npm6001"
source "drivers/mfd/Kconfig.lpflexcomm"
source "drivers/mfd/Kconfig.tle9104"
Expand Down
10 changes: 10 additions & 0 deletions drivers/mfd/Kconfig.npm2100
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

config MFD_NPM2100
bool "nPM2100 PMIC multi-function device driver"
default y
depends on DT_HAS_NORDIC_NPM2100_ENABLED
select I2C
help
Enable the Nordic nPM2100 PMIC multi-function device driver
Loading
Loading