drivers: uart: add pinctrl support in mcux lpuart

enable pinctrl in lpuart driver, and clean up driver instance definition
macros

Signed-off-by: Hake Huang <hake.huang@oss.nxp.com>
This commit is contained in:
Hake Huang 2021-12-24 15:01:58 +08:00 committed by David Leach
commit 40f3ab9f72

View file

@ -14,10 +14,16 @@
#include <drivers/uart.h>
#include <drivers/clock_control.h>
#include <pm/pm.h>
#ifdef CONFIG_PINCTRL
#include <drivers/pinctrl.h>
#endif
struct mcux_lpuart_config {
LPUART_Type *base;
const struct device *clock_dev;
#ifdef CONFIG_PINCTRL
const struct pinctrl_dev_config *pincfg;
#endif
clock_control_subsys_t clock_subsys;
uint32_t baud_rate;
uint8_t flow_ctrl;
@ -454,6 +460,9 @@ static int mcux_lpuart_init(const struct device *dev)
const struct mcux_lpuart_config *config = dev->config;
struct mcux_lpuart_data *data = dev->data;
struct uart_config *uart_api_config = &data->uart_config;
#ifdef CONFIG_PINCTRL
int err;
#endif
uart_api_config->baudrate = config->baud_rate;
uart_api_config->parity = UART_CFG_PARITY_NONE;
@ -463,6 +472,12 @@ static int mcux_lpuart_init(const struct device *dev)
/* set initial configuration */
mcux_lpuart_configure_init(dev, uart_api_config);
#ifdef CONFIG_PINCTRL
err = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT);
if (err < 0) {
return err;
}
#endif
#if defined(CONFIG_UART_INTERRUPT_DRIVEN) || defined(CONFIG_PM)
config->irq_config_func(dev);
@ -505,7 +520,7 @@ static const struct uart_driver_api mcux_lpuart_driver_api = {
#if defined(CONFIG_UART_INTERRUPT_DRIVEN) || defined(CONFIG_PM)
#define MCUX_LPUART_IRQ_INIT(n, i) \
#define MCUX_LPUART_IRQ_INSTALL(n, i) \
do { \
IRQ_CONNECT(DT_INST_IRQ_BY_IDX(n, i, irq), \
DT_INST_IRQ_BY_IDX(n, i, priority), \
@ -513,41 +528,49 @@ static const struct uart_driver_api mcux_lpuart_driver_api = {
\
irq_enable(DT_INST_IRQ_BY_IDX(n, i, irq)); \
} while (0)
#define LPUART_MCUX_CONFIG_FUNC(n) \
static void mcux_lpuart_config_func_##n(const struct device *dev) \
{ \
MCUX_LPUART_IRQ_INIT(n, 0); \
#define MCUX_LPUART_IRQ_INIT(n) .irq_config_func = mcux_lpuart_config_func_##n,
#define MCUX_LPUART_IRQ_DEFINE(n) \
static void mcux_lpuart_config_func_##n(const struct device *dev) \
{ \
MCUX_LPUART_IRQ_INSTALL(n, 0); \
\
IF_ENABLED(DT_INST_IRQ_HAS_IDX(n, 1), \
(MCUX_LPUART_IRQ_INIT(n, 1);)) \
(MCUX_LPUART_IRQ_INSTALL(n, 1);)) \
}
#define LPUART_MCUX_IRQ_CFG_FUNC_INIT(n) \
.irq_config_func = mcux_lpuart_config_func_##n
#define LPUART_MCUX_INIT_CFG(n) \
LPUART_MCUX_DECLARE_CFG(n, LPUART_MCUX_IRQ_CFG_FUNC_INIT(n))
#else
#define LPUART_MCUX_CONFIG_FUNC(n)
#define LPUART_MCUX_IRQ_CFG_FUNC_INIT
#define LPUART_MCUX_INIT_CFG(n) \
LPUART_MCUX_DECLARE_CFG(n, LPUART_MCUX_IRQ_CFG_FUNC_INIT)
#endif
#define MCUX_LPUART_IRQ_INIT(n)
#define MCUX_LPUART_IRQ_DEFINE(n)
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
#define LPUART_MCUX_DECLARE_CFG(n, IRQ_FUNC_INIT) \
static const struct mcux_lpuart_config mcux_lpuart_##n##_config = { \
.base = (LPUART_Type *) DT_INST_REG_ADDR(n), \
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
.clock_subsys = (clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name),\
.baud_rate = DT_INST_PROP(n, current_speed), \
.flow_ctrl = DT_INST_PROP(n, hw_flow_control) ? \
UART_CFG_FLOW_CTRL_RTS_CTS : UART_CFG_FLOW_CTRL_NONE,\
IRQ_FUNC_INIT \
}
#if CONFIG_PINCTRL
#define PINCTRL_DEFINE(n) PINCTRL_DT_INST_DEFINE(n);
#define PINCTRL_INIT(n) .pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n),
#else
#define PINCTRL_DEFINE(n)
#define PINCTRL_INIT(n)
#endif /* CONFIG_PINCTRL */
#define LPUART_MCUX_DECLARE_CFG(n) \
static const struct mcux_lpuart_config mcux_lpuart_##n##_config = { \
.base = (LPUART_Type *) DT_INST_REG_ADDR(n), \
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
.clock_subsys = (clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name), \
.baud_rate = DT_INST_PROP(n, current_speed), \
.flow_ctrl = DT_INST_PROP(n, hw_flow_control) ? \
UART_CFG_FLOW_CTRL_RTS_CTS : UART_CFG_FLOW_CTRL_NONE, \
PINCTRL_INIT(n) \
MCUX_LPUART_IRQ_INIT(n) \
};
#define LPUART_MCUX_INIT(n) \
\
static struct mcux_lpuart_data mcux_lpuart_##n##_data; \
\
static const struct mcux_lpuart_config mcux_lpuart_##n##_config;\
PINCTRL_DEFINE(n) \
MCUX_LPUART_IRQ_DEFINE(n) \
\
LPUART_MCUX_DECLARE_CFG(n) \
\
DEVICE_DT_INST_DEFINE(n, \
&mcux_lpuart_init, \
@ -557,9 +580,5 @@ static const struct mcux_lpuart_config mcux_lpuart_##n##_config = { \
PRE_KERNEL_1, \
CONFIG_SERIAL_INIT_PRIORITY, \
&mcux_lpuart_driver_api); \
\
LPUART_MCUX_CONFIG_FUNC(n) \
\
LPUART_MCUX_INIT_CFG(n);
DT_INST_FOREACH_STATUS_OKAY(LPUART_MCUX_INIT)