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(const struct device * dev)124 static void isl29035_thread(const struct device *dev)
125 {
126 	struct isl29035_driver_data *drv_data = dev->data;
127 
128 	while (1) {
129 		k_sem_take(&drv_data->gpio_sem, K_FOREVER);
130 		isl29035_thread_cb(dev);
131 	}
132 }
133 #endif
134 
135 #ifdef CONFIG_ISL29035_TRIGGER_GLOBAL_THREAD
isl29035_work_cb(struct k_work * work)136 static void isl29035_work_cb(struct k_work *work)
137 {
138 	struct isl29035_driver_data *drv_data =
139 		CONTAINER_OF(work, struct isl29035_driver_data, work);
140 
141 	isl29035_thread_cb(drv_data->dev);
142 }
143 #endif
144 
isl29035_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)145 int isl29035_trigger_set(const struct device *dev,
146 			 const struct sensor_trigger *trig,
147 			 sensor_trigger_handler_t handler)
148 {
149 	struct isl29035_driver_data *drv_data = dev->data;
150 	const struct isl29035_config *config = dev->config;
151 
152 	if (!config->int_gpio.port) {
153 		return -ENOTSUP;
154 	}
155 
156 	/* disable interrupt callback while changing parameters */
157 	setup_int(dev, false);
158 
159 	drv_data->th_handler = handler;
160 	drv_data->th_trigger = trig;
161 
162 	/* enable interrupt callback */
163 	setup_int(dev, true);
164 	if (gpio_pin_get_dt(&config->int_gpio) > 0) {
165 		handle_int(dev);
166 	}
167 
168 	return 0;
169 }
170 
isl29035_init_interrupt(const struct device * dev)171 int isl29035_init_interrupt(const struct device *dev)
172 {
173 	struct isl29035_driver_data *drv_data = dev->data;
174 	const struct isl29035_config *config = dev->config;
175 
176 	/* set interrupt persistence */
177 	if (i2c_reg_update_byte_dt(&config->i2c,
178 				   ISL29035_COMMAND_I_REG,
179 				   ISL29035_INT_PRST_MASK,
180 				   ISL29035_INT_PRST_BITS) < 0) {
181 		LOG_DBG("Failed to set interrupt persistence cycles.");
182 		return -EIO;
183 	}
184 
185 	if (!gpio_is_ready_dt(&config->int_gpio)) {
186 		LOG_ERR("GPIO device not ready");
187 		return -ENODEV;
188 	}
189 
190 	gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
191 
192 	gpio_init_callback(&drv_data->gpio_cb,
193 			   isl29035_gpio_callback,
194 			   BIT(config->int_gpio.pin));
195 
196 	if (gpio_add_callback(config->int_gpio.port, &drv_data->gpio_cb) < 0) {
197 		LOG_DBG("Failed to set gpio callback.");
198 		return -EIO;
199 	}
200 
201 	drv_data->dev = dev;
202 
203 #if defined(CONFIG_ISL29035_TRIGGER_OWN_THREAD)
204 	k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
205 
206 	k_thread_create(&drv_data->thread, drv_data->thread_stack,
207 			CONFIG_ISL29035_THREAD_STACK_SIZE,
208 			(k_thread_entry_t)isl29035_thread,
209 			(void *)dev, NULL, NULL,
210 			K_PRIO_COOP(CONFIG_ISL29035_THREAD_PRIORITY),
211 			0, K_NO_WAIT);
212 #elif defined(CONFIG_ISL29035_TRIGGER_GLOBAL_THREAD)
213 	drv_data->work.handler = isl29035_work_cb;
214 #endif
215 
216 	setup_int(dev, true);
217 
218 	return 0;
219 }
220