@@ -57,6 +57,7 @@ struct i2c_ambiq_config {
57
57
#endif /* CONFIG_I2C_AMBIQ_BUS_RECOVERY */
58
58
uint32_t base ;
59
59
int size ;
60
+ int inst_idx ;
60
61
uint32_t bitrate ;
61
62
const struct pinctrl_dev_config * pcfg ;
62
63
ambiq_i2c_pwr_func_t pwr_func ;
@@ -72,7 +73,6 @@ struct i2c_ambiq_data {
72
73
struct k_sem transfer_sem ;
73
74
i2c_ambiq_callback_t callback ;
74
75
void * callback_data ;
75
- int inst_idx ;
76
76
uint32_t transfer_status ;
77
77
bool pm_policy_state_on ;
78
78
};
@@ -251,6 +251,7 @@ static int i2c_ambiq_write(const struct device *dev, struct i2c_msg *msg, uint16
251
251
static int i2c_ambiq_configure (const struct device * dev , uint32_t dev_config )
252
252
{
253
253
struct i2c_ambiq_data * data = dev -> data ;
254
+ const struct i2c_ambiq_config * cfg = dev -> config ;
254
255
255
256
if (!(I2C_MODE_CONTROLLER & dev_config )) {
256
257
return - EINVAL ;
@@ -271,7 +272,7 @@ static int i2c_ambiq_configure(const struct device *dev, uint32_t dev_config)
271
272
}
272
273
273
274
#ifdef CONFIG_I2C_AMBIQ_DMA
274
- data -> iom_cfg .pNBTxnBuf = i2c_dma_tcb_buf [data -> inst_idx ].buf ;
275
+ data -> iom_cfg .pNBTxnBuf = i2c_dma_tcb_buf [cfg -> inst_idx ].buf ;
275
276
data -> iom_cfg .ui32NBTxnBufLength = CONFIG_I2C_DMA_TCB_BUFFER_SIZE ;
276
277
#endif
277
278
@@ -414,8 +415,7 @@ static int i2c_ambiq_init(const struct device *dev)
414
415
415
416
data -> iom_cfg .eInterfaceMode = AM_HAL_IOM_I2C_MODE ;
416
417
417
- if (AM_HAL_STATUS_SUCCESS !=
418
- am_hal_iom_initialize ((config -> base - IOM0_BASE ) / config -> size , & data -> iom_handler )) {
418
+ if (AM_HAL_STATUS_SUCCESS != am_hal_iom_initialize (config -> inst_idx , & data -> iom_handler )) {
419
419
LOG_ERR ("Fail to initialize I2C\n" );
420
420
return - ENXIO ;
421
421
}
@@ -508,19 +508,18 @@ static int i2c_ambiq_pm_action(const struct device *dev, enum pm_device_action a
508
508
}; \
509
509
static struct i2c_ambiq_data i2c_ambiq_data##n = { \
510
510
.bus_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.bus_sem, 1, 1), \
511
- .transfer_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.transfer_sem, 0, 1), \
512
- .inst_idx = n, \
513
- }; \
511
+ .transfer_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.transfer_sem, 0, 1)}; \
514
512
static const struct i2c_ambiq_config i2c_ambiq_config##n = { \
515
513
.base = DT_INST_REG_ADDR(n), \
516
514
.size = DT_INST_REG_SIZE(n), \
515
+ .inst_idx = (DT_INST_REG_ADDR(n) - IOM0_BASE) / (IOM1_BASE - IOM0_BASE), \
517
516
.bitrate = DT_INST_PROP(n, clock_frequency), \
518
517
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
519
518
.irq_config_func = i2c_irq_config_func_##n, \
520
519
.pwr_func = pwr_on_ambiq_i2c_##n, \
521
- IF_ENABLED(CONFIG_I2C_AMBIQ_BUS_RECOVERY, \
522
- (.scl = GPIO_DT_SPEC_INST_GET_OR(n, scl_gpios, {0}),\
523
- .sda = GPIO_DT_SPEC_INST_GET_OR(n, sda_gpios, {0}),)) }; \
520
+ IF_ENABLED(CONFIG_I2C_AMBIQ_BUS_RECOVERY, \
521
+ (.scl = GPIO_DT_SPEC_INST_GET_OR(n, scl_gpios, {0}), \
522
+ .sda = GPIO_DT_SPEC_INST_GET_OR(n, sda_gpios, {0}),)) }; \
524
523
PM_DEVICE_DT_INST_DEFINE(n, i2c_ambiq_pm_action); \
525
524
I2C_DEVICE_DT_INST_DEFINE(n, i2c_ambiq_init, PM_DEVICE_DT_INST_GET(n), &i2c_ambiq_data##n, \
526
525
&i2c_ambiq_config##n, POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, \
0 commit comments