1 /* ST Microelectronics LIS2DU12 3-axis accelerometer sensor driver
2 *
3 * Copyright (c) 2023 STMicroelectronics
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Datasheet:
8 * https://www.st.com/resource/en/datasheet/lis2du12.pdf
9 */
10
11 #define DT_DRV_COMPAT st_lis2du12
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 "lis2du12.h"
19
20 LOG_MODULE_DECLARE(LIS2DU12, CONFIG_SENSOR_LOG_LEVEL);
21
22 /**
23 * lis2du12_enable_xl_int - XL enable selected int pin to generate interrupt
24 */
lis2du12_enable_xl_int(const struct device * dev,int enable)25 static int lis2du12_enable_xl_int(const struct device *dev, int enable)
26 {
27 const struct lis2du12_config *cfg = dev->config;
28 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
29 int ret;
30
31 if (enable) {
32 lis2du12_md_t md;
33 lis2du12_data_t xl_data;
34
35 /* dummy read: re-trigger interrupt */
36 md.fs = cfg->accel_range;
37 lis2du12_data_get(ctx, &md, &xl_data);
38 }
39
40 /* set interrupt */
41 if (cfg->drdy_pin == 1) {
42 lis2du12_pin_int_route_t val;
43
44 ret = lis2du12_pin_int1_route_get(ctx, &val);
45 if (ret < 0) {
46 LOG_ERR("pint_int1_route_get error");
47 return ret;
48 }
49
50 val.drdy_xl = 1;
51
52 ret = lis2du12_pin_int1_route_set(ctx, &val);
53 } else {
54 lis2du12_pin_int_route_t val;
55
56 ret = lis2du12_pin_int2_route_get(ctx, &val);
57 if (ret < 0) {
58 LOG_ERR("pint_int2_route_get error");
59 return ret;
60 }
61
62 val.drdy_xl = 1;
63
64 ret = lis2du12_pin_int2_route_set(ctx, &val);
65 }
66
67 return ret;
68 }
69
70 /**
71 * lis2du12_trigger_set - link external trigger to event data ready
72 */
lis2du12_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)73 int lis2du12_trigger_set(const struct device *dev,
74 const struct sensor_trigger *trig,
75 sensor_trigger_handler_t handler)
76 {
77 const struct lis2du12_config *cfg = dev->config;
78 struct lis2du12_data *lis2du12 = dev->data;
79
80 if (!cfg->trig_enabled) {
81 LOG_ERR("trigger_set op not supported");
82 return -ENOTSUP;
83 }
84
85 switch (trig->chan) {
86 case SENSOR_CHAN_ACCEL_XYZ:
87 lis2du12->handler_drdy_acc = handler;
88 lis2du12->trig_drdy_acc = trig;
89 if (handler) {
90 return lis2du12_enable_xl_int(dev, LIS2DU12_EN_BIT);
91 }
92
93 return lis2du12_enable_xl_int(dev, LIS2DU12_DIS_BIT);
94
95 default:
96 return -ENOTSUP;
97 }
98
99 }
100
101 /**
102 * lis2du12_handle_interrupt - handle the drdy event
103 * read data and call handler if registered any
104 */
lis2du12_handle_interrupt(const struct device * dev)105 static void lis2du12_handle_interrupt(const struct device *dev)
106 {
107 struct lis2du12_data *lis2du12 = dev->data;
108 const struct lis2du12_config *cfg = dev->config;
109 stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
110 lis2du12_status_t status;
111
112 while (1) {
113 if (lis2du12_status_get(ctx, &status) < 0) {
114 LOG_ERR("failed reading status reg");
115 return;
116 }
117
118 if (status.drdy_xl == 0) {
119 break;
120 }
121
122 if ((status.drdy_xl) && (lis2du12->handler_drdy_acc != NULL)) {
123 lis2du12->handler_drdy_acc(dev, lis2du12->trig_drdy_acc);
124 }
125 }
126
127 gpio_pin_interrupt_configure_dt(lis2du12->drdy_gpio,
128 GPIO_INT_EDGE_TO_ACTIVE);
129 }
130
lis2du12_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)131 static void lis2du12_gpio_callback(const struct device *dev,
132 struct gpio_callback *cb, uint32_t pins)
133 {
134 struct lis2du12_data *lis2du12 =
135 CONTAINER_OF(cb, struct lis2du12_data, gpio_cb);
136
137 ARG_UNUSED(pins);
138
139 gpio_pin_interrupt_configure_dt(lis2du12->drdy_gpio, GPIO_INT_DISABLE);
140
141 #if defined(CONFIG_LIS2DU12_TRIGGER_OWN_THREAD)
142 k_sem_give(&lis2du12->gpio_sem);
143 #elif defined(CONFIG_LIS2DU12_TRIGGER_GLOBAL_THREAD)
144 k_work_submit(&lis2du12->work);
145 #endif /* CONFIG_LIS2DU12_TRIGGER_OWN_THREAD */
146 }
147
148 #ifdef CONFIG_LIS2DU12_TRIGGER_OWN_THREAD
lis2du12_thread(struct lis2du12_data * lis2du12)149 static void lis2du12_thread(struct lis2du12_data *lis2du12)
150 {
151 while (1) {
152 k_sem_take(&lis2du12->gpio_sem, K_FOREVER);
153 lis2du12_handle_interrupt(lis2du12->dev);
154 }
155 }
156 #endif /* CONFIG_LIS2DU12_TRIGGER_OWN_THREAD */
157
158 #ifdef CONFIG_LIS2DU12_TRIGGER_GLOBAL_THREAD
lis2du12_work_cb(struct k_work * work)159 static void lis2du12_work_cb(struct k_work *work)
160 {
161 struct lis2du12_data *lis2du12 =
162 CONTAINER_OF(work, struct lis2du12_data, work);
163
164 lis2du12_handle_interrupt(lis2du12->dev);
165 }
166 #endif /* CONFIG_LIS2DU12_TRIGGER_GLOBAL_THREAD */
167
lis2du12_init_interrupt(const struct device * dev)168 int lis2du12_init_interrupt(const struct device *dev)
169 {
170 struct lis2du12_data *lis2du12 = dev->data;
171 const struct lis2du12_config *cfg = dev->config;
172 int ret;
173
174 lis2du12->drdy_gpio = (cfg->drdy_pin == 1) ?
175 (struct gpio_dt_spec *)&cfg->int1_gpio :
176 (struct gpio_dt_spec *)&cfg->int2_gpio;
177
178 /* setup data ready gpio interrupt (INT1 or INT2) */
179 if (!gpio_is_ready_dt(lis2du12->drdy_gpio)) {
180 LOG_ERR("Cannot get pointer to drdy_gpio device (%p)",
181 lis2du12->drdy_gpio);
182 return -EINVAL;
183 }
184
185 #if defined(CONFIG_LIS2DU12_TRIGGER_OWN_THREAD)
186 k_sem_init(&lis2du12->gpio_sem, 0, K_SEM_MAX_LIMIT);
187
188 k_thread_create(&lis2du12->thread, lis2du12->thread_stack,
189 CONFIG_LIS2DU12_THREAD_STACK_SIZE,
190 (k_thread_entry_t)lis2du12_thread, lis2du12,
191 NULL, NULL, K_PRIO_COOP(CONFIG_LIS2DU12_THREAD_PRIORITY),
192 0, K_NO_WAIT);
193 k_thread_name_set(&lis2du12->thread, dev->name);
194 #elif defined(CONFIG_LIS2DU12_TRIGGER_GLOBAL_THREAD)
195 lis2du12->work.handler = lis2du12_work_cb;
196 #endif /* CONFIG_LIS2DU12_TRIGGER_OWN_THREAD */
197
198 ret = gpio_pin_configure_dt(lis2du12->drdy_gpio, GPIO_INPUT);
199 if (ret < 0) {
200 LOG_ERR("Could not configure gpio: %d", ret);
201 return ret;
202 }
203
204 gpio_init_callback(&lis2du12->gpio_cb,
205 lis2du12_gpio_callback,
206 BIT(lis2du12->drdy_gpio->pin));
207
208 if (gpio_add_callback(lis2du12->drdy_gpio->port, &lis2du12->gpio_cb) < 0) {
209 LOG_ERR("Could not set gpio callback");
210 return -EIO;
211 }
212
213 return gpio_pin_interrupt_configure_dt(lis2du12->drdy_gpio,
214 GPIO_INT_EDGE_TO_ACTIVE);
215 }
216