1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/drivers/sensor.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/device.h>
10 #include <zephyr/init.h>
11 #include <zephyr/drivers/i2c.h>
12 #include <zephyr/sys/byteorder.h>
13 #include <zephyr/sys/__assert.h>
14 #include <zephyr/logging/log.h>
15 #include <zephyr/drivers/gpio.h>
16 
17 #include "lsm9ds0_gyro.h"
18 
19 extern struct lsm9ds0_gyro_data lsm9ds0_gyro_data;
20 
21 LOG_MODULE_DECLARE(LSM9DS0_GYRO, CONFIG_SENSOR_LOG_LEVEL);
22 
setup_drdy(const struct device * dev,bool enable)23 static inline void setup_drdy(const struct device *dev,
24 			      bool enable)
25 {
26 	const struct lsm9ds0_gyro_config *cfg = dev->config;
27 
28 	gpio_pin_interrupt_configure_dt(&cfg->int_gpio,
29 					enable ? GPIO_INT_EDGE_TO_ACTIVE
30 					       : GPIO_INT_DISABLE);
31 }
32 
lsm9ds0_gyro_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)33 int lsm9ds0_gyro_trigger_set(const struct device *dev,
34 			     const struct sensor_trigger *trig,
35 			     sensor_trigger_handler_t handler)
36 {
37 	struct lsm9ds0_gyro_data *data = dev->data;
38 	const struct lsm9ds0_gyro_config * const config =
39 					 dev->config;
40 	uint8_t state;
41 
42 	if (!config->int_gpio.port) {
43 		return -ENOTSUP;
44 	}
45 
46 	if (trig->type == SENSOR_TRIG_DATA_READY) {
47 		setup_drdy(dev, false);
48 
49 		state = 0U;
50 		if (handler) {
51 			state = 1U;
52 		}
53 
54 		data->handler_drdy = handler;
55 		data->trigger_drdy = trig;
56 
57 		if (i2c_reg_update_byte_dt(&config->i2c, LSM9DS0_GYRO_REG_CTRL_REG3_G,
58 					   LSM9DS0_GYRO_MASK_CTRL_REG3_G_I2_DRDY,
59 					   state << LSM9DS0_GYRO_SHIFT_CTRL_REG3_G_I2_DRDY)
60 					   < 0) {
61 			LOG_DBG("failed to set DRDY interrupt");
62 			return -EIO;
63 		}
64 
65 		setup_drdy(dev, true);
66 		return 0;
67 	}
68 
69 	return -ENOTSUP;
70 }
71 
lsm9ds0_gyro_gpio_drdy_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)72 static void lsm9ds0_gyro_gpio_drdy_callback(const struct device *dev,
73 					    struct gpio_callback *cb, uint32_t pins)
74 {
75 	struct lsm9ds0_gyro_data *data =
76 		CONTAINER_OF(cb, struct lsm9ds0_gyro_data, gpio_cb);
77 
78 	setup_drdy(data->dev, false);
79 
80 	k_sem_give(&data->sem);
81 }
82 
lsm9ds0_gyro_thread_main(void * p1,void * p2,void * p3)83 static void lsm9ds0_gyro_thread_main(void *p1, void *p2, void *p3)
84 {
85 	ARG_UNUSED(p2);
86 	ARG_UNUSED(p3);
87 
88 	const struct device *dev = p1;
89 	struct lsm9ds0_gyro_data *data = dev->data;
90 
91 	while (1) {
92 		k_sem_take(&data->sem, K_FOREVER);
93 
94 		if (data->handler_drdy) {
95 			data->handler_drdy(dev, data->trigger_drdy);
96 		}
97 
98 		setup_drdy(dev, true);
99 	}
100 }
101 
lsm9ds0_gyro_init_interrupt(const struct device * dev)102 int lsm9ds0_gyro_init_interrupt(const struct device *dev)
103 {
104 	const struct lsm9ds0_gyro_config * const config =
105 					   dev->config;
106 	struct lsm9ds0_gyro_data *data = dev->data;
107 
108 	data->dev = dev;
109 	k_sem_init(&data->sem, 0, K_SEM_MAX_LIMIT);
110 
111 	k_thread_create(&data->thread, data->thread_stack,
112 			CONFIG_LSM9DS0_GYRO_THREAD_STACK_SIZE,
113 			lsm9ds0_gyro_thread_main,
114 			(void *)dev, NULL, NULL, K_PRIO_COOP(10), 0, K_NO_WAIT);
115 
116 	if (!gpio_is_ready_dt(&config->int_gpio)) {
117 		LOG_ERR("GPIO device not ready");
118 		return -ENODEV;
119 	}
120 
121 	gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
122 
123 	gpio_init_callback(&data->gpio_cb,
124 			   lsm9ds0_gyro_gpio_drdy_callback,
125 			   BIT(config->int_gpio.pin));
126 
127 	if (gpio_add_callback(config->int_gpio.port, &data->gpio_cb) < 0) {
128 		LOG_DBG("failed to set gpio callback");
129 		return -EINVAL;
130 	}
131 
132 	return 0;
133 }
134