1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT ti_tmp007
8 
9 #include <device.h>
10 #include <drivers/gpio.h>
11 #include <sys/util.h>
12 #include <kernel.h>
13 #include <drivers/sensor.h>
14 
15 #include "tmp007.h"
16 
17 extern struct tmp007_data tmp007_driver;
18 
19 #include <logging/log.h>
20 LOG_MODULE_DECLARE(TMP007, CONFIG_SENSOR_LOG_LEVEL);
21 
setup_int(const struct device * dev,bool enable)22 static inline void setup_int(const struct device *dev,
23 			     bool enable)
24 {
25 	struct tmp007_data *data = dev->data;
26 
27 	gpio_pin_interrupt_configure(data->gpio,
28 				     DT_INST_GPIO_PIN(0, int_gpios),
29 				     enable
30 				     ? GPIO_INT_LEVEL_ACTIVE
31 				     : GPIO_INT_DISABLE);
32 }
33 
tmp007_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)34 int tmp007_attr_set(const struct device *dev,
35 		    enum sensor_channel chan,
36 		    enum sensor_attribute attr,
37 		    const struct sensor_value *val)
38 {
39 	struct tmp007_data *drv_data = dev->data;
40 	int64_t value;
41 	uint8_t reg;
42 
43 	if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
44 		return -ENOTSUP;
45 	}
46 
47 	if (attr == SENSOR_ATTR_UPPER_THRESH) {
48 		reg = TMP007_REG_TOBJ_TH_HIGH;
49 	} else if (attr == SENSOR_ATTR_LOWER_THRESH) {
50 		reg = TMP007_REG_TOBJ_TH_LOW;
51 	} else {
52 		return -ENOTSUP;
53 	}
54 
55 	value = (int64_t)val->val1 * 1000000 + val->val2;
56 	value = (value / TMP007_TEMP_TH_SCALE) << 6;
57 
58 	if (tmp007_reg_write(drv_data, reg, value) < 0) {
59 		LOG_DBG("Failed to set attribute!");
60 		return -EIO;
61 	}
62 
63 	return 0;
64 }
65 
tmp007_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)66 static void tmp007_gpio_callback(const struct device *dev,
67 				 struct gpio_callback *cb, uint32_t pins)
68 {
69 	struct tmp007_data *drv_data =
70 		CONTAINER_OF(cb, struct tmp007_data, gpio_cb);
71 
72 	setup_int(drv_data->dev, false);
73 
74 #if defined(CONFIG_TMP007_TRIGGER_OWN_THREAD)
75 	k_sem_give(&drv_data->gpio_sem);
76 #elif defined(CONFIG_TMP007_TRIGGER_GLOBAL_THREAD)
77 	k_work_submit(&drv_data->work);
78 #endif
79 }
80 
tmp007_thread_cb(const struct device * dev)81 static void tmp007_thread_cb(const struct device *dev)
82 {
83 	struct tmp007_data *drv_data = dev->data;
84 	uint16_t status;
85 
86 	if (tmp007_reg_read(drv_data, TMP007_REG_STATUS, &status) < 0) {
87 		return;
88 	}
89 
90 	if (status & TMP007_DATA_READY_INT_BIT &&
91 	    drv_data->drdy_handler != NULL) {
92 		drv_data->drdy_handler(dev, &drv_data->drdy_trigger);
93 	}
94 
95 	if (status & TMP007_TOBJ_TH_INT_BITS &&
96 	    drv_data->th_handler != NULL) {
97 		drv_data->th_handler(dev, &drv_data->th_trigger);
98 	}
99 
100 	setup_int(dev, true);
101 }
102 
103 #ifdef CONFIG_TMP007_TRIGGER_OWN_THREAD
tmp007_thread(struct tmp007_data * drv_data)104 static void tmp007_thread(struct tmp007_data *drv_data)
105 {
106 	while (1) {
107 		k_sem_take(&drv_data->gpio_sem, K_FOREVER);
108 		tmp007_thread_cb(drv_data->dev);
109 	}
110 }
111 #endif
112 
113 #ifdef CONFIG_TMP007_TRIGGER_GLOBAL_THREAD
tmp007_work_cb(struct k_work * work)114 static void tmp007_work_cb(struct k_work *work)
115 {
116 	struct tmp007_data *drv_data =
117 		CONTAINER_OF(work, struct tmp007_data, work);
118 
119 	tmp007_thread_cb(drv_data->dev);
120 }
121 #endif
122 
tmp007_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)123 int tmp007_trigger_set(const struct device *dev,
124 		       const struct sensor_trigger *trig,
125 		       sensor_trigger_handler_t handler)
126 {
127 	struct tmp007_data *drv_data = dev->data;
128 
129 	setup_int(dev, false);
130 
131 	if (trig->type == SENSOR_TRIG_DATA_READY) {
132 		drv_data->drdy_handler = handler;
133 		drv_data->drdy_trigger = *trig;
134 	} else if (trig->type == SENSOR_TRIG_THRESHOLD) {
135 		drv_data->th_handler = handler;
136 		drv_data->th_trigger = *trig;
137 	}
138 
139 	setup_int(dev, true);
140 
141 	return 0;
142 }
143 
tmp007_init_interrupt(const struct device * dev)144 int tmp007_init_interrupt(const struct device *dev)
145 {
146 	struct tmp007_data *drv_data = dev->data;
147 
148 	if (tmp007_reg_update(drv_data, TMP007_REG_CONFIG,
149 			      TMP007_ALERT_EN_BIT, TMP007_ALERT_EN_BIT) < 0) {
150 		LOG_DBG("Failed to enable interrupt pin!");
151 		return -EIO;
152 	}
153 
154 	drv_data->dev = dev;
155 
156 	/* setup gpio interrupt */
157 	drv_data->gpio = device_get_binding(DT_INST_GPIO_LABEL(0, int_gpios));
158 	if (drv_data->gpio == NULL) {
159 		LOG_DBG("Failed to get pointer to %s device!",
160 		    DT_INST_GPIO_LABEL(0, int_gpios));
161 		return -EINVAL;
162 	}
163 
164 	gpio_pin_configure(drv_data->gpio, DT_INST_GPIO_PIN(0, int_gpios),
165 			   DT_INST_GPIO_FLAGS(0, int_gpios)
166 			   | GPIO_INT_LEVEL_ACTIVE);
167 
168 	gpio_init_callback(&drv_data->gpio_cb,
169 			   tmp007_gpio_callback,
170 			   BIT(DT_INST_GPIO_PIN(0, int_gpios)));
171 
172 	if (gpio_add_callback(drv_data->gpio, &drv_data->gpio_cb) < 0) {
173 		LOG_DBG("Failed to set gpio callback!");
174 		return -EIO;
175 	}
176 
177 #if defined(CONFIG_TMP007_TRIGGER_OWN_THREAD)
178 	k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
179 
180 	k_thread_create(&drv_data->thread, drv_data->thread_stack,
181 			CONFIG_TMP007_THREAD_STACK_SIZE,
182 			(k_thread_entry_t)tmp007_thread, drv_data,
183 			NULL, NULL, K_PRIO_COOP(CONFIG_TMP007_THREAD_PRIORITY),
184 			0, K_NO_WAIT);
185 #elif defined(CONFIG_TMP007_TRIGGER_GLOBAL_THREAD)
186 	drv_data->work.handler = tmp007_work_cb;
187 #endif
188 
189 	return 0;
190 }
191