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(struct amg88xx_data * drv_data)116 static void amg88xx_thread(struct amg88xx_data *drv_data)
117 {
118 	while (42) {
119 		k_sem_take(&drv_data->gpio_sem, K_FOREVER);
120 		amg88xx_thread_cb(drv_data->dev);
121 	}
122 }
123 #endif
124 
125 #ifdef CONFIG_AMG88XX_TRIGGER_GLOBAL_THREAD
amg88xx_work_cb(struct k_work * work)126 static void amg88xx_work_cb(struct k_work *work)
127 {
128 	struct amg88xx_data *drv_data =
129 		CONTAINER_OF(work, struct amg88xx_data, work);
130 	amg88xx_thread_cb(drv_data->dev);
131 }
132 #endif
133 
amg88xx_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)134 int amg88xx_trigger_set(const struct device *dev,
135 			const struct sensor_trigger *trig,
136 			sensor_trigger_handler_t handler)
137 {
138 	struct amg88xx_data *drv_data = dev->data;
139 	const struct amg88xx_config *config = dev->config;
140 
141 	if (!config->int_gpio.port) {
142 		return -ENOTSUP;
143 	}
144 
145 	if (i2c_reg_write_byte_dt(&config->i2c,
146 			       AMG88XX_INTC, AMG88XX_INTC_DISABLED)) {
147 		return -EIO;
148 	}
149 
150 	amg88xx_setup_int(config, false);
151 
152 	if (trig->type == SENSOR_TRIG_THRESHOLD) {
153 		drv_data->th_handler = handler;
154 		drv_data->th_trigger = trig;
155 	} else {
156 		LOG_ERR("Unsupported sensor trigger");
157 		return -ENOTSUP;
158 	}
159 
160 	amg88xx_setup_int(config, true);
161 
162 	if (i2c_reg_write_byte_dt(&config->i2c,
163 			       AMG88XX_INTC, AMG88XX_INTC_ABS_MODE)) {
164 		return -EIO;
165 	}
166 
167 	return 0;
168 }
169 
amg88xx_init_interrupt(const struct device * dev)170 int amg88xx_init_interrupt(const struct device *dev)
171 {
172 	struct amg88xx_data *drv_data = dev->data;
173 	const struct amg88xx_config *config = dev->config;
174 
175 	if (!gpio_is_ready_dt(&config->int_gpio)) {
176 		LOG_ERR("%s: device %s is not ready", dev->name,
177 				config->int_gpio.port->name);
178 		return -ENODEV;
179 	}
180 
181 	gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT | config->int_gpio.dt_flags);
182 
183 	gpio_init_callback(&drv_data->gpio_cb,
184 			   amg88xx_gpio_callback,
185 			   BIT(config->int_gpio.pin));
186 
187 	if (gpio_add_callback(config->int_gpio.port, &drv_data->gpio_cb) < 0) {
188 		LOG_DBG("Failed to set gpio callback!");
189 		return -EIO;
190 	}
191 
192 	drv_data->dev = dev;
193 
194 #if defined(CONFIG_AMG88XX_TRIGGER_OWN_THREAD)
195 	k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
196 
197 	k_thread_create(&drv_data->thread, drv_data->thread_stack,
198 			CONFIG_AMG88XX_THREAD_STACK_SIZE,
199 			(k_thread_entry_t)amg88xx_thread, drv_data,
200 			NULL, NULL, K_PRIO_COOP(CONFIG_AMG88XX_THREAD_PRIORITY),
201 			0, K_NO_WAIT);
202 #elif defined(CONFIG_AMG88XX_TRIGGER_GLOBAL_THREAD)
203 	drv_data->work.handler = amg88xx_work_cb;
204 #endif
205 	amg88xx_setup_int(config, true);
206 
207 	return 0;
208 }
209