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(struct lis2ds12_data * data)70 static void lis2ds12_thread(struct lis2ds12_data *data)
71 {
72 while (1) {
73 k_sem_take(&data->trig_sem, K_FOREVER);
74 lis2ds12_handle_int(data->dev);
75 }
76 }
77 #endif
78
79 #ifdef CONFIG_LIS2DS12_TRIGGER_GLOBAL_THREAD
lis2ds12_work_cb(struct k_work * work)80 static void lis2ds12_work_cb(struct k_work *work)
81 {
82 struct lis2ds12_data *data =
83 CONTAINER_OF(work, struct lis2ds12_data, work);
84
85 lis2ds12_handle_int(data->dev);
86 }
87 #endif
88
lis2ds12_init_interrupt(const struct device * dev)89 static int lis2ds12_init_interrupt(const struct device *dev)
90 {
91 const struct lis2ds12_config *cfg = dev->config;
92 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
93 lis2ds12_pin_int1_route_t route;
94 int err;
95
96 /* Enable pulsed mode */
97 err = lis2ds12_int_notification_set(ctx, LIS2DS12_INT_PULSED);
98 if (err < 0) {
99 return err;
100 }
101
102 /* route data-ready interrupt on int1 */
103 err = lis2ds12_pin_int1_route_get(ctx, &route);
104 if (err < 0) {
105 return err;
106 }
107
108 route.int1_drdy = 1;
109
110 err = lis2ds12_pin_int1_route_set(ctx, route);
111 if (err < 0) {
112 return err;
113 }
114
115 return 0;
116 }
117
lis2ds12_trigger_init(const struct device * dev)118 int lis2ds12_trigger_init(const struct device *dev)
119 {
120 struct lis2ds12_data *data = dev->data;
121 const struct lis2ds12_config *cfg = dev->config;
122 int ret;
123
124 /* setup data ready gpio interrupt (INT1 or INT2) */
125 if (!gpio_is_ready_dt(&cfg->gpio_int)) {
126 if (cfg->gpio_int.port) {
127 LOG_ERR("%s: device %s is not ready", dev->name,
128 cfg->gpio_int.port->name);
129 return -ENODEV;
130 }
131
132 LOG_DBG("%s: gpio_int not defined in DT", dev->name);
133 return 0;
134 }
135
136 data->dev = dev;
137
138 ret = gpio_pin_configure_dt(&cfg->gpio_int, GPIO_INPUT);
139 if (ret < 0) {
140 LOG_ERR("Could not configure gpio");
141 return ret;
142 }
143
144 LOG_INF("%s: int on %s.%02u", dev->name, cfg->gpio_int.port->name,
145 cfg->gpio_int.pin);
146
147 gpio_init_callback(&data->gpio_cb,
148 lis2ds12_gpio_callback,
149 BIT(cfg->gpio_int.pin));
150
151 ret = gpio_add_callback(cfg->gpio_int.port, &data->gpio_cb);
152 if (ret < 0) {
153 LOG_ERR("Could not set gpio callback");
154 return ret;
155 }
156
157 #if defined(CONFIG_LIS2DS12_TRIGGER_OWN_THREAD)
158 k_sem_init(&data->trig_sem, 0, K_SEM_MAX_LIMIT);
159
160 k_thread_create(&data->thread, data->thread_stack,
161 CONFIG_LIS2DS12_THREAD_STACK_SIZE,
162 (k_thread_entry_t)lis2ds12_thread,
163 data, NULL, NULL,
164 K_PRIO_COOP(CONFIG_LIS2DS12_THREAD_PRIORITY),
165 0, K_NO_WAIT);
166 #elif defined(CONFIG_LIS2DS12_TRIGGER_GLOBAL_THREAD)
167 data->work.handler = lis2ds12_work_cb;
168 #endif
169
170 return gpio_pin_interrupt_configure_dt(&cfg->gpio_int,
171 GPIO_INT_EDGE_TO_ACTIVE);
172 }
173
lis2ds12_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)174 int lis2ds12_trigger_set(const struct device *dev,
175 const struct sensor_trigger *trig,
176 sensor_trigger_handler_t handler)
177 {
178 struct lis2ds12_data *data = dev->data;
179 const struct lis2ds12_config *cfg = dev->config;
180 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
181 int16_t raw[3];
182 int ret;
183
184 __ASSERT_NO_MSG(trig->type == SENSOR_TRIG_DATA_READY);
185
186 if (cfg->gpio_int.port == NULL) {
187 LOG_ERR("trigger_set is not supported");
188 return -ENOTSUP;
189 }
190
191 ret = gpio_pin_interrupt_configure_dt(&cfg->gpio_int, GPIO_INT_DISABLE);
192 if (ret < 0) {
193 LOG_ERR("%s: Not able to configure pin_int", dev->name);
194 return ret;
195 }
196
197 data->data_ready_handler = handler;
198 if (handler == NULL) {
199 LOG_WRN("lis2ds12: no handler");
200 return 0;
201 }
202
203 /* re-trigger lost interrupt */
204 lis2ds12_acceleration_raw_get(ctx, raw);
205
206 data->data_ready_trigger = trig;
207
208 lis2ds12_init_interrupt(dev);
209 return gpio_pin_interrupt_configure_dt(&cfg->gpio_int,
210 GPIO_INT_EDGE_TO_ACTIVE);
211 }
212