drivers: serial: pl011: Introduce macros to separate device-dependent code

Introduce the `COMPAT_SPECIFIC_...` macros to determine the function name
from the compatible name.
These macros allow the isolation of device-dependent code in a generic way.

For example, if the compatible name is `ambiq,uart`,
The `COMPAT_SPECIFIC_DEFINE` macro is replaced by `AMBIQ_UART_DEFINE`.

Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
This commit is contained in:
TOKITA Hiroshi 2024-04-04 08:19:48 +09:00 committed by Carles Cufí
commit 155fee5924
2 changed files with 46 additions and 19 deletions

View file

@ -468,6 +468,35 @@ static int pl011_init(const struct device *dev)
return 0; return 0;
} }
#define COMPAT_SPECIFIC_FUNC_NAME(prefix, name) _CONCAT(_CONCAT(prefix, name), _)
/*
* The first element of compatible is used to determine the type.
* When compatible defines as "ambiq,uart", "arm,pl011",
* this macro expands to pwr_on_ambiq_uart_[n].
*/
#define COMPAT_SPECIFIC_PWR_ON_FUNC(n) \
_CONCAT(COMPAT_SPECIFIC_FUNC_NAME(pwr_on_, DT_INST_STRING_TOKEN_BY_IDX(n, compatible, 0)), \
n)
/*
* The first element of compatible is used to determine the type.
* When compatible defines as "ambiq,uart", "arm,pl011",
* this macro expands to clk_enable_ambiq_uart_[n].
*/
#define COMPAT_SPECIFIC_CLK_ENABLE_FUNC(n) \
_CONCAT(COMPAT_SPECIFIC_FUNC_NAME(clk_enable_, \
DT_INST_STRING_TOKEN_BY_IDX(n, compatible, 0)), n)
/*
* The first element of compatible is used to determine the type.
* When compatible defines as "ambiq,uart", "arm,pl011",
* this macro expands to AMBIQ_UART_DEFINE(n).
*/
#define COMPAT_SPECIFIC_DEFINE(n) \
_CONCAT(DT_INST_STRING_UPPER_TOKEN_BY_IDX(n, compatible, 0), _DEFINE)(n)
#if defined(CONFIG_PINCTRL) #if defined(CONFIG_PINCTRL)
#define PINCTRL_DEFINE(n) PINCTRL_DT_INST_DEFINE(n); #define PINCTRL_DEFINE(n) PINCTRL_DT_INST_DEFINE(n);
#define PINCTRL_INIT(n) .pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), #define PINCTRL_INIT(n) .pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n),
@ -476,17 +505,15 @@ static int pl011_init(const struct device *dev)
#define PINCTRL_INIT(n) #define PINCTRL_INIT(n)
#endif /* CONFIG_PINCTRL */ #endif /* CONFIG_PINCTRL */
#define PL011_GET_COMPAT_QUIRK_NONE(n) NULL #define ARM_PL011_DEFINE(n) \
static inline int pwr_on_arm_pl011_##n(void) \
#define PL011_GET_COMPAT_CLK_QUIRK_0(n) \ { \
COND_CODE_1(DT_NODE_HAS_COMPAT(DT_DRV_INST(n), ambiq_uart), \ return 0; \
(clk_enable_ambiq_uart), \ } \
PL011_GET_COMPAT_QUIRK_NONE(n)) static inline int clk_enable_arm_pl011_##n(const struct device *dev, uint32_t clk) \
{ \
#define PL011_GET_COMPAT_PWR_QUIRK_0(n) \ return 0; \
COND_CODE_1(DT_NODE_HAS_COMPAT(DT_DRV_INST(n), ambiq_uart), \ }
(pwr_on_ambiq_uart_##n), \
PL011_GET_COMPAT_QUIRK_NONE(n))
#ifdef CONFIG_UART_INTERRUPT_DRIVEN #ifdef CONFIG_UART_INTERRUPT_DRIVEN
void pl011_isr(const struct device *dev) void pl011_isr(const struct device *dev)
@ -523,8 +550,8 @@ void pl011_isr(const struct device *dev)
.sys_clk_freq = DT_INST_PROP_BY_PHANDLE(n, clocks, clock_frequency), \ .sys_clk_freq = DT_INST_PROP_BY_PHANDLE(n, clocks, clock_frequency), \
PINCTRL_INIT(n) \ PINCTRL_INIT(n) \
.irq_config_func = pl011_irq_config_func_##n, \ .irq_config_func = pl011_irq_config_func_##n, \
.clk_enable_func = PL011_GET_COMPAT_CLK_QUIRK_0(n), \ .clk_enable_func = COMPAT_SPECIFIC_CLK_ENABLE_FUNC(n), \
.pwr_on_func = PL011_GET_COMPAT_PWR_QUIRK_0(n), \ .pwr_on_func = COMPAT_SPECIFIC_PWR_ON_FUNC(n), \
}; };
#else #else
#define PL011_CONFIG_PORT(n) \ #define PL011_CONFIG_PORT(n) \
@ -537,7 +564,7 @@ void pl011_isr(const struct device *dev)
#define PL011_INIT(n) \ #define PL011_INIT(n) \
PINCTRL_DEFINE(n) \ PINCTRL_DEFINE(n) \
PL011_QUIRK_AMBIQ_UART_DEFINE(n) \ COMPAT_SPECIFIC_DEFINE(n) \
PL011_CONFIG_PORT(n) \ PL011_CONFIG_PORT(n) \
\ \
static struct pl011_data pl011_data_port_##n = { \ static struct pl011_data pl011_data_port_##n = { \

View file

@ -55,7 +55,7 @@ static inline int clk_enable_ambiq_uart(const struct device *dev, uint32_t clk)
* Note: busy wait is not allowed to use here due to UART is initiated before timer starts. * Note: busy wait is not allowed to use here due to UART is initiated before timer starts.
*/ */
#define QUIRK_AMBIQ_UART_DEFINE(n) \ #define AMBIQ_UART_DEFINE(n) \
static int pwr_on_ambiq_uart_##n(void) \ static int pwr_on_ambiq_uart_##n(void) \
{ \ { \
uint32_t addr = DT_REG_ADDR(DT_INST_PHANDLE(n, ambiq_pwrcfg)) + \ uint32_t addr = DT_REG_ADDR(DT_INST_PHANDLE(n, ambiq_pwrcfg)) + \
@ -67,10 +67,10 @@ static inline int clk_enable_ambiq_uart(const struct device *dev, uint32_t clk)
arch_nop(); \ arch_nop(); \
}; \ }; \
return 0; \ return 0; \
} \
static inline int clk_enable_ambiq_uart_##n(const struct device *dev, uint32_t clk) \
{ \
return clk_enable_ambiq_uart(dev, clk); \
} }
#define PL011_QUIRK_AMBIQ_UART_DEFINE(n) \
COND_CODE_1(DT_NODE_HAS_COMPAT(DT_DRV_INST(n), ambiq_uart), (QUIRK_AMBIQ_UART_DEFINE(n)), \
())
#endif /* ZEPHYR_DRIVERS_SERIAL_UART_PL011_AMBIQ_H_ */ #endif /* ZEPHYR_DRIVERS_SERIAL_UART_PL011_AMBIQ_H_ */