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