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 #define GPIO_PORT_PIN_MASK_FROM_DT_NODE(node_id) \
24 GPIO_PORT_PIN_MASK_FROM_NGPIOS(DT_PROP(node_id, ngpios))
25
26 #define GPIO_PORT_PIN_MASK_FROM_DT_INST(inst) \
27 GPIO_PORT_PIN_MASK_FROM_NGPIOS(DT_INST_PROP(inst, ngpios))
28
29 /**
30 * @brief Generic function to insert or remove a callback from a callback list
31 *
32 * @param callbacks A pointer to the original list of callbacks (can be NULL)
33 * @param callback A pointer of the callback to insert or remove from the list
34 * @param set A boolean indicating insertion or removal of the callback
35 *
36 * @return 0 on success, negative errno otherwise.
37 */
gpio_manage_callback(sys_slist_t * callbacks,struct gpio_callback * callback,bool set)38 static inline int gpio_manage_callback(sys_slist_t *callbacks,
39 struct gpio_callback *callback,
40 bool set)
41 {
42 __ASSERT(callback, "No callback!");
43 __ASSERT(callback->handler, "No callback handler!");
44
45 if (!sys_slist_is_empty(callbacks)) {
46 if (!sys_slist_find_and_remove(callbacks, &callback->node)) {
47 if (!set) {
48 return -EINVAL;
49 }
50 }
51 } else if (!set) {
52 return -EINVAL;
53 }
54
55 if (set) {
56 sys_slist_prepend(callbacks, &callback->node);
57 }
58
59 return 0;
60 }
61
62 /**
63 * @brief Generic function to go through and fire callback from a callback list
64 *
65 * @param list A pointer on the gpio callback list
66 * @param port A pointer on the gpio driver instance
67 * @param pins The actual pin mask that triggered the interrupt
68 */
gpio_fire_callbacks(sys_slist_t * list,const struct device * port,uint32_t pins)69 static inline void gpio_fire_callbacks(sys_slist_t *list,
70 const struct device *port,
71 uint32_t pins)
72 {
73 struct gpio_callback *cb, *tmp;
74
75 SYS_SLIST_FOR_EACH_CONTAINER_SAFE(list, cb, tmp, node) {
76 if (cb->pin_mask & pins) {
77 __ASSERT(cb->handler, "No callback handler!");
78 cb->handler(port, cb, cb->pin_mask & pins);
79 }
80 }
81 }
82
83 #endif /* ZEPHYR_INCLUDE_DRIVERS_GPIO_GPIO_UTILS_H_ */
84