1414#include <drivers/uart.h>
1515#include <drivers/clock_control.h>
1616#include <pm/pm.h>
17+ #ifdef CONFIG_PINCTRL
18+ #include <drivers/pinctrl.h>
19+ #endif
1720
1821struct mcux_lpuart_config {
1922 LPUART_Type * base ;
2023 const struct device * clock_dev ;
24+ #ifdef CONFIG_PINCTRL
25+ const struct pinctrl_dev_config * pincfg ;
26+ #endif
2127 clock_control_subsys_t clock_subsys ;
2228 uint32_t baud_rate ;
2329 uint8_t flow_ctrl ;
@@ -454,6 +460,9 @@ static int mcux_lpuart_init(const struct device *dev)
454460 const struct mcux_lpuart_config * config = dev -> config ;
455461 struct mcux_lpuart_data * data = dev -> data ;
456462 struct uart_config * uart_api_config = & data -> uart_config ;
463+ #ifdef CONFIG_PINCTRL
464+ int err ;
465+ #endif
457466
458467 uart_api_config -> baudrate = config -> baud_rate ;
459468 uart_api_config -> parity = UART_CFG_PARITY_NONE ;
@@ -463,6 +472,12 @@ static int mcux_lpuart_init(const struct device *dev)
463472
464473 /* set initial configuration */
465474 mcux_lpuart_configure_init (dev , uart_api_config );
475+ #ifdef CONFIG_PINCTRL
476+ err = pinctrl_apply_state (config -> pincfg , PINCTRL_STATE_DEFAULT );
477+ if (err < 0 ) {
478+ return err ;
479+ }
480+ #endif
466481
467482#if defined(CONFIG_UART_INTERRUPT_DRIVEN ) || defined(CONFIG_PM )
468483 config -> irq_config_func (dev );
@@ -505,49 +520,57 @@ static const struct uart_driver_api mcux_lpuart_driver_api = {
505520
506521
507522#if defined(CONFIG_UART_INTERRUPT_DRIVEN ) || defined(CONFIG_PM )
508- #define MCUX_LPUART_IRQ_INIT (n , i ) \
523+ #define MCUX_LPUART_IRQ_INSTALL (n , i ) \
509524 do { \
510525 IRQ_CONNECT(DT_INST_IRQ_BY_IDX(n, i, irq), \
511526 DT_INST_IRQ_BY_IDX(n, i, priority), \
512527 mcux_lpuart_isr, DEVICE_DT_INST_GET(n), 0); \
513528 \
514529 irq_enable(DT_INST_IRQ_BY_IDX(n, i, irq)); \
515530 } while (0)
516- #define LPUART_MCUX_CONFIG_FUNC (n ) \
517- static void mcux_lpuart_config_func_##n(const struct device *dev) \
518- { \
519- MCUX_LPUART_IRQ_INIT(n, 0); \
531+ #define MCUX_LPUART_IRQ_INIT (n ) .irq_config_func = mcux_lpuart_config_func_##n,
532+ #define MCUX_LPUART_IRQ_DEFINE (n ) \
533+ static void mcux_lpuart_config_func_##n(const struct device *dev) \
534+ { \
535+ MCUX_LPUART_IRQ_INSTALL(n, 0); \
520536 \
521537 IF_ENABLED(DT_INST_IRQ_HAS_IDX(n, 1), \
522- (MCUX_LPUART_IRQ_INIT (n, 1);)) \
538+ (MCUX_LPUART_IRQ_INSTALL (n, 1);)) \
523539 }
524- #define LPUART_MCUX_IRQ_CFG_FUNC_INIT (n ) \
525- .irq_config_func = mcux_lpuart_config_func_##n
526- #define LPUART_MCUX_INIT_CFG (n ) \
527- LPUART_MCUX_DECLARE_CFG(n, LPUART_MCUX_IRQ_CFG_FUNC_INIT(n))
528540#else
529- #define LPUART_MCUX_CONFIG_FUNC (n )
530- #define LPUART_MCUX_IRQ_CFG_FUNC_INIT
531- #define LPUART_MCUX_INIT_CFG (n ) \
532- LPUART_MCUX_DECLARE_CFG(n, LPUART_MCUX_IRQ_CFG_FUNC_INIT)
533- #endif
541+ #define MCUX_LPUART_IRQ_INIT (n )
542+ #define MCUX_LPUART_IRQ_DEFINE (n )
543+ #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
534544
535- #define LPUART_MCUX_DECLARE_CFG (n , IRQ_FUNC_INIT ) \
536- static const struct mcux_lpuart_config mcux_lpuart_##n##_config = { \
537- .base = (LPUART_Type *) DT_INST_REG_ADDR(n), \
538- .clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
539- .clock_subsys = (clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name),\
540- .baud_rate = DT_INST_PROP(n, current_speed), \
541- .flow_ctrl = DT_INST_PROP(n, hw_flow_control) ? \
542- UART_CFG_FLOW_CTRL_RTS_CTS : UART_CFG_FLOW_CTRL_NONE,\
543- IRQ_FUNC_INIT \
544- }
545+
546+ #if CONFIG_PINCTRL
547+ #define PINCTRL_DEFINE (n ) PINCTRL_DT_INST_DEFINE(n);
548+ #define PINCTRL_INIT (n ) .pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n),
549+ #else
550+ #define PINCTRL_DEFINE (n )
551+ #define PINCTRL_INIT (n )
552+ #endif /* CONFIG_PINCTRL */
553+
554+ #define LPUART_MCUX_DECLARE_CFG (n ) \
555+ static const struct mcux_lpuart_config mcux_lpuart_##n##_config = { \
556+ .base = (LPUART_Type *) DT_INST_REG_ADDR(n), \
557+ .clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
558+ .clock_subsys = (clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name), \
559+ .baud_rate = DT_INST_PROP(n, current_speed), \
560+ .flow_ctrl = DT_INST_PROP(n, hw_flow_control) ? \
561+ UART_CFG_FLOW_CTRL_RTS_CTS : UART_CFG_FLOW_CTRL_NONE, \
562+ PINCTRL_INIT(n) \
563+ MCUX_LPUART_IRQ_INIT(n) \
564+ };
545565
546566#define LPUART_MCUX_INIT (n ) \
547567 \
548568 static struct mcux_lpuart_data mcux_lpuart_##n##_data; \
549569 \
550- static const struct mcux_lpuart_config mcux_lpuart_##n##_config;\
570+ PINCTRL_DEFINE(n) \
571+ MCUX_LPUART_IRQ_DEFINE(n) \
572+ \
573+ LPUART_MCUX_DECLARE_CFG(n) \
551574 \
552575 DEVICE_DT_INST_DEFINE(n, \
553576 &mcux_lpuart_init, \
@@ -557,9 +580,5 @@ static const struct mcux_lpuart_config mcux_lpuart_##n##_config = { \
557580 PRE_KERNEL_1, \
558581 CONFIG_SERIAL_INIT_PRIORITY, \
559582 &mcux_lpuart_driver_api); \
560- \
561- LPUART_MCUX_CONFIG_FUNC(n) \
562- \
563- LPUART_MCUX_INIT_CFG(n);
564583
565584DT_INST_FOREACH_STATUS_OKAY (LPUART_MCUX_INIT )
0 commit comments