1 /* ST Microelectronics STTS751 temperature sensor
2  *
3  * Copyright (c) 2019 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Datasheet:
8  * https://www.st.com/resource/en/datasheet/stts751.pdf
9  */
10 
11 #define DT_DRV_COMPAT st_stts751
12 
13 #include <zephyr/kernel.h>
14 #include <zephyr/drivers/sensor.h>
15 #include <zephyr/drivers/gpio.h>
16 #include <zephyr/logging/log.h>
17 
18 #include "stts751.h"
19 
20 LOG_MODULE_DECLARE(STTS751, CONFIG_SENSOR_LOG_LEVEL);
21 
22 /**
23  * stts751_enable_int - enable selected int pin to generate interrupt
24  */
stts751_enable_int(const struct device * dev,int enable)25 static int stts751_enable_int(const struct device *dev, int enable)
26 {
27 	struct stts751_data *stts751 = dev->data;
28 	uint8_t en = (enable) ? 0 : 1;
29 
30 	return stts751_pin_event_route_set(stts751->ctx, en);
31 }
32 
33 /**
34  * stts751_trigger_set - link external trigger to event data ready
35  */
stts751_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)36 int stts751_trigger_set(const struct device *dev,
37 			  const struct sensor_trigger *trig,
38 			  sensor_trigger_handler_t handler)
39 {
40 	struct stts751_data *stts751 = dev->data;
41 	const struct stts751_config *config = dev->config;
42 
43 	if (!config->int_gpio.port) {
44 		return -ENOTSUP;
45 	}
46 
47 	if (trig->chan == SENSOR_CHAN_ALL) {
48 		stts751->thsld_handler = handler;
49 		stts751->thsld_trigger = trig;
50 		if (handler) {
51 			return stts751_enable_int(dev, 1);
52 		} else {
53 			return stts751_enable_int(dev, 0);
54 		}
55 	}
56 
57 	return -ENOTSUP;
58 }
59 
60 /**
61  * stts751_handle_interrupt - handle the thsld event
62  * read data and call handler if registered any
63  */
stts751_handle_interrupt(const struct device * dev)64 static void stts751_handle_interrupt(const struct device *dev)
65 {
66 	struct stts751_data *stts751 = dev->data;
67 	const struct stts751_config *cfg = dev->config;
68 	stts751_status_t status;
69 
70 	stts751_status_reg_get(stts751->ctx, &status);
71 
72 	if (stts751->thsld_handler != NULL &&
73 	    (status.t_high || status.t_low)) {
74 		stts751->thsld_handler(dev, stts751->thsld_trigger);
75 	}
76 
77 	gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
78 }
79 
stts751_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)80 static void stts751_gpio_callback(const struct device *dev,
81 				  struct gpio_callback *cb, uint32_t pins)
82 {
83 	struct stts751_data *stts751 =
84 		CONTAINER_OF(cb, struct stts751_data, gpio_cb);
85 	const struct stts751_config *cfg = stts751->dev->config;
86 
87 	ARG_UNUSED(pins);
88 
89 	gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_DISABLE);
90 
91 #if defined(CONFIG_STTS751_TRIGGER_OWN_THREAD)
92 	k_sem_give(&stts751->gpio_sem);
93 #elif defined(CONFIG_STTS751_TRIGGER_GLOBAL_THREAD)
94 	k_work_submit(&stts751->work);
95 #endif /* CONFIG_STTS751_TRIGGER_OWN_THREAD */
96 }
97 
98 #ifdef CONFIG_STTS751_TRIGGER_OWN_THREAD
stts751_thread(void * p1,void * p2,void * p3)99 static void stts751_thread(void *p1, void *p2, void *p3)
100 {
101 	ARG_UNUSED(p2);
102 	ARG_UNUSED(p3);
103 
104 	struct stts751_data *stts751 = p1;
105 
106 	while (1) {
107 		k_sem_take(&stts751->gpio_sem, K_FOREVER);
108 		stts751_handle_interrupt(stts751->dev);
109 	}
110 }
111 #endif /* CONFIG_STTS751_TRIGGER_OWN_THREAD */
112 
113 #ifdef CONFIG_STTS751_TRIGGER_GLOBAL_THREAD
stts751_work_cb(struct k_work * work)114 static void stts751_work_cb(struct k_work *work)
115 {
116 	struct stts751_data *stts751 =
117 		CONTAINER_OF(work, struct stts751_data, work);
118 
119 	stts751_handle_interrupt(stts751->dev);
120 }
121 #endif /* CONFIG_STTS751_TRIGGER_GLOBAL_THREAD */
122 
stts751_init_interrupt(const struct device * dev)123 int stts751_init_interrupt(const struct device *dev)
124 {
125 	struct stts751_data *stts751 = dev->data;
126 	const struct stts751_config *cfg = dev->config;
127 	int ret;
128 
129 	if (!gpio_is_ready_dt(&cfg->int_gpio)) {
130 		LOG_ERR("GPIO device not ready");
131 		return -ENODEV;
132 	}
133 
134 #if defined(CONFIG_STTS751_TRIGGER_OWN_THREAD)
135 	k_sem_init(&stts751->gpio_sem, 0, K_SEM_MAX_LIMIT);
136 
137 	k_thread_create(&stts751->thread, stts751->thread_stack,
138 			CONFIG_STTS751_THREAD_STACK_SIZE,
139 			stts751_thread, stts751,
140 			NULL, NULL, K_PRIO_COOP(CONFIG_STTS751_THREAD_PRIORITY),
141 			0, K_NO_WAIT);
142 #elif defined(CONFIG_STTS751_TRIGGER_GLOBAL_THREAD)
143 	stts751->work.handler = stts751_work_cb;
144 #endif /* CONFIG_STTS751_TRIGGER_OWN_THREAD */
145 
146 	ret = gpio_pin_configure_dt(&cfg->int_gpio, GPIO_INPUT);
147 	if (ret < 0) {
148 		LOG_DBG("Could not configure gpio");
149 		return ret;
150 	}
151 
152 	gpio_init_callback(&stts751->gpio_cb, stts751_gpio_callback, BIT(cfg->int_gpio.pin));
153 
154 	if (gpio_add_callback(cfg->int_gpio.port, &stts751->gpio_cb) < 0) {
155 		LOG_DBG("Could not set gpio callback");
156 		return -EIO;
157 	}
158 
159 	/* Enable interrupt on high temperature */
160 	float temp_hi = (float) CONFIG_STTS751_TEMP_HI_THRESHOLD;
161 	float temp_lo = (float) CONFIG_STTS751_TEMP_LO_THRESHOLD;
162 
163 	stts751_high_temperature_threshold_set(stts751->ctx,
164 					stts751_from_celsius_to_lsb(temp_hi));
165 
166 	stts751_low_temperature_threshold_set(stts751->ctx,
167 					stts751_from_celsius_to_lsb(temp_lo));
168 
169 	return gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
170 }
171