From 6d35ec718fcb17346db92195fb208d9b136c1f4b Mon Sep 17 00:00:00 2001 From: Henrik Brix Andersen Date: Mon, 26 Feb 2024 22:34:59 +0100 Subject: [PATCH] drivers: can: add support for specifying minimum supported CAN bitrate Add support for specifying the minimum bitrate supported by a CAN controller in CAN_DT_DRIVER_CONFIG_GET() and CAN_DT_DRIVER_CONFIG_INST_GET(). Signed-off-by: Henrik Brix Andersen --- drivers/can/can_fake.c | 2 +- drivers/can/can_loopback.c | 2 +- drivers/can/can_mcp2515.c | 2 +- drivers/can/can_mcp251xfd.c | 2 +- drivers/can/can_mcux_flexcan.c | 2 +- drivers/can/can_native_linux.c | 2 +- drivers/can/can_nxp_s32_canxl.c | 2 +- drivers/can/can_rcar.c | 2 +- drivers/can/can_stm32_bxcan.c | 2 +- drivers/can/can_xmc4xxx.c | 2 +- include/zephyr/drivers/can.h | 11 ++++++++--- include/zephyr/drivers/can/can_mcan.h | 4 ++-- include/zephyr/drivers/can/can_sja1000.h | 2 +- 13 files changed, 21 insertions(+), 16 deletions(-) diff --git a/drivers/can/can_fake.c b/drivers/can/can_fake.c index 9da4ad80b236eb..7725ebfa13b4b6 100644 --- a/drivers/can/can_fake.c +++ b/drivers/can/can_fake.c @@ -144,7 +144,7 @@ static const struct can_driver_api fake_can_driver_api = { #define FAKE_CAN_INIT(inst) \ static const struct fake_can_config fake_can_config_##inst = { \ - .common = CAN_DT_DRIVER_CONFIG_INST_GET(inst, 0U), \ + .common = CAN_DT_DRIVER_CONFIG_INST_GET(inst, 0, 0), \ }; \ \ static struct fake_can_data fake_can_data_##inst; \ diff --git a/drivers/can/can_loopback.c b/drivers/can/can_loopback.c index dcfb2a6cce86b9..5304c801ea44cc 100644 --- a/drivers/can/can_loopback.c +++ b/drivers/can/can_loopback.c @@ -440,7 +440,7 @@ static int can_loopback_init(const struct device *dev) #define CAN_LOOPBACK_INIT(inst) \ static const struct can_loopback_config can_loopback_config_##inst = { \ - .common = CAN_DT_DRIVER_CONFIG_INST_GET(inst, 0U), \ + .common = CAN_DT_DRIVER_CONFIG_INST_GET(inst, 0, 0), \ }; \ \ static struct can_loopback_data can_loopback_data_##inst; \ diff --git a/drivers/can/can_mcp2515.c b/drivers/can/can_mcp2515.c index 692015cdff3d49..283bd325262c2a 100644 --- a/drivers/can/can_mcp2515.c +++ b/drivers/can/can_mcp2515.c @@ -1008,7 +1008,7 @@ static int mcp2515_init(const struct device *dev) }; \ \ static const struct mcp2515_config mcp2515_config_##inst = { \ - .common = CAN_DT_DRIVER_CONFIG_INST_GET(inst, 1000000), \ + .common = CAN_DT_DRIVER_CONFIG_INST_GET(inst, 0, 1000000), \ .bus = SPI_DT_SPEC_INST_GET(inst, SPI_WORD_SET(8), 0), \ .int_gpio = GPIO_DT_SPEC_INST_GET(inst, int_gpios), \ .int_thread_stack_size = CONFIG_CAN_MCP2515_INT_THREAD_STACK_SIZE, \ diff --git a/drivers/can/can_mcp251xfd.c b/drivers/can/can_mcp251xfd.c index 04ee18bba2a020..24f66f4eea63f2 100644 --- a/drivers/can/can_mcp251xfd.c +++ b/drivers/can/can_mcp251xfd.c @@ -1681,7 +1681,7 @@ static const struct can_driver_api mcp251xfd_api_funcs = { .int_thread_stack = mcp251xfd_int_stack_##inst, \ }; \ static const struct mcp251xfd_config mcp251xfd_config_##inst = { \ - .common = CAN_DT_DRIVER_CONFIG_INST_GET(inst, 8000000), \ + .common = CAN_DT_DRIVER_CONFIG_INST_GET(inst, 0, 8000000), \ .bus = SPI_DT_SPEC_INST_GET(inst, SPI_WORD_SET(8), 0), \ .int_gpio_dt = GPIO_DT_SPEC_INST_GET(inst, int_gpios), \ \ diff --git a/drivers/can/can_mcux_flexcan.c b/drivers/can/can_mcux_flexcan.c index 7a1e5dde4e3787..38642ca3d992e1 100644 --- a/drivers/can/can_mcux_flexcan.c +++ b/drivers/can/can_mcux_flexcan.c @@ -1400,7 +1400,7 @@ static const struct can_driver_api mcux_flexcan_fd_driver_api = { static void mcux_flexcan_irq_disable_##id(void); \ \ static const struct mcux_flexcan_config mcux_flexcan_config_##id = { \ - .common = CAN_DT_DRIVER_CONFIG_INST_GET(id, FLEXCAN_MAX_BITRATE(id)), \ + .common = CAN_DT_DRIVER_CONFIG_INST_GET(id, 0, FLEXCAN_MAX_BITRATE(id)), \ .base = (CAN_Type *)DT_INST_REG_ADDR(id), \ .clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(id)), \ .clock_subsys = (clock_control_subsys_t) \ diff --git a/drivers/can/can_native_linux.c b/drivers/can/can_native_linux.c index 9356588a411324..fb3ff59caa94cd 100644 --- a/drivers/can/can_native_linux.c +++ b/drivers/can/can_native_linux.c @@ -471,7 +471,7 @@ static int can_native_linux_init(const struct device *dev) #define CAN_NATIVE_LINUX_INIT(inst) \ \ static const struct can_native_linux_config can_native_linux_cfg_##inst = { \ - .common = CAN_DT_DRIVER_CONFIG_INST_GET(inst, 0), \ + .common = CAN_DT_DRIVER_CONFIG_INST_GET(inst, 0, 0), \ .if_name = DT_INST_PROP(inst, host_interface), \ }; \ \ diff --git a/drivers/can/can_nxp_s32_canxl.c b/drivers/can/can_nxp_s32_canxl.c index 75df1c65858be8..a568375069659c 100644 --- a/drivers/can/can_nxp_s32_canxl.c +++ b/drivers/can/can_nxp_s32_canxl.c @@ -1151,7 +1151,7 @@ static const struct can_driver_api can_nxp_s32_driver_api = { (Canexcel_Ip_RxFifoFilterID_ADDR *)&rx_fifo_filter##n,))\ }; \ static struct can_nxp_s32_config can_nxp_s32_config_##n = { \ - .common = CAN_DT_DRIVER_CONFIG_INST_GET(n, CAN_NXP_S32_MAX_BITRATE), \ + .common = CAN_DT_DRIVER_CONFIG_INST_GET(n, 0, CAN_NXP_S32_MAX_BITRATE), \ .base_sic = (CANXL_SIC_Type *)DT_INST_REG_ADDR_BY_NAME(n, sic), \ IF_ENABLED(CONFIG_CAN_NXP_S32_RX_FIFO, \ (.base_rx_fifo = (CANXL_RXFIFO_Type *) \ diff --git a/drivers/can/can_rcar.c b/drivers/can/can_rcar.c index b389b638a3b753..eacebb9222b3a5 100644 --- a/drivers/can/can_rcar.c +++ b/drivers/can/can_rcar.c @@ -1190,7 +1190,7 @@ static const struct can_driver_api can_rcar_driver_api = { PINCTRL_DT_INST_DEFINE(n); \ static void can_rcar_##n##_init(const struct device *dev); \ static const struct can_rcar_cfg can_rcar_cfg_##n = { \ - .common = CAN_DT_DRIVER_CONFIG_INST_GET(n, 1000000), \ + .common = CAN_DT_DRIVER_CONFIG_INST_GET(n, 0, 1000000), \ .reg_addr = DT_INST_REG_ADDR(n), \ .reg_size = DT_INST_REG_SIZE(n), \ .init_func = can_rcar_##n##_init, \ diff --git a/drivers/can/can_stm32_bxcan.c b/drivers/can/can_stm32_bxcan.c index 212d7622d9c09b..3f8060944e95c8 100644 --- a/drivers/can/can_stm32_bxcan.c +++ b/drivers/can/can_stm32_bxcan.c @@ -1139,7 +1139,7 @@ static void config_can_##inst##_irq(CAN_TypeDef *can) \ #define CAN_STM32_CONFIG_INST(inst) \ PINCTRL_DT_INST_DEFINE(inst); \ static const struct can_stm32_config can_stm32_cfg_##inst = { \ - .common = CAN_DT_DRIVER_CONFIG_INST_GET(inst, 1000000), \ + .common = CAN_DT_DRIVER_CONFIG_INST_GET(inst, 0, 1000000), \ .can = (CAN_TypeDef *)DT_INST_REG_ADDR(inst), \ .master_can = (CAN_TypeDef *)DT_INST_PROP_OR(inst, \ master_can_reg, DT_INST_REG_ADDR(inst)), \ diff --git a/drivers/can/can_xmc4xxx.c b/drivers/can/can_xmc4xxx.c index 3dc0bb9380c395..cde7cf0654d0a6 100644 --- a/drivers/can/can_xmc4xxx.c +++ b/drivers/can/can_xmc4xxx.c @@ -925,7 +925,7 @@ static const struct can_driver_api can_xmc4xxx_api_funcs = { \ static struct can_xmc4xxx_data can_xmc4xxx_data_##inst; \ static const struct can_xmc4xxx_config can_xmc4xxx_config_##inst = { \ - .common = CAN_DT_DRIVER_CONFIG_INST_GET(inst, 1000000), \ + .common = CAN_DT_DRIVER_CONFIG_INST_GET(inst, 0, 1000000), \ .can = (CAN_NODE_TypeDef *)DT_INST_REG_ADDR(inst), \ .clock_div8 = DT_INST_PROP(inst, clock_div8), \ .irq_config_func = can_xmc4xxx_irq_config_##inst, \ diff --git a/include/zephyr/drivers/can.h b/include/zephyr/drivers/can.h index 62e1047f073161..4653db11846465 100644 --- a/include/zephyr/drivers/can.h +++ b/include/zephyr/drivers/can.h @@ -329,6 +329,8 @@ typedef void (*can_state_change_callback_t)(const struct device *dev, struct can_driver_config { /** Pointer to the device structure for the associated CAN transceiver device or NULL. */ const struct device *phy; + /** The minimum bitrate supported by the CAN controller/transceiver combination. */ + uint32_t min_bitrate; /** The maximum bitrate supported by the CAN controller/transceiver combination. */ uint32_t max_bitrate; /** Initial CAN classic/CAN FD arbitration phase bitrate. */ @@ -347,11 +349,13 @@ struct can_driver_config { * @brief Static initializer for @p can_driver_config struct * * @param node_id Devicetree node identifier + * @param _min_bitrate minimum bitrate supported by the CAN controller * @param _max_bitrate maximum bitrate supported by the CAN controller */ -#define CAN_DT_DRIVER_CONFIG_GET(node_id, _max_bitrate) \ +#define CAN_DT_DRIVER_CONFIG_GET(node_id, _min_bitrate, _max_bitrate) \ { \ .phy = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(node_id, phys)), \ + .min_bitrate = DT_CAN_TRANSCEIVER_MIN_BITRATE(node_id, _min_bitrate), \ .max_bitrate = DT_CAN_TRANSCEIVER_MAX_BITRATE(node_id, _max_bitrate), \ .bus_speed = DT_PROP(node_id, bus_speed), \ .sample_point = DT_PROP_OR(node_id, sample_point, 0), \ @@ -364,11 +368,12 @@ struct can_driver_config { * @brief Static initializer for @p can_driver_config struct from DT_DRV_COMPAT instance * * @param inst DT_DRV_COMPAT instance number + * @param _min_bitrate minimum bitrate supported by the CAN controller * @param _max_bitrate maximum bitrate supported by the CAN controller * @see CAN_DT_DRIVER_CONFIG_GET() */ -#define CAN_DT_DRIVER_CONFIG_INST_GET(inst, _max_bitrate) \ - CAN_DT_DRIVER_CONFIG_GET(DT_DRV_INST(inst), _max_bitrate) +#define CAN_DT_DRIVER_CONFIG_INST_GET(inst, _min_bitrate, _max_bitrate) \ + CAN_DT_DRIVER_CONFIG_GET(DT_DRV_INST(inst), _min_bitrate, _max_bitrate) /** * @brief Common CAN controller driver data. diff --git a/include/zephyr/drivers/can/can_mcan.h b/include/zephyr/drivers/can/can_mcan.h index ca7190412c273f..8a4241aa760ffd 100644 --- a/include/zephyr/drivers/can/can_mcan.h +++ b/include/zephyr/drivers/can/can_mcan.h @@ -1294,7 +1294,7 @@ struct can_mcan_config { #ifdef CONFIG_CAN_FD_MODE #define CAN_MCAN_DT_CONFIG_GET(node_id, _custom, _ops, _cbs) \ { \ - .common = CAN_DT_DRIVER_CONFIG_GET(node_id, 8000000), \ + .common = CAN_DT_DRIVER_CONFIG_GET(node_id, 0, 8000000), \ .ops = _ops, \ .callbacks = _cbs, \ .mram_elements = CAN_MCAN_DT_MRAM_ELEMENTS_GET(node_id), \ @@ -1306,7 +1306,7 @@ struct can_mcan_config { #else /* CONFIG_CAN_FD_MODE */ #define CAN_MCAN_DT_CONFIG_GET(node_id, _custom, _ops, _cbs) \ { \ - .common = CAN_DT_DRIVER_CONFIG_GET(node_id, 8000000), \ + .common = CAN_DT_DRIVER_CONFIG_GET(node_id, 0, 1000000), \ .ops = _ops, \ .callbacks = _cbs, \ .mram_elements = CAN_MCAN_DT_MRAM_ELEMENTS_GET(node_id), \ diff --git a/include/zephyr/drivers/can/can_sja1000.h b/include/zephyr/drivers/can/can_sja1000.h index de11d69031dca9..1d50f9b9a46ed8 100644 --- a/include/zephyr/drivers/can/can_sja1000.h +++ b/include/zephyr/drivers/can/can_sja1000.h @@ -122,7 +122,7 @@ struct can_sja1000_config { */ #define CAN_SJA1000_DT_CONFIG_GET(node_id, _custom, _read_reg, _write_reg, _ocr, _cdr) \ { \ - .common = CAN_DT_DRIVER_CONFIG_GET(node_id, 1000000), \ + .common = CAN_DT_DRIVER_CONFIG_GET(node_id, 0, 1000000), \ .read_reg = _read_reg, \ .write_reg = _write_reg, \ .ocr = _ocr, \