1 /*
2  * Copyright (c) 2017-2019 Phytec Messtechnik GmbH
3  * Copyright (c) 2017 Benedict Ohl (Benedict-Ohl@web.de)
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/gpio.h>
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/sys/util.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/drivers/sensor.h>
14 #include "amg88xx.h"
15 
16 extern struct amg88xx_data amg88xx_driver;
17 
18 #include <zephyr/logging/log.h>
19 LOG_MODULE_DECLARE(AMG88XX, CONFIG_SENSOR_LOG_LEVEL);
20 
amg88xx_setup_int(const struct amg88xx_config * cfg,bool enable)21 static inline void amg88xx_setup_int(const struct amg88xx_config *cfg,
22 				     bool enable)
23 {
24 	unsigned int flags = enable
25 		? GPIO_INT_EDGE_TO_ACTIVE
26 		: GPIO_INT_DISABLE;
27 
28 	gpio_pin_interrupt_configure_dt(&cfg->int_gpio, flags);
29 }
30 
amg88xx_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)31 int amg88xx_attr_set(const struct device *dev,
32 		     enum sensor_channel chan,
33 		     enum sensor_attribute attr,
34 		     const struct sensor_value *val)
35 {
36 	const struct amg88xx_config *config = dev->config;
37 	int16_t int_level = (val->val1 * 1000000 + val->val2) /
38 			  AMG88XX_TREG_LSB_SCALING;
39 	uint8_t intl_reg;
40 	uint8_t inth_reg;
41 
42 	if (!config->int_gpio.port) {
43 		return -ENOTSUP;
44 	}
45 
46 	if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
47 		return -ENOTSUP;
48 	}
49 
50 	LOG_DBG("set threshold to %d", int_level);
51 
52 	if (attr == SENSOR_ATTR_UPPER_THRESH) {
53 		intl_reg = AMG88XX_INTHL;
54 		inth_reg = AMG88XX_INTHH;
55 	} else if (attr == SENSOR_ATTR_LOWER_THRESH) {
56 		intl_reg = AMG88XX_INTLL;
57 		inth_reg = AMG88XX_INTLH;
58 	} else {
59 		return -ENOTSUP;
60 	}
61 
62 	if (i2c_reg_write_byte_dt(&config->i2c,
63 			       intl_reg, (uint8_t)int_level)) {
64 		LOG_DBG("Failed to set INTxL attribute!");
65 		return -EIO;
66 	}
67 
68 	if (i2c_reg_write_byte_dt(&config->i2c,
69 			       inth_reg, (uint8_t)(int_level >> 8))) {
70 		LOG_DBG("Failed to set INTxH attribute!");
71 		return -EIO;
72 	}
73 
74 	return 0;
75 }
76 
amg88xx_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)77 static void amg88xx_gpio_callback(const struct device *dev,
78 				  struct gpio_callback *cb, uint32_t pins)
79 {
80 	struct amg88xx_data *drv_data =
81 		CONTAINER_OF(cb, struct amg88xx_data, gpio_cb);
82 	const struct amg88xx_config *config = drv_data->dev->config;
83 
84 	amg88xx_setup_int(config, false);
85 
86 #if defined(CONFIG_AMG88XX_TRIGGER_OWN_THREAD)
87 	k_sem_give(&drv_data->gpio_sem);
88 #elif defined(CONFIG_AMG88XX_TRIGGER_GLOBAL_THREAD)
89 	k_work_submit(&drv_data->work);
90 #endif
91 }
92 
amg88xx_thread_cb(const struct device * dev)93 static void amg88xx_thread_cb(const struct device *dev)
94 {
95 	struct amg88xx_data *drv_data = dev->data;
96 	const struct amg88xx_config *config = dev->config;
97 	uint8_t status;
98 
99 	if (i2c_reg_read_byte_dt(&config->i2c,
100 			      AMG88XX_STAT, &status) < 0) {
101 		return;
102 	}
103 
104 	if (drv_data->drdy_handler != NULL) {
105 		drv_data->drdy_handler(dev, drv_data->drdy_trigger);
106 	}
107 
108 	if (drv_data->th_handler != NULL) {
109 		drv_data->th_handler(dev, drv_data->th_trigger);
110 	}
111 
112 	amg88xx_setup_int(config, true);
113 }
114 
115 #ifdef CONFIG_AMG88XX_TRIGGER_OWN_THREAD
amg88xx_thread(void * p1,void * p2,void * p3)116 static void amg88xx_thread(void *p1, void *p2, void *p3)
117 {
118 	ARG_UNUSED(p2);
119 	ARG_UNUSED(p3);
120 
121 	struct amg88xx_data *drv_data = p1;
122 
123 	while (42) {
124 		k_sem_take(&drv_data->gpio_sem, K_FOREVER);
125 		amg88xx_thread_cb(drv_data->dev);
126 	}
127 }
128 #endif
129 
130 #ifdef CONFIG_AMG88XX_TRIGGER_GLOBAL_THREAD
amg88xx_work_cb(struct k_work * work)131 static void amg88xx_work_cb(struct k_work *work)
132 {
133 	struct amg88xx_data *drv_data =
134 		CONTAINER_OF(work, struct amg88xx_data, work);
135 	amg88xx_thread_cb(drv_data->dev);
136 }
137 #endif
138 
amg88xx_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)139 int amg88xx_trigger_set(const struct device *dev,
140 			const struct sensor_trigger *trig,
141 			sensor_trigger_handler_t handler)
142 {
143 	struct amg88xx_data *drv_data = dev->data;
144 	const struct amg88xx_config *config = dev->config;
145 
146 	if (!config->int_gpio.port) {
147 		return -ENOTSUP;
148 	}
149 
150 	if (i2c_reg_write_byte_dt(&config->i2c,
151 			       AMG88XX_INTC, AMG88XX_INTC_DISABLED)) {
152 		return -EIO;
153 	}
154 
155 	amg88xx_setup_int(config, false);
156 
157 	if (trig->type == SENSOR_TRIG_THRESHOLD) {
158 		drv_data->th_handler = handler;
159 		drv_data->th_trigger = trig;
160 	} else {
161 		LOG_ERR("Unsupported sensor trigger");
162 		return -ENOTSUP;
163 	}
164 
165 	amg88xx_setup_int(config, true);
166 
167 	if (i2c_reg_write_byte_dt(&config->i2c,
168 			       AMG88XX_INTC, AMG88XX_INTC_ABS_MODE)) {
169 		return -EIO;
170 	}
171 
172 	return 0;
173 }
174 
amg88xx_init_interrupt(const struct device * dev)175 int amg88xx_init_interrupt(const struct device *dev)
176 {
177 	struct amg88xx_data *drv_data = dev->data;
178 	const struct amg88xx_config *config = dev->config;
179 
180 	if (!gpio_is_ready_dt(&config->int_gpio)) {
181 		LOG_ERR("%s: device %s is not ready", dev->name,
182 				config->int_gpio.port->name);
183 		return -ENODEV;
184 	}
185 
186 	gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT | config->int_gpio.dt_flags);
187 
188 	gpio_init_callback(&drv_data->gpio_cb,
189 			   amg88xx_gpio_callback,
190 			   BIT(config->int_gpio.pin));
191 
192 	if (gpio_add_callback(config->int_gpio.port, &drv_data->gpio_cb) < 0) {
193 		LOG_DBG("Failed to set gpio callback!");
194 		return -EIO;
195 	}
196 
197 	drv_data->dev = dev;
198 
199 #if defined(CONFIG_AMG88XX_TRIGGER_OWN_THREAD)
200 	k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
201 
202 	k_thread_create(&drv_data->thread, drv_data->thread_stack,
203 			CONFIG_AMG88XX_THREAD_STACK_SIZE,
204 			amg88xx_thread, drv_data,
205 			NULL, NULL, K_PRIO_COOP(CONFIG_AMG88XX_THREAD_PRIORITY),
206 			0, K_NO_WAIT);
207 #elif defined(CONFIG_AMG88XX_TRIGGER_GLOBAL_THREAD)
208 	drv_data->work.handler = amg88xx_work_cb;
209 #endif
210 	amg88xx_setup_int(config, true);
211 
212 	return 0;
213 }
214