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(struct fdc2x1x_data * drv_data)63 static void fdc2x1x_thread(struct fdc2x1x_data *drv_data)
64 {
65 	while (true) {
66 		k_sem_take(&drv_data->gpio_sem, K_FOREVER);
67 		fdc2x1x_thread_cb(drv_data->dev);
68 	}
69 }
70 
71 #elif CONFIG_FDC2X1X_TRIGGER_GLOBAL_THREAD
fdc2x1x_work_cb(struct k_work * work)72 static void fdc2x1x_work_cb(struct k_work *work)
73 {
74 	struct fdc2x1x_data *drv_data =
75 		CONTAINER_OF(work, struct fdc2x1x_data, work);
76 
77 	fdc2x1x_thread_cb(drv_data->dev);
78 }
79 #endif
80 
fdc2x1x_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)81 int fdc2x1x_trigger_set(const struct device *dev,
82 			const struct sensor_trigger *trig,
83 			sensor_trigger_handler_t handler)
84 {
85 	struct fdc2x1x_data *drv_data = dev->data;
86 	const struct fdc2x1x_config *cfg = dev->config;
87 	uint16_t status, int_mask, int_en;
88 	int ret;
89 
90 	gpio_pin_interrupt_configure_dt(&cfg->intb_gpio, GPIO_INT_DISABLE);
91 
92 	switch (trig->type) {
93 	case SENSOR_TRIG_DATA_READY:
94 		k_mutex_lock(&drv_data->trigger_mutex, K_FOREVER);
95 		drv_data->drdy_handler = handler;
96 		drv_data->drdy_trigger = trig;
97 		k_mutex_unlock(&drv_data->trigger_mutex);
98 
99 		int_mask = FDC2X1X_ERROR_CONFIG_DRDY_2INT_MSK;
100 		break;
101 	default:
102 		LOG_ERR("Unsupported sensor trigger");
103 		ret = -ENOTSUP;
104 		goto out;
105 	}
106 
107 	if (handler) {
108 		int_en = int_mask;
109 		drv_data->int_config |= int_mask;
110 	} else {
111 		int_en = 0U;
112 	}
113 
114 	ret = fdc2x1x_reg_write_mask(dev,
115 				     FDC2X1X_ERROR_CONFIG, int_mask, int_en);
116 
117 	/* Clear INTB pin by reading STATUS register */
118 	fdc2x1x_get_status(dev, &status);
119 out:
120 	gpio_pin_interrupt_configure_dt(&cfg->intb_gpio, GPIO_INT_EDGE_TO_ACTIVE);
121 
122 	return ret;
123 }
124 
fdc2x1x_init_interrupt(const struct device * dev)125 int fdc2x1x_init_interrupt(const struct device *dev)
126 {
127 	struct fdc2x1x_data *drv_data = dev->data;
128 	const struct fdc2x1x_config *cfg = dev->config;
129 	int ret;
130 
131 	k_mutex_init(&drv_data->trigger_mutex);
132 
133 	if (!gpio_is_ready_dt(&cfg->intb_gpio)) {
134 		LOG_ERR("%s: intb_gpio device not ready", cfg->intb_gpio.port->name);
135 		return -ENODEV;
136 	}
137 
138 	ret = fdc2x1x_set_interrupt_pin(dev, true);
139 	if (ret) {
140 		return ret;
141 	}
142 
143 	gpio_pin_configure_dt(&cfg->intb_gpio, GPIO_INPUT);
144 
145 	gpio_init_callback(&drv_data->gpio_cb,
146 			   fdc2x1x_gpio_callback,
147 			   BIT(cfg->intb_gpio.pin));
148 
149 	if (gpio_add_callback(cfg->intb_gpio.port, &drv_data->gpio_cb) < 0) {
150 		LOG_ERR("Failed to set gpio callback!");
151 		return -EIO;
152 	}
153 
154 	drv_data->dev = dev;
155 
156 #ifdef CONFIG_FDC2X1X_TRIGGER_OWN_THREAD
157 	k_sem_init(&drv_data->gpio_sem, 0, UINT_MAX);
158 
159 	k_thread_create(&drv_data->thread, drv_data->thread_stack,
160 			CONFIG_FDC2X1X_THREAD_STACK_SIZE,
161 			(k_thread_entry_t)fdc2x1x_thread,
162 			drv_data, 0, NULL,
163 			K_PRIO_COOP(CONFIG_FDC2X1X_THREAD_PRIORITY),
164 			0, K_NO_WAIT);
165 #elif CONFIG_FDC2X1X_TRIGGER_GLOBAL_THREAD
166 	drv_data->work.handler = fdc2x1x_work_cb;
167 #endif
168 
169 	return 0;
170 }
171