From 41a77be91ca403253b6c03c834b8c4df0497c876 Mon Sep 17 00:00:00 2001 From: Henrik Brix Andersen Date: Tue, 19 Apr 2022 16:23:31 +0200 Subject: [PATCH] drivers: can: mcan: add shared initializer macros Add shared initializer macros for struct can_mcan_config and struct can_mcan_data. Signed-off-by: Henrik Brix Andersen --- drivers/can/can_mcan.h | 48 +++++++++++++++++++++++++ drivers/can/can_mcux_mcan.c | 48 +++---------------------- drivers/can/can_sam.c | 42 +++------------------- drivers/can/can_stm32fd.c | 53 ++++------------------------ drivers/can/can_stm32h7.c | 48 +++---------------------- dts/arm/nxp/nxp_lpc55S1x_common.dtsi | 1 + 6 files changed, 67 insertions(+), 173 deletions(-) diff --git a/drivers/can/can_mcan.h b/drivers/can/can_mcan.h index ce791f5d4f8..7245b8ced99 100644 --- a/drivers/can/can_mcan.h +++ b/drivers/can/can_mcan.h @@ -209,6 +209,54 @@ struct can_mcan_config { struct can_mcan_reg; +#ifdef CONFIG_CAN_FD_MODE +#define CAN_MCAN_DT_CONFIG_GET(node_id, _custom_config) \ + { \ + .can = (struct can_mcan_reg *)DT_REG_ADDR_BY_NAME(node_id, m_can), \ + .bus_speed = DT_PROP(node_id, bus_speed), \ + .sjw = DT_PROP(node_id, sjw), \ + .sample_point = DT_PROP_OR(node_id, sample_point, 0), \ + .prop_ts1 = DT_PROP_OR(node_id, prop_seg, 0) + \ + DT_PROP_OR(node_id, phase_seg1, 0), \ + .ts2 = DT_PROP_OR(node_id, phase_seg2, 0), \ + .bus_speed_data = DT_PROP(node_id, bus_speed_data), \ + .sjw_data = DT_PROP(node_id, sjw_data), \ + .sample_point_data = \ + DT_PROP_OR(node_id, sample_point_data, 0), \ + .prop_ts1_data = DT_PROP_OR(node_id, prop_seg_data, 0) + \ + DT_PROP_OR(node_id, phase_seg1_data, 0), \ + .ts2_data = DT_PROP_OR(node_id, phase_seg2_data, 0), \ + .tx_delay_comp_offset = \ + DT_PROP(node_id, tx_delay_comp_offset), \ + .phy = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(node_id, phys)), \ + .max_bitrate = DT_CAN_TRANSCEIVER_MAX_BITRATE(node_id, 5000000),\ + .custom = _custom_config, \ + } +#else /* CONFIG_CAN_FD_MODE */ +#define CAN_MCAN_DT_CONFIG_GET(node_id, _custom_config) \ + { \ + .can = (struct can_mcan_reg *)DT_REG_ADDR_BY_NAME(node_id, m_can), \ + .bus_speed = DT_PROP(node_id, bus_speed), \ + .sjw = DT_PROP(node_id, sjw), \ + .sample_point = DT_PROP_OR(node_id, sample_point, 0), \ + .prop_ts1 = DT_PROP_OR(node_id, prop_seg, 0) + \ + DT_PROP_OR(node_id, phase_seg1, 0), \ + .ts2 = DT_PROP_OR(node_id, phase_seg2, 0), \ + .phy = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(node_id, phys)), \ + .max_bitrate = DT_CAN_TRANSCEIVER_MAX_BITRATE(node_id, 1000000),\ + .custom = _custom_config, \ + } +#endif /* !CONFIG_CAN_FD_MODE */ + +#define CAN_MCAN_DT_CONFIG_INST_GET(inst, _custom_config) \ + CAN_MCAN_DT_CONFIG_GET(DT_DRV_INST(inst), _custom_config) + +#define CAN_MCAN_DATA_INITIALIZER(_msg_ram, _custom_data) \ + { \ + .msg_ram = _msg_ram, \ + .custom = _custom_data, \ + } + int can_mcan_set_mode(const struct device *dev, enum can_mode mode); int can_mcan_set_timing(const struct device *dev, diff --git a/drivers/can/can_mcux_mcan.c b/drivers/can/can_mcux_mcan.c index 066fdb359da..6f8bdf45986 100644 --- a/drivers/can/can_mcux_mcan.c +++ b/drivers/can/can_mcux_mcan.c @@ -119,45 +119,6 @@ static const struct can_driver_api mcux_mcan_driver_api = { #endif /* CONFIG_CAN_FD_MODE */ }; -#ifdef CONFIG_CAN_FD_MODE -#define MCUX_MCAN_MCAN_INIT(n) \ - { \ - .can = (struct can_mcan_reg *)DT_INST_REG_ADDR(n), \ - .bus_speed = DT_INST_PROP(n, bus_speed), \ - .sjw = DT_INST_PROP(n, sjw), \ - .sample_point = DT_INST_PROP_OR(n, sample_point, 0), \ - .prop_ts1 = DT_INST_PROP_OR(n, prop_seg, 0) + \ - DT_INST_PROP_OR(n, phase_seg1, 0), \ - .ts2 = DT_INST_PROP_OR(n, phase_seg2, 0), \ - .bus_speed_data = DT_INST_PROP(n, bus_speed_data), \ - .sjw_data = DT_INST_PROP(n, sjw_data), \ - .sample_point_data = \ - DT_INST_PROP_OR(n, sample_point_data, 0), \ - .prop_ts1_data = DT_INST_PROP_OR(n, prop_seg_data, 0) + \ - DT_INST_PROP_OR(n, phase_seg1_data, 0), \ - .ts2_data = DT_INST_PROP_OR(n, phase_seg2_data, 0), \ - .tx_delay_comp_offset = \ - DT_INST_PROP(n, tx_delay_comp_offset), \ - .phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(n, phys)), \ - .max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(n, 5000000), \ - .custom = &mcux_mcan_config_##n, \ - } -#else /* CONFIG_CAN_FD_MODE */ -#define MCUX_MCAN_MCAN_INIT(n) \ - { \ - .can = (struct can_mcan_reg *)DT_INST_REG_ADDR(n), \ - .bus_speed = DT_INST_PROP(n, bus_speed), \ - .sjw = DT_INST_PROP(n, sjw), \ - .sample_point = DT_INST_PROP_OR(n, sample_point, 0), \ - .prop_ts1 = DT_INST_PROP_OR(n, prop_seg, 0) + \ - DT_INST_PROP_OR(n, phase_seg1, 0), \ - .ts2 = DT_INST_PROP_OR(n, phase_seg2, 0), \ - .phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(n, phys)), \ - .max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(n, 1000000), \ - .custom = &mcux_mcan_config_##n, \ - } -#endif /* !CONFIG_CAN_FD_MODE */ - #define MCUX_MCAN_INIT(n) \ static void mcux_mcan_irq_config_##n(const struct device *dev); \ \ @@ -169,14 +130,13 @@ static const struct can_driver_api mcux_mcan_driver_api = { }; \ \ static const struct can_mcan_config can_mcan_config_##n = \ - MCUX_MCAN_MCAN_INIT(n); \ + CAN_MCAN_DT_CONFIG_INST_GET(n, &mcux_mcan_config_##n); \ \ static struct mcux_mcan_data mcux_mcan_data_##n; \ \ - static struct can_mcan_data can_mcan_data_##n = { \ - .msg_ram = &mcux_mcan_data_##n.msg_ram, \ - .custom = &mcux_mcan_data_##n, \ - }; \ + static struct can_mcan_data can_mcan_data_##n = \ + CAN_MCAN_DATA_INITIALIZER(&mcux_mcan_data_##n.msg_ram, \ + &mcux_mcan_data_##n); \ \ DEVICE_DT_INST_DEFINE(n, &mcux_mcan_init, NULL, \ &can_mcan_data_##n, \ diff --git a/drivers/can/can_sam.c b/drivers/can/can_sam.c index 86f55872def..3f46442f1d7 100644 --- a/drivers/can/can_sam.c +++ b/drivers/can/can_sam.c @@ -126,39 +126,6 @@ static void config_can_##inst##_irq(void) irq_enable(DT_INST_IRQ_BY_NAME(inst, line_1, irq)); \ } -#ifdef CONFIG_CAN_FD_MODE -#define CAN_SAM_MCAN_CFG(inst) \ -{ \ - .can = (struct can_mcan_reg *)DT_INST_REG_ADDR(inst), \ - .bus_speed = DT_INST_PROP(inst, bus_speed), .sjw = DT_INST_PROP(inst, sjw), \ - .sample_point = DT_INST_PROP_OR(inst, sample_point, 0), \ - .prop_ts1 = DT_INST_PROP_OR(inst, prop_seg, 0) + DT_INST_PROP_OR(inst, phase_seg1, 0), \ - .ts2 = DT_INST_PROP_OR(inst, phase_seg2, 0), \ - .bus_speed_data = DT_INST_PROP(inst, bus_speed_data), \ - .sjw_data = DT_INST_PROP(inst, sjw_data), \ - .sample_point_data = DT_INST_PROP_OR(inst, sample_point_data, 0), \ - .prop_ts1_data = DT_INST_PROP_OR(inst, prop_seg_data, 0) + \ - DT_INST_PROP_OR(inst, phase_seg1_data, 0), \ - .ts2_data = DT_INST_PROP_OR(inst, phase_seg2_data, 0), \ - .tx_delay_comp_offset = DT_INST_PROP(inst, tx_delay_comp_offset), \ - .phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(inst, phys)), \ - .max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(inst, 5000000), \ - .custom = &can_sam_cfg_##inst, \ -} -#else /* CONFIG_CAN_FD_MODE */ -#define CAN_SAM_MCAN_CFG(inst) \ -{ \ - .can = (struct can_mcan_reg *)DT_INST_REG_ADDR(inst), \ - .bus_speed = DT_INST_PROP(inst, bus_speed), .sjw = DT_INST_PROP(inst, sjw), \ - .sample_point = DT_INST_PROP_OR(inst, sample_point, 0), \ - .prop_ts1 = DT_INST_PROP_OR(inst, prop_seg, 0) + DT_INST_PROP_OR(inst, phase_seg1, 0), \ - .ts2 = DT_INST_PROP_OR(inst, phase_seg2, 0), \ - .phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(inst, phys)), \ - .max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(inst, 1000000), \ - .custom = &can_sam_cfg_##inst, \ -} -#endif /* CONFIG_CAN_FD_MODE */ - #define CAN_SAM_CFG_INST(inst) \ static const struct can_sam_config can_sam_cfg_##inst = { \ .pmc_id = DT_INST_PROP(inst, peripheral_id), \ @@ -167,15 +134,14 @@ static void config_can_##inst##_irq(void) }; \ \ static const struct can_mcan_config can_mcan_cfg_##inst = \ - CAN_SAM_MCAN_CFG(inst); + CAN_MCAN_DT_CONFIG_INST_GET(inst, &can_sam_cfg_##inst); #define CAN_SAM_DATA_INST(inst) \ static struct can_sam_data can_sam_data_##inst; \ \ - static struct can_mcan_data can_mcan_data_##inst = { \ - .msg_ram = &can_sam_data_##inst.msg_ram, \ - .custom = &can_sam_data_##inst, \ - }; \ + static struct can_mcan_data can_mcan_data_##inst = \ + CAN_MCAN_DATA_INITIALIZER(&can_sam_data_##inst.msg_ram, \ + &can_sam_data_##inst); \ #define CAN_SAM_DEVICE_INST(inst) \ DEVICE_DT_INST_DEFINE(inst, &can_sam_init, NULL, \ diff --git a/drivers/can/can_stm32fd.c b/drivers/can/can_stm32fd.c index 0ee8cba5dc8..4f67b29dda7 100644 --- a/drivers/can/can_stm32fd.c +++ b/drivers/can/can_stm32fd.c @@ -151,8 +151,6 @@ static void config_can_##inst##_irq(void) \ irq_enable(DT_INST_IRQ_BY_NAME(inst, line_1, irq)); \ } -#ifdef CONFIG_CAN_FD_MODE - #define CAN_STM32FD_CFG_INST(inst) \ PINCTRL_DT_INST_DEFINE(inst); \ \ @@ -161,57 +159,18 @@ static void config_can_##inst##_irq(void) \ .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \ }; \ \ - static const struct can_mcan_config can_mcan_cfg_##inst = { \ - .can = (struct can_mcan_reg *) DT_INST_REG_ADDR_BY_NAME(inst, m_can), \ - .bus_speed = DT_INST_PROP(inst, bus_speed), \ - .sjw = DT_INST_PROP(inst, sjw), \ - .sample_point = DT_INST_PROP_OR(inst, sample_point, 0), \ - .prop_ts1 = DT_INST_PROP_OR(inst, prop_seg, 0) + \ - DT_INST_PROP_OR(inst, phase_seg1, 0), \ - .ts2 = DT_INST_PROP_OR(inst, phase_seg2, 0), \ - .bus_speed_data = DT_INST_PROP(inst, bus_speed_data), \ - .sjw_data = DT_INST_PROP(inst, sjw_data), \ - .sample_point_data = DT_INST_PROP_OR(inst, sample_point_data, 0), \ - .prop_ts1_data = DT_INST_PROP_OR(inst, prop_seg_data, 0) + \ - DT_INST_PROP_OR(inst, phase_seg1_data, 0), \ - .ts2_data = DT_INST_PROP_OR(inst, phase_seg2_data, 0), \ - .tx_delay_comp_offset = DT_INST_PROP(inst, tx_delay_comp_offset), \ - .phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(inst, phys)), \ - .max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(inst, 5000000), \ - .custom = &can_stm32fd_cfg_##inst, \ - }; -#else /* CONFIG_CAN_FD_MODE */ -#define CAN_STM32FD_CFG_INST(inst) \ - PINCTRL_DT_INST_DEFINE(inst); \ - \ - static const struct can_stm32fd_config can_stm32fd_cfg_##inst = { \ - .config_irq = config_can_##inst##_irq, \ - .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \ - }; \ - \ - static const struct can_mcan_config can_mcan_cfg_##inst = { \ - .can = (struct can_mcan_reg *) DT_INST_REG_ADDR_BY_NAME(inst, m_can), \ - .bus_speed = DT_INST_PROP(inst, bus_speed), \ - .sjw = DT_INST_PROP(inst, sjw), \ - .sample_point = DT_INST_PROP_OR(inst, sample_point, 0), \ - .prop_ts1 = DT_INST_PROP_OR(inst, prop_seg, 0) + \ - DT_INST_PROP_OR(inst, phase_seg1, 0), \ - .ts2 = DT_INST_PROP_OR(inst, phase_seg2, 0), \ - .phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(inst, phys)), \ - .max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(inst, 1000000), \ - .custom = &can_stm32fd_cfg_##inst, \ - }; -#endif /* CONFIG_CAN_FD_MODE */ + static const struct can_mcan_config can_mcan_cfg_##inst = \ + CAN_MCAN_DT_CONFIG_INST_GET(inst, &can_stm32fd_cfg_##inst); #define CAN_STM32FD_DATA_INST(inst) \ - static struct can_mcan_data can_mcan_dev_data_##inst = { \ - .msg_ram = (struct can_mcan_msg_sram *) \ + static struct can_mcan_data can_mcan_data_##inst = \ + CAN_MCAN_DATA_INITIALIZER((struct can_mcan_msg_sram *) \ DT_INST_REG_ADDR_BY_NAME(inst, message_ram), \ - }; + NULL); #define CAN_STM32FD_DEVICE_INST(inst) \ DEVICE_DT_INST_DEFINE(inst, &can_stm32fd_init, NULL, \ - &can_mcan_dev_data_##inst, &can_mcan_cfg_##inst, \ + &can_mcan_data_##inst, &can_mcan_cfg_##inst, \ POST_KERNEL, CONFIG_CAN_INIT_PRIORITY, \ &can_stm32fd_driver_api); diff --git a/drivers/can/can_stm32h7.c b/drivers/can/can_stm32h7.c index 13c79a591b4..63a8cbcb7bb 100644 --- a/drivers/can/can_stm32h7.c +++ b/drivers/can/can_stm32h7.c @@ -148,45 +148,6 @@ static const struct can_driver_api can_stm32h7_driver_api = { #endif }; -#ifdef CONFIG_CAN_FD_MODE -#define CAN_STM32H7_MCAN_MCAN_INIT(n) \ - { \ - .can = (struct can_mcan_reg *)DT_INST_REG_ADDR(n), \ - .bus_speed = DT_INST_PROP(n, bus_speed), \ - .sjw = DT_INST_PROP(n, sjw), \ - .sample_point = DT_INST_PROP_OR(n, sample_point, 0), \ - .prop_ts1 = DT_INST_PROP_OR(n, prop_seg, 0) + \ - DT_INST_PROP_OR(n, phase_seg1, 0), \ - .ts2 = DT_INST_PROP_OR(n, phase_seg2, 0), \ - .bus_speed_data = DT_INST_PROP(n, bus_speed_data), \ - .sjw_data = DT_INST_PROP(n, sjw_data), \ - .sample_point_data = \ - DT_INST_PROP_OR(n, sample_point_data, 0), \ - .prop_ts1_data = DT_INST_PROP_OR(n, prop_seg_data, 0) + \ - DT_INST_PROP_OR(n, phase_seg1_data, 0), \ - .ts2_data = DT_INST_PROP_OR(n, phase_seg2_data, 0), \ - .tx_delay_comp_offset = \ - DT_INST_PROP(n, tx_delay_comp_offset), \ - .phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(n, phys)), \ - .max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(n, 5000000), \ - .custom = &can_stm32h7_cfg_##n, \ - } -#else /* CONFIG_CAN_FD_MODE */ -#define CAN_STM32H7_MCAN_MCAN_INIT(n) \ - { \ - .can = (struct can_mcan_reg *)DT_INST_REG_ADDR(n), \ - .bus_speed = DT_INST_PROP(n, bus_speed), \ - .sjw = DT_INST_PROP(n, sjw), \ - .sample_point = DT_INST_PROP_OR(n, sample_point, 0), \ - .prop_ts1 = DT_INST_PROP_OR(n, prop_seg, 0) + \ - DT_INST_PROP_OR(n, phase_seg1, 0), \ - .ts2 = DT_INST_PROP_OR(n, phase_seg2, 0), \ - .phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(n, phys)), \ - .max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(n, 1000000), \ - .custom = &can_stm32h7_cfg_##n, \ - } -#endif /* !CONFIG_CAN_FD_MODE */ - #define CAN_STM32H7_MCAN_INIT(n) \ static void stm32h7_mcan_irq_config_##n(void); \ \ @@ -202,12 +163,11 @@ static const struct can_driver_api can_stm32h7_driver_api = { }; \ \ static const struct can_mcan_config can_mcan_cfg_##n = \ - CAN_STM32H7_MCAN_MCAN_INIT(n); \ + CAN_MCAN_DT_CONFIG_INST_GET(n, &can_stm32h7_cfg_##n); \ \ - static struct can_mcan_data can_mcan_data_##n = { \ - .msg_ram = (struct can_mcan_msg_sram *) \ - DT_INST_REG_ADDR_BY_NAME(n, message_ram), \ - }; \ + static struct can_mcan_data can_mcan_data_##n = \ + CAN_MCAN_DATA_INITIALIZER((struct can_mcan_msg_sram *) \ + DT_INST_REG_ADDR_BY_NAME(n, message_ram), NULL); \ \ DEVICE_DT_INST_DEFINE(n, &can_stm32h7_init, NULL, \ &can_mcan_data_##n, \ diff --git a/dts/arm/nxp/nxp_lpc55S1x_common.dtsi b/dts/arm/nxp/nxp_lpc55S1x_common.dtsi index eb90c9002ef..adb8a0d88b9 100644 --- a/dts/arm/nxp/nxp_lpc55S1x_common.dtsi +++ b/dts/arm/nxp/nxp_lpc55S1x_common.dtsi @@ -221,6 +221,7 @@ can0: can@9d000 { compatible = "nxp,lpc-mcan"; reg = <0x9d000 0x1000>; + reg-names = "m_can"; interrupts = <43 0>, <44 0>; clocks = <&syscon MCUX_MCAN_CLK>; label = "CAN_0";