1 /*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT bosch_bma280
8
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/sys/util.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/drivers/sensor.h>
14
15 #include "bma280.h"
16
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_DECLARE(BMA280, CONFIG_SENSOR_LOG_LEVEL);
19
setup_int1(const struct device * dev,bool enable)20 static inline void setup_int1(const struct device *dev,
21 bool enable)
22 {
23 const struct bma280_config *config = dev->config;
24
25 gpio_pin_interrupt_configure_dt(&config->int1_gpio,
26 (enable ? GPIO_INT_EDGE_TO_ACTIVE : GPIO_INT_DISABLE));
27 }
28
bma280_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)29 int bma280_attr_set(const struct device *dev,
30 enum sensor_channel chan,
31 enum sensor_attribute attr,
32 const struct sensor_value *val)
33 {
34 const struct bma280_config *config = dev->config;
35 uint64_t slope_th;
36
37 if (!config->int1_gpio.port) {
38 return -ENOTSUP;
39 }
40
41 if (chan != SENSOR_CHAN_ACCEL_XYZ) {
42 return -ENOTSUP;
43 }
44
45 if (attr == SENSOR_ATTR_SLOPE_TH) {
46 /* slope_th = (val * 10^6 * 2^10) / BMA280_PMU_FULL_RAGE */
47 slope_th = (uint64_t)val->val1 * 1000000U + (uint64_t)val->val2;
48 slope_th = (slope_th * (1 << 10)) / BMA280_PMU_FULL_RANGE;
49 if (i2c_reg_write_byte_dt(&config->i2c,
50 BMA280_REG_SLOPE_TH, (uint8_t)slope_th)
51 < 0) {
52 LOG_DBG("Could not set slope threshold");
53 return -EIO;
54 }
55 } else if (attr == SENSOR_ATTR_SLOPE_DUR) {
56 if (i2c_reg_update_byte_dt(&config->i2c,
57 BMA280_REG_INT_5,
58 BMA280_SLOPE_DUR_MASK,
59 val->val1 << BMA280_SLOPE_DUR_SHIFT)
60 < 0) {
61 LOG_DBG("Could not set slope duration");
62 return -EIO;
63 }
64 } else {
65 return -ENOTSUP;
66 }
67
68 return 0;
69 }
70
bma280_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)71 static void bma280_gpio_callback(const struct device *dev,
72 struct gpio_callback *cb, uint32_t pins)
73 {
74 struct bma280_data *drv_data =
75 CONTAINER_OF(cb, struct bma280_data, gpio_cb);
76
77 ARG_UNUSED(pins);
78
79 setup_int1(drv_data->dev, false);
80
81 #if defined(CONFIG_BMA280_TRIGGER_OWN_THREAD)
82 k_sem_give(&drv_data->gpio_sem);
83 #elif defined(CONFIG_BMA280_TRIGGER_GLOBAL_THREAD)
84 k_work_submit(&drv_data->work);
85 #endif
86 }
87
bma280_thread_cb(const struct device * dev)88 static void bma280_thread_cb(const struct device *dev)
89 {
90 struct bma280_data *drv_data = dev->data;
91 const struct bma280_config *config = dev->config;
92 uint8_t status = 0U;
93 int err = 0;
94
95 /* check for data ready */
96 err = i2c_reg_read_byte_dt(&config->i2c,
97 BMA280_REG_INT_STATUS_1, &status);
98 if (status & BMA280_BIT_DATA_INT_STATUS &&
99 drv_data->data_ready_handler != NULL &&
100 err == 0) {
101 drv_data->data_ready_handler(dev,
102 drv_data->data_ready_trigger);
103 }
104
105 /* check for any motion */
106 err = i2c_reg_read_byte_dt(&config->i2c,
107 BMA280_REG_INT_STATUS_0, &status);
108 if (status & BMA280_BIT_SLOPE_INT_STATUS &&
109 drv_data->any_motion_handler != NULL &&
110 err == 0) {
111 drv_data->any_motion_handler(dev,
112 drv_data->any_motion_trigger);
113
114 /* clear latched interrupt */
115 err = i2c_reg_update_byte_dt(&config->i2c,
116 BMA280_REG_INT_RST_LATCH,
117 BMA280_BIT_INT_LATCH_RESET,
118 BMA280_BIT_INT_LATCH_RESET);
119
120 if (err < 0) {
121 LOG_DBG("Could not update clear the interrupt");
122 return;
123 }
124 }
125
126 setup_int1(dev, true);
127 }
128
129 #ifdef CONFIG_BMA280_TRIGGER_OWN_THREAD
bma280_thread(void * p1,void * p2,void * p3)130 static void bma280_thread(void *p1, void *p2, void *p3)
131 {
132 ARG_UNUSED(p2);
133 ARG_UNUSED(p3);
134
135 struct bma280_data *drv_data = p1;
136
137 while (1) {
138 k_sem_take(&drv_data->gpio_sem, K_FOREVER);
139 bma280_thread_cb(drv_data->dev);
140 }
141 }
142 #endif
143
144 #ifdef CONFIG_BMA280_TRIGGER_GLOBAL_THREAD
bma280_work_cb(struct k_work * work)145 static void bma280_work_cb(struct k_work *work)
146 {
147 struct bma280_data *drv_data =
148 CONTAINER_OF(work, struct bma280_data, work);
149
150 bma280_thread_cb(drv_data->dev);
151 }
152 #endif
153
bma280_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)154 int bma280_trigger_set(const struct device *dev,
155 const struct sensor_trigger *trig,
156 sensor_trigger_handler_t handler)
157 {
158 struct bma280_data *drv_data = dev->data;
159 const struct bma280_config *config = dev->config;
160
161 if (!config->int1_gpio.port) {
162 return -ENOTSUP;
163 }
164
165 if (trig->type == SENSOR_TRIG_DATA_READY) {
166 /* disable data ready interrupt while changing trigger params */
167 if (i2c_reg_update_byte_dt(&config->i2c,
168 BMA280_REG_INT_EN_1,
169 BMA280_BIT_DATA_EN, 0) < 0) {
170 LOG_DBG("Could not disable data ready interrupt");
171 return -EIO;
172 }
173
174 drv_data->data_ready_handler = handler;
175 if (handler == NULL) {
176 return 0;
177 }
178 drv_data->data_ready_trigger = trig;
179
180 /* enable data ready interrupt */
181 if (i2c_reg_update_byte_dt(&config->i2c,
182 BMA280_REG_INT_EN_1,
183 BMA280_BIT_DATA_EN,
184 BMA280_BIT_DATA_EN) < 0) {
185 LOG_DBG("Could not enable data ready interrupt");
186 return -EIO;
187 }
188 } else if (trig->type == SENSOR_TRIG_DELTA) {
189 /* disable any-motion interrupt while changing trigger params */
190 if (i2c_reg_update_byte_dt(&config->i2c,
191 BMA280_REG_INT_EN_0,
192 BMA280_SLOPE_EN_XYZ, 0) < 0) {
193 LOG_DBG("Could not disable data ready interrupt");
194 return -EIO;
195 }
196
197 drv_data->any_motion_handler = handler;
198 if (handler == NULL) {
199 return 0;
200 }
201 drv_data->any_motion_trigger = trig;
202
203 /* enable any-motion interrupt */
204 if (i2c_reg_update_byte_dt(&config->i2c,
205 BMA280_REG_INT_EN_0,
206 BMA280_SLOPE_EN_XYZ,
207 BMA280_SLOPE_EN_XYZ) < 0) {
208 LOG_DBG("Could not enable data ready interrupt");
209 return -EIO;
210 }
211 } else {
212 return -ENOTSUP;
213 }
214
215 return 0;
216 }
217
bma280_init_interrupt(const struct device * dev)218 int bma280_init_interrupt(const struct device *dev)
219 {
220 struct bma280_data *drv_data = dev->data;
221 const struct bma280_config *config = dev->config;
222
223 /* set latched interrupts */
224 if (i2c_reg_write_byte_dt(&config->i2c,
225 BMA280_REG_INT_RST_LATCH,
226 BMA280_BIT_INT_LATCH_RESET |
227 BMA280_INT_MODE_LATCH) < 0) {
228 LOG_DBG("Could not set latched interrupts");
229 return -EIO;
230 }
231
232 /* setup data ready gpio interrupt */
233 if (!gpio_is_ready_dt(&config->int1_gpio)) {
234 LOG_ERR("GPIO device not ready");
235 return -ENODEV;
236 }
237
238 gpio_pin_configure_dt(&config->int1_gpio, GPIO_INPUT);
239
240 gpio_init_callback(&drv_data->gpio_cb,
241 bma280_gpio_callback,
242 BIT(config->int1_gpio.pin));
243
244 if (gpio_add_callback(config->int1_gpio.port, &drv_data->gpio_cb) < 0) {
245 LOG_DBG("Could not set gpio callback");
246 return -EIO;
247 }
248
249 /* map data ready interrupt to INT1 */
250 if (i2c_reg_update_byte_dt(&config->i2c,
251 BMA280_REG_INT_MAP_1,
252 BMA280_INT_MAP_1_BIT_DATA,
253 BMA280_INT_MAP_1_BIT_DATA) < 0) {
254 LOG_DBG("Could not map data ready interrupt pin");
255 return -EIO;
256 }
257
258 /* map any-motion interrupt to INT1 */
259 if (i2c_reg_update_byte_dt(&config->i2c,
260 BMA280_REG_INT_MAP_0,
261 BMA280_INT_MAP_0_BIT_SLOPE,
262 BMA280_INT_MAP_0_BIT_SLOPE) < 0) {
263 LOG_DBG("Could not map any-motion interrupt pin");
264 return -EIO;
265 }
266
267 if (i2c_reg_update_byte_dt(&config->i2c,
268 BMA280_REG_INT_EN_1,
269 BMA280_BIT_DATA_EN, 0) < 0) {
270 LOG_DBG("Could not disable data ready interrupt");
271 return -EIO;
272 }
273
274 /* disable any-motion interrupt */
275 if (i2c_reg_update_byte_dt(&config->i2c,
276 BMA280_REG_INT_EN_0,
277 BMA280_SLOPE_EN_XYZ, 0) < 0) {
278 LOG_DBG("Could not disable data ready interrupt");
279 return -EIO;
280 }
281
282 drv_data->dev = dev;
283
284 #if defined(CONFIG_BMA280_TRIGGER_OWN_THREAD)
285 k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
286
287 k_thread_create(&drv_data->thread, drv_data->thread_stack,
288 CONFIG_BMA280_THREAD_STACK_SIZE,
289 bma280_thread, drv_data,
290 NULL, NULL, K_PRIO_COOP(CONFIG_BMA280_THREAD_PRIORITY),
291 0, K_NO_WAIT);
292 #elif defined(CONFIG_BMA280_TRIGGER_GLOBAL_THREAD)
293 drv_data->work.handler = bma280_work_cb;
294 #endif
295
296 setup_int1(dev, true);
297
298 return 0;
299 }
300