1 /* ST Microelectronics LIS2DE12 3-axis accelerometer sensor driver
2  *
3  * Copyright (c) 2024 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Datasheet:
8  * https://www.st.com/resource/en/datasheet/lis2de12.pdf
9  */
10 
11 #define DT_DRV_COMPAT st_lis2de12
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 "lis2de12.h"
19 
20 LOG_MODULE_DECLARE(LIS2DE12, CONFIG_SENSOR_LOG_LEVEL);
21 
22 /**
23  * lis2de12_enable_xl_int - XL enable selected int pin to generate interrupt
24  */
lis2de12_enable_xl_int(const struct device * dev,int enable)25 static int lis2de12_enable_xl_int(const struct device *dev, int enable)
26 {
27 	const struct lis2de12_config *cfg = dev->config;
28 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
29 	lis2de12_ctrl_reg3_t val = {0};
30 	int ret;
31 
32 	if (enable) {
33 		int16_t xl_data[3];
34 
35 		/* dummy read: re-trigger interrupt */
36 		lis2de12_acceleration_raw_get(ctx, xl_data);
37 	}
38 
39 	/* set interrupt */
40 	ret = lis2de12_pin_int1_config_get(ctx, &val);
41 	if (ret < 0) {
42 		LOG_ERR("pint_int1_route_get error");
43 		return ret;
44 	}
45 
46 	val.i1_zyxda = 1;
47 
48 	return lis2de12_pin_int1_config_set(ctx, &val);
49 }
50 
51 /**
52  * lis2de12_trigger_set - link external trigger to event data ready
53  */
lis2de12_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)54 int lis2de12_trigger_set(const struct device *dev,
55 			 const struct sensor_trigger *trig,
56 			 sensor_trigger_handler_t handler)
57 {
58 	const struct lis2de12_config *cfg = dev->config;
59 	struct lis2de12_data *lis2de12 = dev->data;
60 
61 	if (!cfg->trig_enabled) {
62 		LOG_ERR("trigger_set op not supported");
63 		return -ENOTSUP;
64 	}
65 
66 	switch (trig->chan) {
67 	case SENSOR_CHAN_ACCEL_XYZ:
68 		lis2de12->handler_drdy_acc = handler;
69 		lis2de12->trig_drdy_acc = trig;
70 		if (handler) {
71 			return lis2de12_enable_xl_int(dev, LIS2DE12_EN_BIT);
72 		}
73 
74 		return lis2de12_enable_xl_int(dev, LIS2DE12_DIS_BIT);
75 
76 	default:
77 		return -ENOTSUP;
78 	}
79 
80 }
81 
82 /**
83  * lis2de12_handle_interrupt - handle the drdy event
84  * read data and call handler if registered any
85  */
lis2de12_handle_interrupt(const struct device * dev)86 static void lis2de12_handle_interrupt(const struct device *dev)
87 {
88 	struct lis2de12_data *lis2de12 = dev->data;
89 	const struct lis2de12_config *cfg = dev->config;
90 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
91 	lis2de12_status_reg_t status;
92 
93 	while (1) {
94 		if (lis2de12_status_get(ctx, &status) < 0) {
95 			LOG_ERR("failed reading status reg");
96 			return;
97 		}
98 
99 		if (status.zyxda == 0) {
100 			/* spurious interrupt */
101 			break;
102 		}
103 
104 		if ((status.zyxda) && (lis2de12->handler_drdy_acc != NULL)) {
105 			lis2de12->handler_drdy_acc(dev, lis2de12->trig_drdy_acc);
106 		}
107 	}
108 
109 	gpio_pin_interrupt_configure_dt(lis2de12->drdy_gpio,
110 					GPIO_INT_EDGE_TO_ACTIVE);
111 }
112 
lis2de12_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)113 static void lis2de12_gpio_callback(const struct device *dev,
114 				   struct gpio_callback *cb, uint32_t pins)
115 {
116 	struct lis2de12_data *lis2de12 =
117 		CONTAINER_OF(cb, struct lis2de12_data, gpio_cb);
118 
119 	ARG_UNUSED(pins);
120 
121 	gpio_pin_interrupt_configure_dt(lis2de12->drdy_gpio, GPIO_INT_DISABLE);
122 
123 #if defined(CONFIG_LIS2DE12_TRIGGER_OWN_THREAD)
124 	k_sem_give(&lis2de12->gpio_sem);
125 #elif defined(CONFIG_LIS2DE12_TRIGGER_GLOBAL_THREAD)
126 	k_work_submit(&lis2de12->work);
127 #endif /* CONFIG_LIS2DE12_TRIGGER_OWN_THREAD */
128 }
129 
130 #ifdef CONFIG_LIS2DE12_TRIGGER_OWN_THREAD
lis2de12_thread(struct lis2de12_data * lis2de12)131 static void lis2de12_thread(struct lis2de12_data *lis2de12)
132 {
133 	while (1) {
134 		k_sem_take(&lis2de12->gpio_sem, K_FOREVER);
135 		lis2de12_handle_interrupt(lis2de12->dev);
136 	}
137 }
138 #endif /* CONFIG_LIS2DE12_TRIGGER_OWN_THREAD */
139 
140 #ifdef CONFIG_LIS2DE12_TRIGGER_GLOBAL_THREAD
lis2de12_work_cb(struct k_work * work)141 static void lis2de12_work_cb(struct k_work *work)
142 {
143 	struct lis2de12_data *lis2de12 =
144 		CONTAINER_OF(work, struct lis2de12_data, work);
145 
146 	lis2de12_handle_interrupt(lis2de12->dev);
147 }
148 #endif /* CONFIG_LIS2DE12_TRIGGER_GLOBAL_THREAD */
149 
lis2de12_init_interrupt(const struct device * dev)150 int lis2de12_init_interrupt(const struct device *dev)
151 {
152 	struct lis2de12_data *lis2de12 = dev->data;
153 	const struct lis2de12_config *cfg = dev->config;
154 	int ret;
155 
156 	lis2de12->drdy_gpio = (struct gpio_dt_spec *)&cfg->int1_gpio;
157 
158 	/* setup data ready gpio interrupt */
159 	if (!gpio_is_ready_dt(lis2de12->drdy_gpio)) {
160 		LOG_ERR("Cannot get pointer to drdy_gpio device (%p)",
161 			lis2de12->drdy_gpio);
162 		return -EINVAL;
163 	}
164 
165 #if defined(CONFIG_LIS2DE12_TRIGGER_OWN_THREAD)
166 	k_sem_init(&lis2de12->gpio_sem, 0, K_SEM_MAX_LIMIT);
167 
168 	k_thread_create(&lis2de12->thread, lis2de12->thread_stack,
169 			CONFIG_LIS2DE12_THREAD_STACK_SIZE,
170 			(k_thread_entry_t)lis2de12_thread, lis2de12,
171 			NULL, NULL, K_PRIO_COOP(CONFIG_LIS2DE12_THREAD_PRIORITY),
172 			0, K_NO_WAIT);
173 	k_thread_name_set(&lis2de12->thread, dev->name);
174 #elif defined(CONFIG_LIS2DE12_TRIGGER_GLOBAL_THREAD)
175 	lis2de12->work.handler = lis2de12_work_cb;
176 #endif /* CONFIG_LIS2DE12_TRIGGER_OWN_THREAD */
177 
178 	ret = gpio_pin_configure_dt(lis2de12->drdy_gpio, GPIO_INPUT);
179 	if (ret < 0) {
180 		LOG_ERR("Could not configure gpio: %d", ret);
181 		return ret;
182 	}
183 
184 	gpio_init_callback(&lis2de12->gpio_cb,
185 			   lis2de12_gpio_callback,
186 			   BIT(lis2de12->drdy_gpio->pin));
187 
188 	if (gpio_add_callback(lis2de12->drdy_gpio->port, &lis2de12->gpio_cb) < 0) {
189 		LOG_ERR("Could not set gpio callback");
190 		return -EIO;
191 	}
192 
193 	return gpio_pin_interrupt_configure_dt(lis2de12->drdy_gpio,
194 					       GPIO_INT_EDGE_TO_ACTIVE);
195 }
196