Skip to content

Commit 41a77be

Browse files
henrikbrixandersencarlescufi
authored andcommitted
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 <[email protected]>
1 parent 5b3712a commit 41a77be

File tree

6 files changed

+67
-173
lines changed

6 files changed

+67
-173
lines changed

drivers/can/can_mcan.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,54 @@ struct can_mcan_config {
209209

210210
struct can_mcan_reg;
211211

212+
#ifdef CONFIG_CAN_FD_MODE
213+
#define CAN_MCAN_DT_CONFIG_GET(node_id, _custom_config) \
214+
{ \
215+
.can = (struct can_mcan_reg *)DT_REG_ADDR_BY_NAME(node_id, m_can), \
216+
.bus_speed = DT_PROP(node_id, bus_speed), \
217+
.sjw = DT_PROP(node_id, sjw), \
218+
.sample_point = DT_PROP_OR(node_id, sample_point, 0), \
219+
.prop_ts1 = DT_PROP_OR(node_id, prop_seg, 0) + \
220+
DT_PROP_OR(node_id, phase_seg1, 0), \
221+
.ts2 = DT_PROP_OR(node_id, phase_seg2, 0), \
222+
.bus_speed_data = DT_PROP(node_id, bus_speed_data), \
223+
.sjw_data = DT_PROP(node_id, sjw_data), \
224+
.sample_point_data = \
225+
DT_PROP_OR(node_id, sample_point_data, 0), \
226+
.prop_ts1_data = DT_PROP_OR(node_id, prop_seg_data, 0) + \
227+
DT_PROP_OR(node_id, phase_seg1_data, 0), \
228+
.ts2_data = DT_PROP_OR(node_id, phase_seg2_data, 0), \
229+
.tx_delay_comp_offset = \
230+
DT_PROP(node_id, tx_delay_comp_offset), \
231+
.phy = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(node_id, phys)), \
232+
.max_bitrate = DT_CAN_TRANSCEIVER_MAX_BITRATE(node_id, 5000000),\
233+
.custom = _custom_config, \
234+
}
235+
#else /* CONFIG_CAN_FD_MODE */
236+
#define CAN_MCAN_DT_CONFIG_GET(node_id, _custom_config) \
237+
{ \
238+
.can = (struct can_mcan_reg *)DT_REG_ADDR_BY_NAME(node_id, m_can), \
239+
.bus_speed = DT_PROP(node_id, bus_speed), \
240+
.sjw = DT_PROP(node_id, sjw), \
241+
.sample_point = DT_PROP_OR(node_id, sample_point, 0), \
242+
.prop_ts1 = DT_PROP_OR(node_id, prop_seg, 0) + \
243+
DT_PROP_OR(node_id, phase_seg1, 0), \
244+
.ts2 = DT_PROP_OR(node_id, phase_seg2, 0), \
245+
.phy = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(node_id, phys)), \
246+
.max_bitrate = DT_CAN_TRANSCEIVER_MAX_BITRATE(node_id, 1000000),\
247+
.custom = _custom_config, \
248+
}
249+
#endif /* !CONFIG_CAN_FD_MODE */
250+
251+
#define CAN_MCAN_DT_CONFIG_INST_GET(inst, _custom_config) \
252+
CAN_MCAN_DT_CONFIG_GET(DT_DRV_INST(inst), _custom_config)
253+
254+
#define CAN_MCAN_DATA_INITIALIZER(_msg_ram, _custom_data) \
255+
{ \
256+
.msg_ram = _msg_ram, \
257+
.custom = _custom_data, \
258+
}
259+
212260
int can_mcan_set_mode(const struct device *dev, enum can_mode mode);
213261

