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