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(struct hts221_data * data)98 static void hts221_thread(struct hts221_data *data)
99 {
100 	while (1) {
101 		k_sem_take(&data->drdy_sem, K_FOREVER);
102 		process_drdy(data->dev);
103 	}
104 }
105 #endif
106 
107 #ifdef CONFIG_HTS221_TRIGGER_GLOBAL_THREAD
hts221_work_cb(struct k_work * work)108 static void hts221_work_cb(struct k_work *work)
109 {
110 	struct hts221_data *data =
111 		CONTAINER_OF(work, struct hts221_data, work);
112 
113 	process_drdy(data->dev);
114 }
115 #endif
116 
hts221_init_interrupt(const struct device * dev)117 int hts221_init_interrupt(const struct device *dev)
118 {
119 	struct hts221_data *data = dev->data;
120 	const struct hts221_config *cfg = dev->config;
121 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
122 	int status;
123 
124 	if (cfg->gpio_drdy.port == NULL) {
125 		LOG_DBG("gpio_drdy not defined in DT");
126 		return 0;
127 	}
128 
129 	if (!gpio_is_ready_dt(&cfg->gpio_drdy)) {
130 		LOG_ERR("device %s is not ready", cfg->gpio_drdy.port->name);
131 		return -ENODEV;
132 	}
133 
134 	data->dev = dev;
135 
136 	/* setup data ready gpio interrupt */
137 	status = gpio_pin_configure_dt(&cfg->gpio_drdy, GPIO_INPUT);
138 	if (status < 0) {
139 		LOG_ERR("Could not configure %s.%02u",
140 			cfg->gpio_drdy.port->name, cfg->gpio_drdy.pin);
141 		return status;
142 	}
143 
144 	gpio_init_callback(&data->drdy_cb,
145 			   hts221_drdy_callback,
146 			   BIT(cfg->gpio_drdy.pin));
147 
148 	status = gpio_add_callback(cfg->gpio_drdy.port, &data->drdy_cb);
149 	if (status < 0) {
150 		LOG_ERR("Could not set gpio callback.");
151 		return status;
152 	}
153 
154 	/* enable data-ready interrupt */
155 	status = hts221_drdy_on_int_set(ctx, 1);
156 	if (status < 0) {
157 		LOG_ERR("Could not enable data-ready interrupt.");
158 		return status;
159 	}
160 
161 #if defined(CONFIG_HTS221_TRIGGER_OWN_THREAD)
162 	k_sem_init(&data->drdy_sem, 0, K_SEM_MAX_LIMIT);
163 
164 	k_thread_create(&data->thread, data->thread_stack,
165 			CONFIG_HTS221_THREAD_STACK_SIZE,
166 			(k_thread_entry_t)hts221_thread, data,
167 			NULL, NULL, K_PRIO_COOP(CONFIG_HTS221_THREAD_PRIORITY),
168 			0, K_NO_WAIT);
169 #elif defined(CONFIG_HTS221_TRIGGER_GLOBAL_THREAD)
170 	data->work.handler = hts221_work_cb;
171 #endif
172 
173 	setup_drdy(dev, true);
174 
175 	return 0;
176 }
177