1 /*
2 * Copyright (c) 2019-2020 Nordic Semiconductor ASA
3 * Copyright (c) 2019 Piotr Mienkowski
4 * Copyright (c) 2017 ARM Ltd
5 * Copyright (c) 2015-2016 Intel Corporation.
6 *
7 * SPDX-License-Identifier: Apache-2.0
8 */
9
10 /**
11 * @file
12 * @brief Public APIs for GPIO drivers
13 */
14
15 #ifndef ZEPHYR_INCLUDE_DRIVERS_GPIO_H_
16 #define ZEPHYR_INCLUDE_DRIVERS_GPIO_H_
17
18 #include <errno.h>
19
20 #include <zephyr/sys/__assert.h>
21 #include <zephyr/sys/slist.h>
22
23 #include <zephyr/types.h>
24 #include <stddef.h>
25 #include <zephyr/device.h>
26 #include <zephyr/dt-bindings/gpio/gpio.h>
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 /**
33 * @brief GPIO Driver APIs
34 * @defgroup gpio_interface GPIO Driver APIs
35 * @ingroup io_interfaces
36 * @{
37 */
38
39 /**
40 * @name GPIO input/output configuration flags
41 * @{
42 */
43
44 /** Enables pin as input. */
45 #define GPIO_INPUT (1U << 16)
46
47 /** Enables pin as output, no change to the output state. */
48 #define GPIO_OUTPUT (1U << 17)
49
50 /** Disables pin for both input and output. */
51 #define GPIO_DISCONNECTED 0
52
53 /** @cond INTERNAL_HIDDEN */
54
55 /* Initializes output to a low state. */
56 #define GPIO_OUTPUT_INIT_LOW (1U << 18)
57
58 /* Initializes output to a high state. */
59 #define GPIO_OUTPUT_INIT_HIGH (1U << 19)
60
61 /* Initializes output based on logic level */
62 #define GPIO_OUTPUT_INIT_LOGICAL (1U << 20)
63
64 /** @endcond */
65
66 /** Configures GPIO pin as output and initializes it to a low state. */
67 #define GPIO_OUTPUT_LOW (GPIO_OUTPUT | GPIO_OUTPUT_INIT_LOW)
68 /** Configures GPIO pin as output and initializes it to a high state. */
69 #define GPIO_OUTPUT_HIGH (GPIO_OUTPUT | GPIO_OUTPUT_INIT_HIGH)
70 /** Configures GPIO pin as output and initializes it to a logic 0. */
71 #define GPIO_OUTPUT_INACTIVE (GPIO_OUTPUT | \
72 GPIO_OUTPUT_INIT_LOW | \
73 GPIO_OUTPUT_INIT_LOGICAL)
74 /** Configures GPIO pin as output and initializes it to a logic 1. */
75 #define GPIO_OUTPUT_ACTIVE (GPIO_OUTPUT | \
76 GPIO_OUTPUT_INIT_HIGH | \
77 GPIO_OUTPUT_INIT_LOGICAL)
78
79 /** @} */
80
81 /**
82 * @name GPIO interrupt configuration flags
83 * The `GPIO_INT_*` flags are used to specify how input GPIO pins will trigger
84 * interrupts. The interrupts can be sensitive to pin physical or logical level.
85 * Interrupts sensitive to pin logical level take into account GPIO_ACTIVE_LOW
86 * flag. If a pin was configured as Active Low, physical level low will be
87 * considered as logical level 1 (an active state), physical level high will
88 * be considered as logical level 0 (an inactive state).
89 * The GPIO controller should reset the interrupt status, such as clearing the
90 * pending bit, etc, when configuring the interrupt triggering properties.
91 * Applications should use the `GPIO_INT_MODE_ENABLE_ONLY` and
92 * `GPIO_INT_MODE_DISABLE_ONLY` flags to enable and disable interrupts on the
93 * pin without changing any GPIO settings.
94 * @{
95 */
96
97 /** Disables GPIO pin interrupt. */
98 #define GPIO_INT_DISABLE (1U << 21)
99
100 /** @cond INTERNAL_HIDDEN */
101
102 /* Enables GPIO pin interrupt. */
103 #define GPIO_INT_ENABLE (1U << 22)
104
105 /* GPIO interrupt is sensitive to logical levels.
106 *
107 * This is a component flag that should be combined with other
108 * `GPIO_INT_*` flags to produce a meaningful configuration.
109 */
110 #define GPIO_INT_LEVELS_LOGICAL (1U << 23)
111
112 /* GPIO interrupt is edge sensitive.
113 *
114 * Note: by default interrupts are level sensitive.
115 *
116 * This is a component flag that should be combined with other
117 * `GPIO_INT_*` flags to produce a meaningful configuration.
118 */
119 #define GPIO_INT_EDGE (1U << 24)
120
121 /* Trigger detection when input state is (or transitions to) physical low or
122 * logical 0 level.
123 *
124 * This is a component flag that should be combined with other
125 * `GPIO_INT_*` flags to produce a meaningful configuration.
126 */
127 #define GPIO_INT_LOW_0 (1U << 25)
128
129 /* Trigger detection on input state is (or transitions to) physical high or
130 * logical 1 level.
131 *
132 * This is a component flag that should be combined with other
133 * `GPIO_INT_*` flags to produce a meaningful configuration.
134 */
135 #define GPIO_INT_HIGH_1 (1U << 26)
136
137 #ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
138 /* Disable/Enable interrupt functionality without changing other interrupt
139 * related register, such as clearing the pending register.
140 *
141 * This is a component flag that should be combined with `GPIO_INT_ENABLE` or
142 * `GPIO_INT_DISABLE` flags to produce a meaningful configuration.
143 */
144 #define GPIO_INT_ENABLE_DISABLE_ONLY (1u << 27)
145 #endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
146
147 #define GPIO_INT_MASK (GPIO_INT_DISABLE | \
148 GPIO_INT_ENABLE | \
149 GPIO_INT_LEVELS_LOGICAL | \
150 GPIO_INT_EDGE | \
151 GPIO_INT_LOW_0 | \
152 GPIO_INT_HIGH_1)
153
154 /** @endcond */
155
156 /** Configures GPIO interrupt to be triggered on pin rising edge and enables it.
157 */
158 #define GPIO_INT_EDGE_RISING (GPIO_INT_ENABLE | \
159 GPIO_INT_EDGE | \
160 GPIO_INT_HIGH_1)
161
162 /** Configures GPIO interrupt to be triggered on pin falling edge and enables
163 * it.
164 */
165 #define GPIO_INT_EDGE_FALLING (GPIO_INT_ENABLE | \
166 GPIO_INT_EDGE | \
167 GPIO_INT_LOW_0)
168
169 /** Configures GPIO interrupt to be triggered on pin rising or falling edge and
170 * enables it.
171 */
172 #define GPIO_INT_EDGE_BOTH (GPIO_INT_ENABLE | \
173 GPIO_INT_EDGE | \
174 GPIO_INT_LOW_0 | \
175 GPIO_INT_HIGH_1)
176
177 /** Configures GPIO interrupt to be triggered on pin physical level low and
178 * enables it.
179 */
180 #define GPIO_INT_LEVEL_LOW (GPIO_INT_ENABLE | \
181 GPIO_INT_LOW_0)
182
183 /** Configures GPIO interrupt to be triggered on pin physical level high and
184 * enables it.
185 */
186 #define GPIO_INT_LEVEL_HIGH (GPIO_INT_ENABLE | \
187 GPIO_INT_HIGH_1)
188
189 /** Configures GPIO interrupt to be triggered on pin state change to logical
190 * level 0 and enables it.
191 */
192 #define GPIO_INT_EDGE_TO_INACTIVE (GPIO_INT_ENABLE | \
193 GPIO_INT_LEVELS_LOGICAL | \
194 GPIO_INT_EDGE | \
195 GPIO_INT_LOW_0)
196
197 /** Configures GPIO interrupt to be triggered on pin state change to logical
198 * level 1 and enables it.
199 */
200 #define GPIO_INT_EDGE_TO_ACTIVE (GPIO_INT_ENABLE | \
201 GPIO_INT_LEVELS_LOGICAL | \
202 GPIO_INT_EDGE | \
203 GPIO_INT_HIGH_1)
204
205 /** Configures GPIO interrupt to be triggered on pin logical level 0 and enables
206 * it.
207 */
208 #define GPIO_INT_LEVEL_INACTIVE (GPIO_INT_ENABLE | \
209 GPIO_INT_LEVELS_LOGICAL | \
210 GPIO_INT_LOW_0)
211
212 /** Configures GPIO interrupt to be triggered on pin logical level 1 and enables
213 * it.
214 */
215 #define GPIO_INT_LEVEL_ACTIVE (GPIO_INT_ENABLE | \
216 GPIO_INT_LEVELS_LOGICAL | \
217 GPIO_INT_HIGH_1)
218
219 /** @} */
220
221 /** @cond INTERNAL_HIDDEN */
222 #define GPIO_DIR_MASK (GPIO_INPUT | GPIO_OUTPUT)
223 /** @endcond */
224
225 /**
226 * @brief Identifies a set of pins associated with a port.
227 *
228 * The pin with index n is present in the set if and only if the bit
229 * identified by (1U << n) is set.
230 */
231 typedef uint32_t gpio_port_pins_t;
232
233 /**
234 * @brief Provides values for a set of pins associated with a port.
235 *
236 * The value for a pin with index n is high (physical mode) or active
237 * (logical mode) if and only if the bit identified by (1U << n) is set.
238 * Otherwise the value for the pin is low (physical mode) or inactive
239 * (logical mode).
240 *
241 * Values of this type are often paired with a `gpio_port_pins_t` value
242 * that specifies which encoded pin values are valid for the operation.
243 */
244 typedef uint32_t gpio_port_value_t;
245
246 /**
247 * @brief Provides a type to hold a GPIO pin index.
248 *
249 * This reduced-size type is sufficient to record a pin number,
250 * e.g. from a devicetree GPIOS property.
251 */
252 typedef uint8_t gpio_pin_t;
253
254 /**
255 * @brief Provides a type to hold GPIO devicetree flags.
256 *
257 * All GPIO flags that can be expressed in devicetree fit in the low 16
258 * bits of the full flags field, so use a reduced-size type to record
259 * that part of a GPIOS property.
260 *
261 * The lower 8 bits are used for standard flags. The upper 8 bits are reserved
262 * for SoC specific flags.
263 */
264 typedef uint16_t gpio_dt_flags_t;
265
266 /**
267 * @brief Provides a type to hold GPIO configuration flags.
268 *
269 * This type is sufficient to hold all flags used to control GPIO
270 * configuration, whether pin or interrupt.
271 */
272 typedef uint32_t gpio_flags_t;
273
274 /**
275 * @brief Container for GPIO pin information specified in devicetree
276 *
277 * This type contains a pointer to a GPIO device, pin number for a pin
278 * controlled by that device, and the subset of pin configuration
279 * flags which may be given in devicetree.
280 *
281 * @see GPIO_DT_SPEC_GET_BY_IDX
282 * @see GPIO_DT_SPEC_GET_BY_IDX_OR
283 * @see GPIO_DT_SPEC_GET
284 * @see GPIO_DT_SPEC_GET_OR
285 */
286 struct gpio_dt_spec {
287 /** GPIO device controlling the pin */
288 const struct device *port;
289 /** The pin's number on the device */
290 gpio_pin_t pin;
291 /** The pin's configuration flags as specified in devicetree */
292 gpio_dt_flags_t dt_flags;
293 };
294
295 /**
296 * @brief Static initializer for a @p gpio_dt_spec
297 *
298 * This returns a static initializer for a @p gpio_dt_spec structure given a
299 * devicetree node identifier, a property specifying a GPIO and an index.
300 *
301 * Example devicetree fragment:
302 *
303 * n: node {
304 * foo-gpios = <&gpio0 1 GPIO_ACTIVE_LOW>,
305 * <&gpio1 2 GPIO_ACTIVE_LOW>;
306 * }
307 *
308 * Example usage:
309 *
310 * const struct gpio_dt_spec spec = GPIO_DT_SPEC_GET_BY_IDX(DT_NODELABEL(n),
311 * foo_gpios, 1);
312 * // Initializes 'spec' to:
313 * // {
314 * // .port = DEVICE_DT_GET(DT_NODELABEL(gpio1)),
315 * // .pin = 2,
316 * // .dt_flags = GPIO_ACTIVE_LOW
317 * // }
318 *
319 * The 'gpio' field must still be checked for readiness, e.g. using
320 * device_is_ready(). It is an error to use this macro unless the node
321 * exists, has the given property, and that property specifies a GPIO
322 * controller, pin number, and flags as shown above.
323 *
324 * @param node_id devicetree node identifier
325 * @param prop lowercase-and-underscores property name
326 * @param idx logical index into "prop"
327 * @return static initializer for a struct gpio_dt_spec for the property
328 */
329 #define GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx) \
330 { \
331 .port = DEVICE_DT_GET(DT_GPIO_CTLR_BY_IDX(node_id, prop, idx)),\
332 .pin = DT_GPIO_PIN_BY_IDX(node_id, prop, idx), \
333 .dt_flags = DT_GPIO_FLAGS_BY_IDX(node_id, prop, idx), \
334 }
335
336 /**
337 * @brief Like GPIO_DT_SPEC_GET_BY_IDX(), with a fallback to a default value
338 *
339 * If the devicetree node identifier 'node_id' refers to a node with a
340 * property 'prop', this expands to
341 * <tt>GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx)</tt>. The @p
342 * default_value parameter is not expanded in this case.
343 *
344 * Otherwise, this expands to @p default_value.
345 *
346 * @param node_id devicetree node identifier
347 * @param prop lowercase-and-underscores property name
348 * @param idx logical index into "prop"
349 * @param default_value fallback value to expand to
350 * @return static initializer for a struct gpio_dt_spec for the property,
351 * or default_value if the node or property do not exist
352 */
353 #define GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, idx, default_value) \
354 COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
355 (GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, idx)), \
356 (default_value))
357
358 /**
359 * @brief Equivalent to GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, 0).
360 *
361 * @param node_id devicetree node identifier
362 * @param prop lowercase-and-underscores property name
363 * @return static initializer for a struct gpio_dt_spec for the property
364 * @see GPIO_DT_SPEC_GET_BY_IDX()
365 */
366 #define GPIO_DT_SPEC_GET(node_id, prop) \
367 GPIO_DT_SPEC_GET_BY_IDX(node_id, prop, 0)
368
369 /**
370 * @brief Equivalent to
371 * GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, 0, default_value).
372 *
373 * @param node_id devicetree node identifier
374 * @param prop lowercase-and-underscores property name
375 * @param default_value fallback value to expand to
376 * @return static initializer for a struct gpio_dt_spec for the property
377 * @see GPIO_DT_SPEC_GET_BY_IDX_OR()
378 */
379 #define GPIO_DT_SPEC_GET_OR(node_id, prop, default_value) \
380 GPIO_DT_SPEC_GET_BY_IDX_OR(node_id, prop, 0, default_value)
381
382 /**
383 * @brief Static initializer for a @p gpio_dt_spec from a DT_DRV_COMPAT
384 * instance's GPIO property at an index.
385 *
386 * @param inst DT_DRV_COMPAT instance number
387 * @param prop lowercase-and-underscores property name
388 * @param idx logical index into "prop"
389 * @return static initializer for a struct gpio_dt_spec for the property
390 * @see GPIO_DT_SPEC_GET_BY_IDX()
391 */
392 #define GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, idx) \
393 GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)
394
395 /**
396 * @brief Static initializer for a @p gpio_dt_spec from a DT_DRV_COMPAT
397 * instance's GPIO property at an index, with fallback
398 *
399 * @param inst DT_DRV_COMPAT instance number
400 * @param prop lowercase-and-underscores property name
401 * @param idx logical index into "prop"
402 * @param default_value fallback value to expand to
403 * @return static initializer for a struct gpio_dt_spec for the property
404 * @see GPIO_DT_SPEC_GET_BY_IDX()
405 */
406 #define GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, idx, default_value) \
407 COND_CODE_1(DT_PROP_HAS_IDX(DT_DRV_INST(inst), prop, idx), \
408 (GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), prop, idx)), \
409 (default_value))
410
411 /**
412 * @brief Equivalent to GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, 0).
413 *
414 * @param inst DT_DRV_COMPAT instance number
415 * @param prop lowercase-and-underscores property name
416 * @return static initializer for a struct gpio_dt_spec for the property
417 * @see GPIO_DT_SPEC_INST_GET_BY_IDX()
418 */
419 #define GPIO_DT_SPEC_INST_GET(inst, prop) \
420 GPIO_DT_SPEC_INST_GET_BY_IDX(inst, prop, 0)
421
422 /**
423 * @brief Equivalent to
424 * GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, 0, default_value).
425 *
426 * @param inst DT_DRV_COMPAT instance number
427 * @param prop lowercase-and-underscores property name
428 * @param default_value fallback value to expand to
429 * @return static initializer for a struct gpio_dt_spec for the property
430 * @see GPIO_DT_SPEC_INST_GET_BY_IDX()
431 */
432 #define GPIO_DT_SPEC_INST_GET_OR(inst, prop, default_value) \
433 GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, prop, 0, default_value)
434
435 /*
436 * @cond INTERNAL_HIDDEN
437 */
438
439 /**
440 * Auxiliary conditional macro that generates a bitmask for the range
441 * from @p "prop" array defined by the (off_idx, sz_idx) pair,
442 * or 0 if the range does not exist.
443 *
444 * @param node_id devicetree node identifier
445 * @param prop lowercase-and-underscores array property name
446 * @param off_idx logical index of bitmask offset value into "prop" array
447 * @param sz_idx logical index of bitmask size value into "prop" array
448 */
449 #define Z_GPIO_GEN_BITMASK_COND(node_id, prop, off_idx, sz_idx) \
450 COND_CODE_1(DT_PROP_HAS_IDX(node_id, prop, off_idx), \
451 (COND_CODE_0(DT_PROP_BY_IDX(node_id, prop, sz_idx), \
452 (0), \
453 (GENMASK64(DT_PROP_BY_IDX(node_id, prop, off_idx) + \
454 DT_PROP_BY_IDX(node_id, prop, sz_idx) - 1, \
455 DT_PROP_BY_IDX(node_id, prop, off_idx)))) \
456 ), (0))
457
458 /**
459 * A helper conditional macro returning generated bitmask for one element
460 * from @p "gpio-reserved-ranges"
461 *
462 * @param odd_it the value of an odd sequential iterator
463 * @param node_id devicetree node identifier
464 */
465 #define Z_GPIO_GEN_RESERVED_RANGES_COND(odd_it, node_id) \
466 COND_CODE_1(DT_PROP_HAS_IDX(node_id, gpio_reserved_ranges, odd_it), \
467 (Z_GPIO_GEN_BITMASK_COND(node_id, \
468 gpio_reserved_ranges, \
469 GET_ARG_N(odd_it, Z_SPARSE_LIST_EVEN_NUMBERS), \
470 odd_it)), \
471 (0))
472
473 /**
474 * @endcond
475 */
476
477 /**
478 * @brief Makes a bitmask of reserved GPIOs from DT @p "gpio-reserved-ranges"
479 * property and @p "ngpios" argument
480 *
481 * This macro returns the value as a bitmask of the @p "gpio-reserved-ranges"
482 * property. This property defines the disabled (or 'reserved') GPIOs in the
483 * range @p 0...ngpios-1 and is specified as an array of value's pairs that
484 * define the start offset and size of the reserved ranges.
485 *
486 * For example, setting "gpio-reserved-ranges = <3 2>, <10 1>;"
487 * means that GPIO offsets 3, 4 and 10 cannot be used even if @p ngpios = <18>.
488 *
489 * The implementation constraint is inherited from common DT limitations:
490 * a maximum of 64 pairs can be used (with result limited to bitsize
491 * of gpio_port_pins_t type).
492 *
493 * NB: Due to the nature of C macros, some incorrect tuple definitions
494 * (for example, overlapping or out of range) will produce undefined results.
495 *
496 * Also be aware that if @p ngpios is less than 32 (bit size of DT int type),
497 * then all unused MSBs outside the range defined by @p ngpios will be
498 * marked as reserved too.
499 *
500 * Example devicetree fragment:
501 *
502 * @code{.dts}
503 * a {
504 * compatible = "some,gpio-controller";
505 * ngpios = <32>;
506 * gpio-reserved-ranges = <0 4>, <5 3>, <9 5>, <11 2>, <15 2>,
507 * <18 2>, <21 1>, <23 1>, <25 4>, <30 2>;
508 * };
509 *
510 * b {
511 * compatible = "some,gpio-controller";
512 * ngpios = <18>;
513 * gpio-reserved-ranges = <3 2>, <10 1>;
514 * };
515 *
516 * @endcode
517 *
518 * Example usage:
519 *
520 * @code{.c}
521 * struct some_config {
522 * uint32_t ngpios;
523 * uint32_t gpios_reserved;
524 * };
525 *
526 * static const struct some_config dev_cfg_a = {
527 * .ngpios = DT_PROP_OR(DT_LABEL(a), ngpios, 0),
528 * .gpios_reserved = GPIO_DT_RESERVED_RANGES_NGPIOS(DT_LABEL(a),
529 * DT_PROP(DT_LABEL(a), ngpios)),
530 * };
531 *
532 * static const struct some_config dev_cfg_b = {
533 * .ngpios = DT_PROP_OR(DT_LABEL(b), ngpios, 0),
534 * .gpios_reserved = GPIO_DT_RESERVED_RANGES_NGPIOS(DT_LABEL(b),
535 * DT_PROP(DT_LABEL(b), ngpios)),
536 * };
537 *@endcode
538 *
539 * This expands to:
540 *
541 * @code{.c}
542 * struct some_config {
543 * uint32_t ngpios;
544 * uint32_t gpios_reserved;
545 * };
546 *
547 * static const struct some_config dev_cfg_a = {
548 * .ngpios = 32,
549 * .gpios_reserved = 0xdeadbeef,
550 * // 0b1101 1110 1010 1101 1011 1110 1110 1111
551 *
552 * static const struct some_config dev_cfg_b = {
553 * .ngpios = 18,
554 * .gpios_reserved = 0xfffc0418,
555 * // 0b1111 1111 1111 1100 0000 0100 0001 1000
556 * // unused MSBs were marked as reserved too
557 * };
558 * @endcode
559 *
560 * @param node_id GPIO controller node identifier.
561 * @param ngpios number of GPIOs.
562 * @return the bitmask of reserved gpios
563 */
564 #define GPIO_DT_RESERVED_RANGES_NGPIOS(node_id, ngpios) \
565 ((gpio_port_pins_t) \
566 COND_CODE_1(DT_NODE_HAS_PROP(node_id, gpio_reserved_ranges), \
567 (GENMASK64(BITS_PER_LONG_LONG - 1, ngpios) \
568 | FOR_EACH_FIXED_ARG(Z_GPIO_GEN_RESERVED_RANGES_COND, \
569 (|), \
570 node_id, \
571 LIST_DROP_EMPTY(Z_SPARSE_LIST_ODD_NUMBERS))), \
572 (0)))
573
574 /**
575 * @brief Makes a bitmask of reserved GPIOs from the @p "gpio-reserved-ranges"
576 * and @p "ngpios" DT properties values
577 *
578 * @param node_id GPIO controller node identifier.
579 * @return the bitmask of reserved gpios
580 */
581 #define GPIO_DT_RESERVED_RANGES(node_id) \
582 GPIO_DT_RESERVED_RANGES_NGPIOS(node_id, DT_PROP(node_id, ngpios))
583
584 /**
585 * @brief Makes a bitmask of reserved GPIOs from a DT_DRV_COMPAT instance's
586 * @p "gpio-reserved-ranges" property and @p "ngpios" argument
587 *
588 * @param inst DT_DRV_COMPAT instance number
589 * @return the bitmask of reserved gpios
590 * @param ngpios number of GPIOs
591 * @see GPIO_DT_RESERVED_RANGES()
592 */
593 #define GPIO_DT_INST_RESERVED_RANGES_NGPIOS(inst, ngpios) \
594 GPIO_DT_RESERVED_RANGES_NGPIOS(DT_DRV_INST(inst), ngpios)
595
596 /**
597 * @brief Make a bitmask of reserved GPIOs from a DT_DRV_COMPAT instance's GPIO
598 * @p "gpio-reserved-ranges" and @p "ngpios" properties
599 *
600 * @param inst DT_DRV_COMPAT instance number
601 * @return the bitmask of reserved gpios
602 * @see GPIO_DT_RESERVED_RANGES()
603 */
604 #define GPIO_DT_INST_RESERVED_RANGES(inst) \
605 GPIO_DT_RESERVED_RANGES(DT_DRV_INST(inst))
606
607 /**
608 * @brief Makes a bitmask of allowed GPIOs from DT @p "gpio-reserved-ranges"
609 * property and @p "ngpios" argument
610 *
611 * This macro is paired with GPIO_DT_RESERVED_RANGES_NGPIOS(), however unlike
612 * the latter, it returns a bitmask of ALLOWED gpios.
613 *
614 * Example devicetree fragment:
615 *
616 * @code{.dts}
617 * a {
618 * compatible = "some,gpio-controller";
619 * ngpios = <32>;
620 * gpio-reserved-ranges = <0 8>, <9 5>, <15 16>;
621 * };
622 *
623 * @endcode
624 *
625 * Example usage:
626 *
627 * @code{.c}
628 * struct some_config {
629 * uint32_t port_pin_mask;
630 * };
631 *
632 * static const struct some_config dev_cfg = {
633 * .port_pin_mask = GPIO_DT_PORT_PIN_MASK_NGPIOS_EXC(
634 * DT_LABEL(a), 32),
635 * };
636 * @endcode
637 *
638 * This expands to:
639 *
640 * @code{.c}
641 * struct some_config {
642 * uint32_t port_pin_mask;
643 * };
644 *
645 * static const struct some_config dev_cfg = {
646 * .port_pin_mask = 0x80004100,
647 * // 0b1000 0000 0000 0000 0100 0001 00000 000
648 * };
649 * @endcode
650 *
651 * @param node_id GPIO controller node identifier.
652 * @param ngpios number of GPIOs
653 * @return the bitmask of allowed gpios
654 */
655 #define GPIO_DT_PORT_PIN_MASK_NGPIOS_EXC(node_id, ngpios) \
656 ((gpio_port_pins_t) \
657 COND_CODE_0(ngpios, \
658 (0), \
659 (COND_CODE_1(DT_NODE_HAS_PROP(node_id, gpio_reserved_ranges), \
660 ((GENMASK64(ngpios - 1, 0) & \
661 ~GPIO_DT_RESERVED_RANGES_NGPIOS(node_id, ngpios))), \
662 (GENMASK64(ngpios - 1, 0))) \
663 ) \
664 ))
665
666 /**
667 * @brief Makes a bitmask of allowed GPIOs from a DT_DRV_COMPAT instance's
668 * @p "gpio-reserved-ranges" property and @p "ngpios" argument
669 *
670 * @param inst DT_DRV_COMPAT instance number
671 * @param ngpios number of GPIOs
672 * @return the bitmask of allowed gpios
673 * @see GPIO_DT_NGPIOS_PORT_PIN_MASK_EXC()
674 */
675 #define GPIO_DT_INST_PORT_PIN_MASK_NGPIOS_EXC(inst, ngpios) \
676 GPIO_DT_PORT_PIN_MASK_NGPIOS_EXC(DT_DRV_INST(inst), ngpios)
677
678 /**
679 * @brief Maximum number of pins that are supported by `gpio_port_pins_t`.
680 */
681 #define GPIO_MAX_PINS_PER_PORT (sizeof(gpio_port_pins_t) * __CHAR_BIT__)
682
683 /**
684 * This structure is common to all GPIO drivers and is expected to be
685 * the first element in the object pointed to by the config field
686 * in the device structure.
687 */
688 struct gpio_driver_config {
689 /** Mask identifying pins supported by the controller.
690 *
691 * Initialization of this mask is the responsibility of device
692 * instance generation in the driver.
693 */
694 gpio_port_pins_t port_pin_mask;
695 };
696
697 /**
698 * This structure is common to all GPIO drivers and is expected to be the first
699 * element in the driver's struct driver_data declaration.
700 */
701 struct gpio_driver_data {
702 /** Mask identifying pins that are configured as active low.
703 *
704 * Management of this mask is the responsibility of the
705 * wrapper functions in this header.
706 */
707 gpio_port_pins_t invert;
708 };
709
710 struct gpio_callback;
711
712 /**
713 * @typedef gpio_callback_handler_t
714 * @brief Define the application callback handler function signature
715 *
716 * @param port Device struct for the GPIO device.
717 * @param cb Original struct gpio_callback owning this handler
718 * @param pins Mask of pins that triggers the callback handler
719 *
720 * Note: cb pointer can be used to retrieve private data through
721 * CONTAINER_OF() if original struct gpio_callback is stored in
722 * another private structure.
723 */
724 typedef void (*gpio_callback_handler_t)(const struct device *port,
725 struct gpio_callback *cb,
726 gpio_port_pins_t pins);
727
728 /**
729 * @brief GPIO callback structure
730 *
731 * Used to register a callback in the driver instance callback list.
732 * As many callbacks as needed can be added as long as each of them
733 * are unique pointers of struct gpio_callback.
734 * Beware such structure should not be allocated on stack.
735 *
736 * Note: To help setting it, see gpio_init_callback() below
737 */
738 struct gpio_callback {
739 /** This is meant to be used in the driver and the user should not
740 * mess with it (see drivers/gpio/gpio_utils.h)
741 */
742 sys_snode_t node;
743
744 /** Actual callback function being called when relevant. */
745 gpio_callback_handler_t handler;
746
747 /** A mask of pins the callback is interested in, if 0 the callback
748 * will never be called. Such pin_mask can be modified whenever
749 * necessary by the owner, and thus will affect the handler being
750 * called or not. The selected pins must be configured to trigger
751 * an interrupt.
752 */
753 gpio_port_pins_t pin_mask;
754 };
755
756 /**
757 * @cond INTERNAL_HIDDEN
758 *
759 * For internal use only, skip these in public documentation.
760 */
761
762 /* Used by driver api function pin_interrupt_configure, these are defined
763 * in terms of the public flags so we can just mask and pass them
764 * through to the driver api
765 */
766 enum gpio_int_mode {
767 GPIO_INT_MODE_DISABLED = GPIO_INT_DISABLE,
768 GPIO_INT_MODE_LEVEL = GPIO_INT_ENABLE,
769 GPIO_INT_MODE_EDGE = GPIO_INT_ENABLE | GPIO_INT_EDGE,
770 #ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
771 GPIO_INT_MODE_DISABLE_ONLY = GPIO_INT_DISABLE | GPIO_INT_ENABLE_DISABLE_ONLY,
772 GPIO_INT_MODE_ENABLE_ONLY = GPIO_INT_ENABLE | GPIO_INT_ENABLE_DISABLE_ONLY,
773 #endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
774 };
775
776 enum gpio_int_trig {
777 /* Trigger detection when input state is (or transitions to)
778 * physical low. (Edge Falling or Active Low)
779 */
780 GPIO_INT_TRIG_LOW = GPIO_INT_LOW_0,
781 /* Trigger detection when input state is (or transitions to)
782 * physical high. (Edge Rising or Active High) */
783 GPIO_INT_TRIG_HIGH = GPIO_INT_HIGH_1,
784 /* Trigger detection on pin rising or falling edge. */
785 GPIO_INT_TRIG_BOTH = GPIO_INT_LOW_0 | GPIO_INT_HIGH_1,
786 };
787
788 __subsystem struct gpio_driver_api {
789 int (*pin_configure)(const struct device *port, gpio_pin_t pin,
790 gpio_flags_t flags);
791 #ifdef CONFIG_GPIO_GET_CONFIG
792 int (*pin_get_config)(const struct device *port, gpio_pin_t pin,
793 gpio_flags_t *flags);
794 #endif
795 int (*port_get_raw)(const struct device *port,
796 gpio_port_value_t *value);
797 int (*port_set_masked_raw)(const struct device *port,
798 gpio_port_pins_t mask,
799 gpio_port_value_t value);
800 int (*port_set_bits_raw)(const struct device *port,
801 gpio_port_pins_t pins);
802 int (*port_clear_bits_raw)(const struct device *port,
803 gpio_port_pins_t pins);
804 int (*port_toggle_bits)(const struct device *port,
805 gpio_port_pins_t pins);
806 int (*pin_interrupt_configure)(const struct device *port,
807 gpio_pin_t pin,
808 enum gpio_int_mode, enum gpio_int_trig);
809 int (*manage_callback)(const struct device *port,
810 struct gpio_callback *cb,
811 bool set);
812 uint32_t (*get_pending_int)(const struct device *dev);
813 #ifdef CONFIG_GPIO_GET_DIRECTION
814 int (*port_get_direction)(const struct device *port, gpio_port_pins_t map,
815 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
816 #endif /* CONFIG_GPIO_GET_DIRECTION */
817 };
818
819 /**
820 * @endcond
821 */
822
823 /**
824 * @brief Validate that GPIO port is ready.
825 *
826 * @param spec GPIO specification from devicetree
827 *
828 * @retval true if the GPIO spec is ready for use.
829 * @retval false if the GPIO spec is not ready for use.
830 */
gpio_is_ready_dt(const struct gpio_dt_spec * spec)831 static inline bool gpio_is_ready_dt(const struct gpio_dt_spec *spec)
832 {
833 /* Validate port is ready */
834 return device_is_ready(spec->port);
835 }
836
837 /**
838 * @brief Configure pin interrupt.
839 *
840 * @note This function can also be used to configure interrupts on pins
841 * not controlled directly by the GPIO module. That is, pins which are
842 * routed to other modules such as I2C, SPI, UART.
843 *
844 * @param port Pointer to device structure for the driver instance.
845 * @param pin Pin number.
846 * @param flags Interrupt configuration flags as defined by GPIO_INT_*.
847 *
848 * @retval 0 If successful.
849 * @retval -ENOSYS If the operation is not implemented by the driver.
850 * @retval -ENOTSUP If any of the configuration options is not supported
851 * (unless otherwise directed by flag documentation).
852 * @retval -EINVAL Invalid argument.
853 * @retval -EBUSY Interrupt line required to configure pin interrupt is
854 * already in use.
855 * @retval -EIO I/O error when accessing an external GPIO chip.
856 * @retval -EWOULDBLOCK if operation would block.
857 */
858 __syscall int gpio_pin_interrupt_configure(const struct device *port,
859 gpio_pin_t pin,
860 gpio_flags_t flags);
861
z_impl_gpio_pin_interrupt_configure(const struct device * port,gpio_pin_t pin,gpio_flags_t flags)862 static inline int z_impl_gpio_pin_interrupt_configure(const struct device *port,
863 gpio_pin_t pin,
864 gpio_flags_t flags)
865 {
866 const struct gpio_driver_api *api =
867 (const struct gpio_driver_api *)port->api;
868 __unused const struct gpio_driver_config *const cfg =
869 (const struct gpio_driver_config *)port->config;
870 const struct gpio_driver_data *const data =
871 (const struct gpio_driver_data *)port->data;
872 enum gpio_int_trig trig;
873 enum gpio_int_mode mode;
874
875 if (api->pin_interrupt_configure == NULL) {
876 return -ENOSYS;
877 }
878
879 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE))
880 != (GPIO_INT_DISABLE | GPIO_INT_ENABLE),
881 "Cannot both enable and disable interrupts");
882
883 __ASSERT((flags & (GPIO_INT_DISABLE | GPIO_INT_ENABLE)) != 0U,
884 "Must either enable or disable interrupts");
885
886 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
887 ((flags & GPIO_INT_EDGE) != 0) ||
888 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) !=
889 (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)),
890 "Only one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 can be "
891 "enabled for a level interrupt.");
892
893 __ASSERT(((flags & GPIO_INT_ENABLE) == 0) ||
894 #ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
895 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0) ||
896 (flags & GPIO_INT_ENABLE_DISABLE_ONLY) != 0,
897 #else
898 ((flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1)) != 0),
899 #endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
900 "At least one of GPIO_INT_LOW_0, GPIO_INT_HIGH_1 has to be "
901 "enabled.");
902
903 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
904 "Unsupported pin");
905
906 if (((flags & GPIO_INT_LEVELS_LOGICAL) != 0) &&
907 ((data->invert & (gpio_port_pins_t)BIT(pin)) != 0)) {
908 /* Invert signal bits */
909 flags ^= (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1);
910 }
911
912 trig = (enum gpio_int_trig)(flags & (GPIO_INT_LOW_0 | GPIO_INT_HIGH_1));
913 #ifdef CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT
914 mode = (enum gpio_int_mode)(flags & (GPIO_INT_EDGE | GPIO_INT_DISABLE | GPIO_INT_ENABLE |
915 GPIO_INT_ENABLE_DISABLE_ONLY));
916 #else
917 mode = (enum gpio_int_mode)(flags & (GPIO_INT_EDGE | GPIO_INT_DISABLE | GPIO_INT_ENABLE));
918 #endif /* CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT */
919
920 return api->pin_interrupt_configure(port, pin, mode, trig);
921 }
922
923 /**
924 * @brief Configure pin interrupts from a @p gpio_dt_spec.
925 *
926 * This is equivalent to:
927 *
928 * gpio_pin_interrupt_configure(spec->port, spec->pin, flags);
929 *
930 * The <tt>spec->dt_flags</tt> value is not used.
931 *
932 * @param spec GPIO specification from devicetree
933 * @param flags interrupt configuration flags
934 * @return a value from gpio_pin_interrupt_configure()
935 */
gpio_pin_interrupt_configure_dt(const struct gpio_dt_spec * spec,gpio_flags_t flags)936 static inline int gpio_pin_interrupt_configure_dt(const struct gpio_dt_spec *spec,
937 gpio_flags_t flags)
938 {
939 return gpio_pin_interrupt_configure(spec->port, spec->pin, flags);
940 }
941
942 /**
943 * @brief Configure a single pin.
944 *
945 * @param port Pointer to device structure for the driver instance.
946 * @param pin Pin number to configure.
947 * @param flags Flags for pin configuration: 'GPIO input/output configuration
948 * flags', 'GPIO pin drive flags', 'GPIO pin bias flags'.
949 *
950 * @retval 0 If successful.
951 * @retval -ENOTSUP if any of the configuration options is not supported
952 * (unless otherwise directed by flag documentation).
953 * @retval -EINVAL Invalid argument.
954 * @retval -EIO I/O error when accessing an external GPIO chip.
955 * @retval -EWOULDBLOCK if operation would block.
956 */
957 __syscall int gpio_pin_configure(const struct device *port,
958 gpio_pin_t pin,
959 gpio_flags_t flags);
960
z_impl_gpio_pin_configure(const struct device * port,gpio_pin_t pin,gpio_flags_t flags)961 static inline int z_impl_gpio_pin_configure(const struct device *port,
962 gpio_pin_t pin,
963 gpio_flags_t flags)
964 {
965 const struct gpio_driver_api *api =
966 (const struct gpio_driver_api *)port->api;
967 __unused const struct gpio_driver_config *const cfg =
968 (const struct gpio_driver_config *)port->config;
969 struct gpio_driver_data *data =
970 (struct gpio_driver_data *)port->data;
971
972 __ASSERT((flags & GPIO_INT_MASK) == 0,
973 "Interrupt flags are not supported");
974
975 __ASSERT((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) !=
976 (GPIO_PULL_UP | GPIO_PULL_DOWN),
977 "Pull Up and Pull Down should not be enabled simultaneously");
978
979 __ASSERT(!((flags & GPIO_INPUT) && !(flags & GPIO_OUTPUT) && (flags & GPIO_SINGLE_ENDED)),
980 "Input cannot be enabled for 'Open Drain', 'Open Source' modes without Output");
981
982 __ASSERT_NO_MSG((flags & GPIO_SINGLE_ENDED) != 0 ||
983 (flags & GPIO_LINE_OPEN_DRAIN) == 0);
984
985 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) == 0
986 || (flags & GPIO_OUTPUT) != 0,
987 "Output needs to be enabled to be initialized low or high");
988
989 __ASSERT((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH))
990 != (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH),
991 "Output cannot be initialized low and high");
992
993 if (((flags & GPIO_OUTPUT_INIT_LOGICAL) != 0)
994 && ((flags & (GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH)) != 0)
995 && ((flags & GPIO_ACTIVE_LOW) != 0)) {
996 flags ^= GPIO_OUTPUT_INIT_LOW | GPIO_OUTPUT_INIT_HIGH;
997 }
998
999 flags &= ~GPIO_OUTPUT_INIT_LOGICAL;
1000
1001 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1002 "Unsupported pin");
1003
1004 if ((flags & GPIO_ACTIVE_LOW) != 0) {
1005 data->invert |= (gpio_port_pins_t)BIT(pin);
1006 } else {
1007 data->invert &= ~(gpio_port_pins_t)BIT(pin);
1008 }
1009
1010 return api->pin_configure(port, pin, flags);
1011 }
1012
1013 /**
1014 * @brief Configure a single pin from a @p gpio_dt_spec and some extra flags.
1015 *
1016 * This is equivalent to:
1017 *
1018 * gpio_pin_configure(spec->port, spec->pin, spec->dt_flags | extra_flags);
1019 *
1020 * @param spec GPIO specification from devicetree
1021 * @param extra_flags additional flags
1022 * @return a value from gpio_pin_configure()
1023 */
gpio_pin_configure_dt(const struct gpio_dt_spec * spec,gpio_flags_t extra_flags)1024 static inline int gpio_pin_configure_dt(const struct gpio_dt_spec *spec,
1025 gpio_flags_t extra_flags)
1026 {
1027 return gpio_pin_configure(spec->port,
1028 spec->pin,
1029 spec->dt_flags | extra_flags);
1030 }
1031
1032 /**
1033 * @brief Get direction of select pins in a port.
1034 *
1035 * Retrieve direction of each pin specified in @p map.
1036 *
1037 * If @p inputs or @p outputs is NULL, then this function does not get the
1038 * respective input or output direction information.
1039 *
1040 * @param port Pointer to the device structure for the driver instance.
1041 * @param map Bitmap of pin directions to query.
1042 * @param inputs Pointer to a variable where input directions will be stored.
1043 * @param outputs Pointer to a variable where output directions will be stored.
1044 *
1045 * @retval 0 If successful.
1046 * @retval -ENOSYS if the underlying driver does not support this call.
1047 * @retval -EIO I/O error when accessing an external GPIO chip.
1048 * @retval -EWOULDBLOCK if operation would block.
1049 */
1050 __syscall int gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
1051 gpio_port_pins_t *inputs, gpio_port_pins_t *outputs);
1052
1053 #ifdef CONFIG_GPIO_GET_DIRECTION
z_impl_gpio_port_get_direction(const struct device * port,gpio_port_pins_t map,gpio_port_pins_t * inputs,gpio_port_pins_t * outputs)1054 static inline int z_impl_gpio_port_get_direction(const struct device *port, gpio_port_pins_t map,
1055 gpio_port_pins_t *inputs,
1056 gpio_port_pins_t *outputs)
1057 {
1058 const struct gpio_driver_api *api = (const struct gpio_driver_api *)port->api;
1059
1060 if (api->port_get_direction == NULL) {
1061 return -ENOSYS;
1062 }
1063
1064 return api->port_get_direction(port, map, inputs, outputs);
1065 }
1066 #endif /* CONFIG_GPIO_GET_DIRECTION */
1067
1068 /**
1069 * @brief Check if @p pin is configured for input
1070 *
1071 * @param port Pointer to device structure for the driver instance.
1072 * @param pin Pin number to query the direction of
1073 *
1074 * @retval 1 if @p pin is configured as @ref GPIO_INPUT.
1075 * @retval 0 if @p pin is not configured as @ref GPIO_INPUT.
1076 * @retval -ENOSYS if the underlying driver does not support this call.
1077 * @retval -EIO I/O error when accessing an external GPIO chip.
1078 * @retval -EWOULDBLOCK if operation would block.
1079 */
gpio_pin_is_input(const struct device * port,gpio_pin_t pin)1080 static inline int gpio_pin_is_input(const struct device *port, gpio_pin_t pin)
1081 {
1082 int rv;
1083 gpio_port_pins_t pins;
1084 __unused const struct gpio_driver_config *cfg =
1085 (const struct gpio_driver_config *)port->config;
1086
1087 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
1088
1089 rv = gpio_port_get_direction(port, BIT(pin), &pins, NULL);
1090 if (rv < 0) {
1091 return rv;
1092 }
1093
1094 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
1095 }
1096
1097 /**
1098 * @brief Check if a single pin from @p gpio_dt_spec is configured for input
1099 *
1100 * This is equivalent to:
1101 *
1102 * gpio_pin_is_input(spec->port, spec->pin);
1103 *
1104 * @param spec GPIO specification from devicetree.
1105 *
1106 * @return A value from gpio_pin_is_input().
1107 */
gpio_pin_is_input_dt(const struct gpio_dt_spec * spec)1108 static inline int gpio_pin_is_input_dt(const struct gpio_dt_spec *spec)
1109 {
1110 return gpio_pin_is_input(spec->port, spec->pin);
1111 }
1112
1113 /**
1114 * @brief Check if @p pin is configured for output
1115 *
1116 * @param port Pointer to device structure for the driver instance.
1117 * @param pin Pin number to query the direction of
1118 *
1119 * @retval 1 if @p pin is configured as @ref GPIO_OUTPUT.
1120 * @retval 0 if @p pin is not configured as @ref GPIO_OUTPUT.
1121 * @retval -ENOSYS if the underlying driver does not support this call.
1122 * @retval -EIO I/O error when accessing an external GPIO chip.
1123 * @retval -EWOULDBLOCK if operation would block.
1124 */
gpio_pin_is_output(const struct device * port,gpio_pin_t pin)1125 static inline int gpio_pin_is_output(const struct device *port, gpio_pin_t pin)
1126 {
1127 int rv;
1128 gpio_port_pins_t pins;
1129 __unused const struct gpio_driver_config *cfg =
1130 (const struct gpio_driver_config *)port->config;
1131
1132 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U, "Unsupported pin");
1133
1134 rv = gpio_port_get_direction(port, BIT(pin), NULL, &pins);
1135 if (rv < 0) {
1136 return rv;
1137 }
1138
1139 return (int)!!((gpio_port_pins_t)BIT(pin) & pins);
1140 }
1141
1142 /**
1143 * @brief Check if a single pin from @p gpio_dt_spec is configured for output
1144 *
1145 * This is equivalent to:
1146 *
1147 * gpio_pin_is_output(spec->port, spec->pin);
1148 *
1149 * @param spec GPIO specification from devicetree.
1150 *
1151 * @return A value from gpio_pin_is_output().
1152 */
gpio_pin_is_output_dt(const struct gpio_dt_spec * spec)1153 static inline int gpio_pin_is_output_dt(const struct gpio_dt_spec *spec)
1154 {
1155 return gpio_pin_is_output(spec->port, spec->pin);
1156 }
1157
1158 /**
1159 * @brief Get a configuration of a single pin.
1160 *
1161 * @param port Pointer to device structure for the driver instance.
1162 * @param pin Pin number which configuration is get.
1163 * @param flags Pointer to variable in which the current configuration will
1164 * be stored if function is successful.
1165 *
1166 * @retval 0 If successful.
1167 * @retval -ENOSYS if getting current pin configuration is not implemented
1168 * by the driver.
1169 * @retval -EINVAL Invalid argument.
1170 * @retval -EIO I/O error when accessing an external GPIO chip.
1171 * @retval -EWOULDBLOCK if operation would block.
1172 */
1173 __syscall int gpio_pin_get_config(const struct device *port, gpio_pin_t pin,
1174 gpio_flags_t *flags);
1175
1176 #ifdef CONFIG_GPIO_GET_CONFIG
z_impl_gpio_pin_get_config(const struct device * port,gpio_pin_t pin,gpio_flags_t * flags)1177 static inline int z_impl_gpio_pin_get_config(const struct device *port,
1178 gpio_pin_t pin,
1179 gpio_flags_t *flags)
1180 {
1181 const struct gpio_driver_api *api =
1182 (const struct gpio_driver_api *)port->api;
1183
1184 if (api->pin_get_config == NULL)
1185 return -ENOSYS;
1186
1187 return api->pin_get_config(port, pin, flags);
1188 }
1189 #endif
1190
1191 /**
1192 * @brief Get a configuration of a single pin from a @p gpio_dt_spec.
1193 *
1194 * This is equivalent to:
1195 *
1196 * gpio_pin_get_config(spec->port, spec->pin, flags);
1197 *
1198 * @param spec GPIO specification from devicetree
1199 * @param flags Pointer to variable in which the current configuration will
1200 * be stored if function is successful.
1201 * @return a value from gpio_pin_configure()
1202 */
gpio_pin_get_config_dt(const struct gpio_dt_spec * spec,gpio_flags_t * flags)1203 static inline int gpio_pin_get_config_dt(const struct gpio_dt_spec *spec,
1204 gpio_flags_t *flags)
1205 {
1206 return gpio_pin_get_config(spec->port, spec->pin, flags);
1207 }
1208
1209 /**
1210 * @brief Get physical level of all input pins in a port.
1211 *
1212 * A low physical level on the pin will be interpreted as value 0. A high
1213 * physical level will be interpreted as value 1. This function ignores
1214 * GPIO_ACTIVE_LOW flag.
1215 *
1216 * Value of a pin with index n will be represented by bit n in the returned
1217 * port value.
1218 *
1219 * @param port Pointer to the device structure for the driver instance.
1220 * @param value Pointer to a variable where pin values will be stored.
1221 *
1222 * @retval 0 If successful.
1223 * @retval -EIO I/O error when accessing an external GPIO chip.
1224 * @retval -EWOULDBLOCK if operation would block.
1225 */
1226 __syscall int gpio_port_get_raw(const struct device *port,
1227 gpio_port_value_t *value);
1228
z_impl_gpio_port_get_raw(const struct device * port,gpio_port_value_t * value)1229 static inline int z_impl_gpio_port_get_raw(const struct device *port,
1230 gpio_port_value_t *value)
1231 {
1232 const struct gpio_driver_api *api =
1233 (const struct gpio_driver_api *)port->api;
1234
1235 return api->port_get_raw(port, value);
1236 }
1237
1238 /**
1239 * @brief Get logical level of all input pins in a port.
1240 *
1241 * Get logical level of an input pin taking into account GPIO_ACTIVE_LOW flag.
1242 * If pin is configured as Active High, a low physical level will be interpreted
1243 * as logical value 0. If pin is configured as Active Low, a low physical level
1244 * will be interpreted as logical value 1.
1245 *
1246 * Value of a pin with index n will be represented by bit n in the returned
1247 * port value.
1248 *
1249 * @param port Pointer to the device structure for the driver instance.
1250 * @param value Pointer to a variable where pin values will be stored.
1251 *
1252 * @retval 0 If successful.
1253 * @retval -EIO I/O error when accessing an external GPIO chip.
1254 * @retval -EWOULDBLOCK if operation would block.
1255 */
gpio_port_get(const struct device * port,gpio_port_value_t * value)1256 static inline int gpio_port_get(const struct device *port,
1257 gpio_port_value_t *value)
1258 {
1259 const struct gpio_driver_data *const data =
1260 (const struct gpio_driver_data *)port->data;
1261 int ret;
1262
1263 ret = gpio_port_get_raw(port, value);
1264 if (ret == 0) {
1265 *value ^= data->invert;
1266 }
1267
1268 return ret;
1269 }
1270
1271 /**
1272 * @brief Set physical level of output pins in a port.
1273 *
1274 * Writing value 0 to the pin will set it to a low physical level. Writing
1275 * value 1 will set it to a high physical level. This function ignores
1276 * GPIO_ACTIVE_LOW flag.
1277 *
1278 * Pin with index n is represented by bit n in mask and value parameter.
1279 *
1280 * @param port Pointer to the device structure for the driver instance.
1281 * @param mask Mask indicating which pins will be modified.
1282 * @param value Value assigned to the output pins.
1283 *
1284 * @retval 0 If successful.
1285 * @retval -EIO I/O error when accessing an external GPIO chip.
1286 * @retval -EWOULDBLOCK if operation would block.
1287 */
1288 __syscall int gpio_port_set_masked_raw(const struct device *port,
1289 gpio_port_pins_t mask,
1290 gpio_port_value_t value);
1291
z_impl_gpio_port_set_masked_raw(const struct device * port,gpio_port_pins_t mask,gpio_port_value_t value)1292 static inline int z_impl_gpio_port_set_masked_raw(const struct device *port,
1293 gpio_port_pins_t mask,
1294 gpio_port_value_t value)
1295 {
1296 const struct gpio_driver_api *api =
1297 (const struct gpio_driver_api *)port->api;
1298
1299 return api->port_set_masked_raw(port, mask, value);
1300 }
1301
1302 /**
1303 * @brief Set logical level of output pins in a port.
1304 *
1305 * Set logical level of an output pin taking into account GPIO_ACTIVE_LOW flag.
1306 * Value 0 sets the pin in logical 0 / inactive state. Value 1 sets the pin in
1307 * logical 1 / active state. If pin is configured as Active High, the default,
1308 * setting it in inactive state will force the pin to a low physical level. If
1309 * pin is configured as Active Low, setting it in inactive state will force the
1310 * pin to a high physical level.
1311 *
1312 * Pin with index n is represented by bit n in mask and value parameter.
1313 *
1314 * @param port Pointer to the device structure for the driver instance.
1315 * @param mask Mask indicating which pins will be modified.
1316 * @param value Value assigned to the output pins.
1317 *
1318 * @retval 0 If successful.
1319 * @retval -EIO I/O error when accessing an external GPIO chip.
1320 * @retval -EWOULDBLOCK if operation would block.
1321 */
gpio_port_set_masked(const struct device * port,gpio_port_pins_t mask,gpio_port_value_t value)1322 static inline int gpio_port_set_masked(const struct device *port,
1323 gpio_port_pins_t mask,
1324 gpio_port_value_t value)
1325 {
1326 const struct gpio_driver_data *const data =
1327 (const struct gpio_driver_data *)port->data;
1328
1329 value ^= data->invert;
1330
1331 return gpio_port_set_masked_raw(port, mask, value);
1332 }
1333
1334 /**
1335 * @brief Set physical level of selected output pins to high.
1336 *
1337 * @param port Pointer to the device structure for the driver instance.
1338 * @param pins Value indicating which pins will be modified.
1339 *
1340 * @retval 0 If successful.
1341 * @retval -EIO I/O error when accessing an external GPIO chip.
1342 * @retval -EWOULDBLOCK if operation would block.
1343 */
1344 __syscall int gpio_port_set_bits_raw(const struct device *port,
1345 gpio_port_pins_t pins);
1346
z_impl_gpio_port_set_bits_raw(const struct device * port,gpio_port_pins_t pins)1347 static inline int z_impl_gpio_port_set_bits_raw(const struct device *port,
1348 gpio_port_pins_t pins)
1349 {
1350 const struct gpio_driver_api *api =
1351 (const struct gpio_driver_api *)port->api;
1352
1353 return api->port_set_bits_raw(port, pins);
1354 }
1355
1356 /**
1357 * @brief Set logical level of selected output pins to active.
1358 *
1359 * @param port Pointer to the device structure for the driver instance.
1360 * @param pins Value indicating which pins will be modified.
1361 *
1362 * @retval 0 If successful.
1363 * @retval -EIO I/O error when accessing an external GPIO chip.
1364 * @retval -EWOULDBLOCK if operation would block.
1365 */
gpio_port_set_bits(const struct device * port,gpio_port_pins_t pins)1366 static inline int gpio_port_set_bits(const struct device *port,
1367 gpio_port_pins_t pins)
1368 {
1369 return gpio_port_set_masked(port, pins, pins);
1370 }
1371
1372 /**
1373 * @brief Set physical level of selected output pins to low.
1374 *
1375 * @param port Pointer to the device structure for the driver instance.
1376 * @param pins Value indicating which pins will be modified.
1377 *
1378 * @retval 0 If successful.
1379 * @retval -EIO I/O error when accessing an external GPIO chip.
1380 * @retval -EWOULDBLOCK if operation would block.
1381 */
1382 __syscall int gpio_port_clear_bits_raw(const struct device *port,
1383 gpio_port_pins_t pins);
1384
z_impl_gpio_port_clear_bits_raw(const struct device * port,gpio_port_pins_t pins)1385 static inline int z_impl_gpio_port_clear_bits_raw(const struct device *port,
1386 gpio_port_pins_t pins)
1387 {
1388 const struct gpio_driver_api *api =
1389 (const struct gpio_driver_api *)port->api;
1390
1391 return api->port_clear_bits_raw(port, pins);
1392 }
1393
1394 /**
1395 * @brief Set logical level of selected output pins to inactive.
1396 *
1397 * @param port Pointer to the device structure for the driver instance.
1398 * @param pins Value indicating which pins will be modified.
1399 *
1400 * @retval 0 If successful.
1401 * @retval -EIO I/O error when accessing an external GPIO chip.
1402 * @retval -EWOULDBLOCK if operation would block.
1403 */
gpio_port_clear_bits(const struct device * port,gpio_port_pins_t pins)1404 static inline int gpio_port_clear_bits(const struct device *port,
1405 gpio_port_pins_t pins)
1406 {
1407 return gpio_port_set_masked(port, pins, 0);
1408 }
1409
1410 /**
1411 * @brief Toggle level of selected output pins.
1412 *
1413 * @param port Pointer to the device structure for the driver instance.
1414 * @param pins Value indicating which pins will be modified.
1415 *
1416 * @retval 0 If successful.
1417 * @retval -EIO I/O error when accessing an external GPIO chip.
1418 * @retval -EWOULDBLOCK if operation would block.
1419 */
1420 __syscall int gpio_port_toggle_bits(const struct device *port,
1421 gpio_port_pins_t pins);
1422
z_impl_gpio_port_toggle_bits(const struct device * port,gpio_port_pins_t pins)1423 static inline int z_impl_gpio_port_toggle_bits(const struct device *port,
1424 gpio_port_pins_t pins)
1425 {
1426 const struct gpio_driver_api *api =
1427 (const struct gpio_driver_api *)port->api;
1428
1429 return api->port_toggle_bits(port, pins);
1430 }
1431
1432 /**
1433 * @brief Set physical level of selected output pins.
1434 *
1435 * @param port Pointer to the device structure for the driver instance.
1436 * @param set_pins Value indicating which pins will be set to high.
1437 * @param clear_pins Value indicating which pins will be set to low.
1438 *
1439 * @retval 0 If successful.
1440 * @retval -EIO I/O error when accessing an external GPIO chip.
1441 * @retval -EWOULDBLOCK if operation would block.
1442 */
gpio_port_set_clr_bits_raw(const struct device * port,gpio_port_pins_t set_pins,gpio_port_pins_t clear_pins)1443 static inline int gpio_port_set_clr_bits_raw(const struct device *port,
1444 gpio_port_pins_t set_pins,
1445 gpio_port_pins_t clear_pins)
1446 {
1447 __ASSERT((set_pins & clear_pins) == 0, "Set and Clear pins overlap");
1448
1449 return gpio_port_set_masked_raw(port, set_pins | clear_pins, set_pins);
1450 }
1451
1452 /**
1453 * @brief Set logical level of selected output pins.
1454 *
1455 * @param port Pointer to the device structure for the driver instance.
1456 * @param set_pins Value indicating which pins will be set to active.
1457 * @param clear_pins Value indicating which pins will be set to inactive.
1458 *
1459 * @retval 0 If successful.
1460 * @retval -EIO I/O error when accessing an external GPIO chip.
1461 * @retval -EWOULDBLOCK if operation would block.
1462 */
gpio_port_set_clr_bits(const struct device * port,gpio_port_pins_t set_pins,gpio_port_pins_t clear_pins)1463 static inline int gpio_port_set_clr_bits(const struct device *port,
1464 gpio_port_pins_t set_pins,
1465 gpio_port_pins_t clear_pins)
1466 {
1467 __ASSERT((set_pins & clear_pins) == 0, "Set and Clear pins overlap");
1468
1469 return gpio_port_set_masked(port, set_pins | clear_pins, set_pins);
1470 }
1471
1472 /**
1473 * @brief Get physical level of an input pin.
1474 *
1475 * A low physical level on the pin will be interpreted as value 0. A high
1476 * physical level will be interpreted as value 1. This function ignores
1477 * GPIO_ACTIVE_LOW flag.
1478 *
1479 * @param port Pointer to the device structure for the driver instance.
1480 * @param pin Pin number.
1481 *
1482 * @retval 1 If pin physical level is high.
1483 * @retval 0 If pin physical level is low.
1484 * @retval -EIO I/O error when accessing an external GPIO chip.
1485 * @retval -EWOULDBLOCK if operation would block.
1486 */
gpio_pin_get_raw(const struct device * port,gpio_pin_t pin)1487 static inline int gpio_pin_get_raw(const struct device *port, gpio_pin_t pin)
1488 {
1489 __unused const struct gpio_driver_config *const cfg =
1490 (const struct gpio_driver_config *)port->config;
1491 gpio_port_value_t value;
1492 int ret;
1493
1494 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1495 "Unsupported pin");
1496
1497 ret = gpio_port_get_raw(port, &value);
1498 if (ret == 0) {
1499 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1500 }
1501
1502 return ret;
1503 }
1504
1505 /**
1506 * @brief Get logical level of an input pin.
1507 *
1508 * Get logical level of an input pin taking into account GPIO_ACTIVE_LOW flag.
1509 * If pin is configured as Active High, a low physical level will be interpreted
1510 * as logical value 0. If pin is configured as Active Low, a low physical level
1511 * will be interpreted as logical value 1.
1512 *
1513 * Note: If pin is configured as Active High, the default, gpio_pin_get()
1514 * function is equivalent to gpio_pin_get_raw().
1515 *
1516 * @param port Pointer to the device structure for the driver instance.
1517 * @param pin Pin number.
1518 *
1519 * @retval 1 If pin logical value is 1 / active.
1520 * @retval 0 If pin logical value is 0 / inactive.
1521 * @retval -EIO I/O error when accessing an external GPIO chip.
1522 * @retval -EWOULDBLOCK if operation would block.
1523 */
gpio_pin_get(const struct device * port,gpio_pin_t pin)1524 static inline int gpio_pin_get(const struct device *port, gpio_pin_t pin)
1525 {
1526 __unused const struct gpio_driver_config *const cfg =
1527 (const struct gpio_driver_config *)port->config;
1528 gpio_port_value_t value;
1529 int ret;
1530
1531 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1532 "Unsupported pin");
1533
1534 ret = gpio_port_get(port, &value);
1535 if (ret == 0) {
1536 ret = (value & (gpio_port_pins_t)BIT(pin)) != 0 ? 1 : 0;
1537 }
1538
1539 return ret;
1540 }
1541
1542 /**
1543 * @brief Get logical level of an input pin from a @p gpio_dt_spec.
1544 *
1545 * This is equivalent to:
1546 *
1547 * gpio_pin_get(spec->port, spec->pin);
1548 *
1549 * @param spec GPIO specification from devicetree
1550 * @return a value from gpio_pin_get()
1551 */
gpio_pin_get_dt(const struct gpio_dt_spec * spec)1552 static inline int gpio_pin_get_dt(const struct gpio_dt_spec *spec)
1553 {
1554 return gpio_pin_get(spec->port, spec->pin);
1555 }
1556
1557 /**
1558 * @brief Set physical level of an output pin.
1559 *
1560 * Writing value 0 to the pin will set it to a low physical level. Writing any
1561 * value other than 0 will set it to a high physical level. This function
1562 * ignores GPIO_ACTIVE_LOW flag.
1563 *
1564 * @param port Pointer to the device structure for the driver instance.
1565 * @param pin Pin number.
1566 * @param value Value assigned to the pin.
1567 *
1568 * @retval 0 If successful.
1569 * @retval -EIO I/O error when accessing an external GPIO chip.
1570 * @retval -EWOULDBLOCK if operation would block.
1571 */
gpio_pin_set_raw(const struct device * port,gpio_pin_t pin,int value)1572 static inline int gpio_pin_set_raw(const struct device *port, gpio_pin_t pin,
1573 int value)
1574 {
1575 __unused const struct gpio_driver_config *const cfg =
1576 (const struct gpio_driver_config *)port->config;
1577 int ret;
1578
1579 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1580 "Unsupported pin");
1581
1582 if (value != 0) {
1583 ret = gpio_port_set_bits_raw(port, (gpio_port_pins_t)BIT(pin));
1584 } else {
1585 ret = gpio_port_clear_bits_raw(port, (gpio_port_pins_t)BIT(pin));
1586 }
1587
1588 return ret;
1589 }
1590
1591 /**
1592 * @brief Set logical level of an output pin.
1593 *
1594 * Set logical level of an output pin taking into account GPIO_ACTIVE_LOW flag.
1595 * Value 0 sets the pin in logical 0 / inactive state. Any value other than 0
1596 * sets the pin in logical 1 / active state. If pin is configured as Active
1597 * High, the default, setting it in inactive state will force the pin to a low
1598 * physical level. If pin is configured as Active Low, setting it in inactive
1599 * state will force the pin to a high physical level.
1600 *
1601 * Note: If pin is configured as Active High, gpio_pin_set() function is
1602 * equivalent to gpio_pin_set_raw().
1603 *
1604 * @param port Pointer to the device structure for the driver instance.
1605 * @param pin Pin number.
1606 * @param value Value assigned to the pin.
1607 *
1608 * @retval 0 If successful.
1609 * @retval -EIO I/O error when accessing an external GPIO chip.
1610 * @retval -EWOULDBLOCK if operation would block.
1611 */
gpio_pin_set(const struct device * port,gpio_pin_t pin,int value)1612 static inline int gpio_pin_set(const struct device *port, gpio_pin_t pin,
1613 int value)
1614 {
1615 __unused const struct gpio_driver_config *const cfg =
1616 (const struct gpio_driver_config *)port->config;
1617 const struct gpio_driver_data *const data =
1618 (const struct gpio_driver_data *)port->data;
1619
1620 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1621 "Unsupported pin");
1622
1623 if (data->invert & (gpio_port_pins_t)BIT(pin)) {
1624 value = (value != 0) ? 0 : 1;
1625 }
1626
1627 return gpio_pin_set_raw(port, pin, value);
1628 }
1629
1630 /**
1631 * @brief Set logical level of a output pin from a @p gpio_dt_spec.
1632 *
1633 * This is equivalent to:
1634 *
1635 * gpio_pin_set(spec->port, spec->pin, value);
1636 *
1637 * @param spec GPIO specification from devicetree
1638 * @param value Value assigned to the pin.
1639 * @return a value from gpio_pin_set()
1640 */
gpio_pin_set_dt(const struct gpio_dt_spec * spec,int value)1641 static inline int gpio_pin_set_dt(const struct gpio_dt_spec *spec, int value)
1642 {
1643 return gpio_pin_set(spec->port, spec->pin, value);
1644 }
1645
1646 /**
1647 * @brief Toggle pin level.
1648 *
1649 * @param port Pointer to the device structure for the driver instance.
1650 * @param pin Pin number.
1651 *
1652 * @retval 0 If successful.
1653 * @retval -EIO I/O error when accessing an external GPIO chip.
1654 * @retval -EWOULDBLOCK if operation would block.
1655 */
gpio_pin_toggle(const struct device * port,gpio_pin_t pin)1656 static inline int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)
1657 {
1658 __unused const struct gpio_driver_config *const cfg =
1659 (const struct gpio_driver_config *)port->config;
1660
1661 __ASSERT((cfg->port_pin_mask & (gpio_port_pins_t)BIT(pin)) != 0U,
1662 "Unsupported pin");
1663
1664 return gpio_port_toggle_bits(port, (gpio_port_pins_t)BIT(pin));
1665 }
1666
1667 /**
1668 * @brief Toggle pin level from a @p gpio_dt_spec.
1669 *
1670 * This is equivalent to:
1671 *
1672 * gpio_pin_toggle(spec->port, spec->pin);
1673 *
1674 * @param spec GPIO specification from devicetree
1675 * @return a value from gpio_pin_toggle()
1676 */
gpio_pin_toggle_dt(const struct gpio_dt_spec * spec)1677 static inline int gpio_pin_toggle_dt(const struct gpio_dt_spec *spec)
1678 {
1679 return gpio_pin_toggle(spec->port, spec->pin);
1680 }
1681
1682 /**
1683 * @brief Helper to initialize a struct gpio_callback properly
1684 * @param callback A valid Application's callback structure pointer.
1685 * @param handler A valid handler function pointer.
1686 * @param pin_mask A bit mask of relevant pins for the handler
1687 */
gpio_init_callback(struct gpio_callback * callback,gpio_callback_handler_t handler,gpio_port_pins_t pin_mask)1688 static inline void gpio_init_callback(struct gpio_callback *callback,
1689 gpio_callback_handler_t handler,
1690 gpio_port_pins_t pin_mask)
1691 {
1692 __ASSERT(callback, "Callback pointer should not be NULL");
1693 __ASSERT(handler, "Callback handler pointer should not be NULL");
1694
1695 callback->handler = handler;
1696 callback->pin_mask = pin_mask;
1697 }
1698
1699 /**
1700 * @brief Add an application callback.
1701 * @param port Pointer to the device structure for the driver instance.
1702 * @param callback A valid Application's callback structure pointer.
1703 * @retval 0 If successful
1704 * @retval -ENOSYS If driver does not implement the operation
1705 * @retval -errno Other negative errno code on failure.
1706 *
1707 * @note Callbacks may be added to the device from within a callback
1708 * handler invocation, but whether they are invoked for the current
1709 * GPIO event is not specified.
1710 *
1711 * Note: enables to add as many callback as needed on the same port.
1712 */
gpio_add_callback(const struct device * port,struct gpio_callback * callback)1713 static inline int gpio_add_callback(const struct device *port,
1714 struct gpio_callback *callback)
1715 {
1716 const struct gpio_driver_api *api =
1717 (const struct gpio_driver_api *)port->api;
1718
1719 if (api->manage_callback == NULL) {
1720 return -ENOSYS;
1721 }
1722
1723 return api->manage_callback(port, callback, true);
1724 }
1725
1726 /**
1727 * @brief Add an application callback.
1728 *
1729 * This is equivalent to:
1730 *
1731 * gpio_add_callback(spec->port, callback);
1732 *
1733 * @param spec GPIO specification from devicetree.
1734 * @param callback A valid application's callback structure pointer.
1735 * @return a value from gpio_add_callback().
1736 */
gpio_add_callback_dt(const struct gpio_dt_spec * spec,struct gpio_callback * callback)1737 static inline int gpio_add_callback_dt(const struct gpio_dt_spec *spec,
1738 struct gpio_callback *callback)
1739 {
1740 return gpio_add_callback(spec->port, callback);
1741 }
1742
1743 /**
1744 * @brief Remove an application callback.
1745 * @param port Pointer to the device structure for the driver instance.
1746 * @param callback A valid application's callback structure pointer.
1747 * @retval 0 If successful
1748 * @retval -ENOSYS If driver does not implement the operation
1749 * @retval -errno Other negative errno code on failure.
1750 *
1751 * @warning It is explicitly permitted, within a callback handler, to
1752 * remove the registration for the callback that is running, i.e. @p
1753 * callback. Attempts to remove other registrations on the same
1754 * device may result in undefined behavior, including failure to
1755 * invoke callbacks that remain registered and unintended invocation
1756 * of removed callbacks.
1757 *
1758 * Note: enables to remove as many callbacks as added through
1759 * gpio_add_callback().
1760 */
gpio_remove_callback(const struct device * port,struct gpio_callback * callback)1761 static inline int gpio_remove_callback(const struct device *port,
1762 struct gpio_callback *callback)
1763 {
1764 const struct gpio_driver_api *api =
1765 (const struct gpio_driver_api *)port->api;
1766
1767 if (api->manage_callback == NULL) {
1768 return -ENOSYS;
1769 }
1770
1771 return api->manage_callback(port, callback, false);
1772 }
1773
1774 /**
1775 * @brief Remove an application callback.
1776 *
1777 * This is equivalent to:
1778 *
1779 * gpio_remove_callback(spec->port, callback);
1780 *
1781 * @param spec GPIO specification from devicetree.
1782 * @param callback A valid application's callback structure pointer.
1783 * @return a value from gpio_remove_callback().
1784 */
gpio_remove_callback_dt(const struct gpio_dt_spec * spec,struct gpio_callback * callback)1785 static inline int gpio_remove_callback_dt(const struct gpio_dt_spec *spec,
1786 struct gpio_callback *callback)
1787 {
1788 return gpio_remove_callback(spec->port, callback);
1789 }
1790
1791 /**
1792 * @brief Function to get pending interrupts
1793 *
1794 * The purpose of this function is to return the interrupt
1795 * status register for the device.
1796 * This is especially useful when waking up from
1797 * low power states to check the wake up source.
1798 *
1799 * @param dev Pointer to the device structure for the driver instance.
1800 *
1801 * @retval status != 0 if at least one gpio interrupt is pending.
1802 * @retval 0 if no gpio interrupt is pending.
1803 * @retval -ENOSYS If driver does not implement the operation
1804 */
1805 __syscall int gpio_get_pending_int(const struct device *dev);
1806
z_impl_gpio_get_pending_int(const struct device * dev)1807 static inline int z_impl_gpio_get_pending_int(const struct device *dev)
1808 {
1809 const struct gpio_driver_api *api =
1810 (const struct gpio_driver_api *)dev->api;
1811
1812 if (api->get_pending_int == NULL) {
1813 return -ENOSYS;
1814 }
1815
1816 return api->get_pending_int(dev);
1817 }
1818
1819 /**
1820 * @}
1821 */
1822
1823 #ifdef __cplusplus
1824 }
1825 #endif
1826
1827 #include <syscalls/gpio.h>
1828
1829 #endif /* ZEPHYR_INCLUDE_DRIVERS_GPIO_H_ */
1830