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(struct stts751_data * stts751)99 static void stts751_thread(struct stts751_data *stts751)
100 {
101 while (1) {
102 k_sem_take(&stts751->gpio_sem, K_FOREVER);
103 stts751_handle_interrupt(stts751->dev);
104 }
105 }
106 #endif /* CONFIG_STTS751_TRIGGER_OWN_THREAD */
107
108 #ifdef CONFIG_STTS751_TRIGGER_GLOBAL_THREAD
stts751_work_cb(struct k_work * work)109 static void stts751_work_cb(struct k_work *work)
110 {
111 struct stts751_data *stts751 =
112 CONTAINER_OF(work, struct stts751_data, work);
113
114 stts751_handle_interrupt(stts751->dev);
115 }
116 #endif /* CONFIG_STTS751_TRIGGER_GLOBAL_THREAD */
117
stts751_init_interrupt(const struct device * dev)118 int stts751_init_interrupt(const struct device *dev)
119 {
120 struct stts751_data *stts751 = dev->data;
121 const struct stts751_config *cfg = dev->config;
122 int ret;
123
124 if (!gpio_is_ready_dt(&cfg->int_gpio)) {
125 LOG_ERR("GPIO device not ready");
126 return -ENODEV;
127 }
128
129 #if defined(CONFIG_STTS751_TRIGGER_OWN_THREAD)
130 k_sem_init(&stts751->gpio_sem, 0, K_SEM_MAX_LIMIT);
131
132 k_thread_create(&stts751->thread, stts751->thread_stack,
133 CONFIG_STTS751_THREAD_STACK_SIZE,
134 (k_thread_entry_t)stts751_thread, stts751,
135 NULL, NULL, K_PRIO_COOP(CONFIG_STTS751_THREAD_PRIORITY),
136 0, K_NO_WAIT);
137 #elif defined(CONFIG_STTS751_TRIGGER_GLOBAL_THREAD)
138 stts751->work.handler = stts751_work_cb;
139 #endif /* CONFIG_STTS751_TRIGGER_OWN_THREAD */
140
141 ret = gpio_pin_configure_dt(&cfg->int_gpio, GPIO_INPUT);
142 if (ret < 0) {
143 LOG_DBG("Could not configure gpio");
144 return ret;
145 }
146
147 gpio_init_callback(&stts751->gpio_cb, stts751_gpio_callback, BIT(cfg->int_gpio.pin));
148
149 if (gpio_add_callback(cfg->int_gpio.port, &stts751->gpio_cb) < 0) {
150 LOG_DBG("Could not set gpio callback");
151 return -EIO;
152 }
153
154 /* Enable interrupt on high temperature */
155 float temp_hi = (float) CONFIG_STTS751_TEMP_HI_THRESHOLD;
156 float temp_lo = (float) CONFIG_STTS751_TEMP_LO_THRESHOLD;
157
158 stts751_high_temperature_threshold_set(stts751->ctx,
159 stts751_from_celsius_to_lsb(temp_hi));
160
161 stts751_low_temperature_threshold_set(stts751->ctx,
162 stts751_from_celsius_to_lsb(temp_lo));
163
164 return gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
165 }
166