1 /*
2 * Copyright (c) 2016-2021 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT st_hts221
8
9 #include <zephyr/device.h>
10 #include <zephyr/sys/__assert.h>
11 #include <zephyr/sys/util.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/logging/log.h>
14
15 #include "hts221.h"
16
17 LOG_MODULE_DECLARE(HTS221, CONFIG_SENSOR_LOG_LEVEL);
18
setup_drdy(const struct device * dev,bool enable)19 static inline void setup_drdy(const struct device *dev,
20 bool enable)
21 {
22 const struct hts221_config *cfg = dev->config;
23 unsigned int flags = enable
24 ? GPIO_INT_EDGE_TO_ACTIVE
25 : GPIO_INT_DISABLE;
26
27 gpio_pin_interrupt_configure_dt(&cfg->gpio_drdy, flags);
28 }
29
handle_drdy(const struct device * dev)30 static inline void handle_drdy(const struct device *dev)
31 {
32 struct hts221_data *data = dev->data;
33
34 setup_drdy(dev, false);
35
36 #if defined(CONFIG_HTS221_TRIGGER_OWN_THREAD)
37 k_sem_give(&data->drdy_sem);
38 #elif defined(CONFIG_HTS221_TRIGGER_GLOBAL_THREAD)
39 k_work_submit(&data->work);
40 #endif
41 }
42
process_drdy(const struct device * dev)43 static void process_drdy(const struct device *dev)
44 {
45 struct hts221_data *data = dev->data;
46
47 if (data->data_ready_handler != NULL) {
48 data->data_ready_handler(dev, data->data_ready_trigger);
49 }
50
51 if (data->data_ready_handler != NULL) {
52 setup_drdy(dev, true);
53 }
54 }
55
hts221_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)56 int hts221_trigger_set(const struct device *dev,
57 const struct sensor_trigger *trig,
58 sensor_trigger_handler_t handler)
59 {
60 struct hts221_data *data = dev->data;
61 const struct hts221_config *cfg = dev->config;
62
63 __ASSERT_NO_MSG(trig->type == SENSOR_TRIG_DATA_READY);
64
65 setup_drdy(dev, false);
66
67 data->data_ready_handler = handler;
68 if (handler == NULL) {
69 return 0;
70 }
71
72 data->data_ready_trigger = trig;
73
74 setup_drdy(dev, true);
75
76 /* If DRDY is active we probably won't get the rising edge, so
77 * invoke the callback manually.
78 */
79 if (gpio_pin_get_dt(&cfg->gpio_drdy) > 0) {
80 handle_drdy(dev);
81 }
82
83 return 0;
84 }
85
hts221_drdy_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)86 static void hts221_drdy_callback(const struct device *dev,
87 struct gpio_callback *cb, uint32_t pins)
88 {
89 struct hts221_data *data =
90 CONTAINER_OF(cb, struct hts221_data, drdy_cb);
91
92 ARG_UNUSED(pins);
93
94 handle_drdy(data->dev);
95 }
96
97 #ifdef CONFIG_HTS221_TRIGGER_OWN_THREAD
hts221_thread(void * p1,void * p2,void * p3)98 static void hts221_thread(void *p1, void *p2, void *p3)
99 {
100 ARG_UNUSED(p2);
101 ARG_UNUSED(p3);
102
103 struct hts221_data *data = p1;
104
105 while (1) {
106 k_sem_take(&data->drdy_sem, K_FOREVER);
107 process_drdy(data->dev);
108 }
109 }
110 #endif
111
112 #ifdef CONFIG_HTS221_TRIGGER_GLOBAL_THREAD
hts221_work_cb(struct k_work * work)113 static void hts221_work_cb(struct k_work *work)
114 {
115 struct hts221_data *data =
116 CONTAINER_OF(work, struct hts221_data, work);
117
118 process_drdy(data->dev);
119 }
120 #endif
121
hts221_init_interrupt(const struct device * dev)122 int hts221_init_interrupt(const struct device *dev)
123 {
124 struct hts221_data *data = dev->data;
125 const struct hts221_config *cfg = dev->config;
126 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
127 int status;
128
129 if (cfg->gpio_drdy.port == NULL) {
130 LOG_DBG("gpio_drdy not defined in DT");
131 return 0;
132 }
133
134 if (!gpio_is_ready_dt(&cfg->gpio_drdy)) {
135 LOG_ERR("device %s is not ready", cfg->gpio_drdy.port->name);
136 return -ENODEV;
137 }
138
139 data->dev = dev;
140
141 /* setup data ready gpio interrupt */
142 status = gpio_pin_configure_dt(&cfg->gpio_drdy, GPIO_INPUT);
143 if (status < 0) {
144 LOG_ERR("Could not configure %s.%02u",
145 cfg->gpio_drdy.port->name, cfg->gpio_drdy.pin);
146 return status;
147 }
148
149 gpio_init_callback(&data->drdy_cb,
150 hts221_drdy_callback,
151 BIT(cfg->gpio_drdy.pin));
152
153 status = gpio_add_callback(cfg->gpio_drdy.port, &data->drdy_cb);
154 if (status < 0) {
155 LOG_ERR("Could not set gpio callback.");
156 return status;
157 }
158
159 /* enable data-ready interrupt */
160 status = hts221_drdy_on_int_set(ctx, 1);
161 if (status < 0) {
162 LOG_ERR("Could not enable data-ready interrupt.");
163 return status;
164 }
165
166 #if defined(CONFIG_HTS221_TRIGGER_OWN_THREAD)
167 k_sem_init(&data->drdy_sem, 0, K_SEM_MAX_LIMIT);
168
169 k_thread_create(&data->thread, data->thread_stack,
170 CONFIG_HTS221_THREAD_STACK_SIZE,
171 hts221_thread, data,
172 NULL, NULL, K_PRIO_COOP(CONFIG_HTS221_THREAD_PRIORITY),
173 0, K_NO_WAIT);
174 #elif defined(CONFIG_HTS221_TRIGGER_GLOBAL_THREAD)
175 data->work.handler = hts221_work_cb;
176 #endif
177
178 setup_drdy(dev, true);
179
180 return 0;
181 }
182