1 /*
2  * Copyright (c) 2016 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 
8 #ifndef ZEPHYR_INCLUDE_DRIVERS_GPIO_GPIO_UTILS_H_
9 #define ZEPHYR_INCLUDE_DRIVERS_GPIO_GPIO_UTILS_H_
10 
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <errno.h>
14 
15 #include <zephyr/devicetree.h>
16 #include <zephyr/drivers/gpio.h>
17 #include <zephyr/sys/__assert.h>
18 #include <zephyr/sys/slist.h>
19 
20 #define GPIO_PORT_PIN_MASK_FROM_NGPIOS(ngpios)			\
21 	((gpio_port_pins_t)(((uint64_t)1 << (ngpios)) - 1U))
22 
23 /**
24  * @brief Makes a bitmask of allowed GPIOs from the @p "gpio-reserved-ranges"
25  *        and @p "ngpios" DT properties values
26  *
27  * @param node_id GPIO controller node identifier.
28  * @return the bitmask of allowed gpios
29  * @see GPIO_DT_PORT_PIN_MASK_NGPIOS_EXC()
30  */
31 #define GPIO_PORT_PIN_MASK_FROM_DT_NODE(node_id)		\
32 	GPIO_DT_PORT_PIN_MASK_NGPIOS_EXC(node_id, DT_PROP(node_id, ngpios))
33 
34 /**
35  * @brief Make a bitmask of allowed GPIOs from a DT_DRV_COMPAT instance's GPIO
36  *        @p "gpio-reserved-ranges" and @p "ngpios" DT properties values
37  *
38  * @param inst DT_DRV_COMPAT instance number
39  * @return the bitmask of allowed gpios
40  * @see GPIO_DT_PORT_PIN_MASK_NGPIOS_EXC()
41  */
42 #define GPIO_PORT_PIN_MASK_FROM_DT_INST(inst)			\
43 	GPIO_PORT_PIN_MASK_FROM_DT_NODE(DT_DRV_INST(inst))
44 
45 /**
46  * @brief Generic function to insert or remove a callback from a callback list
47  *
48  * @param callbacks A pointer to the original list of callbacks (can be NULL)
49  * @param callback A pointer of the callback to insert or remove from the list
50  * @param set A boolean indicating insertion or removal of the callback
51  *
52  * @return 0 on success, negative errno otherwise.
53  */
gpio_manage_callback(sys_slist_t * callbacks,struct gpio_callback * callback,bool set)54 static inline int gpio_manage_callback(sys_slist_t *callbacks,
55 					struct gpio_callback *callback,
56 					bool set)
57 {
58 	__ASSERT(callback, "No callback!");
59 	__ASSERT(callback->handler, "No callback handler!");
60 
61 	if (!sys_slist_is_empty(callbacks)) {
62 		if (!sys_slist_find_and_remove(callbacks, &callback->node)) {
63 			if (!set) {
64 				return -EINVAL;
65 			}
66 		}
67 	} else if (!set) {
68 		return -EINVAL;
69 	}
70 
71 	if (set) {
72 		sys_slist_prepend(callbacks, &callback->node);
73 	}
74 
75 	return 0;
76 }
77 
78 /**
79  * @brief Generic function to go through and fire callback from a callback list
80  *
81  * @param list A pointer on the gpio callback list
82  * @param port A pointer on the gpio driver instance
83  * @param pins The actual pin mask that triggered the interrupt
84  */
gpio_fire_callbacks(sys_slist_t * list,const struct device * port,uint32_t pins)85 static inline void gpio_fire_callbacks(sys_slist_t *list,
86 					const struct device *port,
87 					uint32_t pins)
88 {
89 	struct gpio_callback *cb, *tmp;
90 
91 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(list, cb, tmp, node) {
92 		if (cb->pin_mask & pins) {
93 			__ASSERT(cb->handler, "No callback handler!");
94 			cb->handler(port, cb, cb->pin_mask & pins);
95 		}
96 	}
97 }
98 
99 #endif /* ZEPHYR_INCLUDE_DRIVERS_GPIO_GPIO_UTILS_H_ */
100