1 /*
2  * Copyright (c) 2017, NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT nxp_fxas21002
8 
9 #include <zephyr/logging/log.h>
10 
11 #include "fxas21002.h"
12 
13 LOG_MODULE_DECLARE(FXAS21002, CONFIG_SENSOR_LOG_LEVEL);
14 
fxas21002_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pin_mask)15 static void fxas21002_gpio_callback(const struct device *dev,
16 				    struct gpio_callback *cb,
17 				    uint32_t pin_mask)
18 {
19 	struct fxas21002_data *data =
20 		CONTAINER_OF(cb, struct fxas21002_data, gpio_cb);
21 	const struct fxas21002_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_FXAS21002_TRIGGER_OWN_THREAD)
30 	k_sem_give(&data->trig_sem);
31 #elif defined(CONFIG_FXAS21002_TRIGGER_GLOBAL_THREAD)
32 	k_work_submit(&data->work);
33 #endif
34 }
35 
fxas21002_handle_drdy_int(const struct device * dev)36 static int fxas21002_handle_drdy_int(const struct device *dev)
37 {
38 	struct fxas21002_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 
fxas21002_handle_int(const struct device * dev)47 static void fxas21002_handle_int(const struct device *dev)
48 {
49 	const struct fxas21002_config *config = dev->config;
50 	struct fxas21002_data *data = dev->data;
51 	uint8_t int_source;
52 
53 	k_sem_take(&data->sem, K_FOREVER);
54 
55 	if (config->ops->byte_read(dev, FXAS21002_REG_INT_SOURCE,
56 				   &int_source)) {
57 		LOG_ERR("Could not read interrupt source");
58 		int_source = 0U;
59 	}
60 
61 	k_sem_give(&data->sem);
62 
63 	if (int_source & FXAS21002_INT_SOURCE_DRDY_MASK) {
64 		fxas21002_handle_drdy_int(dev);
65 	}
66 
67 	gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
68 }
69 
70 #ifdef CONFIG_FXAS21002_TRIGGER_OWN_THREAD
fxas21002_thread_main(void * p1,void * p2,void * p3)71 static void fxas21002_thread_main(void *p1, void *p2, void *p3)
72 {
73 	ARG_UNUSED(p2);
74 	ARG_UNUSED(p3);
75 
76 	struct fxas21002_data *data = p1;
77 
78 	while (true) {
79 		k_sem_take(&data->trig_sem, K_FOREVER);
80 		fxas21002_handle_int(data->dev);
81 	}
82 }
83 #endif
84 
85 #ifdef CONFIG_FXAS21002_TRIGGER_GLOBAL_THREAD
fxas21002_work_handler(struct k_work * work)86 static void fxas21002_work_handler(struct k_work *work)
87 {
88 	struct fxas21002_data *data =
89 		CONTAINER_OF(work, struct fxas21002_data, work);
90 
91 	fxas21002_handle_int(data->dev);
92 }
93 #endif
94 
fxas21002_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)95 int fxas21002_trigger_set(const struct device *dev,
96 			  const struct sensor_trigger *trig,
97 			  sensor_trigger_handler_t handler)
98 {
99 	const struct fxas21002_config *config = dev->config;
100 	struct fxas21002_data *data = dev->data;
101 	enum fxas21002_power power = FXAS21002_POWER_STANDBY;
102 	uint32_t transition_time;
103 	uint8_t mask;
104 	int ret = 0;
105 
106 	if (!config->int_gpio.port) {
107 		return -ENOTSUP;
108 	}
109 
110 	k_sem_take(&data->sem, K_FOREVER);
111 
112 	switch (trig->type) {
113 	case SENSOR_TRIG_DATA_READY:
114 		mask = FXAS21002_CTRLREG2_CFG_EN_MASK;
115 		data->drdy_handler = handler;
116 		data->drdy_trig = trig;
117 		break;
118 	default:
119 		LOG_ERR("Unsupported sensor trigger");
120 		ret = -ENOTSUP;
121 		goto exit;
122 	}
123 
124 	/* The sensor must be in standby or ready mode when writing the
125 	 * configuration registers, therefore get the current power mode so we
126 	 * can restore it later.
127 	 */
128 	if (fxas21002_get_power(dev, &power)) {
129 		LOG_ERR("Could not get power mode");
130 		ret = -EIO;
131 		goto exit;
132 	}
133 
134 	/* Put the sensor in ready mode */
135 	if (fxas21002_set_power(dev, FXAS21002_POWER_READY)) {
136 		LOG_ERR("Could not set ready mode");
137 		ret = -EIO;
138 		goto exit;
139 	}
140 
141 	/* Configure the sensor interrupt */
142 	if (config->ops->reg_field_update(dev, FXAS21002_REG_CTRLREG2, mask,
143 					  handler ? mask : 0)) {
144 		LOG_ERR("Could not configure interrupt");
145 		ret = -EIO;
146 		goto exit;
147 	}
148 
149 	/* Restore the previous power mode */
150 	if (fxas21002_set_power(dev, power)) {
151 		LOG_ERR("Could not restore power mode");
152 		ret = -EIO;
153 		goto exit;
154 	}
155 
156 	/* Wait the transition time from ready mode */
157 	transition_time = fxas21002_get_transition_time(FXAS21002_POWER_READY,
158 							power,
159 							config->dr);
160 	k_busy_wait(transition_time);
161 
162 exit:
163 	k_sem_give(&data->sem);
164 
165 	return ret;
166 }
167 
fxas21002_trigger_init(const struct device * dev)168 int fxas21002_trigger_init(const struct device *dev)
169 {
170 	const struct fxas21002_config *config = dev->config;
171 	struct fxas21002_data *data = dev->data;
172 	uint8_t ctrl_reg2;
173 	int ret;
174 
175 	data->dev = dev;
176 
177 #if defined(CONFIG_FXAS21002_TRIGGER_OWN_THREAD)
178 	k_sem_init(&data->trig_sem, 0, K_SEM_MAX_LIMIT);
179 	k_thread_create(&data->thread, data->thread_stack,
180 			CONFIG_FXAS21002_THREAD_STACK_SIZE,
181 			fxas21002_thread_main, data, 0, NULL,
182 			K_PRIO_COOP(CONFIG_FXAS21002_THREAD_PRIORITY),
183 			0, K_NO_WAIT);
184 #elif defined(CONFIG_FXAS21002_TRIGGER_GLOBAL_THREAD)
185 	data->work.handler = fxas21002_work_handler;
186 #endif
187 
188 	/* Route the interrupts to INT1/INT2 pins */
189 	ctrl_reg2 = 0U;
190 #if CONFIG_FXAS21002_DRDY_INT1
191 	ctrl_reg2 |= FXAS21002_CTRLREG2_CFG_DRDY_MASK;
192 #endif
193 
194 	if (config->ops->byte_write(dev, FXAS21002_REG_CTRLREG2,
195 				    ctrl_reg2)) {
196 		LOG_ERR("Could not configure interrupt pin routing");
197 		return -EIO;
198 	}
199 
200 	if (!gpio_is_ready_dt(&config->int_gpio)) {
201 		LOG_ERR("GPIO device not ready");
202 		return -ENODEV;
203 	}
204 
205 	ret = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
206 	if (ret < 0) {
207 		return ret;
208 	}
209 
210 	gpio_init_callback(&data->gpio_cb, fxas21002_gpio_callback,
211 			   BIT(config->int_gpio.pin));
212 
213 	ret = gpio_add_callback(config->int_gpio.port, &data->gpio_cb);
214 	if (ret < 0) {
215 		return ret;
216 	}
217 
218 	ret = gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
219 	if (ret < 0) {
220 		return ret;
221 	}
222 
223 	return 0;
224 }
225