Line data Source code
1 1 : /**
2 : * @file
3 : * @brief SPI Devicetree macro public API header file.
4 : */
5 :
6 : /*
7 : * Copyright (c) 2020 Nordic Semiconductor
8 : *
9 : * SPDX-License-Identifier: Apache-2.0
10 : */
11 :
12 : #ifndef ZEPHYR_INCLUDE_DEVICETREE_SPI_H_
13 : #define ZEPHYR_INCLUDE_DEVICETREE_SPI_H_
14 :
15 : #ifdef __cplusplus
16 : extern "C" {
17 : #endif
18 :
19 : /**
20 : * @defgroup devicetree-spi Devicetree SPI API
21 : * @ingroup devicetree
22 : * @ingroup spi_interface
23 : * @{
24 : */
25 :
26 : /**
27 : * @brief Does a SPI controller node have chip select GPIOs configured?
28 : *
29 : * SPI bus controllers use the "cs-gpios" property for configuring
30 : * chip select GPIOs. Its value is a phandle-array which specifies the
31 : * chip select lines.
32 : *
33 : * Example devicetree fragment:
34 : *
35 : * spi1: spi@... {
36 : * compatible = "vnd,spi";
37 : * cs-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>,
38 : * <&gpio2 20 GPIO_ACTIVE_LOW>;
39 : * };
40 : *
41 : * spi2: spi@... {
42 : * compatible = "vnd,spi";
43 : * };
44 : *
45 : * Example usage:
46 : *
47 : * DT_SPI_HAS_CS_GPIOS(DT_NODELABEL(spi1)) // 1
48 : * DT_SPI_HAS_CS_GPIOS(DT_NODELABEL(spi2)) // 0
49 : *
50 : * @param spi a SPI bus controller node identifier
51 : * @return 1 if "spi" has a cs-gpios property, 0 otherwise
52 : */
53 1 : #define DT_SPI_HAS_CS_GPIOS(spi) DT_NODE_HAS_PROP(spi, cs_gpios)
54 :
55 : /**
56 : * @brief Number of chip select GPIOs in a SPI controller's cs-gpios property
57 : *
58 : * Example devicetree fragment:
59 : *
60 : * spi1: spi@... {
61 : * compatible = "vnd,spi";
62 : * cs-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>,
63 : * <&gpio2 20 GPIO_ACTIVE_LOW>;
64 : * };
65 : *
66 : * spi2: spi@... {
67 : * compatible = "vnd,spi";
68 : * };
69 : *
70 : * Example usage:
71 : *
72 : * DT_SPI_NUM_CS_GPIOS(DT_NODELABEL(spi1)) // 2
73 : * DT_SPI_NUM_CS_GPIOS(DT_NODELABEL(spi2)) // 0
74 : *
75 : * @param spi a SPI bus controller node identifier
76 : * @return Logical length of spi's cs-gpios property, or 0 if "spi" doesn't
77 : * have a cs-gpios property
78 : */
79 1 : #define DT_SPI_NUM_CS_GPIOS(spi) \
80 : COND_CODE_1(DT_SPI_HAS_CS_GPIOS(spi), \
81 : (DT_PROP_LEN(spi, cs_gpios)), (0))
82 :
83 : /**
84 : * @brief Does a SPI device have a chip select line configured?
85 : * Example devicetree fragment:
86 : *
87 : * spi1: spi@... {
88 : * compatible = "vnd,spi";
89 : * cs-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>,
90 : * <&gpio2 20 GPIO_ACTIVE_LOW>;
91 : *
92 : * a: spi-dev-a@0 {
93 : * reg = <0>;
94 : * };
95 : *
96 : * b: spi-dev-b@1 {
97 : * reg = <1>;
98 : * };
99 : * };
100 : *
101 : * spi2: spi@... {
102 : * compatible = "vnd,spi";
103 : * c: spi-dev-c@0 {
104 : * reg = <0>;
105 : * };
106 : * };
107 : *
108 : * Example usage:
109 : *
110 : * DT_SPI_DEV_HAS_CS_GPIOS(DT_NODELABEL(a)) // 1
111 : * DT_SPI_DEV_HAS_CS_GPIOS(DT_NODELABEL(b)) // 1
112 : * DT_SPI_DEV_HAS_CS_GPIOS(DT_NODELABEL(c)) // 0
113 : *
114 : * @param spi_dev a SPI device node identifier
115 : * @return 1 if spi_dev's bus node DT_BUS(spi_dev) has a chip select
116 : * pin at index DT_REG_ADDR(spi_dev), 0 otherwise
117 : */
118 1 : #define DT_SPI_DEV_HAS_CS_GPIOS(spi_dev) DT_SPI_HAS_CS_GPIOS(DT_BUS(spi_dev))
119 :
120 : /**
121 : * @brief Get a SPI device's chip select GPIO controller's node identifier
122 : *
123 : * Example devicetree fragment:
124 : *
125 : * gpio1: gpio@... { ... };
126 : *
127 : * gpio2: gpio@... { ... };
128 : *
129 : * spi@... {
130 : * compatible = "vnd,spi";
131 : * cs-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>,
132 : * <&gpio2 20 GPIO_ACTIVE_LOW>;
133 : *
134 : * a: spi-dev-a@0 {
135 : * reg = <0>;
136 : * };
137 : *
138 : * b: spi-dev-b@1 {
139 : * reg = <1>;
140 : * };
141 : * };
142 : *
143 : * Example usage:
144 : *
145 : * DT_SPI_DEV_CS_GPIOS_CTLR(DT_NODELABEL(a)) // DT_NODELABEL(gpio1)
146 : * DT_SPI_DEV_CS_GPIOS_CTLR(DT_NODELABEL(b)) // DT_NODELABEL(gpio2)
147 : *
148 : * @param spi_dev a SPI device node identifier
149 : * @return node identifier for spi_dev's chip select GPIO controller
150 : */
151 1 : #define DT_SPI_DEV_CS_GPIOS_CTLR(spi_dev) \
152 : DT_GPIO_CTLR_BY_IDX(DT_BUS(spi_dev), cs_gpios, DT_REG_ADDR_RAW(spi_dev))
153 :
154 : /**
155 : * @brief Get a SPI device's chip select GPIO pin number
156 : *
157 : * It's an error if the GPIO specifier for spi_dev's entry in its
158 : * bus node's cs-gpios property has no pin cell.
159 : *
160 : * Example devicetree fragment:
161 : *
162 : * spi1: spi@... {
163 : * compatible = "vnd,spi";
164 : * cs-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>,
165 : * <&gpio2 20 GPIO_ACTIVE_LOW>;
166 : *
167 : * a: spi-dev-a@0 {
168 : * reg = <0>;
169 : * };
170 : *
171 : * b: spi-dev-b@1 {
172 : * reg = <1>;
173 : * };
174 : * };
175 : *
176 : * Example usage:
177 : *
178 : * DT_SPI_DEV_CS_GPIOS_PIN(DT_NODELABEL(a)) // 10
179 : * DT_SPI_DEV_CS_GPIOS_PIN(DT_NODELABEL(b)) // 20
180 : *
181 : * @param spi_dev a SPI device node identifier
182 : * @return pin number of spi_dev's chip select GPIO
183 : */
184 1 : #define DT_SPI_DEV_CS_GPIOS_PIN(spi_dev) \
185 : DT_GPIO_PIN_BY_IDX(DT_BUS(spi_dev), cs_gpios, DT_REG_ADDR_RAW(spi_dev))
186 :
187 : /**
188 : * @brief Get a SPI device's chip select GPIO flags
189 : *
190 : * Example devicetree fragment:
191 : *
192 : * spi1: spi@... {
193 : * compatible = "vnd,spi";
194 : * cs-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>;
195 : *
196 : * a: spi-dev-a@0 {
197 : * reg = <0>;
198 : * };
199 : * };
200 : *
201 : * Example usage:
202 : *
203 : * DT_SPI_DEV_CS_GPIOS_FLAGS(DT_NODELABEL(a)) // GPIO_ACTIVE_LOW
204 : *
205 : * If the GPIO specifier for spi_dev's entry in its bus node's
206 : * cs-gpios property has no flags cell, this expands to zero.
207 : *
208 : * @param spi_dev a SPI device node identifier
209 : * @return flags value of spi_dev's chip select GPIO specifier, or
210 : * zero if there is none
211 : */
212 1 : #define DT_SPI_DEV_CS_GPIOS_FLAGS(spi_dev) \
213 : DT_GPIO_FLAGS_BY_IDX(DT_BUS(spi_dev), cs_gpios, DT_REG_ADDR_RAW(spi_dev))
214 :
215 : /**
216 : * @brief Equivalent to DT_SPI_DEV_HAS_CS_GPIOS(DT_DRV_INST(inst)).
217 : * @param inst DT_DRV_COMPAT instance number
218 : * @return 1 if the instance's bus has a CS pin at index
219 : * DT_INST_REG_ADDR(inst), 0 otherwise
220 : * @see DT_SPI_DEV_HAS_CS_GPIOS()
221 : */
222 1 : #define DT_INST_SPI_DEV_HAS_CS_GPIOS(inst) \
223 : DT_SPI_DEV_HAS_CS_GPIOS(DT_DRV_INST(inst))
224 :
225 : /**
226 : * @brief Get GPIO controller node identifier for a SPI device instance
227 : * This is equivalent to DT_SPI_DEV_CS_GPIOS_CTLR(DT_DRV_INST(inst)).
228 : * @param inst DT_DRV_COMPAT instance number
229 : * @return node identifier for instance's chip select GPIO controller
230 : * @see DT_SPI_DEV_CS_GPIOS_CTLR()
231 : */
232 1 : #define DT_INST_SPI_DEV_CS_GPIOS_CTLR(inst) \
233 : DT_SPI_DEV_CS_GPIOS_CTLR(DT_DRV_INST(inst))
234 :
235 : /**
236 : * @brief Equivalent to DT_SPI_DEV_CS_GPIOS_PIN(DT_DRV_INST(inst)).
237 : * @param inst DT_DRV_COMPAT instance number
238 : * @return pin number of the instance's chip select GPIO
239 : * @see DT_SPI_DEV_CS_GPIOS_PIN()
240 : */
241 1 : #define DT_INST_SPI_DEV_CS_GPIOS_PIN(inst) \
242 : DT_SPI_DEV_CS_GPIOS_PIN(DT_DRV_INST(inst))
243 :
244 : /**
245 : * @brief DT_SPI_DEV_CS_GPIOS_FLAGS(DT_DRV_INST(inst)).
246 : * @param inst DT_DRV_COMPAT instance number
247 : * @return flags value of the instance's chip select GPIO specifier,
248 : * or zero if there is none
249 : * @see DT_SPI_DEV_CS_GPIOS_FLAGS()
250 : */
251 1 : #define DT_INST_SPI_DEV_CS_GPIOS_FLAGS(inst) \
252 : DT_SPI_DEV_CS_GPIOS_FLAGS(DT_DRV_INST(inst))
253 :
254 : /**
255 : * @}
256 : */
257 :
258 : #ifdef __cplusplus
259 : }
260 : #endif
261 :
262 : #endif /* ZEPHYR_INCLUDE_DEVICETREE_SPI_H_ */
|