33 * SPDX-License-Identifier: Apache-2.0
44 */
55
6- #define DT_DRV_COMPAT x_powers_axp192
7-
86#include <errno.h>
97#include <stdbool.h>
108
119#include <zephyr/drivers/mfd/axp192.h>
10+ #include <zephyr/drivers/mfd/axp2101.h>
1211#include <zephyr/drivers/i2c.h>
1312#include <zephyr/sys/util.h>
1413#include <zephyr/logging/log.h>
1514
1615LOG_MODULE_REGISTER (mfd_axp192 , CONFIG_MFD_LOG_LEVEL );
1716
17+ struct mfd_axp192_config {
18+ struct i2c_dt_spec i2c ;
19+ #ifdef CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED
20+ bool vbusen_disable ;
21+ #endif
22+ uint8_t reg_chip_id ;
23+ uint8_t vbus_config_reg ;
24+ uint8_t chip_id ;
25+ uint8_t val_vbusen_disable ;
26+ };
27+
28+ #ifdef CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED
29+
1830/* Chip ID value */
1931#define AXP192_CHIP_ID 0x03U
2032
@@ -97,11 +109,6 @@ LOG_MODULE_REGISTER(mfd_axp192, CONFIG_MFD_LOG_LEVEL);
97109#define AXP192_GPIO5_OUTPUT_VAL 0x04U
98110#define AXP192_GPIO5_OUTPUT_SHIFT 3U
99111
100- struct mfd_axp192_config {
101- struct i2c_dt_spec i2c ;
102- bool vbusen_disable ;
103- };
104-
105112struct mfd_axp192_data {
106113 const struct device * gpio_mask_used [AXP192_GPIO_MAX_NUM ];
107114 uint8_t gpio_mask_output ;
@@ -139,12 +146,12 @@ const struct mfd_axp192_func_reg_desc gpio_reg_desc[AXP192_GPIO_MAX_NUM] = {
139146 .mask = AXP192_GPIO4_FUNC_MASK ,
140147 },
141148};
149+ #endif /* CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED */
142150
143151static int mfd_axp192_init (const struct device * dev )
144152{
145153 const struct mfd_axp192_config * config = dev -> config ;
146154 uint8_t chip_id ;
147- uint8_t vbus_val ;
148155 int ret ;
149156
150157 LOG_DBG ("Initializing instance" );
@@ -155,29 +162,29 @@ static int mfd_axp192_init(const struct device *dev)
155162 }
156163
157164 /* Check if axp192 chip is available */
158- ret = i2c_reg_read_byte_dt (& config -> i2c , AXP192_REG_CHIP_ID , & chip_id );
165+ ret = i2c_reg_read_byte_dt (& config -> i2c , config -> reg_chip_id , & chip_id );
159166 if (ret < 0 ) {
160167 return ret ;
161168 }
162- if (chip_id != AXP192_CHIP_ID ) {
169+ if (chip_id != config -> chip_id ) {
163170 LOG_ERR ("Invalid Chip detected (%d)" , chip_id );
164171 return - EINVAL ;
165172 }
166173
174+ #ifdef CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED
167175 /* Disable N_VBUSEN */
168- vbus_val = 0 ;
169- if (config -> vbusen_disable ) {
170- vbus_val = AXP192_VBUS_CFG_VAL_VBUSEN_DISABLE ;
171- }
172- ret = i2c_reg_update_byte_dt (& config -> i2c , AXP192_VBUS_CFG_REG ,
173- AXP192_VBUS_CFG_VAL_VBUSEN_DISABLE , vbus_val );
176+ ret = i2c_reg_update_byte_dt (
177+ & config -> i2c , config -> vbus_config_reg , config -> val_vbusen_disable ,
178+ config -> vbusen_disable ? config -> val_vbusen_disable : 0 );
174179 if (ret < 0 ) {
175180 return ret ;
176181 }
182+ #endif
177183
178184 return 0 ;
179185}
180186
187+ #ifdef CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED
181188int mfd_axp192_gpio_func_get (const struct device * dev , uint8_t gpio , enum axp192_gpio_func * func )
182189{
183190 const struct mfd_axp192_config * config = dev -> config ;
@@ -605,16 +612,29 @@ int mfd_axp192_gpio_write_port(const struct device *dev, uint8_t value, uint8_t
605612
606613 return 0 ;
607614}
608-
609- #define MFD_AXP192_DEFINE (inst ) \
610- static const struct mfd_axp192_config config##inst = { \
611- .i2c = I2C_DT_SPEC_INST_GET(inst), \
612- .vbusen_disable = DT_INST_PROP_OR(inst, vbusen_disable, false), \
615+ #endif
616+
617+ #define MFD_AXP192_CONST_CONFIG (model ) \
618+ .reg_chip_id = AXP##model##_REG_CHIP_ID, \
619+ .vbus_config_reg = AXP##model##_VBUS_CFG_REG, \
620+ .chip_id = AXP##model##_CHIP_ID, \
621+ .val_vbusen_disable = AXP##model##_CHIP_ID,
622+
623+ #define MFD_AXP192_DEFINE (node , model ) \
624+ static const struct mfd_axp192_config config##node = { \
625+ .i2c = I2C_DT_SPEC_GET(node), \
626+ IF_ENABLED(CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED, \
627+ (.vbusen_disable = DT_PROP_OR(node, vbusen_disable, false),)) \
628+ MFD_AXP192_CONST_CONFIG(model) \
613629 }; \
614630 \
615- static struct mfd_axp192_data data##inst; \
631+ IF_ENABLED(CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED, \
632+ (static struct mfd_axp192_data data##node;)) \
616633 \
617- DEVICE_DT_INST_DEFINE(inst, mfd_axp192_init, NULL, &data##inst, &config##inst, \
618- POST_KERNEL, CONFIG_MFD_INIT_PRIORITY, NULL);
634+ DEVICE_DT_DEFINE(node, mfd_axp192_init, NULL, \
635+ COND_CODE_1(CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED, \
636+ (&data##node), (NULL)), \
637+ &config##node, POST_KERNEL, CONFIG_MFD_INIT_PRIORITY, NULL);
619638
620- DT_INST_FOREACH_STATUS_OKAY (MFD_AXP192_DEFINE );
639+ DT_FOREACH_STATUS_OKAY_VARGS (x_powers_axp192 , MFD_AXP192_DEFINE , 192 );
640+ DT_FOREACH_STATUS_OKAY_VARGS (x_powers_axp2101 , MFD_AXP192_DEFINE , 2101 );
0 commit comments