@@ -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
}
@@ -420,40 +419,39 @@ static int i2c_ambiq_pm_action(const struct device *dev, enum pm_device_action a
420
419
}
421
420
#endif /* CONFIG_PM_DEVICE */
422
421
423
- #define AMBIQ_I2C_DEFINE (n ) \
422
+ #define AMBIQ_I2C_DEFINE (n ) \
424
423
PINCTRL_DT_INST_DEFINE(n); \
425
424
static int pwr_on_ambiq_i2c_##n(void) \
426
425
{ \
427
- uint32_t addr = DT_REG_ADDR(DT_INST_PHANDLE(n, ambiq_pwrcfg)) + \
428
- DT_INST_PHA(n, ambiq_pwrcfg, offset); \
429
- sys_write32((sys_read32(addr) | DT_INST_PHA(n, ambiq_pwrcfg, mask)), addr); \
430
- k_busy_wait(PWRCTRL_MAX_WAIT_US); \
431
- return 0; \
426
+ uint32_t addr = DT_REG_ADDR(DT_INST_PHANDLE(n, ambiq_pwrcfg)) + \
427
+ DT_INST_PHA(n, ambiq_pwrcfg, offset); \
428
+ sys_write32((sys_read32(addr) | DT_INST_PHA(n, ambiq_pwrcfg, mask)), addr); \
429
+ k_busy_wait(PWRCTRL_MAX_WAIT_US); \
430
+ return 0; \
432
431
} \
433
432
static void i2c_irq_config_func_##n(void) \
434
433
{ \
435
- IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), i2c_ambiq_isr, \
436
- DEVICE_DT_INST_GET(n), 0); \
437
- irq_enable(DT_INST_IRQN(n)); \
434
+ IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), i2c_ambiq_isr, \
435
+ DEVICE_DT_INST_GET(n), 0); \
436
+ irq_enable(DT_INST_IRQN(n)); \
438
437
}; \
439
438
static struct i2c_ambiq_data i2c_ambiq_data##n = { \
440
- .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
- }; \
439
+ .bus_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.bus_sem, 1, 1), \
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
- .base = DT_INST_REG_ADDR(n), \
446
- .size = DT_INST_REG_SIZE(n), \
447
- .bitrate = DT_INST_PROP(n, clock_frequency), \
448
- .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
449
- .irq_config_func = i2c_irq_config_func_##n, \
450
- .pwr_func = pwr_on_ambiq_i2c_##n, \
451
- IF_ENABLED(CONFIG_I2C_AMBIQ_BUS_RECOVERY, \
452
- (.scl = GPIO_DT_SPEC_INST_GET_OR(n, scl_gpios, {0}),\
453
- .sda = GPIO_DT_SPEC_INST_GET_OR(n, sda_gpios, {0}),)) }; \
442
+ .base = DT_INST_REG_ADDR(n), \
443
+ .size = DT_INST_REG_SIZE(n), \
444
+ .inst_idx = (DT_INST_REG_ADDR(n) - IOM0_BASE) / (IOM1_BASE - IOM0_BASE), \
445
+ .bitrate = DT_INST_PROP(n, clock_frequency), \
446
+ .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
447
+ .irq_config_func = i2c_irq_config_func_##n, \
448
+ .pwr_func = pwr_on_ambiq_i2c_##n, \
449
+ IF_ENABLED(CONFIG_I2C_AMBIQ_BUS_RECOVERY, \
450
+ (.scl = GPIO_DT_SPEC_INST_GET_OR(n, scl_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
- &i2c_ambiq_config##n, POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, \
454
+ &i2c_ambiq_config##n, POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, \
457
455
&i2c_ambiq_driver_api);
458
456
459
457
DT_INST_FOREACH_STATUS_OKAY (AMBIQ_I2C_DEFINE )
0 commit comments