1 /* sensor_isl29035.h - header file for ISL29035 light sensor driver */
2 
3 /*
4  * Copyright (c) 2016 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #ifndef ZEPHYR_DRIVERS_SENSOR_ISL29035_ISL29035_H_
10 #define ZEPHYR_DRIVERS_SENSOR_ISL29035_ISL29035_H_
11 
12 #include <zephyr/device.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/drivers/sensor.h>
15 #include <zephyr/drivers/i2c.h>
16 #include <zephyr/drivers/gpio.h>
17 
18 #define ISL29035_COMMAND_I_REG		0x00
19 #define ISL29035_OPMODE_SHIFT		5
20 #define ISL29035_OPMODE_MASK		(7 << ISL29035_OPMODE_SHIFT)
21 #define ISL29035_INT_BIT_SHIFT		2
22 #define ISL29035_INT_BIT_MASK		(1 << ISL29035_INT_BIT_SHIFT)
23 #define ISL29035_INT_PRST_SHIFT		0
24 #define ISL29035_INT_PRST_MASK		(3 << ISL29035_INT_BIT_SHIFT)
25 
26 #define ISL29035_OPMODE_OFF		0
27 #define ISL29035_OPMODE_ALS_ONCE	1
28 #define ISL29035_OPMODE_IR_ONCE		2
29 #define ISL29035_OPMODE_ALS_CONT	5
30 #define ISL29035_OPMODE_IR_CONT		6
31 
32 #define ISL29035_COMMAND_II_REG		0x01
33 #define ISL29035_LUX_RANGE_SHIFT	0
34 #define ISL29035_LUX_RANGE_MASK		(3 << ISL29035_LUX_RANGE_SHIFT)
35 #define ISL29035_ADC_RES_SHIFT		2
36 #define ISL29035_ADC_RES_MASK		(3 << ISL29035_ADC_RES_SHIFT)
37 
38 #define ISL29035_DATA_LSB_REG		0x02
39 #define ISL29035_DATA_MSB_REG		0x03
40 #define ISL29035_INT_LT_LSB_REG		0x04
41 #define ISL29035_INT_LT_MSB_REG		0x05
42 #define ISL29035_INT_HT_LSB_REG		0x06
43 #define ISL29035_INT_HT_MSB_REG		0x07
44 
45 #define ISL29035_ID_REG			0x0F
46 #define ISL29035_BOUT_SHIFT		7
47 #define ISL29035_BOUT_MASK		(1 << ISL29035_BOUT_SHIFT)
48 #define ISL29035_ID_SHIFT		3
49 #define ISL29035_ID_MASK		(3 << ISL29035_ID_SHIFT)
50 
51 #if CONFIG_ISL29035_MODE_ALS
52 	#define ISL29035_ACTIVE_OPMODE		ISL29035_OPMODE_ALS_CONT
53 	#define ISL29035_ACTIVE_CHAN		SENSOR_CHAN_LIGHT
54 #elif CONFIG_ISL29035_MODE_IR
55 	#define ISL29035_ACTIVE_OPMODE		ISL29035_OPMODE_IR_CONT
56 	#define ISL29035_ACTIVE_CHAN		SENSOR_CHAN_IR
57 #endif
58 
59 #define ISL29035_ACTIVE_OPMODE_BITS		\
60 	(ISL29035_ACTIVE_OPMODE << ISL29035_OPMODE_SHIFT)
61 
62 #if CONFIG_ISL29035_LUX_RANGE_1K
63 	#define ISL29035_LUX_RANGE_IDX		0
64 	#define ISL29035_LUX_RANGE		1000
65 #elif CONFIG_ISL29035_LUX_RANGE_4K
66 	#define ISL29035_LUX_RANGE_IDX		1
67 	#define ISL29035_LUX_RANGE		4000
68 #elif CONFIG_ISL29035_LUX_RANGE_16K
69 	#define ISL29035_LUX_RANGE_IDX		2
70 	#define ISL29035_LUX_RANGE		16000
71 #elif CONFIG_ISL29035_LUX_RANGE_64K
72 	#define ISL29035_LUX_RANGE_IDX		3
73 	#define ISL29035_LUX_RANGE		64000
74 #endif
75 
76 #define ISL29035_LUX_RANGE_BITS			\
77 	(ISL29035_LUX_RANGE_IDX << ISL29035_LUX_RANGE_SHIFT)
78 
79 #if CONFIG_ISL29035_INTEGRATION_TIME_26
80 	#define ISL29035_ADC_RES_IDX		3
81 #elif CONFIG_ISL29035_INTEGRATION_TIME_410
82 	#define ISL29035_ADC_RES_IDX		2
83 #elif CONFIG_ISL29035_INTEGRATION_TIME_6500
84 	#define ISL29035_ADC_RES_IDX		1
85 #elif CONFIG_ISL29035_INTEGRATION_TIME_105K
86 	#define ISL29035_ADC_RES_IDX		0
87 #endif
88 
89 #define ISL29035_ADC_RES_BITS			\
90 	(ISL29035_ADC_RES_IDX << ISL29035_ADC_RES_SHIFT)
91 
92 #define ISL29035_ADC_DATA_BITS	(16 - 4 * ISL29035_ADC_RES_IDX)
93 #define ISL29035_ADC_DATA_MASK	(0xFFFF >> (16 - ISL29035_ADC_DATA_BITS))
94 
95 #if CONFIG_ISL29035_INT_PERSIST_1
96 	#define ISL29035_INT_PRST_IDX		0
97 	#define ISL29035_INT_PRST_CYCLES	1
98 #elif CONFIG_ISL29035_INT_PERSIST_4
99 	#define ISL29035_INT_PRST_IDX		1
100 	#define ISL29035_INT_PRST_CYCLES	4
101 #elif CONFIG_ISL29035_INT_PERSIST_8
102 	#define ISL29035_INT_PRST_IDX		2
103 	#define ISL29035_INT_PRST_CYCLES	8
104 #elif CONFIG_ISL29035_INT_PERSIST_16
105 	#define ISL29035_INT_PRST_IDX		3
106 	#define ISL29035_INT_PRST_CYCLES	16
107 #endif
108 
109 #define ISL29035_INT_PRST_BITS			\
110 	(ISL29035_INT_PRST_IDX << ISL29035_INT_PRST_SHIFT)
111 
112 struct isl29035_driver_data {
113 	uint16_t data_sample;
114 
115 #if CONFIG_ISL29035_TRIGGER
116 	const struct device *dev;
117 	struct gpio_callback gpio_cb;
118 
119 	const struct sensor_trigger *th_trigger;
120 	sensor_trigger_handler_t th_handler;
121 
122 #if defined(CONFIG_ISL29035_TRIGGER_OWN_THREAD)
123 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_ISL29035_THREAD_STACK_SIZE);
124 	struct k_thread thread;
125 	struct k_sem gpio_sem;
126 #elif defined(CONFIG_ISL29035_TRIGGER_GLOBAL_THREAD)
127 	struct k_work work;
128 #endif
129 
130 #endif /* CONFIG_ISL29035_TRIGGER */
131 };
132 
133 struct isl29035_config {
134 	struct i2c_dt_spec i2c;
135 #if CONFIG_ISL29035_TRIGGER
136 	struct gpio_dt_spec int_gpio;
137 #endif
138 };
139 
140 #ifdef CONFIG_ISL29035_TRIGGER
141 int isl29035_attr_set(const struct device *dev,
142 		      enum sensor_channel chan,
143 		      enum sensor_attribute attr,
144 		      const struct sensor_value *val);
145 
146 int isl29035_trigger_set(const struct device *dev,
147 			 const struct sensor_trigger *trig,
148 			 sensor_trigger_handler_t handler);
149 
150 int isl29035_init_interrupt(const struct device *dev);
151 #endif
152 
153 #endif /* ZEPHYR_DRIVERS_SENSOR_ISL29035_ISL29035_H_ */
154