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 <hebad@vestas.com>
This commit is contained in:
Henrik Brix Andersen 2022-04-19 16:23:31 +02:00 committed by Carles Cufí
commit 41a77be91c
6 changed files with 67 additions and 173 deletions

View file

@ -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,

View file

@ -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, \

View file

@ -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, \

View file

@ -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);

View file

@ -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, \