Skip to content

Commit 734adf5

Browse files
CkovMkcarlescufi
authored andcommitted
driver: mucx_lpi2c: enable runtime mmio configuration
Enable runtime mmio configuration in mcux_lpi2c driver. The fsl_lpi2c driver relies on physical address to determine instance number. So mmap flag 'K_MEM_DIRECT_MAP' is required. Signed-off-by: Chekhov Ma <[email protected]>
1 parent ef867e4 commit 734adf5

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

drivers/i2c/i2c_mcux_lpi2c.c

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ LOG_MODULE_REGISTER(mcux_lpi2c);
3232
*/
3333
#define SCAN_DELAY_US(baudrate) (12 * USEC_PER_SEC / baudrate)
3434

35+
/* Required by DEVICE_MMIO_NAMED_* macros */
36+
#define DEV_CFG(_dev) \
37+
((const struct mcux_lpi2c_config *)(_dev)->config)
38+
#define DEV_DATA(_dev) ((struct mcux_lpi2c_data *)(_dev)->data)
39+
3540
struct mcux_lpi2c_config {
36-
LPI2C_Type *base;
41+
DEVICE_MMIO_NAMED_ROM(reg_base);
3742
const struct device *clock_dev;
3843
clock_control_subsys_t clock_subsys;
3944
void (*irq_config_func)(const struct device *dev);
@@ -47,6 +52,7 @@ struct mcux_lpi2c_config {
4752
};
4853

4954
struct mcux_lpi2c_data {
55+
DEVICE_MMIO_NAMED_RAM(reg_base);
5056
lpi2c_master_handle_t handle;
5157
struct k_sem lock;
5258
struct k_sem device_sync_sem;
@@ -66,7 +72,7 @@ static int mcux_lpi2c_configure(const struct device *dev,
6672
{
6773
const struct mcux_lpi2c_config *config = dev->config;
6874
struct mcux_lpi2c_data *data = dev->data;
69-
LPI2C_Type *base = config->base;
75+
LPI2C_Type *base = (LPI2C_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
7076
uint32_t clock_freq;
7177
uint32_t baudrate;
7278
int ret;
@@ -138,11 +144,11 @@ static uint32_t mcux_lpi2c_convert_flags(int msg_flags)
138144
}
139145

140146
static int mcux_lpi2c_transfer(const struct device *dev, struct i2c_msg *msgs,
141-
uint8_t num_msgs, uint16_t addr)
147+
uint8_t num_msgs, uint16_t addr)
142148
{
143149
const struct mcux_lpi2c_config *config = dev->config;
144150
struct mcux_lpi2c_data *data = dev->data;
145-
LPI2C_Type *base = config->base;
151+
LPI2C_Type *base = (LPI2C_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
146152
lpi2c_master_transfer_t transfer;
147153
status_t status;
148154
int ret = 0;
@@ -306,6 +312,7 @@ static void mcux_lpi2c_slave_irq_handler(const struct device *dev)
306312
{
307313
const struct mcux_lpi2c_config *config = dev->config;
308314
struct mcux_lpi2c_data *data = dev->data;
315+
LPI2C_Type *base = (LPI2C_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
309316
const struct i2c_target_callbacks *target_cb = data->target_cfg->callbacks;
310317
int ret;
311318
uint32_t flags;
@@ -319,11 +326,11 @@ static void mcux_lpi2c_slave_irq_handler(const struct device *dev)
319326
* every byte. For these reason, we handle the LPI2C IRQ
320327
* directly.
321328
*/
322-
flags = LPI2C_SlaveGetStatusFlags(config->base);
329+
flags = LPI2C_SlaveGetStatusFlags(base);
323330

324331
if (flags & kLPI2C_SlaveAddressValidFlag) {
325332
/* Read Slave address to clear flag */
326-
LPI2C_SlaveGetReceivedAddress(config->base);
333+
LPI2C_SlaveGetReceivedAddress(base);
327334
data->first_tx = true;
328335
/* Reset to sending ACK, in case we NAK'ed before */
329336
data->send_ack = true;
@@ -384,26 +391,27 @@ static void mcux_lpi2c_slave_irq_handler(const struct device *dev)
384391
}
385392

386393
if (flags & kLPI2C_SlaveStopDetectFlag) {
387-
LPI2C_SlaveClearStatusFlags(config->base, flags);
394+
LPI2C_SlaveClearStatusFlags(base, flags);
388395
if (target_cb->stop) {
389396
target_cb->stop(data->target_cfg);
390397
}
391398
}
392399

393400
if (flags & kLPI2C_SlaveTransmitAckFlag) {
394-
LPI2C_SlaveTransmitAck(config->base, data->send_ack);
401+
LPI2C_SlaveTransmitAck(base, data->send_ack);
395402
}
396403
}
397404

398405
static int mcux_lpi2c_target_register(const struct device *dev,
399-
struct i2c_target_config *target_config)
406+
struct i2c_target_config *target_config)
400407
{
401408
const struct mcux_lpi2c_config *config = dev->config;
402409
struct mcux_lpi2c_data *data = dev->data;
410+
LPI2C_Type *base = (LPI2C_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
403411
lpi2c_slave_config_t slave_config;
404412
uint32_t clock_freq;
405413

406-
LPI2C_MasterDeinit(config->base);
414+
LPI2C_MasterDeinit(base);
407415

408416
/* Get the clock frequency */
409417
if (clock_control_get_rate(config->clock_dev, config->clock_subsys,
@@ -430,11 +438,11 @@ static int mcux_lpi2c_target_register(const struct device *dev,
430438
* this behavior may cause issues with some I2C controllers.
431439
*/
432440
slave_config.sclStall.enableAck = true;
433-
LPI2C_SlaveInit(config->base, &slave_config, clock_freq);
441+
LPI2C_SlaveInit(base, &slave_config, clock_freq);
434442
/* Clear all flags. */
435-
LPI2C_SlaveClearStatusFlags(config->base, (uint32_t)kLPI2C_SlaveClearFlags);
443+
LPI2C_SlaveClearStatusFlags(base, (uint32_t)kLPI2C_SlaveClearFlags);
436444
/* Enable interrupt */
437-
LPI2C_SlaveEnableInterrupts(config->base,
445+
LPI2C_SlaveEnableInterrupts(base,
438446
(kLPI2C_SlaveTxReadyFlag |
439447
kLPI2C_SlaveRxReadyFlag |
440448
kLPI2C_SlaveStopDetectFlag |
@@ -448,6 +456,7 @@ static int mcux_lpi2c_target_unregister(const struct device *dev,
448456
{
449457
const struct mcux_lpi2c_config *config = dev->config;
450458
struct mcux_lpi2c_data *data = dev->data;
459+
LPI2C_Type *base = (LPI2C_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
451460

452461
if (!data->target_attached) {
453462
return -EINVAL;
@@ -456,17 +465,16 @@ static int mcux_lpi2c_target_unregister(const struct device *dev,
456465
data->target_cfg = NULL;
457466
data->target_attached = false;
458467

459-
LPI2C_SlaveDeinit(config->base);
468+
LPI2C_SlaveDeinit(base);
460469

461470
return 0;
462471
}
463472
#endif /* CONFIG_I2C_TARGET */
464473

465474
static void mcux_lpi2c_isr(const struct device *dev)
466475
{
467-
const struct mcux_lpi2c_config *config = dev->config;
468476
struct mcux_lpi2c_data *data = dev->data;
469-
LPI2C_Type *base = config->base;
477+
LPI2C_Type *base = (LPI2C_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
470478

471479
#ifdef CONFIG_I2C_TARGET
472480
if (data->target_attached) {
@@ -481,11 +489,15 @@ static int mcux_lpi2c_init(const struct device *dev)
481489
{
482490
const struct mcux_lpi2c_config *config = dev->config;
483491
struct mcux_lpi2c_data *data = dev->data;
484-
LPI2C_Type *base = config->base;
492+
LPI2C_Type *base;
485493
uint32_t clock_freq, bitrate_cfg;
486494
lpi2c_master_config_t master_config;
487495
int error;
488496

497+
DEVICE_MMIO_NAMED_MAP(dev, reg_base, K_MEM_CACHE_NONE | K_MEM_DIRECT_MAP);
498+
499+
base = (LPI2C_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
500+
489501
k_sem_init(&data->lock, 1, 1);
490502
k_sem_init(&data->device_sync_sem, 0, K_SEM_MAX_LIMIT);
491503

@@ -549,7 +561,7 @@ static const struct i2c_driver_api mcux_lpi2c_driver_api = {
549561
static void mcux_lpi2c_config_func_##n(const struct device *dev); \
550562
\
551563
static const struct mcux_lpi2c_config mcux_lpi2c_config_##n = { \
552-
.base = (LPI2C_Type *)DT_INST_REG_ADDR(n), \
564+
DEVICE_MMIO_NAMED_ROM_INIT(reg_base, DT_DRV_INST(n)), \
553565
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
554566
.clock_subsys = \
555567
(clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name),\
@@ -566,17 +578,17 @@ static const struct i2c_driver_api mcux_lpi2c_driver_api = {
566578
static struct mcux_lpi2c_data mcux_lpi2c_data_##n; \
567579
\
568580
I2C_DEVICE_DT_INST_DEFINE(n, mcux_lpi2c_init, NULL, \
569-
&mcux_lpi2c_data_##n, \
570-
&mcux_lpi2c_config_##n, POST_KERNEL, \
571-
CONFIG_I2C_INIT_PRIORITY, \
572-
&mcux_lpi2c_driver_api); \
581+
&mcux_lpi2c_data_##n, \
582+
&mcux_lpi2c_config_##n, POST_KERNEL, \
583+
CONFIG_I2C_INIT_PRIORITY, \
584+
&mcux_lpi2c_driver_api); \
573585
\
574586
static void mcux_lpi2c_config_func_##n(const struct device *dev) \
575587
{ \
576588
IRQ_CONNECT(DT_INST_IRQN(n), \
577-
DT_INST_IRQ(n, priority), \
578-
mcux_lpi2c_isr, \
579-
DEVICE_DT_INST_GET(n), 0); \
589+
DT_INST_IRQ(n, priority), \
590+
mcux_lpi2c_isr, \
591+
DEVICE_DT_INST_GET(n), 0); \
580592
\
581593
irq_enable(DT_INST_IRQN(n)); \
582594
}

0 commit comments

Comments
 (0)