1 /*
2  * Copyright (c) 2024 Gustavo Silva
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT sciosense_ens160
8 
9 #include <zephyr/kernel.h>
10 
11 #include "ens160.h"
12 
13 LOG_MODULE_DECLARE(ENS160, CONFIG_SENSOR_LOG_LEVEL);
14 
ens160_setup_int(const struct device * dev,bool enable)15 static inline void ens160_setup_int(const struct device *dev, bool enable)
16 {
17 	const struct ens160_config *config = dev->config;
18 	gpio_flags_t flags = enable ? GPIO_INT_EDGE_TO_ACTIVE : GPIO_INT_DISABLE;
19 
20 	(void)gpio_pin_interrupt_configure_dt(&config->int_gpio, flags);
21 }
22 
ens160_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)23 static void ens160_gpio_callback(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
24 {
25 	struct ens160_data *data = CONTAINER_OF(cb, struct ens160_data, gpio_cb);
26 
27 	ens160_setup_int(data->dev, false);
28 
29 #if defined(CONFIG_ENS160_TRIGGER_OWN_THREAD)
30 	k_sem_give(&data->gpio_sem);
31 #elif defined(CONFIG_ENS160_TRIGGER_GLOBAL_THREAD)
32 	k_work_submit(&data->work);
33 #endif
34 }
35 
ens160_thread_cb(const struct device * dev)36 static void ens160_thread_cb(const struct device *dev)
37 {
38 	struct ens160_data *data = dev->data;
39 	uint8_t status;
40 	int ret;
41 
42 	ret = data->tf->read_reg(dev, ENS160_REG_DEVICE_STATUS, &status);
43 	if (ret < 0) {
44 		return;
45 	}
46 
47 	if (FIELD_GET(ENS160_STATUS_NEWDAT, status) != 0x01) {
48 		LOG_ERR("Data is not ready");
49 		return;
50 	}
51 
52 	if (data->data_ready_handler != NULL) {
53 		data->data_ready_handler(dev, data->data_ready_trigger);
54 	}
55 
56 	ens160_setup_int(dev, true);
57 }
58 
59 #ifdef CONFIG_ENS160_TRIGGER_OWN_THREAD
ens160_thread(void * p1,void * p2,void * p3)60 static void ens160_thread(void *p1, void *p2, void *p3)
61 {
62 	ARG_UNUSED(p2);
63 	ARG_UNUSED(p3);
64 
65 	struct ens160_data *data = p1;
66 
67 	while (1) {
68 		k_sem_take(&data->gpio_sem, K_FOREVER);
69 		ens160_thread_cb(data->dev);
70 	}
71 }
72 #endif /* CONFIG_ENS160_TRIGGER_OWN_THREAD */
73 
74 #ifdef CONFIG_ENS160_TRIGGER_GLOBAL_THREAD
ens160_work_cb(struct k_work * work)75 static void ens160_work_cb(struct k_work *work)
76 {
77 	struct ens160_data *data = CONTAINER_OF(work, struct ens160_data, work);
78 
79 	ens160_thread_cb(data->dev);
80 }
81 #endif /* CONFIG_ENS160_TRIGGER_GLOBAL_THREAD */
82 
ens160_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)83 int ens160_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
84 		       sensor_trigger_handler_t handler)
85 {
86 	const struct ens160_config *config = dev->config;
87 	struct ens160_data *data = dev->data;
88 
89 	if (!config->int_gpio.port) {
90 		return -ENOTSUP;
91 	}
92 
93 	ens160_setup_int(dev, false);
94 
95 	if (trig->type == SENSOR_TRIG_DATA_READY) {
96 		data->data_ready_handler = handler;
97 		data->data_ready_trigger = trig;
98 	}
99 
100 	ens160_setup_int(dev, true);
101 
102 	return 0;
103 }
104 
ens160_init_interrupt(const struct device * dev)105 int ens160_init_interrupt(const struct device *dev)
106 {
107 	const struct ens160_config *config = dev->config;
108 	struct ens160_data *data = dev->data;
109 	int ret;
110 
111 	uint8_t int_cfg = ENS160_CONFIG_INTEN | ENS160_CONFIG_INTDAT | ENS160_CONFIG_INT_CFG;
112 
113 	ret = data->tf->write_reg(dev, ENS160_REG_CONFIG, int_cfg);
114 	if (ret < 0) {
115 		LOG_ERR("Failed to write to config register");
116 		return -EIO;
117 	}
118 
119 	if (!gpio_is_ready_dt(&config->int_gpio)) {
120 		LOG_ERR("%s: device %s is not ready", dev->name, config->int_gpio.port->name);
121 		return -ENODEV;
122 	}
123 
124 	gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
125 
126 	gpio_init_callback(&data->gpio_cb, ens160_gpio_callback, BIT(config->int_gpio.pin));
127 
128 	ret = gpio_add_callback(config->int_gpio.port, &data->gpio_cb);
129 	if (ret < 0) {
130 		LOG_ERR("Failed to set gpio callback");
131 		return -EIO;
132 	}
133 
134 	ret = gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
135 	if (ret < 0) {
136 		return ret;
137 	}
138 
139 	data->dev = dev;
140 
141 #if defined(CONFIG_ENS160_TRIGGER_OWN_THREAD)
142 	k_sem_init(&data->gpio_sem, 0, K_SEM_MAX_LIMIT);
143 
144 	k_thread_create(&data->thread, data->thread_stack, CONFIG_ENS160_THREAD_STACK_SIZE,
145 			ens160_thread, data, NULL, NULL, K_PRIO_COOP(CONFIG_ENS160_THREAD_PRIORITY),
146 			0, K_NO_WAIT);
147 #elif defined(CONFIG_ENS160_TRIGGER_GLOBAL_THREAD)
148 	data->work.handler = ens160_work_cb;
149 #endif
150 
151 	return 0;
152 }
153