214262
int can_mcan_set_timing(const struct device *dev,

drivers/can/can_mcux_mcan.c

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -119,45 +119,6 @@ static const struct can_driver_api mcux_mcan_driver_api = {
119119
#endif /* CONFIG_CAN_FD_MODE */
120120
};
121121

122-
#ifdef CONFIG_CAN_FD_MODE
123-
#define MCUX_MCAN_MCAN_INIT(n) \
124-
{ \
125-
.can = (struct can_mcan_reg *)DT_INST_REG_ADDR(n), \
126-
.bus_speed = DT_INST_PROP(n, bus_speed), \
127-
.sjw = DT_INST_PROP(n, sjw), \
128-
.sample_point = DT_INST_PROP_OR(n, sample_point, 0), \
129-
.prop_ts1 = DT_INST_PROP_OR(n, prop_seg, 0) + \
130-
DT_INST_PROP_OR(n, phase_seg1, 0), \
131-
.ts2 = DT_INST_PROP_OR(n, phase_seg2, 0), \
132-
.bus_speed_data = DT_INST_PROP(n, bus_speed_data), \
133-
.sjw_data = DT_INST_PROP(n, sjw_data), \
134-
.sample_point_data = \
135-
DT_INST_PROP_OR(n, sample_point_data, 0), \
136-
.prop_ts1_data = DT_INST_PROP_OR(n, prop_seg_data, 0) + \
137-
DT_INST_PROP_OR(n, phase_seg1_data, 0), \
138-
.ts2_data = DT_INST_PROP_OR(n, phase_seg2_data, 0), \
139-
.tx_delay_comp_offset = \
140-
DT_INST_PROP(n, tx_delay_comp_offset), \
141-
.phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(n, phys)), \
142-
.max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(n, 5000000), \
143-
.custom = &mcux_mcan_config_##n, \
144-
}
145-
#else /* CONFIG_CAN_FD_MODE */
146-
#define MCUX_MCAN_MCAN_INIT(n) \
147-
{ \
148-
.can = (struct can_mcan_reg *)DT_INST_REG_ADDR(n), \
149-
.bus_speed = DT_INST_PROP(n, bus_speed), \
150-
.sjw = DT_INST_PROP(n, sjw), \
151-
.sample_point = DT_INST_PROP_OR(n, sample_point, 0), \
152-
.prop_ts1 = DT_INST_PROP_OR(n, prop_seg, 0) + \
153-
DT_INST_PROP_OR(n, phase_seg1, 0), \
154-
.ts2 = DT_INST_PROP_OR(n, phase_seg2, 0), \
155-
.phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(n, phys)), \
156-
.max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(n, 1000000), \
157-
.custom = &mcux_mcan_config_##n, \
158-
}
159-
#endif /* !CONFIG_CAN_FD_MODE */
160-
161122
#define MCUX_MCAN_INIT(n) \
162123
static void mcux_mcan_irq_config_##n(const struct device *dev); \
163124
\
@@ -169,14 +130,13 @@ static const struct can_driver_api mcux_mcan_driver_api = {
169130
}; \
170131
\
171132
static const struct can_mcan_config can_mcan_config_##n = \
172-
MCUX_MCAN_MCAN_INIT(n); \
133+
CAN_MCAN_DT_CONFIG_INST_GET(n, &mcux_mcan_config_##n); \
173134
\
174135
static struct mcux_mcan_data mcux_mcan_data_##n; \
175136
\
176-
static struct can_mcan_data can_mcan_data_##n = { \
177-
.msg_ram = &mcux_mcan_data_##n.msg_ram, \
178-
.custom = &mcux_mcan_data_##n, \
179-
}; \
137+
static struct can_mcan_data can_mcan_data_##n = \
138+
CAN_MCAN_DATA_INITIALIZER(&mcux_mcan_data_##n.msg_ram, \
139+
&mcux_mcan_data_##n); \
180140
\
181141
DEVICE_DT_INST_DEFINE(n, &mcux_mcan_init, NULL, \
182142
&can_mcan_data_##n, \

drivers/can/can_sam.c

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -126,39 +126,6 @@ static void config_can_##inst##_irq(void)
126126
irq_enable(DT_INST_IRQ_BY_NAME(inst, line_1, irq)); \
127127
}
128128

129-
#ifdef CONFIG_CAN_FD_MODE
130-
#define CAN_SAM_MCAN_CFG(inst) \
131-
{ \
132-
.can = (struct can_mcan_reg *)DT_INST_REG_ADDR(inst), \
133-
.bus_speed = DT_INST_PROP(inst, bus_speed), .sjw = DT_INST_PROP(inst, sjw), \
134-
.sample_point = DT_INST_PROP_OR(inst, sample_point, 0), \
135-
.prop_ts1 = DT_INST_PROP_OR(inst, prop_seg, 0) + DT_INST_PROP_OR(inst, phase_seg1, 0), \
136-
.ts2 = DT_INST_PROP_OR(inst, phase_seg2, 0), \
137-
.bus_speed_data = DT_INST_PROP(inst, bus_speed_data), \
138-
.sjw_data = DT_INST_PROP(inst, sjw_data), \
139-
.sample_point_data = DT_INST_PROP_OR(inst, sample_point_data, 0), \
140-
.prop_ts1_data = DT_INST_PROP_OR(inst, prop_seg_data, 0) + \
141-
DT_INST_PROP_OR(inst, phase_seg1_data, 0), \
142-
.ts2_data = DT_INST_PROP_OR(inst, phase_seg2_data, 0), \
143-
.tx_delay_comp_offset = DT_INST_PROP(inst, tx_delay_comp_offset), \
144-
.phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(inst, phys)), \
145-
.max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(inst, 5000000), \
146-
.custom = &can_sam_cfg_##inst, \
147-
}
148-
#else /* CONFIG_CAN_FD_MODE */
149-
#define CAN_SAM_MCAN_CFG(inst) \
150-
{ \
151-
.can = (struct can_mcan_reg *)DT_INST_REG_ADDR(inst), \
152-
.bus_speed = DT_INST_PROP(inst, bus_speed), .sjw = DT_INST_PROP(inst, sjw), \
153-
.sample_point = DT_INST_PROP_OR(inst, sample_point, 0), \
154-
.prop_ts1 = DT_INST_PROP_OR(inst, prop_seg, 0) + DT_INST_PROP_OR(inst, phase_seg1, 0), \
155-
.ts2 = DT_INST_PROP_OR(inst, phase_seg2, 0), \
156-
.phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(inst, phys)), \
157-
.max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(inst, 1000000), \
158-
.custom = &can_sam_cfg_##inst, \
159-
}
160-
#endif /* CONFIG_CAN_FD_MODE */
161-
162129
#define CAN_SAM_CFG_INST(inst) \
163130
static const struct can_sam_config can_sam_cfg_##inst = { \
164131
.pmc_id = DT_INST_PROP(inst, peripheral_id), \
@@ -167,15 +134,14 @@ static void config_can_##inst##_irq(void)
167134
}; \
168135
\
169136
static const struct can_mcan_config can_mcan_cfg_##inst = \
170-
CAN_SAM_MCAN_CFG(inst);
137+
CAN_MCAN_DT_CONFIG_INST_GET(inst, &can_sam_cfg_##inst);
171138

172139
#define CAN_SAM_DATA_INST(inst) \
173140
static struct can_sam_data can_sam_data_##inst; \
174141
\
175-
static struct can_mcan_data can_mcan_data_##inst = { \
176-
.msg_ram = &can_sam_data_##inst.msg_ram, \
177-
.custom = &can_sam_data_##inst, \
178-
}; \
142+
static struct can_mcan_data can_mcan_data_##inst = \
143+
CAN_MCAN_DATA_INITIALIZER(&can_sam_data_##inst.msg_ram, \
144+
&can_sam_data_##inst); \
179145

180146
#define CAN_SAM_DEVICE_INST(inst) \
181147
DEVICE_DT_INST_DEFINE(inst, &can_sam_init, NULL, \

drivers/can/can_stm32fd.c

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ static void config_can_##inst##_irq(void) \
151151
irq_enable(DT_INST_IRQ_BY_NAME(inst, line_1, irq)); \
152152
}
153153

154-
#ifdef CONFIG_CAN_FD_MODE
155-
156154
#define CAN_STM32FD_CFG_INST(inst) \
157155
PINCTRL_DT_INST_DEFINE(inst); \
158156
\
@@ -161,57 +159,18 @@ static void config_can_##inst##_irq(void) \
161159
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \
162160
}; \
163161
\
164-
static const struct can_mcan_config can_mcan_cfg_##inst = { \
165-
.can = (struct can_mcan_reg *) DT_INST_REG_ADDR_BY_NAME(inst, m_can), \
166-
.bus_speed = DT_INST_PROP(inst, bus_speed), \
167-
.sjw = DT_INST_PROP(inst, sjw), \
168-
.sample_point = DT_INST_PROP_OR(inst, sample_point, 0), \
169-
.prop_ts1 = DT_INST_PROP_OR(inst, prop_seg, 0) + \
170-
DT_INST_PROP_OR(inst, phase_seg1, 0), \
171-
.ts2 = DT_INST_PROP_OR(inst, phase_seg2, 0), \
172-
.bus_speed_data = DT_INST_PROP(inst, bus_speed_data), \
173-
.sjw_data = DT_INST_PROP(inst, sjw_data), \
174-
.sample_point_data = DT_INST_PROP_OR(inst, sample_point_data, 0), \
175-
.prop_ts1_data = DT_INST_PROP_OR(inst, prop_seg_data, 0) + \
176-
DT_INST_PROP_OR(inst, phase_seg1_data, 0), \
177-
.ts2_data = DT_INST_PROP_OR(inst, phase_seg2_data, 0), \
178-
.tx_delay_comp_offset = DT_INST_PROP(inst, tx_delay_comp_offset), \
179-
.phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(inst, phys)), \
180-
.max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(inst, 5000000), \
181-
.custom = &can_stm32fd_cfg_##inst, \
182-
};
183-
#else /* CONFIG_CAN_FD_MODE */
184-
#define CAN_STM32FD_CFG_INST(inst) \
185-
PINCTRL_DT_INST_DEFINE(inst); \
186-
\
187-
static const struct can_stm32fd_config can_stm32fd_cfg_##inst = { \
188-
.config_irq = config_can_##inst##_irq, \
189-
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \
190-
}; \
191-
\
192-
static const struct can_mcan_config can_mcan_cfg_##inst = { \
193-
.can = (struct can_mcan_reg *) DT_INST_REG_ADDR_BY_NAME(inst, m_can), \
194-
.bus_speed = DT_INST_PROP(inst, bus_speed), \
195-
.sjw = DT_INST_PROP(inst, sjw), \
196-
.sample_point = DT_INST_PROP_OR(inst, sample_point, 0), \
197-
.prop_ts1 = DT_INST_PROP_OR(inst, prop_seg, 0) + \
198-
DT_INST_PROP_OR(inst, phase_seg1, 0), \
199-
.ts2 = DT_INST_PROP_OR(inst, phase_seg2, 0), \
200-
.phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(inst, phys)), \
201-
.max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(inst, 1000000), \
202-
.custom = &can_stm32fd_cfg_##inst, \
203-
};
204-
#endif /* CONFIG_CAN_FD_MODE */
162+
static const struct can_mcan_config can_mcan_cfg_##inst = \
163+
CAN_MCAN_DT_CONFIG_INST_GET(inst, &can_stm32fd_cfg_##inst);
205164

206165
#define CAN_STM32FD_DATA_INST(inst) \
207-
static struct can_mcan_data can_mcan_dev_data_##inst = { \
208-
.msg_ram = (struct can_mcan_msg_sram *) \
166+
static struct can_mcan_data can_mcan_data_##inst = \
167+
CAN_MCAN_DATA_INITIALIZER((struct can_mcan_msg_sram *) \
209168
DT_INST_REG_ADDR_BY_NAME(inst, message_ram), \
210-
};
169+
NULL);
211170

