1 /*
2 * Copyright (c) 2020 arithmetics.io
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT ti_fdc2x1x
8
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/gpio.h>
11 #include <zephyr/sys/util.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/drivers/sensor.h>
14 #include "fdc2x1x.h"
15
16 #include <stdio.h>
17
18 #include <zephyr/logging/log.h>
19 LOG_MODULE_DECLARE(FDC2X1X, CONFIG_SENSOR_LOG_LEVEL);
20
fdc2x1x_thread_cb(const struct device * dev)21 static void fdc2x1x_thread_cb(const struct device *dev)
22 {
23 struct fdc2x1x_data *drv_data = dev->data;
24 uint16_t status;
25
26 #ifdef CONFIG_PM_DEVICE
27 enum pm_device_state state;
28
29 /* INTB asserts after exiting shutdown mode. Drop this interrupt */
30 (void)pm_device_state_get(dev, &state);
31 if (state == PM_DEVICE_STATE_OFF) {
32 return;
33 }
34 #endif
35
36 /* Clear the status */
37 if (fdc2x1x_get_status(dev, &status) < 0) {
38 LOG_ERR("Unable to get status.");
39 return;
40 }
41
42 k_mutex_lock(&drv_data->trigger_mutex, K_FOREVER);
43 if ((drv_data->drdy_handler != NULL) && FDC2X1X_STATUS_DRDY(status)) {
44 drv_data->drdy_handler(dev, drv_data->drdy_trigger);
45 }
46 k_mutex_unlock(&drv_data->trigger_mutex);
47 }
48
fdc2x1x_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)49 static void fdc2x1x_gpio_callback(const struct device *dev,
50 struct gpio_callback *cb, uint32_t pins)
51 {
52 struct fdc2x1x_data *drv_data =
53 CONTAINER_OF(cb, struct fdc2x1x_data, gpio_cb);
54
55 #ifdef CONFIG_FDC2X1X_TRIGGER_OWN_THREAD
56 k_sem_give(&drv_data->gpio_sem);
57 #elif CONFIG_FDC2X1X_TRIGGER_GLOBAL_THREAD
58 k_work_submit(&drv_data->work);
59 #endif
60 }
61
62 #ifdef CONFIG_FDC2X1X_TRIGGER_OWN_THREAD
fdc2x1x_thread(void * p1,void * p2,void * p3)63 static void fdc2x1x_thread(void *p1, void *p2, void *p3)
64 {
65 ARG_UNUSED(p2);
66 ARG_UNUSED(p3);
67
68 struct fdc2x1x_data *drv_data = p1;
69
70 while (true) {
71 k_sem_take(&drv_data->gpio_sem, K_FOREVER);
72 fdc2x1x_thread_cb(drv_data->dev);
73 }
74 }
75
76 #elif CONFIG_FDC2X1X_TRIGGER_GLOBAL_THREAD
fdc2x1x_work_cb(struct k_work * work)77 static void fdc2x1x_work_cb(struct k_work *work)
78 {
79 struct fdc2x1x_data *drv_data =
80 CONTAINER_OF(work, struct fdc2x1x_data, work);
81
82 fdc2x1x_thread_cb(drv_data->dev);
83 }
84 #endif
85
fdc2x1x_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)86 int fdc2x1x_trigger_set(const struct device *dev,
87 const struct sensor_trigger *trig,
88 sensor_trigger_handler_t handler)
89 {
90 struct fdc2x1x_data *drv_data = dev->data;
91 const struct fdc2x1x_config *cfg = dev->config;
92 uint16_t status, int_mask, int_en;
93 int ret;
94
95 gpio_pin_interrupt_configure_dt(&cfg->intb_gpio, GPIO_INT_DISABLE);
96
97 switch (trig->type) {
98 case SENSOR_TRIG_DATA_READY:
99 k_mutex_lock(&drv_data->trigger_mutex, K_FOREVER);
100 drv_data->drdy_handler = handler;
101 drv_data->drdy_trigger = trig;
102 k_mutex_unlock(&drv_data->trigger_mutex);
103
104 int_mask = FDC2X1X_ERROR_CONFIG_DRDY_2INT_MSK;
105 break;
106 default:
107 LOG_ERR("Unsupported sensor trigger");
108 ret = -ENOTSUP;
109 goto out;
110 }
111
112 if (handler) {
113 int_en = int_mask;
114 drv_data->int_config |= int_mask;
115 } else {
116 int_en = 0U;
117 }
118
119 ret = fdc2x1x_reg_write_mask(dev,
120 FDC2X1X_ERROR_CONFIG, int_mask, int_en);
121
122 /* Clear INTB pin by reading STATUS register */
123 fdc2x1x_get_status(dev, &status);
124 out:
125 gpio_pin_interrupt_configure_dt(&cfg->intb_gpio, GPIO_INT_EDGE_TO_ACTIVE);
126
127 return ret;
128 }
129
fdc2x1x_init_interrupt(const struct device * dev)130 int fdc2x1x_init_interrupt(const struct device *dev)
131 {
132 struct fdc2x1x_data *drv_data = dev->data;
133 const struct fdc2x1x_config *cfg = dev->config;
134 int ret;
135
136 k_mutex_init(&drv_data->trigger_mutex);
137
138 if (!gpio_is_ready_dt(&cfg->intb_gpio)) {
139 LOG_ERR("%s: intb_gpio device not ready", cfg->intb_gpio.port->name);
140 return -ENODEV;
141 }
142
143 ret = fdc2x1x_set_interrupt_pin(dev, true);
144 if (ret) {
145 return ret;
146 }
147
148 gpio_pin_configure_dt(&cfg->intb_gpio, GPIO_INPUT);
149
150 gpio_init_callback(&drv_data->gpio_cb,
151 fdc2x1x_gpio_callback,
152 BIT(cfg->intb_gpio.pin));
153
154 if (gpio_add_callback(cfg->intb_gpio.port, &drv_data->gpio_cb) < 0) {
155 LOG_ERR("Failed to set gpio callback!");
156 return -EIO;
157 }
158
159 drv_data->dev = dev;
160
161 #ifdef CONFIG_FDC2X1X_TRIGGER_OWN_THREAD
162 k_sem_init(&drv_data->gpio_sem, 0, UINT_MAX);
163
164 k_thread_create(&drv_data->thread, drv_data->thread_stack,
165 CONFIG_FDC2X1X_THREAD_STACK_SIZE,
166 fdc2x1x_thread,
167 drv_data, 0, NULL,
168 K_PRIO_COOP(CONFIG_FDC2X1X_THREAD_PRIORITY),
169 0, K_NO_WAIT);
170 #elif CONFIG_FDC2X1X_TRIGGER_GLOBAL_THREAD
171 drv_data->work.handler = fdc2x1x_work_cb;
172 #endif
173
174 return 0;
175 }
176