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
1513
1614LOG_MODULE_REGISTER (mfd_axp192 , CONFIG_MFD_LOG_LEVEL );
1715
16+ struct mfd_axp192_config {
17+ struct i2c_dt_spec i2c ;
18+ #ifdef CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED
19+ bool vbusen_disable ;
20+ #endif
21+ uint8_t reg_chip_id ;
22+ uint8_t vbus_config_reg ;
23+ uint8_t chip_id ;
24+ uint8_t val_vbusen_disable ;
25+ };
26+
27+ #ifdef CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED
28+
1829/* Chip ID value */
1930#define AXP192_CHIP_ID 0x03U
31+ #define AXP2101_CHIP_ID 0x4A
2032
2133/* Registers definitions */
2234#define AXP192_REG_CHIP_ID 0x03U
35+ #define AXP2101_REG_CHIP_ID 0x03U
2336
2437/* AXP192 GPIO register addresses */
2538#define AXP192_EXTEN_DCDC2_CONTROL_REG 0x10U
@@ -31,9 +44,11 @@ LOG_MODULE_REGISTER(mfd_axp192, CONFIG_MFD_LOG_LEVEL);
3144#define AXP192_GPIO012_PINVAL_REG 0x94U
3245#define AXP192_GPIO34_PINVAL_REG 0x96U
3346#define AXP192_GPIO012_PULLDOWN_REG 0x97U
47+ #define AXP2101_VBUS_CFG_REG 0x00U
3448
3549/* VBUS control reg values */
3650#define AXP192_VBUS_CFG_VAL_VBUSEN_DISABLE 0x80U
51+ #define AXP2101_VBUS_CFG_VAL_VBUSEN_DISABLE 0x00U
3752
3853/* GPIO function control parameters */
3954#define AXP192_GPIO012_FUNC_VAL_OUTPUT_OD 0x00U
@@ -97,11 +112,6 @@ LOG_MODULE_REGISTER(mfd_axp192, CONFIG_MFD_LOG_LEVEL);
97112#define AXP192_GPIO5_OUTPUT_VAL 0x04U
98113#define AXP192_GPIO5_OUTPUT_SHIFT 3U
99114
100- struct mfd_axp192_config {
101- struct i2c_dt_spec i2c ;
102- bool vbusen_disable ;
103- };
104-
105115struct mfd_axp192_data {
106116 const struct device * gpio_mask_used [AXP192_GPIO_MAX_NUM ];
107117 uint8_t gpio_mask_output ;
@@ -139,12 +149,12 @@ const struct mfd_axp192_func_reg_desc gpio_reg_desc[AXP192_GPIO_MAX_NUM] = {
139149 .mask = AXP192_GPIO4_FUNC_MASK ,
140150 },
141151};
152+ #endif /* CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED */
142153
143154static int mfd_axp192_init (const struct device * dev )
144155{
145156 const struct mfd_axp192_config * config = dev -> config ;
146157 uint8_t chip_id ;
147- uint8_t vbus_val ;
148158 int ret ;
149159
150160 LOG_DBG ("Initializing instance" );
@@ -155,29 +165,29 @@ static int mfd_axp192_init(const struct device *dev)
155165 }
156166
157167 /* Check if axp192 chip is available */
158- ret = i2c_reg_read_byte_dt (& config -> i2c , AXP192_REG_CHIP_ID , & chip_id );
168+ ret = i2c_reg_read_byte_dt (& config -> i2c , config -> reg_chip_id , & chip_id );
159169 if (ret < 0 ) {
160170 return ret ;
161171 }
162- if (chip_id != AXP192_CHIP_ID ) {
172+ if (chip_id != config -> chip_id ) {
163173 LOG_ERR ("Invalid Chip detected (%d)" , chip_id );
164174 return - EINVAL ;
165175 }
166176
177+ #ifdef CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED
167178 /* 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 );
179+ ret = i2c_reg_update_byte_dt (
180+ & config -> i2c , config -> vbus_config_reg , config -> val_vbusen_disable ,
181+ config -> vbusen_disable ? config -> val_vbusen_disable : 0 );
174182 if (ret < 0 ) {
175183 return ret ;
176184 }
185+ #endif
177186
178187 return 0 ;
179188}
180189
190+ #ifdef CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED
181191int mfd_axp192_gpio_func_get (const struct device * dev , uint8_t gpio , enum axp192_gpio_func * func )
182192{
183193 const struct mfd_axp192_config * config = dev -> config ;
@@ -605,16 +615,29 @@ int mfd_axp192_gpio_write_port(const struct device *dev, uint8_t value, uint8_t
605615
606616 return 0 ;
607617}
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), \
618+ #endif
619+
620+ #define MFD_AXP192_CONST_CONFIG (model ) \
621+ .reg_chip_id = AXP##model##_REG_CHIP_ID, \
622+ .vbus_config_reg = AXP##model##_VBUS_CFG_REG, \
623+ .chip_id = AXP##model##_CHIP_ID, \
624+ .val_vbusen_disable = AXP##model##_CHIP_ID,
625+
626+ #define MFD_AXP192_DEFINE (node , model ) \
627+ static const struct mfd_axp192_config config##node = { \
628+ .i2c = I2C_DT_SPEC_GET(node), \
629+ IF_ENABLED(CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED, \
630+ (.vbusen_disable = DT_PROP_OR(node, vbusen_disable, false),)) \
631+ MFD_AXP192_CONST_CONFIG(model) \
613632 }; \
614633 \
615- static struct mfd_axp192_data data##inst; \
634+ IF_ENABLED(CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED, \
635+ (static struct mfd_axp192_data data##node;)) \
616636 \
617- DEVICE_DT_INST_DEFINE(inst, mfd_axp192_init, NULL, &data##inst, &config##inst, \
618- POST_KERNEL, CONFIG_MFD_INIT_PRIORITY, NULL);
637+ DEVICE_DT_DEFINE(node, mfd_axp192_init, NULL, \
638+ COND_CODE_1(CONFIG_DT_HAS_X_POWERS_AXP192_ENABLED, \
639+ (&data##node), (NULL)), \
640+ &config##node, POST_KERNEL, CONFIG_MFD_INIT_PRIORITY, NULL);
619641
620- DT_INST_FOREACH_STATUS_OKAY (MFD_AXP192_DEFINE );
642+ DT_FOREACH_STATUS_OKAY_VARGS (x_powers_axp192 , MFD_AXP192_DEFINE , 192 );
643+ DT_FOREACH_STATUS_OKAY_VARGS (x_powers_axp2101 , MFD_AXP192_DEFINE , 2101 );
0 commit comments