1 /* sensor_isl29035.c - trigger support for ISL29035 light sensor */
2 
3 /*
4  * Copyright (c) 2016 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #define DT_DRV_COMPAT isil_isl29035
10 
11 #include <zephyr/drivers/i2c.h>
12 #include <zephyr/sys/util.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/logging/log.h>
15 #include "isl29035.h"
16 
17 extern struct isl29035_driver_data isl29035_data;
18 
19 LOG_MODULE_DECLARE(ISL29035, CONFIG_SENSOR_LOG_LEVEL);
20 
setup_int(const struct device * dev,bool enable)21 static inline void setup_int(const struct device *dev, bool enable)
22 {
23 	const struct isl29035_config *config = dev->config;
24 
25 	unsigned int flags = enable
26 		? GPIO_INT_EDGE_TO_ACTIVE
27 		: GPIO_INT_DISABLE;
28 
29 	gpio_pin_interrupt_configure_dt(&config->int_gpio, flags);
30 }
31 
handle_int(const struct device * dev)32 static inline void handle_int(const struct device *dev)
33 {
34 	struct isl29035_driver_data *drv_data = dev->data;
35 
36 	setup_int(dev, false);
37 
38 #if defined(CONFIG_ISL29035_TRIGGER_OWN_THREAD)
39 	k_sem_give(&drv_data->gpio_sem);
40 #elif defined(CONFIG_ISL29035_TRIGGER_GLOBAL_THREAD)
41 	k_work_submit(&drv_data->work);
42 #endif
43 }
44 
isl29035_lux_processed_to_raw(struct sensor_value const * val)45 static uint16_t isl29035_lux_processed_to_raw(struct sensor_value const *val)
46 {
47 	uint64_t raw_val;
48 
49 	/* raw_val = val * (2 ^ adc_data_bits) / lux_range */
50 	raw_val = (((uint64_t)val->val1) << ISL29035_ADC_DATA_BITS) +
51 		  (((uint64_t)val->val2) << ISL29035_ADC_DATA_BITS) / 1000000U;
52 
53 	return raw_val / ISL29035_LUX_RANGE;
54 }
55 
isl29035_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)56 int isl29035_attr_set(const struct device *dev,
57 		      enum sensor_channel chan,
58 		      enum sensor_attribute attr,
59 		      const struct sensor_value *val)
60 {
61 	const struct isl29035_config *config = dev->config;
62 	uint8_t lsb_reg, msb_reg;
63 	uint16_t raw_val;
64 
65 	if (!config->int_gpio.port) {
66 		return -ENOTSUP;
67 	}
68 
69 	if (attr == SENSOR_ATTR_UPPER_THRESH) {
70 		lsb_reg = ISL29035_INT_HT_LSB_REG;
71 		msb_reg = ISL29035_INT_HT_MSB_REG;
72 	} else if (attr == SENSOR_ATTR_LOWER_THRESH) {
73 		lsb_reg = ISL29035_INT_LT_LSB_REG;
74 		msb_reg = ISL29035_INT_LT_MSB_REG;
75 	} else {
76 		return -ENOTSUP;
77 	}
78 
79 	raw_val = isl29035_lux_processed_to_raw(val);
80 
81 	if (i2c_reg_write_byte_dt(&config->i2c,
82 				  lsb_reg, raw_val & 0xFF) < 0 ||
83 	    i2c_reg_write_byte_dt(&config->i2c,
84 				  msb_reg, raw_val >> 8) < 0) {
85 		LOG_DBG("Failed to set attribute.");
86 		return -EIO;
87 	}
88 
89 	return 0;
90 }
91 
isl29035_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)92 static void isl29035_gpio_callback(const struct device *dev,
93 				   struct gpio_callback *cb, uint32_t pins)
94 {
95 	struct isl29035_driver_data *drv_data =
96 		CONTAINER_OF(cb, struct isl29035_driver_data, gpio_cb);
97 
98 
99 	ARG_UNUSED(pins);
100 	handle_int(drv_data->dev);
101 }
102 
isl29035_thread_cb(const struct device * dev)103 static void isl29035_thread_cb(const struct device *dev)
104 {
105 	struct isl29035_driver_data *drv_data = dev->data;
106 	const struct isl29035_config *config = dev->config;
107 	uint8_t val;
108 
109 	/* clear interrupt */
110 	if (i2c_reg_read_byte_dt(&config->i2c,
111 				 ISL29035_COMMAND_I_REG, &val) < 0) {
112 		LOG_ERR("isl29035: Error reading command register");
113 		return;
114 	}
115 
116 	if (drv_data->th_handler != NULL) {
117 		drv_data->th_handler(dev, drv_data->th_trigger);
118 	}
119 
120 	setup_int(dev, true);
121 }
122 
123 #ifdef CONFIG_ISL29035_TRIGGER_OWN_THREAD
isl29035_thread(void * p1,void * p2,void * p3)124 static void isl29035_thread(void *p1, void *p2, void *p3)
125 {
126 	ARG_UNUSED(p2);
127 	ARG_UNUSED(p3);
128 
129 	const struct device *dev = p1;
130 	struct isl29035_driver_data *drv_data = dev->data;
131 
132 	while (1) {
133 		k_sem_take(&drv_data->gpio_sem, K_FOREVER);
134 		isl29035_thread_cb(dev);
135 	}
136 }
137 #endif
138 
139 #ifdef CONFIG_ISL29035_TRIGGER_GLOBAL_THREAD
isl29035_work_cb(struct k_work * work)140 static void isl29035_work_cb(struct k_work *work)
141 {
142 	struct isl29035_driver_data *drv_data =
143 		CONTAINER_OF(work, struct isl29035_driver_data, work);
144 
145 	isl29035_thread_cb(drv_data->dev);
146 }
147 #endif
148 
isl29035_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)149 int isl29035_trigger_set(const struct device *dev,
150 			 const struct sensor_trigger *trig,
151 			 sensor_trigger_handler_t handler)
152 {
153 	struct isl29035_driver_data *drv_data = dev->data;
154 	const struct isl29035_config *config = dev->config;
155 
156 	if (!config->int_gpio.port) {
157 		return -ENOTSUP;
158 	}
159 
160 	/* disable interrupt callback while changing parameters */
161 	setup_int(dev, false);
162 
163 	drv_data->th_handler = handler;
164 	drv_data->th_trigger = trig;
165 
166 	/* enable interrupt callback */
167 	setup_int(dev, true);
168 	if (gpio_pin_get_dt(&config->int_gpio) > 0) {
169 		handle_int(dev);
170 	}
171 
172 	return 0;
173 }
174 
isl29035_init_interrupt(const struct device * dev)175 int isl29035_init_interrupt(const struct device *dev)
176 {
177 	struct isl29035_driver_data *drv_data = dev->data;
178 	const struct isl29035_config *config = dev->config;
179 
180 	/* set interrupt persistence */
181 	if (i2c_reg_update_byte_dt(&config->i2c,
182 				   ISL29035_COMMAND_I_REG,
183 				   ISL29035_INT_PRST_MASK,
184 				   ISL29035_INT_PRST_BITS) < 0) {
185 		LOG_DBG("Failed to set interrupt persistence cycles.");
186 		return -EIO;
187 	}
188 
189 	if (!gpio_is_ready_dt(&config->int_gpio)) {
190 		LOG_ERR("GPIO device not ready");
191 		return -ENODEV;
192 	}
193 
194 	gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
195 
196 	gpio_init_callback(&drv_data->gpio_cb,
197 			   isl29035_gpio_callback,
198 			   BIT(config->int_gpio.pin));
199 
200 	if (gpio_add_callback(config->int_gpio.port, &drv_data->gpio_cb) < 0) {
201 		LOG_DBG("Failed to set gpio callback.");
202 		return -EIO;
203 	}
204 
205 	drv_data->dev = dev;
206 
207 #if defined(CONFIG_ISL29035_TRIGGER_OWN_THREAD)
208 	k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
209 
210 	k_thread_create(&drv_data->thread, drv_data->thread_stack,
211 			CONFIG_ISL29035_THREAD_STACK_SIZE,
212 			isl29035_thread,
213 			(void *)dev, NULL, NULL,
214 			K_PRIO_COOP(CONFIG_ISL29035_THREAD_PRIORITY),
215 			0, K_NO_WAIT);
216 #elif defined(CONFIG_ISL29035_TRIGGER_GLOBAL_THREAD)
217 	drv_data->work.handler = isl29035_work_cb;
218 #endif
219 
220 	setup_int(dev, true);
221 
222 	return 0;
223 }
224