@@ -39,6 +39,7 @@ struct i2c_ambiq_config {
39
39
#endif /* CONFIG_I2C_AMBIQ_BUS_RECOVERY */
40
40
uint32_t base ;
41
41
int size ;
42
+ int inst_idx ;
42
43
uint32_t bitrate ;
43
44
const struct pinctrl_dev_config * pcfg ;
44
45
ambiq_i2c_pwr_func_t pwr_func ;
@@ -54,7 +55,6 @@ struct i2c_ambiq_data {
54
55
struct k_sem transfer_sem ;
55
56
i2c_ambiq_callback_t callback ;
56
57
void * callback_data ;
57
- int inst_idx ;
58
58
uint32_t transfer_status ;
59
59
bool pm_policy_state_on ;
60
60
};
@@ -186,6 +186,7 @@ static int i2c_ambiq_write(const struct device *dev, struct i2c_msg *msg, uint16
186
186
static int i2c_ambiq_configure (const struct device * dev , uint32_t dev_config )
187
187
{
188
188
struct i2c_ambiq_data * data = dev -> data ;
189
+ const struct i2c_ambiq_config * cfg = dev -> config ;
189
190
190
191
if (!(I2C_MODE_CONTROLLER & dev_config )) {
191
192
return - EINVAL ;
@@ -206,7 +207,7 @@ static int i2c_ambiq_configure(const struct device *dev, uint32_t dev_config)
206
207
}
207
208
208
209
#ifdef CONFIG_I2C_AMBIQ_DMA
209
- data -> iom_cfg .pNBTxnBuf = i2c_dma_tcb_buf [data -> inst_idx ].buf ;
210
+ data -> iom_cfg .pNBTxnBuf = i2c_dma_tcb_buf [cfg -> inst_idx ].buf ;
210
211
data -> iom_cfg .ui32NBTxnBufLength = CONFIG_I2C_DMA_TCB_BUFFER_SIZE ;
211
212
#endif
212
213
@@ -343,9 +344,7 @@ static int i2c_ambiq_init(const struct device *dev)
343
344
344
345
data -> iom_cfg .eInterfaceMode = AM_HAL_IOM_I2C_MODE ;
345
346
346
- if (AM_HAL_STATUS_SUCCESS !=
347
- am_hal_iom_initialize ((config -> base - IOM0_BASE ) / config -> size ,
348
- & data -> iom_handler )) {
347
+ if (AM_HAL_STATUS_SUCCESS != am_hal_iom_initialize (config -> inst_idx , & data -> iom_handler )) {
349
348
LOG_ERR ("Fail to initialize I2C\n" );
350
349
return - ENXIO ;
351
350
}
@@ -438,19 +437,18 @@ static int i2c_ambiq_pm_action(const struct device *dev, enum pm_device_action a
438
437
}; \
439
438
static struct i2c_ambiq_data i2c_ambiq_data##n = { \
440
439
.bus_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.bus_sem, 1, 1), \
441
- .transfer_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.transfer_sem, 0, 1), \
442
- .inst_idx = n, \
443
- }; \
440
+ .transfer_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.transfer_sem, 0, 1)}; \
444
441
static const struct i2c_ambiq_config i2c_ambiq_config##n = { \
445
442
.base = DT_INST_REG_ADDR(n), \
446
443
.size = DT_INST_REG_SIZE(n), \
444
+ .inst_idx = (DT_INST_REG_ADDR(n) - IOM0_BASE) / (IOM1_BASE - IOM0_BASE), \
447
445
.bitrate = DT_INST_PROP(n, clock_frequency), \
448
446
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
449
447
.irq_config_func = i2c_irq_config_func_##n, \
450
448
.pwr_func = pwr_on_ambiq_i2c_##n, \
451
449
IF_ENABLED(CONFIG_I2C_AMBIQ_BUS_RECOVERY, \
452
450
(.scl = GPIO_DT_SPEC_INST_GET_OR(n, scl_gpios, {0}),\
453
- .sda = GPIO_DT_SPEC_INST_GET_OR(n, sda_gpios, {0}),)) }; \
451
+ .sda = GPIO_DT_SPEC_INST_GET_OR(n, sda_gpios, {0}),)) }; \
454
452
PM_DEVICE_DT_INST_DEFINE(n, i2c_ambiq_pm_action); \
455
453
I2C_DEVICE_DT_INST_DEFINE(n, i2c_ambiq_init, PM_DEVICE_DT_INST_GET(n), &i2c_ambiq_data##n, \
456
454
&i2c_ambiq_config##n, POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, \
0 commit comments