1 /* ST Microelectronics STTS22H temperature sensor
2 *
3 * Copyright (c) 2024 STMicroelectronics
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Datasheet:
8 * https://www.st.com/resource/en/datasheet/stts22h.pdf
9 */
10
11 #define DT_DRV_COMPAT st_stts22h
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 "stts22h.h"
19
20 LOG_MODULE_DECLARE(STTS22H, CONFIG_SENSOR_LOG_LEVEL);
21
22 /**
23 * stts22h_trigger_set - link external trigger to event data ready
24 */
stts22h_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)25 int stts22h_trigger_set(const struct device *dev,
26 const struct sensor_trigger *trig,
27 sensor_trigger_handler_t handler)
28 {
29 struct stts22h_data *stts22h = dev->data;
30 const struct stts22h_config *config = dev->config;
31
32 if (!config->int_gpio.port) {
33 return -ENOTSUP;
34 }
35
36 if (trig->chan != SENSOR_CHAN_ALL &&
37 trig->chan != SENSOR_CHAN_AMBIENT_TEMP) {
38 LOG_ERR("Unsupported sensor trigger %d", trig->chan);
39 return -ENOTSUP;
40 }
41
42 stts22h->thsld_handler = handler;
43 stts22h->thsld_trigger = trig;
44
45 return 0;
46 }
47
48 /**
49 * stts22h_handle_interrupt - handle the thsld event
50 * read data and call handler if registered any
51 */
stts22h_handle_interrupt(const struct device * dev)52 static void stts22h_handle_interrupt(const struct device *dev)
53 {
54 struct stts22h_data *stts22h = dev->data;
55 const struct stts22h_config *cfg = dev->config;
56 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
57 stts22h_temp_trlhd_src_t status;
58
59 stts22h_temp_trshld_src_get(ctx, &status);
60
61 if (stts22h->thsld_handler != NULL &&
62 (status.under_thl || status.over_thh)) {
63 stts22h->thsld_handler(dev, stts22h->thsld_trigger);
64 }
65
66 gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
67 }
68
stts22h_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)69 static void stts22h_gpio_callback(const struct device *dev,
70 struct gpio_callback *cb, uint32_t pins)
71 {
72 struct stts22h_data *stts22h =
73 CONTAINER_OF(cb, struct stts22h_data, gpio_cb);
74 const struct stts22h_config *cfg = stts22h->dev->config;
75
76 ARG_UNUSED(pins);
77
78 gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_DISABLE);
79
80 #if defined(CONFIG_STTS22H_TRIGGER_OWN_THREAD)
81 k_sem_give(&stts22h->gpio_sem);
82 #elif defined(CONFIG_STTS22H_TRIGGER_GLOBAL_THREAD)
83 k_work_submit(&stts22h->work);
84 #endif /* CONFIG_STTS22H_TRIGGER_OWN_THREAD */
85 }
86
87 #ifdef CONFIG_STTS22H_TRIGGER_OWN_THREAD
stts22h_thread(void * p1,void * p2,void * p3)88 static void stts22h_thread(void *p1, void *p2, void *p3)
89 {
90 ARG_UNUSED(p2);
91 ARG_UNUSED(p3);
92
93 struct stts22h_data *stts22h = p1;
94
95 while (1) {
96 k_sem_take(&stts22h->gpio_sem, K_FOREVER);
97 stts22h_handle_interrupt(stts22h->dev);
98 }
99 }
100 #endif /* CONFIG_STTS22H_TRIGGER_OWN_THREAD */
101
102 #ifdef CONFIG_STTS22H_TRIGGER_GLOBAL_THREAD
stts22h_work_cb(struct k_work * work)103 static void stts22h_work_cb(struct k_work *work)
104 {
105 struct stts22h_data *stts22h =
106 CONTAINER_OF(work, struct stts22h_data, work);
107
108 stts22h_handle_interrupt(stts22h->dev);
109 }
110 #endif /* CONFIG_STTS22H_TRIGGER_GLOBAL_THREAD */
111
stts22h_init_interrupt(const struct device * dev)112 int stts22h_init_interrupt(const struct device *dev)
113 {
114 struct stts22h_data *stts22h = dev->data;
115 const struct stts22h_config *cfg = dev->config;
116 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
117 int ret;
118
119 if (!gpio_is_ready_dt(&cfg->int_gpio)) {
120 LOG_ERR("GPIO device not ready");
121 return -ENODEV;
122 }
123
124 #if defined(CONFIG_STTS22H_TRIGGER_OWN_THREAD)
125 k_sem_init(&stts22h->gpio_sem, 0, K_SEM_MAX_LIMIT);
126
127 k_thread_create(&stts22h->thread, stts22h->thread_stack,
128 CONFIG_STTS22H_THREAD_STACK_SIZE,
129 stts22h_thread, stts22h,
130 NULL, NULL, K_PRIO_COOP(CONFIG_STTS22H_THREAD_PRIORITY),
131 0, K_NO_WAIT);
132 k_thread_name_set(&stts22h->thread, dev->name);
133 #elif defined(CONFIG_STTS22H_TRIGGER_GLOBAL_THREAD)
134 stts22h->work.handler = stts22h_work_cb;
135 #endif /* CONFIG_STTS22H_TRIGGER_OWN_THREAD */
136
137 ret = gpio_pin_configure_dt(&cfg->int_gpio, GPIO_INPUT);
138 if (ret < 0) {
139 LOG_DBG("Could not configure gpio");
140 return ret;
141 }
142
143 gpio_init_callback(&stts22h->gpio_cb, stts22h_gpio_callback, BIT(cfg->int_gpio.pin));
144
145 if (gpio_add_callback(cfg->int_gpio.port, &stts22h->gpio_cb) < 0) {
146 LOG_DBG("Could not set gpio callback");
147 return -EIO;
148 }
149
150 /* Enable interrupt on high/low temperature threshold */
151 if (stts22h_temp_trshld_high_set(ctx, cfg->temp_hi) < 0) {
152 LOG_DBG("Could not set gpio callback");
153 return -EIO;
154 }
155
156 if (stts22h_temp_trshld_low_set(ctx, cfg->temp_lo) < 0) {
157 LOG_DBG("Could not set gpio callback");
158 return -EIO;
159 }
160
161 return gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
162 }
163