@@ -54,6 +54,7 @@ struct i2c_ambiq_config {
54
54
#endif /* CONFIG_I2C_AMBIQ_BUS_RECOVERY */
55
55
uint32_t base ;
56
56
int size ;
57
+ int inst_idx ;
57
58
uint32_t bitrate ;
58
59
const struct pinctrl_dev_config * pcfg ;
59
60
void (* irq_config_func )(void );
@@ -68,7 +69,6 @@ struct i2c_ambiq_data {
68
69
struct k_sem transfer_sem ;
69
70
i2c_ambiq_callback_t callback ;
70
71
void * callback_data ;
71
- int inst_idx ;
72
72
uint32_t transfer_status ;
73
73
bool pm_policy_state_on ;
74
74
};
@@ -247,6 +247,7 @@ static int i2c_ambiq_write(const struct device *dev, struct i2c_msg *msg, uint16
247
247
static int i2c_ambiq_configure (const struct device * dev , uint32_t dev_config )
248
248
{
249
249
struct i2c_ambiq_data * data = dev -> data ;
250
+ const struct i2c_ambiq_config * cfg = dev -> config ;
250
251
251
252
if (!(I2C_MODE_CONTROLLER & dev_config )) {
252
253
return - EINVAL ;
@@ -267,7 +268,7 @@ static int i2c_ambiq_configure(const struct device *dev, uint32_t dev_config)
267
268
}
268
269
269
270
#ifdef CONFIG_I2C_AMBIQ_DMA
270
- data -> iom_cfg .pNBTxnBuf = i2c_dma_tcb_buf [data -> inst_idx ].buf ;
271
+ data -> iom_cfg .pNBTxnBuf = i2c_dma_tcb_buf [cfg -> inst_idx ].buf ;
271
272
data -> iom_cfg .ui32NBTxnBufLength = CONFIG_I2C_DMA_TCB_BUFFER_SIZE ;
272
273
#endif
273
274
@@ -410,8 +411,7 @@ static int i2c_ambiq_init(const struct device *dev)
410
411
411
412
data -> iom_cfg .eInterfaceMode = AM_HAL_IOM_I2C_MODE ;
412
413
413
- if (AM_HAL_STATUS_SUCCESS !=
414
- am_hal_iom_initialize ((config -> base - IOM0_BASE ) / config -> size , & data -> iom_handler )) {
414
+ if (AM_HAL_STATUS_SUCCESS != am_hal_iom_initialize (config -> inst_idx , & data -> iom_handler )) {
415
415
LOG_ERR ("Fail to initialize I2C\n" );
416
416
return - ENXIO ;
417
417
}
@@ -496,18 +496,17 @@ static int i2c_ambiq_pm_action(const struct device *dev, enum pm_device_action a
496
496
}; \
497
497
static struct i2c_ambiq_data i2c_ambiq_data##n = { \
498
498
.bus_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.bus_sem, 1, 1), \
499
- .transfer_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.transfer_sem, 0, 1), \
500
- .inst_idx = n, \
501
- }; \
499
+ .transfer_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.transfer_sem, 0, 1)}; \
502
500
static const struct i2c_ambiq_config i2c_ambiq_config##n = { \
503
501
.base = DT_INST_REG_ADDR(n), \
504
502
.size = DT_INST_REG_SIZE(n), \
503
+ .inst_idx = (DT_INST_REG_ADDR(n) - IOM0_BASE) / (IOM1_BASE - IOM0_BASE), \
505
504
.bitrate = DT_INST_PROP(n, clock_frequency), \
506
505
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
507
506
.irq_config_func = i2c_irq_config_func_##n, \
508
- IF_ENABLED(CONFIG_I2C_AMBIQ_BUS_RECOVERY, \
509
- (.scl = GPIO_DT_SPEC_INST_GET_OR(n, scl_gpios, {0}),\
510
- .sda = GPIO_DT_SPEC_INST_GET_OR(n, sda_gpios, {0}),)) }; \
507
+ IF_ENABLED(CONFIG_I2C_AMBIQ_BUS_RECOVERY, \
508
+ (.scl = GPIO_DT_SPEC_INST_GET_OR(n, scl_gpios, {0}), \
509
+ .sda = GPIO_DT_SPEC_INST_GET_OR(n, sda_gpios, {0}),)) }; \
511
510
PM_DEVICE_DT_INST_DEFINE(n, i2c_ambiq_pm_action); \
512
511
I2C_DEVICE_DT_INST_DEFINE(n, i2c_ambiq_init, PM_DEVICE_DT_INST_GET(n), &i2c_ambiq_data##n, \
513
512
&i2c_ambiq_config##n, POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, \
0 commit comments