1 /**
2  * @file
3  * @brief CAN devicetree macro public API header file.
4  */
5 
6 /*
7  * Copyright (c) 2022 Vestas Wind Systems A/S
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_DEVICETREE_CAN_H_
13 #define ZEPHYR_INCLUDE_DEVICETREE_CAN_H_
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /**
20  * @defgroup devicetree-can Devicetree CAN API
21  * @ingroup devicetree
22  * @{
23  */
24 
25 /**
26  * @brief Get the minimum transceiver bitrate for a CAN controller
27  *
28  * The bitrate will be limited to the minimum bitrate supported by the CAN
29  * controller. If no CAN transceiver is present in the devicetree, the minimum
30  * bitrate will be that of the CAN controller.
31  *
32  * Example devicetree fragment:
33  *
34  *     transceiver0: can-phy0 {
35  *             compatible = "vnd,can-transceiver";
36  *             min-bitrate = <15000>;
37  *             max-bitrate = <1000000>;
38  *             #phy-cells = <0>;
39  *     };
40  *
41  *     can0: can@... {
42  *             compatible = "vnd,can-controller";
43  *             phys = <&transceiver0>;
44  *     };
45  *
46  *     can1: can@... {
47  *             compatible = "vnd,can-controller";
48  *
49  *             can-transceiver {
50  *                     min-bitrate = <25000>;
51  *                     max-bitrate = <2000000>;
52  *             };
53  *     };
54  *
55  *     can2: can@... {
56  *             compatible = "vnd,can-controller";
57  *
58  *             can-transceiver {
59  *                     max-bitrate = <2000000>;
60  *             };
61  *     };
62  *
63  * Example usage:
64  *
65  *     DT_CAN_TRANSCEIVER_MIN_BITRATE(DT_NODELABEL(can0), 10000) // 15000
66  *     DT_CAN_TRANSCEIVER_MIN_BITRATE(DT_NODELABEL(can1), 0)     // 250000
67  *     DT_CAN_TRANSCEIVER_MIN_BITRATE(DT_NODELABEL(can1), 50000) // 500000
68  *     DT_CAN_TRANSCEIVER_MIN_BITRATE(DT_NODELABEL(can2), 0)     // 0
69  *
70  * @param node_id node identifier
71  * @param min minimum bitrate supported by the CAN controller
72  * @return the minimum bitrate supported by the CAN controller/transceiver combination
73  */
74 #define DT_CAN_TRANSCEIVER_MIN_BITRATE(node_id, min)					\
75 	COND_CODE_1(DT_NODE_HAS_PROP(node_id, phys),					\
76 		    MAX(DT_PROP_OR(DT_PHANDLE(node_id, phys), min_bitrate, 0), min),	\
77 		    MAX(DT_PROP_OR(DT_CHILD(node_id, can_transceiver), min_bitrate, min), min))
78 
79 /**
80  * @brief Get the maximum transceiver bitrate for a CAN controller
81  *
82  * The bitrate will be limited to the maximum bitrate supported by the CAN
83  * controller. If no CAN transceiver is present in the devicetree, the maximum
84  * bitrate will be that of the CAN controller.
85  *
86  * Example devicetree fragment:
87  *
88  *     transceiver0: can-phy0 {
89  *             compatible = "vnd,can-transceiver";
90  *             max-bitrate = <1000000>;
91  *             #phy-cells = <0>;
92  *     };
93  *
94  *     can0: can@... {
95  *             compatible = "vnd,can-controller";
96  *             phys = <&transceiver0>;
97  *     };
98  *
99  *     can1: can@... {
100  *             compatible = "vnd,can-controller";
101  *
102  *             can-transceiver {
103  *                     max-bitrate = <2000000>;
104  *             };
105  *     };
106  *
107  * Example usage:
108  *
109  *     DT_CAN_TRANSCEIVER_MAX_BITRATE(DT_NODELABEL(can0), 5000000) // 1000000
110  *     DT_CAN_TRANSCEIVER_MAX_BITRATE(DT_NODELABEL(can1), 5000000) // 2000000
111  *     DT_CAN_TRANSCEIVER_MAX_BITRATE(DT_NODELABEL(can1), 1000000) // 1000000
112  *
113  * @param node_id node identifier
114  * @param max maximum bitrate supported by the CAN controller
115  * @return the maximum bitrate supported by the CAN controller/transceiver combination
116  */
117 #define DT_CAN_TRANSCEIVER_MAX_BITRATE(node_id, max)					\
118 	COND_CODE_1(DT_NODE_HAS_PROP(node_id, phys),					\
119 		    MIN(DT_PROP(DT_PHANDLE(node_id, phys), max_bitrate), max),		\
120 		    MIN(DT_PROP_OR(DT_CHILD(node_id, can_transceiver), max_bitrate, max), max))
121 
122 /**
123  * @brief Get the minimum transceiver bitrate for a DT_DRV_COMPAT CAN controller
124  * @param inst DT_DRV_COMPAT instance number
125  * @param min minimum bitrate supported by the CAN controller
126  * @return the minimum bitrate supported by the CAN controller/transceiver combination
127  * @see DT_CAN_TRANSCEIVER_MIN_BITRATE()
128  */
129 #define DT_INST_CAN_TRANSCEIVER_MIN_BITRATE(inst, min) \
130 	DT_CAN_TRANSCEIVER_MIN_BITRATE(DT_DRV_INST(inst), min)
131 
132 /**
133  * @brief Get the maximum transceiver bitrate for a DT_DRV_COMPAT CAN controller
134  * @param inst DT_DRV_COMPAT instance number
135  * @param max maximum bitrate supported by the CAN controller
136  * @return the maximum bitrate supported by the CAN controller/transceiver combination
137  * @see DT_CAN_TRANSCEIVER_MAX_BITRATE()
138  */
139 #define DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(inst, max) \
140 	DT_CAN_TRANSCEIVER_MAX_BITRATE(DT_DRV_INST(inst), max)
141 
142 /**
143  * @}
144  */
145 
146 #ifdef __cplusplus
147 }
148 #endif
149 
150 #endif /* ZEPHYR_INCLUDE_DEVICETREE_CAN_H_ */
151