1 /* Bosch BMP388 pressure sensor
2 *
3 * Copyright (c) 2020 Facebook, Inc. and its affiliates
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Datasheet:
8 * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp388-ds001.pdf
9 */
10
11 #include <zephyr/kernel.h>
12 #include <zephyr/pm/device.h>
13 #include <zephyr/logging/log.h>
14
15 #include "bmp388.h"
16
17 LOG_MODULE_DECLARE(BMP388, CONFIG_SENSOR_LOG_LEVEL);
18
bmp388_handle_interrupts(const void * arg)19 static void bmp388_handle_interrupts(const void *arg)
20 {
21 const struct device *dev = (const struct device *)arg;
22 struct bmp388_data *data = dev->data;
23
24 if (data->handler_drdy) {
25 data->handler_drdy(dev, data->trig_drdy);
26 }
27 }
28
29 #ifdef CONFIG_BMP388_TRIGGER_OWN_THREAD
30 static K_THREAD_STACK_DEFINE(bmp388_thread_stack,
31 CONFIG_BMP388_THREAD_STACK_SIZE);
32 static struct k_thread bmp388_thread;
33
bmp388_thread_main(void * arg1,void * unused1,void * unused2)34 static void bmp388_thread_main(void *arg1, void *unused1, void *unused2)
35 {
36 ARG_UNUSED(unused1);
37 ARG_UNUSED(unused2);
38 const struct device *dev = (const struct device *)arg1;
39 struct bmp388_data *data = dev->data;
40
41 while (1) {
42 k_sem_take(&data->sem, K_FOREVER);
43 bmp388_handle_interrupts(dev);
44 }
45 }
46 #endif
47
48 #ifdef CONFIG_BMP388_TRIGGER_GLOBAL_THREAD
bmp388_work_handler(struct k_work * work)49 static void bmp388_work_handler(struct k_work *work)
50 {
51 struct bmp388_data *data = CONTAINER_OF(work,
52 struct bmp388_data,
53 work);
54
55 bmp388_handle_interrupts(data->dev);
56 }
57 #endif
58
bmp388_gpio_callback(const struct device * port,struct gpio_callback * cb,uint32_t pin)59 static void bmp388_gpio_callback(const struct device *port,
60 struct gpio_callback *cb,
61 uint32_t pin)
62 {
63 struct bmp388_data *data = CONTAINER_OF(cb,
64 struct bmp388_data,
65 gpio_cb);
66
67 ARG_UNUSED(port);
68 ARG_UNUSED(pin);
69
70 #if defined(CONFIG_BMP388_TRIGGER_OWN_THREAD)
71 k_sem_give(&data->sem);
72 #elif defined(CONFIG_BMP388_TRIGGER_GLOBAL_THREAD)
73 k_work_submit(&data->work);
74 #elif defined(CONFIG_BMP388_TRIGGER_DIRECT)
75 bmp388_handle_interrupts(data->dev);
76 #endif
77 }
78
bmp388_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)79 int bmp388_trigger_set(
80 const struct device *dev,
81 const struct sensor_trigger *trig,
82 sensor_trigger_handler_t handler)
83 {
84 struct bmp388_data *data = dev->data;
85
86 #ifdef CONFIG_PM_DEVICE
87 enum pm_device_state state;
88
89 (void)pm_device_state_get(dev, &state);
90 if (state != PM_DEVICE_STATE_ACTIVE) {
91 return -EBUSY;
92 }
93 #endif
94
95 if (trig->type != SENSOR_TRIG_DATA_READY) {
96 return -ENOTSUP;
97 }
98
99 if (bmp388_reg_field_update(
100 dev,
101 BMP388_REG_INT_CTRL,
102 BMP388_INT_CTRL_DRDY_EN_MASK,
103 (handler != NULL) << BMP388_INT_CTRL_DRDY_EN_POS) < 0) {
104 LOG_ERR("Failed to enable DRDY interrupt");
105 return -EIO;
106 }
107
108 data->handler_drdy = handler;
109 data->trig_drdy = trig;
110
111 return 0;
112 }
113
bmp388_trigger_mode_init(const struct device * dev)114 int bmp388_trigger_mode_init(const struct device *dev)
115 {
116 struct bmp388_data *data = dev->data;
117 const struct bmp388_config *cfg = dev->config;
118 int ret;
119
120 if (!gpio_is_ready_dt(&cfg->gpio_int)) {
121 LOG_ERR("INT device is not ready");
122 return -ENODEV;
123 }
124
125 #if defined(CONFIG_BMP388_TRIGGER_OWN_THREAD)
126 k_sem_init(&data->sem, 0, 1);
127 k_thread_create(
128 &bmp388_thread,
129 bmp388_thread_stack,
130 CONFIG_BMP388_THREAD_STACK_SIZE,
131 bmp388_thread_main,
132 (void *)dev,
133 NULL,
134 NULL,
135 K_PRIO_COOP(CONFIG_BMP388_THREAD_PRIORITY),
136 0,
137 K_NO_WAIT);
138 #elif defined(CONFIG_BMP388_TRIGGER_GLOBAL_THREAD)
139 data->work.handler = bmp388_work_handler;
140 #endif
141
142 #if defined(CONFIG_BMP388_TRIGGER_GLOBAL_THREAD) || \
143 defined(CONFIG_BMP388_TRIGGER_DIRECT)
144 data->dev = dev;
145 #endif
146
147 ret = gpio_pin_configure(cfg->gpio_int.port,
148 cfg->gpio_int.pin,
149 GPIO_INPUT | cfg->gpio_int.dt_flags);
150 if (ret < 0) {
151 return ret;
152 }
153
154 gpio_init_callback(&data->gpio_cb,
155 bmp388_gpio_callback,
156 BIT(cfg->gpio_int.pin));
157
158 ret = gpio_add_callback(cfg->gpio_int.port, &data->gpio_cb);
159 if (ret < 0) {
160 return ret;
161 }
162
163 ret = gpio_pin_interrupt_configure(cfg->gpio_int.port,
164 cfg->gpio_int.pin,
165 GPIO_INT_EDGE_TO_ACTIVE);
166 if (ret < 0) {
167 return ret;
168 }
169
170 return 0;
171 }
172