1 /* ST Microelectronics LIS2DS12 3-axis accelerometer driver
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/lis2ds12.pdf
9 */
10
11 #define DT_DRV_COMPAT st_lis2ds12
12
13 #include <zephyr/logging/log.h>
14 #include "lis2ds12.h"
15
16 LOG_MODULE_DECLARE(LIS2DS12, CONFIG_SENSOR_LOG_LEVEL);
17
lis2ds12_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)18 static void lis2ds12_gpio_callback(const struct device *dev,
19 struct gpio_callback *cb, uint32_t pins)
20 {
21 struct lis2ds12_data *data =
22 CONTAINER_OF(cb, struct lis2ds12_data, gpio_cb);
23 const struct lis2ds12_config *cfg = data->dev->config;
24 int ret;
25
26 ARG_UNUSED(pins);
27
28 ret = gpio_pin_interrupt_configure_dt(&cfg->gpio_int, GPIO_INT_DISABLE);
29 if (ret < 0) {
30 LOG_ERR("%s: Not able to configure pin_int", dev->name);
31 }
32
33 #if defined(CONFIG_LIS2DS12_TRIGGER_OWN_THREAD)
34 k_sem_give(&data->trig_sem);
35 #elif defined(CONFIG_LIS2DS12_TRIGGER_GLOBAL_THREAD)
36 k_work_submit(&data->work);
37 #endif
38 }
39
lis2ds12_handle_drdy_int(const struct device * dev)40 static void lis2ds12_handle_drdy_int(const struct device *dev)
41 {
42 struct lis2ds12_data *data = dev->data;
43
44 if (data->data_ready_handler != NULL) {
45 data->data_ready_handler(dev, data->data_ready_trigger);
46 }
47 }
48
lis2ds12_handle_int(const struct device * dev)49 static void lis2ds12_handle_int(const struct device *dev)
50 {
51 const struct lis2ds12_config *cfg = dev->config;
52 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
53 lis2ds12_all_sources_t sources;
54 int ret;
55
56 lis2ds12_all_sources_get(ctx, &sources);
57
58 if (sources.status_dup.drdy) {
59 lis2ds12_handle_drdy_int(dev);
60 }
61
62 ret = gpio_pin_interrupt_configure_dt(&cfg->gpio_int,
63 GPIO_INT_EDGE_TO_ACTIVE);
64 if (ret < 0) {
65 LOG_ERR("%s: Not able to configure pin_int", dev->name);
66 }
67 }
68
69 #ifdef CONFIG_LIS2DS12_TRIGGER_OWN_THREAD
lis2ds12_thread(void * p1,void * p2,void * p3)70 static void lis2ds12_thread(void *p1, void *p2, void *p3)
71 {
72 ARG_UNUSED(p2);
73 ARG_UNUSED(p3);
74
75 struct lis2ds12_data *data = p1;
76
77 while (1) {
78 k_sem_take(&data->trig_sem, K_FOREVER);
79 lis2ds12_handle_int(data->dev);
80 }
81 }
82 #endif
83
84 #ifdef CONFIG_LIS2DS12_TRIGGER_GLOBAL_THREAD
lis2ds12_work_cb(struct k_work * work)85 static void lis2ds12_work_cb(struct k_work *work)
86 {
87 struct lis2ds12_data *data =
88 CONTAINER_OF(work, struct lis2ds12_data, work);
89
90 lis2ds12_handle_int(data->dev);
91 }
92 #endif
93
lis2ds12_init_interrupt(const struct device * dev)94 static int lis2ds12_init_interrupt(const struct device *dev)
95 {
96 const struct lis2ds12_config *cfg = dev->config;
97 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
98 lis2ds12_pin_int1_route_t route;
99 int err;
100
101 /* Enable pulsed mode */
102 err = lis2ds12_int_notification_set(ctx, LIS2DS12_INT_PULSED);
103 if (err < 0) {
104 return err;
105 }
106
107 /* route data-ready interrupt on int1 */
108 err = lis2ds12_pin_int1_route_get(ctx, &route);
109 if (err < 0) {
110 return err;
111 }
112
113 route.int1_drdy = 1;
114
115 err = lis2ds12_pin_int1_route_set(ctx, route);
116 if (err < 0) {
117 return err;
118 }
119
120 return 0;
121 }
122
lis2ds12_trigger_init(const struct device * dev)123 int lis2ds12_trigger_init(const struct device *dev)
124 {
125 struct lis2ds12_data *data = dev->data;
126 const struct lis2ds12_config *cfg = dev->config;
127 int ret;
128
129 /* setup data ready gpio interrupt (INT1 or INT2) */
130 if (!gpio_is_ready_dt(&cfg->gpio_int)) {
131 if (cfg->gpio_int.port) {
132 LOG_ERR("%s: device %s is not ready", dev->name,
133 cfg->gpio_int.port->name);
134 return -ENODEV;
135 }
136
137 LOG_DBG("%s: gpio_int not defined in DT", dev->name);
138 return 0;
139 }
140
141 data->dev = dev;
142
143 ret = gpio_pin_configure_dt(&cfg->gpio_int, GPIO_INPUT);
144 if (ret < 0) {
145 LOG_ERR("Could not configure gpio");
146 return ret;
147 }
148
149 LOG_INF("%s: int on %s.%02u", dev->name, cfg->gpio_int.port->name,
150 cfg->gpio_int.pin);
151
152 gpio_init_callback(&data->gpio_cb,
153 lis2ds12_gpio_callback,
154 BIT(cfg->gpio_int.pin));
155
156 ret = gpio_add_callback(cfg->gpio_int.port, &data->gpio_cb);
157 if (ret < 0) {
158 LOG_ERR("Could not set gpio callback");
159 return ret;
160 }
161
162 #if defined(CONFIG_LIS2DS12_TRIGGER_OWN_THREAD)
163 k_sem_init(&data->trig_sem, 0, K_SEM_MAX_LIMIT);
164
165 k_thread_create(&data->thread, data->thread_stack,
166 CONFIG_LIS2DS12_THREAD_STACK_SIZE,
167 lis2ds12_thread,
168 data, NULL, NULL,
169 K_PRIO_COOP(CONFIG_LIS2DS12_THREAD_PRIORITY),
170 0, K_NO_WAIT);
171 #elif defined(CONFIG_LIS2DS12_TRIGGER_GLOBAL_THREAD)
172 data->work.handler = lis2ds12_work_cb;
173 #endif
174
175 return gpio_pin_interrupt_configure_dt(&cfg->gpio_int,
176 GPIO_INT_EDGE_TO_ACTIVE);
177 }
178
lis2ds12_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)179 int lis2ds12_trigger_set(const struct device *dev,
180 const struct sensor_trigger *trig,
181 sensor_trigger_handler_t handler)
182 {
183 struct lis2ds12_data *data = dev->data;
184 const struct lis2ds12_config *cfg = dev->config;
185 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
186 int16_t raw[3];
187 int ret;
188
189 __ASSERT_NO_MSG(trig->type == SENSOR_TRIG_DATA_READY);
190
191 if (cfg->gpio_int.port == NULL) {
192 LOG_ERR("trigger_set is not supported");
193 return -ENOTSUP;
194 }
195
196 ret = gpio_pin_interrupt_configure_dt(&cfg->gpio_int, GPIO_INT_DISABLE);
197 if (ret < 0) {
198 LOG_ERR("%s: Not able to configure pin_int", dev->name);
199 return ret;
200 }
201
202 data->data_ready_handler = handler;
203 if (handler == NULL) {
204 LOG_WRN("lis2ds12: no handler");
205 return 0;
206 }
207
208 /* re-trigger lost interrupt */
209 lis2ds12_acceleration_raw_get(ctx, raw);
210
211 data->data_ready_trigger = trig;
212
213 lis2ds12_init_interrupt(dev);
214 return gpio_pin_interrupt_configure_dt(&cfg->gpio_int,
215 GPIO_INT_EDGE_TO_ACTIVE);
216 }
217