1 /*
2 * Copyright (c) 2022 T-Mobile USA, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "tsl2540.h"
8 #include <zephyr/logging/log.h>
9
10 LOG_MODULE_DECLARE(tsl2540, CONFIG_SENSOR_LOG_LEVEL);
11
tsl2540_setup_int(const struct device * dev,bool enable)12 static void tsl2540_setup_int(const struct device *dev, bool enable)
13 {
14 const struct tsl2540_config *config = dev->config;
15 gpio_flags_t flags = enable
16 ? GPIO_INT_EDGE_TO_ACTIVE
17 : GPIO_INT_DISABLE;
18
19 gpio_pin_interrupt_configure_dt(&config->int_gpio, flags);
20 }
21
tsl2540_handle_int(const struct device * dev)22 static void tsl2540_handle_int(const struct device *dev)
23 {
24 struct tsl2540_data *drv_data = dev->data;
25
26 tsl2540_setup_int(dev, false);
27
28 #if defined(CONFIG_TSL2540_TRIGGER_OWN_THREAD)
29 k_sem_give(&drv_data->trig_sem);
30 #elif defined(CONFIG_TSL2540_TRIGGER_GLOBAL_THREAD)
31 k_work_submit(&drv_data->work);
32 #endif
33 }
34
tsl2540_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pin_mask)35 static void tsl2540_gpio_callback(const struct device *dev, struct gpio_callback *cb,
36 uint32_t pin_mask)
37 {
38 struct tsl2540_data *data = CONTAINER_OF(cb, struct tsl2540_data, gpio_cb);
39
40 tsl2540_handle_int(data->dev);
41 }
42
tsl2540_process_int(const struct device * dev)43 static void tsl2540_process_int(const struct device *dev)
44 {
45 const struct tsl2540_config *config = dev->config;
46 struct tsl2540_data *data = dev->data;
47 uint8_t status;
48
49 /* Read the status, cleared automatically in CFG3 */
50 int ret = i2c_reg_read_byte_dt(&config->i2c_spec, TSL2540_REG_STATUS, &status);
51
52 if (ret) {
53 LOG_ERR("Could not read status register (%#x), errno: %d", TSL2540_REG_STATUS,
54 ret);
55 return;
56 }
57
58 if (BIT(7) & status) { /* ASAT */
59 LOG_ERR("Interrupt status(%#x): %#x: ASAT", TSL2540_REG_STATUS, status);
60 }
61
62 if (BIT(3) & status) { /* CINT */
63 LOG_DBG("Interrupt status(%#x): %#x: CINT", TSL2540_REG_STATUS, status);
64 }
65
66 if (BIT(4) & status) { /* AINT */
67 LOG_DBG("Interrupt status(%#x): %#x: AINT", TSL2540_REG_STATUS, status);
68 if (data->als_handler != NULL) {
69 data->als_handler(dev, data->als_trigger);
70 }
71 }
72
73 tsl2540_setup_int(dev, true);
74
75 /* Check for pin that may be asserted while we were busy */
76 int pv = gpio_pin_get_dt(&config->int_gpio);
77
78 if (pv > 0) {
79 tsl2540_handle_int(dev);
80 }
81 }
82
83 #ifdef CONFIG_TSL2540_TRIGGER_OWN_THREAD
tsl2540_thread_main(void * p1,void * p2,void * p3)84 static void tsl2540_thread_main(void *p1, void *p2, void *p3)
85 {
86 ARG_UNUSED(p2);
87 ARG_UNUSED(p3);
88
89 struct tsl2540_data *data = p1;
90
91 while (true) {
92 k_sem_take(&data->trig_sem, K_FOREVER);
93 tsl2540_process_int(data->dev);
94 }
95 }
96 #endif
97
98 #ifdef CONFIG_TSL2540_TRIGGER_GLOBAL_THREAD
tsl2540_work_handler(struct k_work * work)99 static void tsl2540_work_handler(struct k_work *work)
100 {
101 struct tsl2540_data *data = CONTAINER_OF(work, struct tsl2540_data, work);
102
103 tsl2540_process_int(data->dev);
104 }
105 #endif
106
tsl2540_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)107 int tsl2540_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
108 sensor_trigger_handler_t handler)
109 {
110 const struct tsl2540_config *config = dev->config;
111 struct tsl2540_data *data = dev->data;
112 int ret;
113
114 if (trig->type != SENSOR_TRIG_THRESHOLD) {
115 LOG_ERR("Unsupported sensor trigger type: %d", trig->type);
116 return -ENOTSUP;
117 }
118
119 if (trig->chan != SENSOR_CHAN_LIGHT) {
120 LOG_ERR("Unsupported sensor trigger channel: %d", trig->chan);
121 return -ENOTSUP;
122 }
123
124
125 const struct i2c_dt_spec *i2c_spec = &config->i2c_spec;
126
127 ret = i2c_reg_update_byte_dt(i2c_spec, TSL2540_INTENAB_ADDR,
128 TSL2540_INTENAB_MASK, TSL2540_INTENAB_CONF);
129 if (ret) {
130 LOG_ERR("%#x: I/O error: %d", TSL2540_INTENAB_ADDR, ret);
131 return -EIO;
132 }
133
134 ret = i2c_reg_update_byte_dt(i2c_spec, TSL2540_CFG3_ADDR,
135 TSL2540_CFG3_MASK, TSL2540_CFG3_CONF);
136 if (ret) {
137 LOG_ERR("%#x: I/O error: %d", TSL2540_CFG3_ADDR, ret);
138 return -EIO;
139 }
140
141 k_sem_take(&data->sem, K_FOREVER);
142
143 data->als_handler = handler;
144 data->als_trigger = trig;
145
146 if (handler != NULL) {
147 tsl2540_setup_int(dev, true);
148
149 /* Check whether already asserted */
150 int pv = gpio_pin_get_dt(&config->int_gpio);
151
152 if (pv > 0) {
153 tsl2540_handle_int(dev);
154 }
155 }
156
157 k_sem_give(&data->sem);
158
159 return ret;
160 }
161
tsl2540_trigger_init(const struct device * dev)162 int tsl2540_trigger_init(const struct device *dev)
163 {
164 const struct tsl2540_config *config = dev->config;
165 struct tsl2540_data *data = dev->data;
166 int rc;
167
168 /* Check device is defined */
169 if (config->int_gpio.port == NULL) {
170 LOG_ERR("int-gpios is not defined in the device tree.");
171 return -EINVAL;
172 }
173
174 /* Get the GPIO device */
175 if (!gpio_is_ready_dt(&config->int_gpio)) {
176 LOG_ERR("%s: gpio controller %s not ready", dev->name, config->int_gpio.port->name);
177 return -ENODEV;
178 }
179
180 rc = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
181 if (rc < 0) {
182 return rc;
183 }
184
185 gpio_init_callback(&data->gpio_cb, tsl2540_gpio_callback, BIT(config->int_gpio.pin));
186
187 if (gpio_add_callback(config->int_gpio.port, &data->gpio_cb) < 0) {
188 LOG_ERR("Failed to set gpio callback!");
189 return -EIO;
190 }
191
192 data->dev = dev;
193
194 #if defined(CONFIG_TSL2540_TRIGGER_OWN_THREAD)
195 k_sem_init(&data->trig_sem, 0, K_SEM_MAX_LIMIT);
196 k_thread_create(&data->thread, data->thread_stack, CONFIG_TSL2540_THREAD_STACK_SIZE,
197 tsl2540_thread_main, data, NULL, NULL,
198 K_PRIO_COOP(CONFIG_TSL2540_THREAD_PRIORITY), 0, K_NO_WAIT);
199 k_thread_name_set(&data->thread, "TSL2540 trigger");
200 #elif defined(CONFIG_TSL2540_TRIGGER_GLOBAL_THREAD)
201 data->work.handler = tsl2540_work_handler;
202 #endif
203
204 return 0;
205 }
206