1 /* ST Microelectronics LIS2DUX12 3-axis accelerometer driver
2  *
3  * Copyright (c) 2024 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Datasheet:
8  * https://www.st.com/resource/en/datasheet/lis2dux12.pdf
9  */
10 
11 #include <zephyr/logging/log.h>
12 #include "lis2dux12.h"
13 
14 LOG_MODULE_DECLARE(LIS2DUX12, CONFIG_SENSOR_LOG_LEVEL);
15 
lis2dux12_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)16 static void lis2dux12_gpio_callback(const struct device *dev, struct gpio_callback *cb,
17 				    uint32_t pins)
18 {
19 	struct lis2dux12_data *data = CONTAINER_OF(cb, struct lis2dux12_data, gpio_cb);
20 	int ret;
21 
22 	ARG_UNUSED(pins);
23 
24 	ret = gpio_pin_interrupt_configure_dt(data->drdy_gpio, GPIO_INT_DISABLE);
25 	if (ret < 0) {
26 		LOG_ERR("%s: Not able to configure pin_int", dev->name);
27 	}
28 
29 #if defined(CONFIG_LIS2DUX12_TRIGGER_OWN_THREAD)
30 	k_sem_give(&data->trig_sem);
31 #elif defined(CONFIG_LIS2DUX12_TRIGGER_GLOBAL_THREAD)
32 	k_work_submit(&data->work);
33 #endif
34 }
35 
36 #ifdef CONFIG_LIS2DUX12_TRIGGER_OWN_THREAD
lis2dux12_thread(struct lis2dux12_data * data)37 static void lis2dux12_thread(struct lis2dux12_data *data)
38 {
39 	const struct device *dev = data->dev;
40 	const struct lis2dux12_config *const cfg = dev->config;
41 	const struct lis2dux12_chip_api *chip_api = cfg->chip_api;
42 
43 	while (1) {
44 		k_sem_take(&data->trig_sem, K_FOREVER);
45 		chip_api->handle_interrupt(dev);
46 	}
47 }
48 #endif
49 
50 #ifdef CONFIG_LIS2DUX12_TRIGGER_GLOBAL_THREAD
lis2dux12_work_cb(struct k_work * work)51 static void lis2dux12_work_cb(struct k_work *work)
52 {
53 	struct lis2dux12_data *data = CONTAINER_OF(work, struct lis2dux12_data, work);
54 	const struct device *dev = data->dev;
55 	const struct lis2dux12_config *const cfg = dev->config;
56 	const struct lis2dux12_chip_api *chip_api = cfg->chip_api;
57 
58 	chip_api->handle_interrupt(dev);
59 }
60 #endif
61 
lis2dux12_trigger_init(const struct device * dev)62 int lis2dux12_trigger_init(const struct device *dev)
63 {
64 	struct lis2dux12_data *data = dev->data;
65 	const struct lis2dux12_config *cfg = dev->config;
66 	int ret;
67 
68 	data->drdy_gpio = (cfg->drdy_pin == 1) ? (struct gpio_dt_spec *)&cfg->int1_gpio
69 							 : (struct gpio_dt_spec *)&cfg->int2_gpio;
70 
71 	/* setup data ready gpio interrupt (INT1 or INT2) */
72 	if (!gpio_is_ready_dt(data->drdy_gpio)) {
73 		LOG_ERR("Cannot get pointer to drdy_gpio device");
74 		return -ENODEV;
75 	}
76 
77 	data->dev = dev;
78 
79 	ret = gpio_pin_configure_dt(data->drdy_gpio, GPIO_INPUT);
80 	if (ret < 0) {
81 		LOG_ERR("Could not configure gpio");
82 		return ret;
83 	}
84 
85 	gpio_init_callback(&data->gpio_cb, lis2dux12_gpio_callback, BIT(data->drdy_gpio->pin));
86 
87 	ret = gpio_add_callback(data->drdy_gpio->port, &data->gpio_cb);
88 	if (ret < 0) {
89 		LOG_ERR("Could not set gpio callback");
90 		return ret;
91 	}
92 
93 #if defined(CONFIG_LIS2DUX12_TRIGGER_OWN_THREAD)
94 	k_sem_init(&data->trig_sem, 0, K_SEM_MAX_LIMIT);
95 
96 	k_thread_create(&data->thread, data->thread_stack, CONFIG_LIS2DUX12_THREAD_STACK_SIZE,
97 			(k_thread_entry_t)lis2dux12_thread, data, NULL, NULL,
98 			K_PRIO_COOP(CONFIG_LIS2DUX12_THREAD_PRIORITY), 0, K_NO_WAIT);
99 	k_thread_name_set(&data->thread, dev->name);
100 #elif defined(CONFIG_LIS2DUX12_TRIGGER_GLOBAL_THREAD)
101 	data->work.handler = lis2dux12_work_cb;
102 #endif
103 
104 	return gpio_pin_interrupt_configure_dt(data->drdy_gpio, GPIO_INT_EDGE_TO_ACTIVE);
105 }
106 
lis2dux12_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)107 int lis2dux12_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
108 			  sensor_trigger_handler_t handler)
109 {
110 	struct lis2dux12_data *data = dev->data;
111 	const struct lis2dux12_config *cfg = dev->config;
112 	const struct lis2dux12_chip_api *chip_api = cfg->chip_api;
113 	int ret;
114 
115 	if (!cfg->trig_enabled) {
116 		LOG_ERR("trigger_set op not supported");
117 		return -ENOTSUP;
118 	}
119 
120 	if (data->drdy_gpio->port == NULL) {
121 		LOG_ERR("trigger_set is not supported");
122 		return -ENOTSUP;
123 	}
124 
125 	ret = gpio_pin_interrupt_configure_dt(data->drdy_gpio, GPIO_INT_DISABLE);
126 	if (ret < 0) {
127 		LOG_ERR("%s: Not able to configure pin_int", dev->name);
128 		return ret;
129 	}
130 
131 	data->data_ready_handler = handler;
132 	if (handler == NULL) {
133 		LOG_WRN("lis2dux12: no handler");
134 		return 0;
135 	}
136 
137 	/* re-trigger lost interrupt */
138 	chip_api->sample_fetch_accel(dev);
139 
140 	data->data_ready_trigger = trig;
141 
142 	ret = chip_api->init_interrupt(dev);
143 	if (ret < 0) {
144 		LOG_ERR("%s: Not able to initialize device interrupt", dev->name);
145 		return ret;
146 	}
147 
148 	return gpio_pin_interrupt_configure_dt(data->drdy_gpio, GPIO_INT_EDGE_TO_ACTIVE);
149 }
150