Skip to content
Open
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/mfd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ zephyr_library_sources_ifdef(CONFIG_MFD_MCHP_SAM_FLEXCOM mfd_mchp_sam_flexcom.c)
zephyr_library_sources_ifdef(CONFIG_MFD_PF1550 mfd_pf1550.c)
zephyr_library_sources_ifdef(CONFIG_MFD_PCA9422 mfd_pca9422.c)
zephyr_library_sources_ifdef(CONFIG_MFD_MOTOROLA_MC146818 mfd_mc146818.c)
zephyr_library_sources_ifdef(CONFIG_MFD_MAX2221X mfd_max2221x.c)
1 change: 1 addition & 0 deletions drivers/mfd/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ source "drivers/mfd/Kconfig.bd8lb600fs"
source "drivers/mfd/Kconfig.ds3231"
source "drivers/mfd/Kconfig.max20335"
source "drivers/mfd/Kconfig.max22017"
source "drivers/mfd/Kconfig.max2221x"
source "drivers/mfd/Kconfig.max31790"
source "drivers/mfd/Kconfig.maxq10xx"
source "drivers/mfd/Kconfig.mchp_sam"
Expand Down
10 changes: 10 additions & 0 deletions drivers/mfd/Kconfig.max2221x
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2025 Analog Devices Inc.
# SPDX-License-Identifier: Apache-2.0

config MFD_MAX2221X
bool "Analog Devices MAX2221X multi-function device driver"
default y
depends on SPI
depends on DT_HAS_ADI_MAX2221X_ENABLED
help
Enable the driver for the Analog Devices MAX2221X multi-function device driver
135 changes: 135 additions & 0 deletions drivers/mfd/mfd_max2221x.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright (c) 2025 Analog Devices Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

#define DT_DRV_COMPAT adi_max2221x

#include <zephyr/drivers/mfd/max2221x.h>
#include <zephyr/device.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(MFD_MAX2221X, CONFIG_MFD_LOG_LEVEL);

struct mfd_max2221x_config {
struct spi_dt_spec spi;
};

int max2221x_reg_read(const struct device *dev, uint8_t addr, uint16_t *value)
{
int ret;
const struct mfd_max2221x_config *config = dev->config;
uint8_t rxbuffer[3] = {0};
size_t rx_len = 3;

*value = 0;
addr = FIELD_PREP(MAX2221X_SPI_TRANS_ADDR, addr) | FIELD_PREP(MAX2221X_SPI_TRANS_DIR, 0);

const struct spi_buf txb[] = {
{
.buf = &addr,
.len = 1,
},
};

const struct spi_buf rxb[] = {
{
.buf = rxbuffer,
.len = rx_len,
},
};

struct spi_buf_set tx = {
.buffers = txb,
.count = ARRAY_SIZE(txb),
};

struct spi_buf_set rx = {
.buffers = rxb,
.count = ARRAY_SIZE(rxb),
};

ret = spi_transceive_dt(&config->spi, &tx, &rx);
if (ret) {
return ret;
}

memset(rxbuffer, 0, sizeof(rxbuffer));

ret = spi_transceive_dt(&config->spi, &tx, &rx);
if (ret) {
return ret;
}

*value = sys_be16_to_cpu(*(uint16_t *)&rxbuffer[1]);
return 0;
}

int max2221x_reg_write(const struct device *dev, uint8_t addr, uint16_t value)
{
const struct mfd_max2221x_config *config = dev->config;

addr = FIELD_PREP(MAX2221X_SPI_TRANS_ADDR, addr) | FIELD_PREP(MAX2221X_SPI_TRANS_DIR, 1);
value = sys_cpu_to_be16(value);

const struct spi_buf buf[] = {
{
.buf = &addr,
.len = 1,
},
{
.buf = &value,
.len = 2,
},
};

struct spi_buf_set tx = {
.buffers = buf,
.count = ARRAY_SIZE(buf),
};

return spi_write_dt(&config->spi, &tx);
}

int max2221x_reg_update(const struct device *dev, uint8_t addr, uint16_t mask, uint16_t val)
{
uint16_t tmp;
int ret;

ret = max2221x_reg_read(dev, addr, &tmp);
if (ret) {
return ret;
}

tmp = (tmp & ~mask) | FIELD_PREP(mask, val);

return max2221x_reg_write(dev, addr, tmp);
}

static int max2221x_init(const struct device *dev)
{
const struct mfd_max2221x_config *config = dev->config;

if (!spi_is_ready_dt(&config->spi)) {
LOG_ERR("SPI device %s not ready", config->spi.bus->name);
return -ENODEV;
}

return 0;
}

#define MAX2221X_DEFINE(inst) \
static const struct mfd_max2221x_config mfd_max2221x_config_##inst = { \
.spi = SPI_DT_SPEC_INST_GET(inst, SPI_WORD_SET(8) | SPI_TRANSFER_MSB, 0), \
}; \
\
DEVICE_DT_INST_DEFINE(inst, max2221x_init, NULL, NULL, &mfd_max2221x_config_##inst, \
POST_KERNEL, CONFIG_MFD_INIT_PRIORITY, NULL);

DT_INST_FOREACH_STATUS_OKAY(MAX2221X_DEFINE)
1 change: 1 addition & 0 deletions drivers/misc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ add_subdirectory_ifdef(CONFIG_RENESAS_RX_DTC renesas_rx_dtc)
add_subdirectory_ifdef(CONFIG_RENESAS_RX_EXTERNAL_INTERRUPT renesas_rx_external_interrupt)
add_subdirectory_ifdef(CONFIG_NXP_RTXXX_DSP_CTRL nxp_rtxxx_dsp_ctrl)
add_subdirectory_ifdef(CONFIG_STM32N6_AXISRAM stm32n6_axisram)
add_subdirectory_ifdef(CONFIG_MISC_MAX2221X max2221x)

add_subdirectory(interconn)
1 change: 1 addition & 0 deletions drivers/misc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ source "drivers/misc/renesas_rx_external_interrupt/Kconfig"
source "drivers/misc/nxp_rtxxx_dsp_ctrl/Kconfig"
source "drivers/misc/stm32n6_axisram/Kconfig"
source "drivers/misc/nxp_inputmux/Kconfig"
source "drivers/misc/max2221x/Kconfig"

endmenu
5 changes: 5 additions & 0 deletions drivers/misc/max2221x/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-License-Identifier: Apache-2.0

zephyr_library()

zephyr_library_sources(max2221x.c)
24 changes: 24 additions & 0 deletions drivers/misc/max2221x/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2025 Analog Devices Inc.
# SPDX-License-Identifier: Apache-2.0

config MISC_MAX2221X
bool "Analog Devices MAX22216/MAX22217 Misc driver"
default y
depends on DT_HAS_ADI_MAX2221X_MISC_ENABLED
select MFD
help
Enable driver for MAX2221X MISC.

if MISC_MAX2221X

config MISC_MAX2221X_INIT_PRIORITY
int "Init priority"
default 90
help
Analog Devices MAX22216/MAX22217 Misc driver initialization priority.

module = MISC_MAX2221X
module-str = misc_max2221x
source "subsys/logging/Kconfig.template.log_config"

endif # MISC_MAX2221X
Loading