1 /*
2  * Copyright (c) 2017 Intel Corporation
3  * Copyright (c) 2018 PHYTEC Messtechnik GmbH
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #ifndef ZEPHYR_DRIVERS_SENSOR_APDS9960_APDS9960_H_
9 #define ZEPHYR_DRIVERS_SENSOR_APDS9960_APDS9960_H_
10 
11 #include <zephyr/drivers/gpio.h>
12 
13 #define APDS9960_ENABLE_REG		0x80
14 #define APDS9960_ENABLE_GEN		BIT(6)
15 #define APDS9960_ENABLE_PIEN		BIT(5)
16 #define APDS9960_ENABLE_AIEN		BIT(4)
17 #define APDS9960_ENABLE_WEN		BIT(3)
18 #define APDS9960_ENABLE_PEN		BIT(2)
19 #define APDS9960_ENABLE_AEN		BIT(1)
20 #define APDS9960_ENABLE_PON		BIT(0)
21 
22 #define APDS9960_ATIME_REG		0x81
23 #define APDS9960_WTIME_REG		0x83
24 #define APDS9960_INT_AILTL_REG		0x84
25 #define APDS9960_INT_AILTH_REG		0x85
26 #define APDS9960_INT_AIHTL_REG		0x86
27 #define APDS9960_INT_AIHTH_REG		0x87
28 #define APDS9960_PILT_REG		0x89
29 #define APDS9960_PIHT_REG		0x8B
30 
31 #define APDS9960_PERS_REG		0x8C
32 #define APDS9960_PERS_PPERS		(BIT(4) | BIT(5) | BIT(6) | BIT(7))
33 #define APDS9960_APERS_MASK		(BIT(0) | BIT(1) | BIT(2) | BIT(3))
34 
35 #define APDS9960_CONFIG1_REG		0x8D
36 #define APDS9960_CONFIG1_WLONG		BIT(1)
37 
38 #define APDS9960_PPULSE_REG		0x8E
39 #define APDS9960_PPULSE_LENGTH_4US	0
40 #define APDS9960_PPULSE_LENGTH_8US	BIT(6)
41 #define APDS9960_PPULSE_LENGTH_16US	BIT(7)
42 #define APDS9960_PPULSE_LENGTH_32US	(BIT(7) | BIT(6))
43 
44 #define APDS9960_CONTROL_REG		0x8F
45 #define APDS9960_CONTROL_LDRIVE		(BIT(6) | BIT(7))
46 #define APDS9960_CONTROL_PGAIN		(BIT(3) | BIT(2))
47 #define APDS9960_CONTROL_AGAIN		(BIT(0) | BIT(1))
48 /* LED Drive values */
49 #define APDS9960_LED_DRIVE_100MA	0
50 #define APDS9960_LED_DRIVE_50MA		BIT(6)
51 #define APDS9960_LED_DRIVE_25MA		BIT(7)
52 #define APDS9960_LED_DRIVE_12_5MA	(BIT(6) | BIT(7))
53 /* Proximity Gain (PGAIN) values */
54 #define APDS9960_PGAIN_1X		0
55 #define APDS9960_PGAIN_2X		BIT(2)
56 #define APDS9960_PGAIN_4X		BIT(3)
57 #define APDS9960_PGAIN_8X		(BIT(2) | BIT(3))
58 /* ALS Gain (AGAIN) values */
59 #define APDS9960_AGAIN_1X		0
60 #define APDS9960_AGAIN_4X		BIT(0)
61 #define APDS9960_AGAIN_16X		BIT(1)
62 #define APDS9960_AGAIN_64X		(BIT(0) | BIT(1))
63 
64 #define APDS9960_CONFIG2_REG		0x90
65 #define APDS9960_CONFIG2_CPSIEN		BIT(6)
66 #define APDS9960_CONFIG2_PSIEN		BIT(7)
67 /* LED Boost values */
68 #define APDS9960_PLED_BOOST_100		0
69 #define APDS9960_PLED_BOOST_150		BIT(4)
70 #define APDS9960_PLED_BOOST_200		BIT(5)
71 #define APDS9960_PLED_BOOST_300		(BIT(5) | BIT(4))
72 
73 #define APDS9960_ID_REG			0x92
74 /* Acceptable device IDs */
75 #define APDS9960_ID_1			0xAB
76 #define APDS9960_ID_2			0x9C
77 
78 #define APDS9960_STATUS_REG		0x93
79 #define APDS9960_STATUS_CPSAT		BIT(7)
80 #define APDS9960_STATUS_PGSAT		BIT(6)
81 #define APDS9960_STATUS_PINT		BIT(5)
82 #define APDS9960_STATUS_AINT		BIT(4)
83 #define APDS9960_STATUS_GINT		BIT(2)
84 #define APDS9960_STATUS_PVALID		BIT(1)
85 #define APDS9960_STATUS_AVALID		BIT(0)
86 
87 #define APDS9960_CDATAL_REG		0x94
88 #define APDS9960_CDATAH_REG		0x95
89 #define APDS9960_RDATAL_REG		0x96
90 #define APDS9960_RDATAH_REG		0x97
91 #define APDS9960_GDATAL_REG		0x98
92 #define APDS9960_GDATAH_REG		0x99
93 #define APDS9960_BDATAL_REG		0x9A
94 #define APDS9960_BDATAH_REG		0x9B
95 #define APDS9960_PDATA_REG		0x9C
96 
97 #define APDS9960_POFFSET_UR_REG		0x9D
98 #define APDS9960_POFFSET_DL_REG		0x9E
99 
100 #define APDS9960_CONFIG3_REG		0x9F
101 #define APDS9960_CONFIG3_PCMP		BIT(5)
102 #define APDS9960_CONFIG3_SAI		BIT(4)
103 #define APDS9960_CONFIG3_PMSK_U		BIT(3)
104 #define APDS9960_CONFIG3_PMSK_D		BIT(2)
105 #define APDS9960_CONFIG3_PMSK_L		BIT(1)
106 #define APDS9960_CONFIG3_PMSK_R		BIT(0)
107 
108 #define APDS9960_GPENTH_REG		0xA0
109 #define APDS9960_GEXTH_REG		0xA1
110 
111 #define APDS9960_GCONFIG1_REG		0xA2
112 #define APDS9960_GCONFIG1_GFIFOTH	(BIT(7) | BIT(6))
113 #define APDS9960_GCONFIG1_GEXMSK	(BIT(5) | BIT(4) | BIT(3) | BIT(2))
114 #define APDS9960_GCONFIG1_GEXPERS	(BIT(1) | BIT(0))
115 
116 #define APDS9960_GCONFIG2_REG		0xA3
117 #define APDS9960_GCONFIG2_GGAIN		(BIT(6) | BIT(5))
118 #define APDS9960_GCONFIG2_GLDRIVE	(BIT(4) | BIT(3))
119 #define APDS9960_GCONFIG2_WTIME		(BIT(2) | BIT(1) | BIT(0))
120 /* Gesture Gain (GGAIN) values */
121 #define APDS9960_GGAIN_1X		0
122 #define APDS9960_GGAIN_2X		BIT(5)
123 #define APDS9960_GGAIN_4X		BIT(6)
124 #define APDS9960_GGAIN_8X		(BIT(6) | BIT(5))
125 /* Gesture LED Drive Strength values */
126 #define APDS9960_LED_GDRIVE_100MA	0
127 #define APDS9960_LED_GDRIVE_50MA	BIT(3)
128 #define APDS9960_LED_GDRIVE_25MA	BIT(4)
129 #define APDS9960_LED_GDRIVE_12_5MA	(BIT(4) | BIT(3))
130 /* Gesture wait time values */
131 #define APDS9960_GWTIME_0MS		0
132 #define APDS9960_GWTIME_2_8MS		1
133 #define APDS9960_GWTIME_5_6MS		2
134 #define APDS9960_GWTIME_8_4MS		3
135 #define APDS9960_GWTIME_14_0MS		4
136 #define APDS9960_GWTIME_22_4MS		5
137 #define APDS9960_GWTIME_30_8MS		6
138 #define APDS9960_GWTIME_39_2MS		7
139 
140 #define APDS9960_GOFFSET_U_REG		0xA4
141 #define APDS9960_GOFFSET_D_REG		0xA5
142 #define APDS9960_GOFFSET_L_REG		0xA7
143 #define APDS9960_GOFFSET_R_REG		0xA9
144 
145 #define APDS9960_GPULSE_REG		0xA6
146 #define APDS9960_GPULSE_GPLEN		(BIT(7) | BIT(6))
147 #define APDS9960_GPULSE_GPULSE		(BIT(5) | BIT(4) | BIT(3) |\
148 					BIT(2) | BIT(1) | BIT(0))
149 /* Gesture Pulse Length values */
150 #define APDS9960_GPLEN_0US		0
151 #define APDS9960_GPLEN_8US		BIT(6)
152 #define APDS9960_GPLEN_16US		BIT(7)
153 #define APDS9960_GPLEN_32US		(BIT(7) | BIT(6))
154 
155 #define APDS9960_GCONFIG3_REG		0xAA
156 #define APDS9960_GCONFIG3_GDIMS		(BIT(1) | BIT(0))
157 
158 /* Gesture Registers */
159 #define APDS9960_GCONFIG4_REG		0xAB
160 #define APDS9960_GCONFIG4_GFIFO_CLR	BIT(2)
161 #define APDS9960_GCONFIG4_GIEN		BIT(1)
162 #define APDS9960_GCONFIG4_GMODE		BIT(0)
163 
164 #define APDS9960_GFLVL_REG		0xAE
165 
166 #define APDS9960_GSTATUS_REG		0xAF
167 #define APDS9960_GSTATUS_GFOV		BIT(1)
168 #define APDS9960_GSTATUS_GVALID		BIT(0)
169 
170 #define APDS9960_IFORCE_REG		0xE4
171 #define APDS9960_PICLEAR_REG		0xE5
172 #define APDS9960_CICLEAR_REG		0xE6
173 #define APDS9960_AICLEAR_REG		0xE7
174 
175 #define APDS9960_GFIFO_U_REG		0xFC
176 #define APDS9960_GFIFO_D_REG		0xFD
177 #define APDS9960_GFIFO_L_REG		0xFE
178 #define APDS9960_GFIFO_R_REG		0xFF
179 
180 /* Default values */
181 #define APDS9960_DEFAULT_ATIME		219
182 #define APDS9960_DEFAULT_WTIME		255
183 #define APDS9960_DEFAULT_CONFIG1	0x60
184 #define APDS9960_DEFAULT_PERS		BIT(4)
185 #define APDS9960_DEFAULT_CONFIG2	(BIT(6) | BIT(0))
186 #define APDS9960_DEFAULT_GESTURE_PPULSE	0x89
187 #define APDS9960_DEFAULT_POFFSET_UR	0
188 #define APDS9960_DEFAULT_POFFSET_DL	0
189 #define APDS9960_DEFAULT_LDRIVE		APDS9960_LED_DRIVE_100MA
190 
191 #ifdef CONFIG_APDS9960_TRIGGER
192 #define APDS9960_DEFAULT_PILT		0
193 #define APDS9960_DEFAULT_PIHT		50
194 #define APDS9960_DEFAULT_AILT		10
195 #define APDS9960_DEFAULT_AIHT		1000
196 #define APDS9960_DEFAULT_CONFIG3	0
197 #else
198 #define APDS9960_DEFAULT_PILT		0
199 #define APDS9960_DEFAULT_PIHT		1
200 #define APDS9960_DEFAULT_AILT		0xFFFF
201 #define APDS9960_DEFAULT_AIHT		0
202 #define APDS9960_DEFAULT_CONFIG3	APDS9960_CONFIG3_SAI
203 #endif
204 
205 #define APDS9960_DEFAULT_GPENTH		40
206 #define APDS9960_DEFAULT_GEXTH		30
207 #define APDS9960_DEFAULT_GCONF1		0x40
208 #define APDS9960_DEFAULT_GGAIN		APDS9960_GGAIN_4X
209 #define APDS9960_DEFAULT_GLDRIVE	APDS9960_LED_DRIVE_100MA
210 #define APDS9960_DEFAULT_GWTIME		APDS9960_GWTIME_2_8MS
211 #define APDS9960_DEFAULT_GOFFSET	0
212 #define APDS9960_DEFAULT_GPULSE		0xC9
213 #define APDS9960_DEFAULT_GCONF3		0
214 
215 struct apds9960_config {
216 	struct i2c_dt_spec i2c;
217 	struct gpio_dt_spec int_gpio;
218 	uint8_t pgain;
219 	uint8_t again;
220 	uint8_t ppcount;
221 	uint8_t pled_boost;
222 };
223 
224 struct apds9960_data {
225 	struct gpio_callback gpio_cb;
226 	struct k_work work;
227 	const struct device *dev;
228 	uint16_t sample_crgb[4];
229 	uint8_t pdata;
230 
231 #ifdef CONFIG_APDS9960_TRIGGER
232 	sensor_trigger_handler_t p_th_handler;
233 	const struct sensor_trigger *p_th_trigger;
234 #else
235 	struct k_sem data_sem;
236 #endif
237 };
238 
apds9960_setup_int(const struct apds9960_config * cfg,bool enable)239 static inline void apds9960_setup_int(const struct apds9960_config *cfg,
240 				      bool enable)
241 {
242 	unsigned int flags = enable
243 		? GPIO_INT_EDGE_TO_ACTIVE
244 		: GPIO_INT_DISABLE;
245 
246 	gpio_pin_interrupt_configure_dt(&cfg->int_gpio, flags);
247 }
248 
249 #ifdef CONFIG_APDS9960_TRIGGER
250 void apds9960_work_cb(struct k_work *work);
251 
252 int apds9960_attr_set(const struct device *dev,
253 		      enum sensor_channel chan,
254 		      enum sensor_attribute attr,
255 		      const struct sensor_value *val);
256 
257 int apds9960_trigger_set(const struct device *dev,
258 			 const struct sensor_trigger *trig,
259 			 sensor_trigger_handler_t handler);
260 #endif /* CONFIG_APDS9960_TRIGGER */
261 
262 #endif /* ZEPHYR_DRIVERS_SENSOR_APDS9960_APDS9960_H_*/
263