1 /*
2  * Copyright (c) 2016 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file Header where utility code can be found for GPIO drivers
9  */
10 
11 #ifndef ZEPHYR_DRIVERS_GPIO_GPIO_UTILS_H_
12 #define ZEPHYR_DRIVERS_GPIO_GPIO_UTILS_H_
13 
14 #define GPIO_PORT_PIN_MASK_FROM_NGPIOS(ngpios)			\
15 	((gpio_port_pins_t)(((uint64_t)1 << (ngpios)) - 1U))
16 
17 #define GPIO_PORT_PIN_MASK_FROM_DT_NODE(node_id)		\
18 	GPIO_PORT_PIN_MASK_FROM_NGPIOS(DT_PROP(node_id, ngpios))
19 
20 #define GPIO_PORT_PIN_MASK_FROM_DT_INST(inst)			\
21 	GPIO_PORT_PIN_MASK_FROM_NGPIOS(DT_INST_PROP(inst, ngpios))
22 
23 /**
24  * @brief Generic function to insert or remove a callback from a callback list
25  *
26  * @param callbacks A pointer to the original list of callbacks (can be NULL)
27  * @param callback A pointer of the callback to insert or remove from the list
28  * @param set A boolean indicating insertion or removal of the callback
29  *
30  * @return 0 on success, negative errno otherwise.
31  */
gpio_manage_callback(sys_slist_t * callbacks,struct gpio_callback * callback,bool set)32 static inline int gpio_manage_callback(sys_slist_t *callbacks,
33 					struct gpio_callback *callback,
34 					bool set)
35 {
36 	__ASSERT(callback, "No callback!");
37 	__ASSERT(callback->handler, "No callback handler!");
38 
39 	if (!sys_slist_is_empty(callbacks)) {
40 		if (!sys_slist_find_and_remove(callbacks, &callback->node)) {
41 			if (!set) {
42 				return -EINVAL;
43 			}
44 		}
45 	}
46 
47 	if (set) {
48 		sys_slist_prepend(callbacks, &callback->node);
49 	}
50 
51 	return 0;
52 }
53 
54 /**
55  * @brief Generic function to go through and fire callback from a callback list
56  *
57  * @param list A pointer on the gpio callback list
58  * @param port A pointer on the gpio driver instance
59  * @param pins The actual pin mask that triggered the interrupt
60  */
gpio_fire_callbacks(sys_slist_t * list,const struct device * port,uint32_t pins)61 static inline void gpio_fire_callbacks(sys_slist_t *list,
62 					const struct device *port,
63 					uint32_t pins)
64 {
65 	struct gpio_callback *cb, *tmp;
66 
67 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(list, cb, tmp, node) {
68 		if (cb->pin_mask & pins) {
69 			__ASSERT(cb->handler, "No callback handler!");
70 			cb->handler(port, cb, cb->pin_mask & pins);
71 		}
72 	}
73 }
74 
75 #endif /* ZEPHYR_DRIVERS_GPIO_GPIO_UTILS_H_ */
76