1 /*
2 * Copyright 2024 NXP
3 * Copyright (c) 2018 Phytec Messtechnik GmbH
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #define DT_DRV_COMPAT nxp_fxls8974
9
10 #include "fxls8974.h"
11 #include <zephyr/logging/log.h>
12
13 LOG_MODULE_DECLARE(FXLS8974, CONFIG_SENSOR_LOG_LEVEL);
14
fxls8974_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pin_mask)15 static void fxls8974_gpio_callback(const struct device *dev,
16 struct gpio_callback *cb,
17 uint32_t pin_mask)
18 {
19 struct fxls8974_data *data =
20 CONTAINER_OF(cb, struct fxls8974_data, gpio_cb);
21 const struct fxls8974_config *config = data->dev->config;
22
23 if ((pin_mask & BIT(config->int_gpio.pin)) == 0U) {
24 return;
25 }
26
27 gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_DISABLE);
28
29 #if defined(CONFIG_FXLS8974_TRIGGER_OWN_THREAD)
30 k_sem_give(&data->trig_sem);
31 #elif defined(CONFIG_FXLS8974_TRIGGER_GLOBAL_THREAD)
32 k_work_submit(&data->work);
33 #endif
34 }
35
fxls8974_handle_drdy_int(const struct device * dev)36 static int fxls8974_handle_drdy_int(const struct device *dev)
37 {
38 struct fxls8974_data *data = dev->data;
39
40 if (data->drdy_handler) {
41 data->drdy_handler(dev, data->drdy_trig);
42 }
43
44 return 0;
45 }
46
fxls8974_handle_int(const struct device * dev)47 static void fxls8974_handle_int(const struct device *dev)
48 {
49 const struct fxls8974_config *config = dev->config;
50
51 fxls8974_handle_drdy_int(dev);
52
53 gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_LEVEL_HIGH);
54 }
55
56 #ifdef CONFIG_FXLS8974_TRIGGER_OWN_THREAD
fxls8974_thread_main(void * p1,void * p2,void * p3)57 static void fxls8974_thread_main(void *p1, void *p2, void *p3)
58 {
59 ARG_UNUSED(p2);
60 ARG_UNUSED(p3);
61
62 struct fxls8974_data *data = p1;
63
64 while (true) {
65 k_sem_take(&data->trig_sem, K_FOREVER);
66 fxls8974_handle_int(data->dev);
67 }
68 }
69 #endif
70
71 #ifdef CONFIG_FXLS8974_TRIGGER_GLOBAL_THREAD
fxls8974_work_handler(struct k_work * work)72 static void fxls8974_work_handler(struct k_work *work)
73 {
74 struct fxls8974_data *data =
75 CONTAINER_OF(work, struct fxls8974_data, work);
76
77 fxls8974_handle_int(data->dev);
78 }
79 #endif
80
fxls8974_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)81 int fxls8974_trigger_set(const struct device *dev,
82 const struct sensor_trigger *trig,
83 sensor_trigger_handler_t handler)
84 {
85 struct fxls8974_data *data = dev->data;
86 int ret = 0;
87
88 k_sem_take(&data->sem, K_FOREVER);
89
90 /* Put the sensor in standby mode */
91 if (fxls8974_set_active(dev, FXLS8974_ACTIVE_OFF)) {
92 LOG_ERR("Could not set standby mode");
93 ret = -EIO;
94 goto exit;
95 }
96
97 if (trig->type == SENSOR_TRIG_DATA_READY) {
98 data->drdy_handler = handler;
99 data->drdy_trig = trig;
100 } else {
101 LOG_ERR("Unsupported sensor trigger");
102 ret = -ENOTSUP;
103 goto exit;
104 }
105
106
107 /* Restore the previous active mode */
108 if (fxls8974_set_active(dev, FXLS8974_ACTIVE_ON)) {
109 LOG_ERR("Could not restore active mode");
110 ret = -EIO;
111 goto exit;
112 }
113
114 exit:
115 k_sem_give(&data->sem);
116
117 return ret;
118 }
119
120
fxls8974_trigger_init(const struct device * dev)121 int fxls8974_trigger_init(const struct device *dev)
122 {
123 const struct fxls8974_config *config = dev->config;
124 struct fxls8974_data *data = dev->data;
125 int ret;
126
127 data->dev = dev;
128
129 #if defined(CONFIG_FXLS8974_TRIGGER_OWN_THREAD)
130 k_sem_init(&data->trig_sem, 0, K_SEM_MAX_LIMIT);
131 k_thread_create(&data->thread, data->thread_stack,
132 CONFIG_FXLS8974_THREAD_STACK_SIZE,
133 fxls8974_thread_main,
134 data, NULL, NULL,
135 K_PRIO_COOP(CONFIG_FXLS8974_THREAD_PRIORITY),
136 0, K_NO_WAIT);
137 #elif defined(CONFIG_FXLS8974_TRIGGER_GLOBAL_THREAD)
138 data->work.handler = fxls8974_work_handler;
139 #endif
140
141 if (config->ops->byte_write(dev, FXLS8974_INTREG_EN,
142 FXLS8974_DRDY_MASK)) {
143 LOG_ERR("Could not enable interrupt");
144 return -EIO;
145 }
146
147 #if !(CONFIG_FXLS8974_DRDY_INT1)
148 if (config->ops->byte_write(dev, FXLS8974_INT_PIN_SEL_REG,
149 FXLS8974_DRDY_MASK)) {
150 LOG_ERR("Could not configure interrupt pin routing");
151 return -EIO;
152 }
153 #endif
154
155 if (!gpio_is_ready_dt(&config->int_gpio)) {
156 LOG_ERR("GPIO device not ready");
157 return -ENODEV;
158 }
159
160 ret = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
161 if (ret < 0) {
162 return ret;
163 }
164
165 gpio_init_callback(&data->gpio_cb, fxls8974_gpio_callback,
166 BIT(config->int_gpio.pin));
167
168 ret = gpio_add_callback(config->int_gpio.port, &data->gpio_cb);
169 if (ret < 0) {
170 return ret;
171 }
172
173 ret = gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
174 if (ret < 0) {
175 return ret;
176 }
177
178 return 0;
179 }
180