1 /*
2 * Copyright (c) 2018 STMicroelectronics
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT st_lsm6dsl
8
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/sys/__assert.h>
12 #include <zephyr/sys/util.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/drivers/sensor.h>
15 #include <zephyr/logging/log.h>
16 #include "lsm6dsl.h"
17
18 LOG_MODULE_DECLARE(LSM6DSL, CONFIG_SENSOR_LOG_LEVEL);
19
setup_irq(const struct device * dev,bool enable)20 static inline void setup_irq(const struct device *dev, bool enable)
21 {
22 const struct lsm6dsl_config *config = dev->config;
23
24 unsigned int flags = enable
25 ? GPIO_INT_EDGE_TO_ACTIVE
26 : GPIO_INT_DISABLE;
27
28 gpio_pin_interrupt_configure_dt(&config->int_gpio, flags);
29 }
30
handle_irq(const struct device * dev)31 static inline void handle_irq(const struct device *dev)
32 {
33 struct lsm6dsl_data *drv_data = dev->data;
34
35 setup_irq(dev, false);
36
37 #if defined(CONFIG_LSM6DSL_TRIGGER_OWN_THREAD)
38 k_sem_give(&drv_data->gpio_sem);
39 #elif defined(CONFIG_LSM6DSL_TRIGGER_GLOBAL_THREAD)
40 k_work_submit(&drv_data->work);
41 #endif
42 }
43
lsm6dsl_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)44 int lsm6dsl_trigger_set(const struct device *dev,
45 const struct sensor_trigger *trig,
46 sensor_trigger_handler_t handler)
47 {
48 const struct lsm6dsl_config *config = dev->config;
49 struct lsm6dsl_data *drv_data = dev->data;
50
51 __ASSERT_NO_MSG(trig->type == SENSOR_TRIG_DATA_READY);
52
53 /* If irq_gpio is not configured in DT just return error */
54 if (!config->int_gpio.port) {
55 LOG_ERR("triggers not supported");
56 return -ENOTSUP;
57 }
58
59 setup_irq(dev, false);
60
61 drv_data->data_ready_handler = handler;
62 if (handler == NULL) {
63 return 0;
64 }
65
66 drv_data->data_ready_trigger = trig;
67
68 setup_irq(dev, true);
69 if (gpio_pin_get_dt(&config->int_gpio) > 0) {
70 handle_irq(dev);
71 }
72
73 return 0;
74 }
75
lsm6dsl_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)76 static void lsm6dsl_gpio_callback(const struct device *dev,
77 struct gpio_callback *cb, uint32_t pins)
78 {
79 struct lsm6dsl_data *drv_data =
80 CONTAINER_OF(cb, struct lsm6dsl_data, gpio_cb);
81
82 ARG_UNUSED(pins);
83
84 handle_irq(drv_data->dev);
85 }
86
lsm6dsl_thread_cb(const struct device * dev)87 static void lsm6dsl_thread_cb(const struct device *dev)
88 {
89 struct lsm6dsl_data *drv_data = dev->data;
90
91 if (drv_data->data_ready_handler != NULL) {
92 drv_data->data_ready_handler(dev,
93 drv_data->data_ready_trigger);
94 }
95
96 setup_irq(dev, true);
97 }
98
99 #ifdef CONFIG_LSM6DSL_TRIGGER_OWN_THREAD
lsm6dsl_thread(void * p1,void * p2,void * p3)100 static void lsm6dsl_thread(void *p1, void *p2, void *p3)
101 {
102 ARG_UNUSED(p2);
103 ARG_UNUSED(p3);
104
105 const struct device *dev = p1;
106 struct lsm6dsl_data *drv_data = dev->data;
107
108 while (1) {
109 k_sem_take(&drv_data->gpio_sem, K_FOREVER);
110 lsm6dsl_thread_cb(dev);
111 }
112 }
113 #endif
114
115 #ifdef CONFIG_LSM6DSL_TRIGGER_GLOBAL_THREAD
lsm6dsl_work_cb(struct k_work * work)116 static void lsm6dsl_work_cb(struct k_work *work)
117 {
118 struct lsm6dsl_data *drv_data =
119 CONTAINER_OF(work, struct lsm6dsl_data, work);
120
121 lsm6dsl_thread_cb(drv_data->dev);
122 }
123 #endif
124
lsm6dsl_init_interrupt(const struct device * dev)125 int lsm6dsl_init_interrupt(const struct device *dev)
126 {
127 const struct lsm6dsl_config *config = dev->config;
128 struct lsm6dsl_data *drv_data = dev->data;
129
130 if (!gpio_is_ready_dt(&config->int_gpio)) {
131 LOG_ERR("GPIO device not ready");
132 return -ENODEV;
133 }
134
135 gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
136
137 gpio_init_callback(&drv_data->gpio_cb,
138 lsm6dsl_gpio_callback, BIT(config->int_gpio.pin));
139
140 if (gpio_add_callback(config->int_gpio.port, &drv_data->gpio_cb) < 0) {
141 LOG_ERR("Could not set gpio callback.");
142 return -EIO;
143 }
144
145 /* enable data-ready interrupt */
146 if (drv_data->hw_tf->update_reg(dev,
147 LSM6DSL_REG_INT1_CTRL,
148 LSM6DSL_MASK_INT1_CTRL_DRDY_XL |
149 LSM6DSL_MASK_INT1_CTRL_DRDY_G,
150 BIT(LSM6DSL_SHIFT_INT1_CTRL_DRDY_XL) |
151 BIT(LSM6DSL_SHIFT_INT1_CTRL_DRDY_G)) < 0) {
152 LOG_ERR("Could not enable data-ready interrupt.");
153 return -EIO;
154 }
155
156 drv_data->dev = dev;
157
158 #if defined(CONFIG_LSM6DSL_TRIGGER_OWN_THREAD)
159 k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
160
161 k_thread_create(&drv_data->thread, drv_data->thread_stack,
162 CONFIG_LSM6DSL_THREAD_STACK_SIZE,
163 lsm6dsl_thread, (void *)dev,
164 NULL, NULL, K_PRIO_COOP(CONFIG_LSM6DSL_THREAD_PRIORITY),
165 0, K_NO_WAIT);
166 #elif defined(CONFIG_LSM6DSL_TRIGGER_GLOBAL_THREAD)
167 drv_data->work.handler = lsm6dsl_work_cb;
168 #endif
169
170 setup_irq(dev, true);
171
172 return 0;
173 }
174