212171
#define CAN_STM32FD_DEVICE_INST(inst) \
213172
DEVICE_DT_INST_DEFINE(inst, &can_stm32fd_init, NULL, \
214-
&can_mcan_dev_data_##inst, &can_mcan_cfg_##inst, \
173+
&can_mcan_data_##inst, &can_mcan_cfg_##inst, \
215174
POST_KERNEL, CONFIG_CAN_INIT_PRIORITY, \
216175
&can_stm32fd_driver_api);
217176

drivers/can/can_stm32h7.c

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -148,45 +148,6 @@ static const struct can_driver_api can_stm32h7_driver_api = {
148148
#endif
149149
};
150150

151-
#ifdef CONFIG_CAN_FD_MODE
152-
#define CAN_STM32H7_MCAN_MCAN_INIT(n) \
153-
{ \
154-
.can = (struct can_mcan_reg *)DT_INST_REG_ADDR(n), \
155-
.bus_speed = DT_INST_PROP(n, bus_speed), \
156-
.sjw = DT_INST_PROP(n, sjw), \
157-
.sample_point = DT_INST_PROP_OR(n, sample_point, 0), \
158-
.prop_ts1 = DT_INST_PROP_OR(n, prop_seg, 0) + \
159-
DT_INST_PROP_OR(n, phase_seg1, 0), \
160-
.ts2 = DT_INST_PROP_OR(n, phase_seg2, 0), \
161-
.bus_speed_data = DT_INST_PROP(n, bus_speed_data), \
162-
.sjw_data = DT_INST_PROP(n, sjw_data), \
163-
.sample_point_data = \
164-
DT_INST_PROP_OR(n, sample_point_data, 0), \
165-
.prop_ts1_data = DT_INST_PROP_OR(n, prop_seg_data, 0) + \
166-
DT_INST_PROP_OR(n, phase_seg1_data, 0), \
167-
.ts2_data = DT_INST_PROP_OR(n, phase_seg2_data, 0), \
168-
.tx_delay_comp_offset = \
169-
DT_INST_PROP(n, tx_delay_comp_offset), \
170-
.phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(n, phys)), \
171-
.max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(n, 5000000), \
172-
.custom = &can_stm32h7_cfg_##n, \
173-
}
174-
#else /* CONFIG_CAN_FD_MODE */
175-
#define CAN_STM32H7_MCAN_MCAN_INIT(n) \
176-
{ \
177-
.can = (struct can_mcan_reg *)DT_INST_REG_ADDR(n), \
178-
.bus_speed = DT_INST_PROP(n, bus_speed), \
179-
.sjw = DT_INST_PROP(n, sjw), \
180-
.sample_point = DT_INST_PROP_OR(n, sample_point, 0), \
181-
.prop_ts1 = DT_INST_PROP_OR(n, prop_seg, 0) + \
182-
DT_INST_PROP_OR(n, phase_seg1, 0), \
183-
.ts2 = DT_INST_PROP_OR(n, phase_seg2, 0), \
184-
.phy = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(n, phys)), \
185-
.max_bitrate = DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(n, 1000000), \
186-
.custom = &can_stm32h7_cfg_##n, \
187-
}
188-
#endif /* !CONFIG_CAN_FD_MODE */
189-
190151
#define CAN_STM32H7_MCAN_INIT(n) \
191152
static void stm32h7_mcan_irq_config_##n(void); \
192153
\
@@ -202,12 +163,11 @@ static const struct can_driver_api can_stm32h7_driver_api = {
202163
}; \
203164
\
204165
static const struct can_mcan_config can_mcan_cfg_##n = \
205-
CAN_STM32H7_MCAN_MCAN_INIT(n); \
166+
CAN_MCAN_DT_CONFIG_INST_GET(n, &can_stm32h7_cfg_##n); \
206167
\
207-
static struct can_mcan_data can_mcan_data_##n = { \
208-
.msg_ram = (struct can_mcan_msg_sram *) \
209-
DT_INST_REG_ADDR_BY_NAME(n, message_ram), \
210-
}; \
168+
static struct can_mcan_data can_mcan_data_##n = \
169+
CAN_MCAN_DATA_INITIALIZER((struct can_mcan_msg_sram *) \
170+
DT_INST_REG_ADDR_BY_NAME(n, message_ram), NULL); \
211171
\
212172
DEVICE_DT_INST_DEFINE(n, &can_stm32h7_init, NULL, \
213173
&can_mcan_data_##n, \

dts/arm/nxp/nxp_lpc55S1x_common.dtsi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
can0: can@9d000 {
222222
compatible = "nxp,lpc-mcan";
223223
reg = <0x9d000 0x1000>;
224+
reg-names = "m_can";
224225
interrupts = <43 0>, <44 0>;
225226
clocks = <&syscon MCUX_MCAN_CLK>;
226227
label = "CAN_0";

0 commit comments

Comments
 (0)