1 /* ST Microelectronics IIS2DH 3-axis accelerometer driver
2 *
3 * Copyright (c) 2020 STMicroelectronics
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Datasheet:
8 * https://www.st.com/resource/en/datasheet/iis2dh.pdf
9 */
10
11 #define DT_DRV_COMPAT st_iis2dh
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 "iis2dh.h"
19
20 LOG_MODULE_DECLARE(IIS2DH, CONFIG_SENSOR_LOG_LEVEL);
21
22 /**
23 * iis2dh_enable_int - enable selected int pin to generate interrupt
24 */
iis2dh_enable_drdy(const struct device * dev,enum sensor_trigger_type type,int enable)25 static int iis2dh_enable_drdy(const struct device *dev,
26 enum sensor_trigger_type type, int enable)
27 {
28 struct iis2dh_data *iis2dh = dev->data;
29 iis2dh_ctrl_reg3_t reg3;
30
31 /* set interrupt for pin INT1 */
32 iis2dh_pin_int1_config_get(iis2dh->ctx, ®3);
33
34 reg3.i1_drdy1 = enable;
35
36 return iis2dh_pin_int1_config_set(iis2dh->ctx, ®3);
37 }
38
39 /**
40 * iis2dh_trigger_set - link external trigger to event data ready
41 */
iis2dh_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)42 int iis2dh_trigger_set(const struct device *dev,
43 const struct sensor_trigger *trig,
44 sensor_trigger_handler_t handler)
45 {
46 struct iis2dh_data *iis2dh = dev->data;
47 const struct iis2dh_device_config *cfg = dev->config;
48 int16_t raw[3];
49 int state = (handler != NULL) ? PROPERTY_ENABLE : PROPERTY_DISABLE;
50
51 if (!cfg->int_gpio.port) {
52 return -ENOTSUP;
53 }
54
55 switch (trig->type) {
56 case SENSOR_TRIG_DATA_READY:
57 iis2dh->drdy_handler = handler;
58 iis2dh->drdy_trig = trig;
59 if (state) {
60 /* dummy read: re-trigger interrupt */
61 iis2dh_acceleration_raw_get(iis2dh->ctx, raw);
62 }
63 return iis2dh_enable_drdy(dev, SENSOR_TRIG_DATA_READY, state);
64 default:
65 LOG_ERR("Unsupported sensor trigger");
66 return -ENOTSUP;
67 }
68 }
69
iis2dh_handle_drdy_int(const struct device * dev)70 static int iis2dh_handle_drdy_int(const struct device *dev)
71 {
72 struct iis2dh_data *data = dev->data;
73
74 if (data->drdy_handler) {
75 data->drdy_handler(dev, data->drdy_trig);
76 }
77
78 return 0;
79 }
80
81 /**
82 * iis2dh_handle_interrupt - handle the drdy event
83 * read data and call handler if registered any
84 */
iis2dh_handle_interrupt(const struct device * dev)85 static void iis2dh_handle_interrupt(const struct device *dev)
86 {
87 const struct iis2dh_device_config *cfg = dev->config;
88
89 iis2dh_handle_drdy_int(dev);
90
91 gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
92 }
93
iis2dh_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)94 static void iis2dh_gpio_callback(const struct device *dev,
95 struct gpio_callback *cb, uint32_t pins)
96 {
97 struct iis2dh_data *iis2dh =
98 CONTAINER_OF(cb, struct iis2dh_data, gpio_cb);
99 const struct iis2dh_device_config *cfg = iis2dh->dev->config;
100
101 if ((pins & BIT(cfg->int_gpio.pin)) == 0U) {
102 return;
103 }
104
105 gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_DISABLE);
106
107 #if defined(CONFIG_IIS2DH_TRIGGER_OWN_THREAD)
108 k_sem_give(&iis2dh->gpio_sem);
109 #elif defined(CONFIG_IIS2DH_TRIGGER_GLOBAL_THREAD)
110 k_work_submit(&iis2dh->work);
111 #endif /* CONFIG_IIS2DH_TRIGGER_OWN_THREAD */
112 }
113
114 #ifdef CONFIG_IIS2DH_TRIGGER_OWN_THREAD
iis2dh_thread(void * p1,void * p2,void * p3)115 static void iis2dh_thread(void *p1, void *p2, void *p3)
116 {
117 ARG_UNUSED(p2);
118 ARG_UNUSED(p3);
119
120 struct iis2dh_data *iis2dh = p1;
121
122 while (1) {
123 k_sem_take(&iis2dh->gpio_sem, K_FOREVER);
124 iis2dh_handle_interrupt(iis2dh->dev);
125 }
126 }
127 #endif /* CONFIG_IIS2DH_TRIGGER_OWN_THREAD */
128
129 #ifdef CONFIG_IIS2DH_TRIGGER_GLOBAL_THREAD
iis2dh_work_cb(struct k_work * work)130 static void iis2dh_work_cb(struct k_work *work)
131 {
132 struct iis2dh_data *iis2dh =
133 CONTAINER_OF(work, struct iis2dh_data, work);
134
135 iis2dh_handle_interrupt(iis2dh->dev);
136 }
137 #endif /* CONFIG_IIS2DH_TRIGGER_GLOBAL_THREAD */
138
iis2dh_init_interrupt(const struct device * dev)139 int iis2dh_init_interrupt(const struct device *dev)
140 {
141 struct iis2dh_data *iis2dh = dev->data;
142 const struct iis2dh_device_config *cfg = dev->config;
143 int ret;
144
145 if (!gpio_is_ready_dt(&cfg->int_gpio)) {
146 LOG_ERR("%s: device %s is not ready", dev->name, cfg->int_gpio.port->name);
147 return -ENODEV;
148 }
149
150 iis2dh->dev = dev;
151
152 #if defined(CONFIG_IIS2DH_TRIGGER_OWN_THREAD)
153 k_sem_init(&iis2dh->gpio_sem, 0, K_SEM_MAX_LIMIT);
154
155 k_thread_create(&iis2dh->thread, iis2dh->thread_stack,
156 CONFIG_IIS2DH_THREAD_STACK_SIZE,
157 iis2dh_thread, iis2dh,
158 0, NULL, K_PRIO_COOP(CONFIG_IIS2DH_THREAD_PRIORITY),
159 0, K_NO_WAIT);
160 #elif defined(CONFIG_IIS2DH_TRIGGER_GLOBAL_THREAD)
161 iis2dh->work.handler = iis2dh_work_cb;
162 #endif /* CONFIG_IIS2DH_TRIGGER_OWN_THREAD */
163
164 ret = gpio_pin_configure_dt(&cfg->int_gpio, GPIO_INPUT);
165 if (ret < 0) {
166 LOG_DBG("Could not configure gpio");
167 return ret;
168 }
169
170 gpio_init_callback(&iis2dh->gpio_cb,
171 iis2dh_gpio_callback,
172 BIT(cfg->int_gpio.pin));
173
174 if (gpio_add_callback(cfg->int_gpio.port, &iis2dh->gpio_cb) < 0) {
175 LOG_DBG("Could not set gpio callback");
176 return -EIO;
177 }
178
179 /* enable drdy on int1 in pulse mode */
180 if (iis2dh_int1_pin_notification_mode_set(iis2dh->ctx, IIS2DH_INT1_PULSED)) {
181 return -EIO;
182 }
183
184 return gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
185 }
186