1 /*
2  * Copyright (c) 2023 Kurtis Dinelle
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "tsl2591.h"
8 
9 LOG_MODULE_DECLARE(TSL2591, CONFIG_SENSOR_LOG_LEVEL);
10 
tsl2591_setup_int(const struct device * dev,bool enable)11 static inline void tsl2591_setup_int(const struct device *dev, bool enable)
12 {
13 	const struct tsl2591_config *config = dev->config;
14 	gpio_flags_t flags = enable ? GPIO_INT_EDGE_TO_ACTIVE : GPIO_INT_DISABLE;
15 
16 	gpio_pin_interrupt_configure_dt(&config->int_gpio, flags);
17 }
18 
tsl2591_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)19 static void tsl2591_gpio_callback(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
20 {
21 	ARG_UNUSED(dev);
22 	ARG_UNUSED(pins);
23 
24 	struct tsl2591_data *data = CONTAINER_OF(cb, struct tsl2591_data, gpio_cb);
25 
26 	tsl2591_setup_int(data->dev, false);
27 
28 #if defined(CONFIG_TSL2591_TRIGGER_OWN_THREAD)
29 	k_sem_give(&data->trig_sem);
30 #elif defined(CONFIG_TSL2591_TRIGGER_GLOBAL_THREAD)
31 	k_work_submit(&data->work);
32 #endif
33 }
34 
tsl2591_handle_int(const struct device * dev)35 static void tsl2591_handle_int(const struct device *dev)
36 {
37 	struct tsl2591_data *data = dev->data;
38 	const struct tsl2591_config *config = dev->config;
39 	uint8_t clear_cmd;
40 	int ret;
41 
42 	/* Interrupt must be cleared manually */
43 	clear_cmd = TSL2591_CLEAR_INT_CMD;
44 	ret = i2c_write_dt(&config->i2c, &clear_cmd, 1U);
45 	if (ret < 0) {
46 		LOG_ERR("Failed to clear interrupt");
47 		return;
48 	}
49 
50 	if (data->th_handler != NULL) {
51 		data->th_handler(dev, data->th_trigger);
52 	}
53 
54 	tsl2591_setup_int(dev, true);
55 }
56 
57 #ifdef CONFIG_TSL2591_TRIGGER_OWN_THREAD
tsl2591_thread(void * p1,void * p2,void * p3)58 static void tsl2591_thread(void *p1, void *p2, void *p3)
59 {
60 	ARG_UNUSED(p2);
61 	ARG_UNUSED(p3);
62 
63 	struct tsl2591_data *data = p1;
64 
65 	while (1) {
66 		k_sem_take(&data->trig_sem, K_FOREVER);
67 		tsl2591_handle_int(data->dev);
68 	}
69 }
70 #endif
71 
72 #ifdef CONFIG_TSL2591_TRIGGER_GLOBAL_THREAD
tsl2591_work_handler(struct k_work * work)73 static void tsl2591_work_handler(struct k_work *work)
74 {
75 	struct tsl2591_data *data = CONTAINER_OF(work, struct tsl2591_data, work);
76 
77 	tsl2591_handle_int(data->dev);
78 }
79 #endif
80 
tsl2591_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)81 int tsl2591_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
82 			sensor_trigger_handler_t handler)
83 {
84 	struct tsl2591_data *data = dev->data;
85 	const struct tsl2591_config *config = dev->config;
86 	int ret;
87 
88 	if (!config->int_gpio.port) {
89 		return -ENOTSUP;
90 	}
91 
92 	if (trig->chan != SENSOR_CHAN_LIGHT) {
93 		LOG_ERR("Unsupported sensor trigger channel");
94 		return -ENOTSUP;
95 	}
96 
97 	if (trig->type != SENSOR_TRIG_THRESHOLD) {
98 		LOG_ERR("Unsupported sensor trigger type");
99 		return -ENOTSUP;
100 	}
101 
102 	data->th_handler = handler;
103 	data->th_trigger = trig;
104 	tsl2591_setup_int(dev, true);
105 
106 	ret = tsl2591_reg_update(dev, TSL2591_REG_ENABLE, TSL2591_AIEN_MASK, TSL2591_AIEN_ON);
107 	if (ret < 0) {
108 		LOG_ERR("Failed to enable interrupt on sensor");
109 	}
110 
111 	return ret;
112 }
113 
tsl2591_initialize_int(const struct device * dev)114 int tsl2591_initialize_int(const struct device *dev)
115 {
116 	struct tsl2591_data *data = dev->data;
117 	const struct tsl2591_config *config = dev->config;
118 	int ret;
119 
120 	if (!gpio_is_ready_dt(&config->int_gpio)) {
121 		LOG_ERR("%s: gpio controller %s not ready", dev->name, config->int_gpio.port->name);
122 		return -ENODEV;
123 	}
124 
125 	ret = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT | config->int_gpio.dt_flags);
126 	if (ret < 0) {
127 		LOG_ERR("Failed to configure gpio pin for input");
128 		return ret;
129 	}
130 
131 	gpio_init_callback(&data->gpio_cb, tsl2591_gpio_callback, BIT(config->int_gpio.pin));
132 
133 	ret = gpio_add_callback(config->int_gpio.port, &data->gpio_cb);
134 	if (ret < 0) {
135 		LOG_DBG("Failed to set gpio callback");
136 		return ret;
137 	}
138 
139 	data->dev = dev;
140 
141 #if defined(CONFIG_TSL2591_TRIGGER_OWN_THREAD)
142 	ret = k_sem_init(&data->trig_sem, 0, K_SEM_MAX_LIMIT);
143 	if (ret < 0) {
144 		LOG_ERR("Failed to initialize trigger semaphore");
145 		return ret;
146 	}
147 
148 	k_thread_create(&data->thread, data->thread_stack, CONFIG_TSL2591_THREAD_STACK_SIZE,
149 			tsl2591_thread, data, NULL, NULL,
150 			K_PRIO_COOP(CONFIG_TSL2591_THREAD_PRIORITY), 0, K_NO_WAIT);
151 #elif defined(CONFIG_TSL2591_TRIGGER_GLOBAL_THREAD)
152 	data->work.handler = tsl2591_work_handler;
153 #endif
154 
155 	return 0;
156 }
157