1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "aos/kernel.h"
5 #include "sensor_drv_api.h"
6 #include "sensor_hal.h"
7
8 #define LTR559_I2C_SLAVE_ADDR 0x23
9
10 #define LTR559_ALS_CONTR 0x80
11 #define LTR559_PS_CONTR 0x81 /* PS operation mode */
12 #define LTR559_PS_LED 0x82 /* LED pulse freq, current duty, peak current */
13 #define LTR559_PS_N_PULSES 0x83 /* PS number of pulses */
14 #define LTR559_PS_MEAS_RATE 0x84 /* measurement rate*/
15 #define LTR559_ALS_MEAS_RATE 0x85
16 #define LTR559_PART_ID 0x86
17 #define LTR559_MANUFAC_ID 0x87
18 #define LTR559_ALS_DATA_CH1_0 0x88
19 #define LTR559_ALS_DATA_CH1_1 0x89
20 #define LTR559_ALS_DATA_CH0_0 0x8A
21 #define LTR559_ALS_DATA_CH0_1 0x8B
22 #define LTR559_ALS_PS_STATUS 0x8C
23 #define LTR559_PS_DATA_0 0x8D
24 #define LTR559_PS_DATA_1 0x8E
25 #define LTR559_INTERRUPT 0x8F
26 #define LTR559_PS_THRES_UP_0 0x90 /* PS interrupt upper threshold, lower byte */
27 #define LTR559_PS_THRES_UP_1 0x91 /* PS interrupt upper threshold, upper byte */
28 #define LTR559_PS_THRES_LOW_0 0x92 /* PS interrupt lower threshold, lower byte */
29 #define LTR559_PS_THRES_LOW_1 0x93 /* PS interrupt lower threshold, upper byte */
30 #define LTR559_PS_OFFSET_1 0x94 /* PS offset, upper byte */
31 #define LTR559_PS_OFFSET_0 0x95 /* PS offset, lower byte */
32 #define LTR559_ALS_THRES_UP_0 0x97
33 #define LTR559_ALS_THRES_UP_1 0x98
34 #define LTR559_ALS_THRES_LOW_0 0x99
35 #define LTR559_ALS_THRES_LOW_1 0x9A
36 #define LTR559_INTR_PRST 0x9E /* PS interrupt persist setting */
37
38 #define LTR559_ADDR_TRANS(n) ((n) << 1)
39 #define LTR559_I2C_ADDR LTR559_ADDR_TRANS(LTR559_I2C_SLAVE_ADDR)
40 #define LTR559_PART_ID_VAL 0x92
41 #define LTR559_MANUFAC_ID_VAL 0x05
42
43 #define LTR559_ALS_CONTR_REG_ALS_MODE__POS (0)
44 #define LTR559_ALS_CONTR_REG_ALS_MODE__MSK (0x01)
45 #define LTR559_ALS_CONTR_REG_ALS_MODE__REG (LTR559_ALS_CONTR)
46
47 #define LTR559_ALS_CONTR_REG_SW_RESET__POS (1)
48 #define LTR559_ALS_CONTR_REG_SW_RESET__MSK (0x02)
49 #define LTR559_ALS_CONTR_REG_SW_RESET__REG (LTR559_ALS_CONTR)
50
51 #define LTR559_ALS_CONTR_REG_ALS_GAIN__POS (2)
52 #define LTR559_ALS_CONTR_REG_ALS_GAIN__MSK (0x1C)
53 #define LTR559_ALS_CONTR_REG_ALS_GAIN__REG (LTR559_ALS_CONTR)
54
55 #define LTR559_PS_CONTR_REG_PS_MODE__POS (0)
56 #define LTR559_PS_CONTR_REG_PS_MODE__MSK (0x03)
57 #define LTR559_PS_CONTR_REG_PS_MODE__REG (LTR559_PS_CONTR)
58
59 #define LTR559_PS_CONTR_REG_PS_SAT_IND_EN__POS (5)
60 #define LTR559_PS_CONTR_REG_PS_SAT_IND_EN__MSK (0x20)
61 #define LTR559_PS_CONTR_REG_PS_SAT_IND_EN__REG (LTR559_PS_CONTR)
62
63 #define LTR559_PS_LED_REG_LED_CURR__POS (0)
64 #define LTR559_PS_LED_REG_LED_CURR__MSK (0x07)
65 #define LTR559_PS_LED_REG_LED_CURR__REG (LTR559_PS_LED)
66
67 #define LTR559_PS_LED_REG_LED_DUTY__POS (3)
68 #define LTR559_PS_LED_REG_LED_DUTY__MSK (0x18)
69 #define LTR559_PS_LED_REG_LED_DUTY__REG (LTR559_PS_LED)
70
71 #define LTR559_PS_LED_REG_LED_PULSE__POS (5)
72 #define LTR559_PS_LED_REG_LED_PULSE__MSK (0xE0)
73 #define LTR559_PS_LED_REG_LED_PULSE__REG (LTR559_PS_LED)
74
75 #define LTR559_PS_N_PULSES_REG_PULSES__POS (0)
76 #define LTR559_PS_N_PULSES_REG_PULSES__MSK (0x0F)
77 #define LTR559_PS_N_PULSES_REG_PULSES__REG (LTR559_PS_N_PULSES)
78
79 #define LTR559_PS_MEAS_RATE_REG_MEAS_RPT_RATE__POS (0)
80 #define LTR559_PS_MEAS_RATE_REG_MEAS_RPT_RATE__MSK (0x0F)
81 #define LTR559_PS_MEAS_RATE_REG_MEAS_RPT_RATE__REG (LTR559_PS_MEAS_RATE)
82
83 #define LTR559_ALS_MEAS_RATE_REG_MEAS_RPT_RATE__POS (0)
84 #define LTR559_ALS_MEAS_RATE_REG_MEAS_RPT_RATE__MSK (0x07)
85 #define LTR559_ALS_MEAS_RATE_REG_MEAS_RPT_RATE__REG (LTR559_ALS_MEAS_RATE)
86
87 #define LTR559_ALS_MEAS_RATE_REG_INTEG_TIME__POS (3)
88 #define LTR559_ALS_MEAS_RATE_REG_INTEG_TIME__MSK (0x38)
89 #define LTR559_ALS_MEAS_RATE_REG_INTEG_TIME__REG (LTR559_ALS_MEAS_RATE)
90
91 #define LTR559_ALS_PS_STATUS_REG_PS_DATA_STATUS__POS (0)
92 #define LTR559_ALS_PS_STATUS_REG_PS_DATA_STATUS__MSK (0x01)
93 #define LTR559_ALS_PS_STATUS_REG_PS_DATA_STATUS__REG (LTR559_ALS_PS_STATUS)
94
95 #define LTR559_ALS_PS_STATUS_REG_PS_INT_STATUS__POS (1)
96 #define LTR559_ALS_PS_STATUS_REG_PS_INT_STATUS__MSK (0x02)
97 #define LTR559_ALS_PS_STATUS_REG_PS_INT_STATUS__REG (LTR559_ALS_PS_STATUS)
98
99 #define LTR559_ALS_PS_STATUS_REG_ALS_DATA_STATUS__POS (2)
100 #define LTR559_ALS_PS_STATUS_REG_ALS_DATA_STATUS__MSK (0x04)
101 #define LTR559_ALS_PS_STATUS_REG_ALS_DATA_STATUS__REG (LTR559_ALS_PS_STATUS)
102
103 #define LTR559_ALS_PS_STATUS_REG_ALS_INT_STATUS__POS (3)
104 #define LTR559_ALS_PS_STATUS_REG_ALS_INT_STATUS__MSK (0x08)
105 #define LTR559_ALS_PS_STATUS_REG_ALS_INT_STATUS__REG (LTR559_ALS_PS_STATUS)
106
107 #define LTR559_ALS_PS_STATUS_REG_ALS_GAIN__POS (4)
108 #define LTR559_ALS_PS_STATUS_REG_ALS_GAIN__MSK (0x70)
109 #define LTR559_ALS_PS_STATUS_REG_ALS_GAIN__REG (LTR559_ALS_PS_STATUS)
110
111 #define LTR559_ALS_PS_STATUS_REG_ALS_DATA_VALID__POS (7)
112 #define LTR559_ALS_PS_STATUS_REG_ALS_DATA_VALID__MSK (0x80)
113 #define LTR559_ALS_PS_STATUS_REG_ALS_DATA_VALID__REG (LTR559_ALS_PS_STATUS)
114
115 #define LTR559_INTERRUPT_REG_INT_MODE__POS (0)
116 #define LTR559_INTERRUPT_REG_INT_MODE__MSK (0x03)
117 #define LTR559_INTERRUPT_REG_INT_MODE__REG (LTR559_INTERRUPT)
118
119 #define LTR559_INTERRUPT_REG_INT_POLARITY__POS (2)
120 #define LTR559_INTERRUPT_REG_INT_POLARITY__MSK (0x04)
121 #define LTR559_INTERRUPT_REG_INT_POLARITY__REG (LTR559_INTERRUPT)
122
123 #define LTR559_INTR_PRST_REG_ALS_PERSIST__POS (0)
124 #define LTR559_INTR_PRST_REG_ALS_PERSIST__MSK (0x0F)
125 #define LTR559_INTR_PRST_REG_ALS_PERSIST__REG (LTR559_INTR_PRST)
126
127 #define LTR559_INTR_PRST_REG_PS_PERSIST__POS (4)
128 #define LTR559_INTR_PRST_REG_PS_PERSIST__MSK (0xF0)
129 #define LTR559_INTR_PRST_REG_PS_PERSIST__REG (LTR559_INTR_PRST)
130
131 #define LTR559_GET_BITSLICE(regvar, bitname) ((regvar & LTR559_##bitname##__MSK) >> LTR559_##bitname##__POS)
132 #define LTR559_SET_BITSLICE(regvar, bitname, val) ((regvar & ~LTR559_##bitname##__MSK) | ((val<<LTR559_##bitname##__POS)<R559_##bitname##__MSK))
133
134 #define LTR559_WAIT_TIME_PER_CHECK (10)
135 #define LTR559_WAIT_TIME_TOTAL (100)
136
137 typedef enum {
138 LTR559_ALS_STANDBY = 0x00,
139 LTR559_ALS_ACTIVE = 0x01,
140 } LTR559_CFG_ALS_MODE;
141
142 typedef enum {
143 LTR559_SW_RESET_FALSE = 0x00,
144 LTR559_SW_RESET_TRUE = 0x01,
145 } LTR559_CFG_SW_RESET;
146
147 typedef enum {
148 LTR559_ALS_GAIN_1X = 0x00, /* 1 lux to 64k lux (default) */
149 LTR559_ALS_GAIN_2X = 0x01, /* 0.5 lux to 32k lux */
150 LTR559_ALS_GAIN_4X = 0x02, /* 0.25 lux to 16k lux */
151 LTR559_ALS_GAIN_8X = 0x03, /* 0.125 lux to 8k lux */
152 LTR559_ALS_GAIN_48X = 0x06, /* 0.02 lux to 1.3k lux */
153 LTR559_ALS_GAIN_96X = 0x07, /* 0.01 lux to 600 lux */
154 } LTR559_CFG_ALS_Gain;
155
156 typedef enum {
157 LTR559_PS_STANDBY = 0x00,
158 LTR559_PS_ACTIVE = 0x02,
159 } LTR559_CFG_PS_MODE;
160
161 typedef enum {
162 LTR559_PS_SAT_IND_FALSE = 0x00,
163 LTR559_PS_SAT_IND_TRUE = 0x01,
164 } LTR559_CFG_PS_SAT_IND_EN;
165
166 typedef enum {
167 LTR559_LED_PEAK_CURRENT_5 = 0x00, /* LED pulse current level = 5mA */
168 LTR559_LED_PEAK_CURRENT_10 = 0x01, /* LED pulse current level = 10mA */
169 LTR559_LED_PEAK_CURRENT_20 = 0x02, /* LED pulse current level = 20mA */
170 LTR559_LED_PEAK_CURRENT_50 = 0x03, /* LED pulse current level = 50mA */
171 LTR559_LED_PEAK_CURRENT_100 = 0x04, /* LED pulse current level = 100mA (default) */
172 } LTR559_CFG_LED_PEAK_CURRENT;
173
174 typedef enum {
175 LTR559_LED_DUTY_CYCLE_25PCT = 0x00, /* Duty = 25% */
176 LTR559_LED_DUTY_CYCLE_50PCT = 0x01, /* Duty = 50% */
177 LTR559_LED_DUTY_CYCLE_75PCT = 0x02, /* Duty = 75% */
178 LTR559_LED_DUTY_CYCLE_100PCT = 0x03, /* Duty = 100% (default) */
179 } LTR559_CFG_LED_DUTY_CYCLE;
180
181 typedef enum {
182 LTR559_LED_PULSE_30kHZ = 0x00, /* LED pulse period = 30kHz */
183 LTR559_LED_PULSE_40kHZ = 0x01, /* LED pulse period = 40kHz */
184 LTR559_LED_PULSE_50kHZ = 0x02, /* LED pulse period = 50kHz */
185 LTR559_LED_PULSE_60kHZ = 0x03, /* LED pulse period = 60kHz */
186 LTR559_LED_PULSE_70kHZ = 0x04, /* LED pulse period = 70kHz */
187 LTR559_LED_PULSE_80kHZ = 0x05, /* LED pulse period = 80kHz */
188 LTR559_LED_PULSE_90kHZ = 0x06, /* LED pulse period = 90kHz */
189 LTR559_LED_PULSE_100kHZ = 0x07, /* LED pulse period = 100kHz */
190 } LTR559_CFG_LED_PULSE_FREQUENCY;
191
192 typedef enum {
193 LTR559_PS_MEAS_RATE_50 = 0x00, /* PS Measurement Repeat Rate = 50ms */
194 LTR559_PS_MEAS_RATE_70 = 0x01, /* PS Measurement Repeat Rate = 70ms */
195 LTR559_PS_MEAS_RATE_100 = 0x02, /* PS Measurement Repeat Rate = 100ms (default) */
196 LTR559_PS_MEAS_RATE_200 = 0x03, /* PS Measurement Repeat Rate = 20ms */
197 LTR559_PS_MEAS_RATE_500 = 0x04, /* PS Measurement Repeat Rate = 500ms */
198 LTR559_PS_MEAS_RATE_1000 = 0x05, /* PS Measurement Repeat Rate = 1000ms */
199 LTR559_PS_MEAS_RATE_2000 = 0x06, /* PS Measurement Repeat Rate = 2000ms */
200 LTR559_PS_MEAS_RATE_10 = 0x08, /* PS Measurement Repeat Rate = 10ms */
201 } LTR559_CFG_PS_MEAS_RPT_RATE;
202
203 typedef enum {
204 LTR559_ALS_MEAS_RATE_50 = 0x00, /* ALS Measurement Repeat Rate = 50ms */
205 LTR559_ALS_MEAS_RATE_100 = 0x01, /* ALS Measurement Repeat Rate = 100ms */
206 LTR559_ALS_MEAS_RATE_200 = 0x02, /* ALS Measurement Repeat Rate = 200ms */
207 LTR559_ALS_MEAS_RATE_500 = 0x03, /* ALS Measurement Repeat Rate = 500ms (default) */
208 LTR559_ALS_MEAS_RATE_1000 = 0x04, /* ALS Measurement Repeat Rate = 1000ms */
209 LTR559_ALS_MEAS_RATE_2000 = 0x05, /* ALS Measurement Repeat Rate = 2000ms */
210 } LTR559_CFG_ALS_MEAS_RATE;
211
212 typedef enum {
213 LTR559_ALS_INTEG_TIME_100 = 0x00, /* ALS Integration Time = 100ms (default) */
214 LTR559_ALS_INTEG_TIME_50 = 0x01, /* ALS Integration Time = 50ms */
215 LTR559_ALS_INTEG_TIME_200 = 0x02, /* ALS Integration Time = 200ms */
216 LTR559_ALS_INTEG_TIME_400 = 0x03, /* ALS Integration Time = 400ms */
217 LTR559_ALS_INTEG_TIME_150 = 0x04, /* ALS Integration Time = 150ms */
218 LTR559_ALS_INTEG_TIME_250 = 0x05, /* ALS Integration Time = 250ms */
219 LTR559_ALS_INTEG_TIME_300 = 0x06, /* ALS Integration Time = 300ms */
220 LTR559_ALS_INTEG_TIME_350 = 0x07, /* ALS Integration Time = 350ms */
221 } LTR559_CFG_ALS_INTEG_TIME;
222
223 typedef enum {
224 LTR559_PS_DATA_STATUS_OLD = 0x00,
225 LTR559_PS_DATA_STATUS_NEW = 0x01,
226 } LTR559_CFG_PS_DATA_STATUS;
227
228 typedef enum {
229 LTR559_PS_INT_STATUS_INACTIVE = 0x00,
230 LTR559_PS_INT_STATUS_ACTIVE = 0x01,
231 } LTR559_CFG_PS_INT_STATUS;
232
233 typedef enum {
234 LTR559_ALS_DATA_STATUS_OLD = 0x00,
235 LTR559_ALS_DATA_STATUS_NEW = 0x01,
236 } LTR559_CFG_ALS_DATA_STATUS;
237
238 typedef enum {
239 LTR559_ALS_INT_STATUS_INACTIVE = 0x00,
240 LTR559_ALS_INT_STATUS_ACTIVE = 0x01,
241 } LTR559_CFG_ALS_INT_STATUS;
242
243 typedef enum {
244 LTR559_ALS_DATA_VALID = 0x00,
245 LTR559_ALS_DATA_INVALID = 0x01,
246 } LTR559_CFG_ALS_DATA_VALID_STATUS;
247
248 typedef enum {
249 LTR559_INT_MODE_DISABLE = 0x00,
250 LTR559_INT_MODE_PS = 0x01,
251 LTR559_INT_MODE_ALS = 0x02,
252 LTR559_INT_MODE_PS_ALS = 0x03,
253 } LTR559_CFG_INT_MODE;
254
255 typedef enum {
256 LTR559_INT_POLARITY_ACTIVE_LO = 0x00,
257 LTR559_INT_POLARITY_ACTIVE_HI = 0x01,
258 } LTR559_CFG_INT_POLARITY;
259
260 typedef enum {
261 LTR559_FLAG_INIT_ALS = 0,
262 LTR559_FLAG_INIT_PS,
263 } LTR559_FLAG_INIT_BIT;
264
265 i2c_dev_t ltr559_ctx = {
266 .port = 3,
267 .config.address_width = 8,
268 .config.freq = 100000,
269 .config.dev_addr = LTR559_I2C_ADDR,
270 };
271
272 static uint8_t g_init_bitwise = 0;
273
drv_als_ps_liteon_ltr559_validate_id(i2c_dev_t * drv,uint8_t part_id,uint8_t manufac_id)274 static int drv_als_ps_liteon_ltr559_validate_id(i2c_dev_t* drv, uint8_t part_id, uint8_t manufac_id)
275 {
276 int ret = 0;
277 uint8_t part_id_value = 0;
278 uint8_t manufac_id_value = 0;
279
280 if (drv == NULL) {
281 return -1;
282 }
283
284 ret = sensor_i2c_read(drv, LTR559_PART_ID, &part_id_value, I2C_DATA_LEN, I2C_OP_RETRIES);
285 if (unlikely(ret)) {
286 return ret;
287 }
288
289 ret = sensor_i2c_read(drv, LTR559_MANUFAC_ID, &manufac_id_value, I2C_DATA_LEN, I2C_OP_RETRIES);
290 if (unlikely(ret)) {
291 return ret;
292 }
293
294 if (part_id_value != part_id || manufac_id_value != manufac_id) {
295 return -1;
296 }
297
298 return 0;
299 }
300
drv_als_liteon_ltr559_set_power_mode(i2c_dev_t * drv,dev_power_mode_e mode)301 static int drv_als_liteon_ltr559_set_power_mode(i2c_dev_t* drv, dev_power_mode_e mode)
302 {
303 int ret = 0;
304 uint8_t dev_mode = 0;
305 uint8_t value = 0;
306
307 ret = sensor_i2c_read(drv, LTR559_ALS_CONTR, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
308 if (unlikely(ret)) {
309 return ret;
310 }
311
312 switch (mode) {
313 case DEV_POWER_OFF:
314 case DEV_SLEEP:
315 dev_mode = LTR559_SET_BITSLICE(value, ALS_CONTR_REG_ALS_MODE, LTR559_ALS_STANDBY);
316 break;
317 case DEV_POWER_ON:
318 dev_mode = LTR559_SET_BITSLICE(value, ALS_CONTR_REG_ALS_MODE, LTR559_ALS_ACTIVE);
319 break;
320 default:
321 return -1;
322 }
323
324 ret = sensor_i2c_write(drv, LTR559_ALS_CONTR, &dev_mode, I2C_DATA_LEN, I2C_OP_RETRIES);
325 if (unlikely(ret)) {
326 return ret;
327 }
328
329 return 0;
330 }
331
drv_ps_liteon_ltr559_set_power_mode(i2c_dev_t * drv,dev_power_mode_e mode)332 static int drv_ps_liteon_ltr559_set_power_mode(i2c_dev_t* drv, dev_power_mode_e mode)
333 {
334 int ret = 0;
335 uint8_t dev_mode = 0;
336 uint8_t value = 0;
337
338 ret = sensor_i2c_read(drv, LTR559_PS_CONTR, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
339 if (unlikely(ret)) {
340 return ret;
341 }
342
343 switch (mode) {
344 case DEV_POWER_OFF:
345 case DEV_SLEEP:
346 dev_mode = LTR559_SET_BITSLICE(value, PS_CONTR_REG_PS_MODE, LTR559_PS_STANDBY);
347 break;
348 case DEV_POWER_ON:
349 dev_mode = LTR559_SET_BITSLICE(value, PS_CONTR_REG_PS_MODE, LTR559_PS_ACTIVE);
350 break;
351 default:
352 return -1;
353 }
354
355 ret = sensor_i2c_write(drv, LTR559_PS_CONTR, &dev_mode, I2C_DATA_LEN, I2C_OP_RETRIES);
356 if (unlikely(ret)) {
357 return ret;
358 }
359
360 return 0;
361 }
362
drv_als_liteon_ltr559_is_ready(i2c_dev_t * drv)363 UNUSED static int drv_als_liteon_ltr559_is_ready(i2c_dev_t* drv)
364 {
365 int ret = 0;
366 uint8_t value = 0;
367
368 ret = sensor_i2c_read(drv, LTR559_ALS_PS_STATUS, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
369 if (unlikely(ret)) {
370 return 0;
371 }
372
373 ret = (LTR559_GET_BITSLICE(value, ALS_PS_STATUS_REG_ALS_DATA_STATUS) == LTR559_ALS_DATA_STATUS_NEW) ? 1 : 0;
374
375 return ret;
376 }
377
drv_ps_liteon_ltr559_is_ready(i2c_dev_t * drv)378 UNUSED static int drv_ps_liteon_ltr559_is_ready(i2c_dev_t* drv)
379 {
380 int ret = 0;
381 uint8_t value = 0;
382
383 ret = sensor_i2c_read(drv, LTR559_ALS_PS_STATUS, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
384 if (unlikely(ret)) {
385 return 0;
386 }
387
388 ret = (LTR559_GET_BITSLICE(value, ALS_PS_STATUS_REG_PS_DATA_STATUS) == LTR559_PS_DATA_STATUS_NEW) ? 1 : 0;
389
390 return ret;
391 }
392
drv_als_liteon_ltr559_set_default_config(i2c_dev_t * drv)393 UNUSED static int drv_als_liteon_ltr559_set_default_config(i2c_dev_t* drv)
394 {
395 int ret = 0;
396 uint8_t value = 0;
397
398 ret = sensor_i2c_read(drv, LTR559_ALS_CONTR, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
399 if (unlikely(ret)) {
400 return ret;
401 }
402 value = LTR559_SET_BITSLICE(value, ALS_CONTR_REG_ALS_GAIN, LTR559_ALS_GAIN_1X);
403 ret = sensor_i2c_write(drv, LTR559_ALS_CONTR, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
404 if (unlikely(ret)) {
405 return ret;
406 }
407
408 value = 0;
409 value = LTR559_SET_BITSLICE(value, ALS_MEAS_RATE_REG_MEAS_RPT_RATE, LTR559_ALS_MEAS_RATE_500);
410 value = LTR559_SET_BITSLICE(value, ALS_MEAS_RATE_REG_INTEG_TIME, LTR559_ALS_INTEG_TIME_100);
411 ret = sensor_i2c_write(drv, LTR559_ALS_MEAS_RATE, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
412 if (unlikely(ret)) {
413 return ret;
414 }
415
416 return 0;
417 }
418
drv_ps_liteon_ltr559_set_default_config(i2c_dev_t * drv)419 static int drv_ps_liteon_ltr559_set_default_config(i2c_dev_t* drv)
420 {
421 int ret = 0;
422 uint8_t value = 0;
423
424 ret = sensor_i2c_read(drv, LTR559_PS_CONTR, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
425 if (unlikely(ret)) {
426 return ret;
427 }
428 value = LTR559_SET_BITSLICE(value, PS_CONTR_REG_PS_SAT_IND_EN, LTR559_PS_SAT_IND_FALSE);
429 ret = sensor_i2c_write(drv, LTR559_PS_CONTR, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
430 if (unlikely(ret)) {
431 return ret;
432 }
433
434 value = 0;
435 value = LTR559_SET_BITSLICE(value, PS_LED_REG_LED_CURR, LTR559_LED_PEAK_CURRENT_100);
436 value = LTR559_SET_BITSLICE(value, PS_LED_REG_LED_DUTY, LTR559_LED_DUTY_CYCLE_100PCT);
437 value = LTR559_SET_BITSLICE(value, PS_LED_REG_LED_PULSE, LTR559_LED_PULSE_60kHZ);
438 ret = sensor_i2c_write(drv, LTR559_PS_LED, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
439 if (unlikely(ret)) {
440 return ret;
441 }
442
443 value = 0;
444 value = LTR559_SET_BITSLICE(value, PS_N_PULSES_REG_PULSES, 4);
445 ret = sensor_i2c_write(drv, LTR559_PS_N_PULSES, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
446 if (unlikely(ret)) {
447 return ret;
448 }
449
450 value = 0;
451 value = LTR559_SET_BITSLICE(value, PS_MEAS_RATE_REG_MEAS_RPT_RATE, LTR559_PS_MEAS_RATE_100);
452 ret = sensor_i2c_write(drv, LTR559_PS_MEAS_RATE, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
453 if (unlikely(ret)) {
454 return ret;
455 }
456
457 return 0;
458 }
459
drv_als_liteon_ltr559_irq_handle(void)460 static void drv_als_liteon_ltr559_irq_handle(void)
461 {
462 /* no handle so far */
463 }
464
drv_ps_liteon_ltr559_irq_handle(void)465 static void drv_ps_liteon_ltr559_irq_handle(void)
466 {
467 /* no handle so far */
468 }
469
drv_als_liteon_ltr559_open(void)470 static int drv_als_liteon_ltr559_open(void)
471 {
472 int ret = 0;
473
474 ret = drv_als_liteon_ltr559_set_power_mode(<r559_ctx, DEV_POWER_ON);
475 if (unlikely(ret)) {
476 return -1;
477 }
478
479 LOG("%s %s successfully \n", SENSOR_STR, __func__);
480 return 0;
481
482 }
483
drv_ps_liteon_ltr559_open(void)484 static int drv_ps_liteon_ltr559_open(void)
485 {
486 int ret = 0;
487
488 ret = drv_ps_liteon_ltr559_set_power_mode(<r559_ctx, DEV_POWER_ON);
489 if (unlikely(ret)) {
490 return -1;
491 }
492
493 LOG("%s %s successfully \n", SENSOR_STR, __func__);
494 return 0;
495
496 }
497
drv_als_liteon_ltr559_close(void)498 static int drv_als_liteon_ltr559_close(void)
499 {
500 int ret = 0;
501
502 ret = drv_als_liteon_ltr559_set_power_mode(<r559_ctx, DEV_POWER_OFF);
503 if (unlikely(ret)) {
504 return -1;
505 }
506
507 LOG("%s %s successfully \n", SENSOR_STR, __func__);
508 return 0;
509
510 }
511
drv_ps_liteon_ltr559_close(void)512 static int drv_ps_liteon_ltr559_close(void)
513 {
514 int ret = 0;
515
516 ret = drv_ps_liteon_ltr559_set_power_mode(<r559_ctx, DEV_POWER_OFF);
517 if (unlikely(ret)) {
518 return -1;
519 }
520
521 LOG("%s %s successfully \n", SENSOR_STR, __func__);
522 return 0;
523 }
524
drv_als_liteon_ltr559_get_gain_val(i2c_dev_t * drv)525 static uint8_t drv_als_liteon_ltr559_get_gain_val(i2c_dev_t* drv)
526 {
527 uint8_t als_gain = 0, als_gain_val = 0;
528 bool ret = false;
529
530 ret = sensor_i2c_read(drv, LTR559_ALS_PS_STATUS, &als_gain, I2C_DATA_LEN, I2C_OP_RETRIES);
531 if (!unlikely(ret))
532 {
533 als_gain = LTR559_GET_BITSLICE(als_gain, ALS_PS_STATUS_REG_ALS_GAIN);
534 switch (als_gain)
535 {
536 case LTR559_ALS_GAIN_1X:
537 als_gain_val = 1;
538 break;
539 case LTR559_ALS_GAIN_2X:
540 als_gain_val = 2;
541 break;
542 case LTR559_ALS_GAIN_4X:
543 als_gain_val = 4;
544 break;
545 case LTR559_ALS_GAIN_8X:
546 als_gain_val = 8;
547 break;
548 case LTR559_ALS_GAIN_48X:
549 als_gain_val = 48;
550 break;
551 case LTR559_ALS_GAIN_96X:
552 als_gain_val = 96;
553 break;
554 default:
555 als_gain_val = 1;
556 break;
557 }
558 }
559 else
560 {
561 als_gain_val = 0;
562 }
563
564 return als_gain_val;
565 }
566
drv_als_liteon_ltr559_get_integ_time_val(i2c_dev_t * drv)567 static uint16_t drv_als_liteon_ltr559_get_integ_time_val(i2c_dev_t* drv)
568 {
569 uint16_t als_integ = 0, als_integ_val = 0;
570 bool ret = false;
571
572 ret = sensor_i2c_read(drv, LTR559_ALS_MEAS_RATE, (uint8_t*)&als_integ, I2C_DATA_LEN, I2C_OP_RETRIES);
573 if (!unlikely(ret))
574 {
575 als_integ = LTR559_GET_BITSLICE(als_integ, ALS_MEAS_RATE_REG_INTEG_TIME);
576 switch (als_integ)
577 {
578 case LTR559_ALS_INTEG_TIME_100:
579 als_integ_val = 10;
580 break;
581 case LTR559_ALS_INTEG_TIME_50:
582 als_integ_val = 5;
583 break;
584 case LTR559_ALS_INTEG_TIME_200:
585 als_integ_val = 20;
586 break;
587 case LTR559_ALS_INTEG_TIME_400:
588 als_integ_val = 40;
589 break;
590 case LTR559_ALS_INTEG_TIME_150:
591 als_integ_val = 15;
592 break;
593 case LTR559_ALS_INTEG_TIME_250:
594 als_integ_val = 25;
595 break;
596 case LTR559_ALS_INTEG_TIME_300:
597 als_integ_val = 30;
598 break;
599 case LTR559_ALS_INTEG_TIME_350:
600 als_integ_val = 35;
601 break;
602 default:
603 als_integ_val = 10;
604 break;
605 }
606 }
607 else
608 {
609 als_integ_val = 0;
610 }
611
612 return als_integ_val;
613 }
614
drv_als_liteon_ltr559_read(void * buf,size_t len)615 static int drv_als_liteon_ltr559_read(void *buf, size_t len)
616 {
617 int ret = 0;
618 size_t size;
619 uint8_t reg_ch1_data[2] = { 0 };
620 uint8_t reg_ch0_data[2] = { 0 };
621 uint32_t als_ch0_data = 0, als_ch1_data = 0, chRatio = 0, tmpCalc = 0;
622 uint16_t als_gain_val = 0, als_integ_time_val = 0;
623 als_data_t * pdata = (als_data_t *) buf;
624
625 if (buf == NULL){
626 return -1;
627 }
628
629 size = sizeof(als_data_t);
630 if (len < size){
631 return -1;
632 }
633
634
635 ret = sensor_i2c_read(<r559_ctx, LTR559_ALS_DATA_CH1_0, ®_ch1_data[0], I2C_DATA_LEN, I2C_OP_RETRIES);
636 if (unlikely(ret)) {
637 return -1;
638 }
639 ret = sensor_i2c_read(<r559_ctx, LTR559_ALS_DATA_CH1_1, ®_ch1_data[1], I2C_DATA_LEN, I2C_OP_RETRIES);
640 if (unlikely(ret)) {
641 return -1;
642 }
643 ret = sensor_i2c_read(<r559_ctx, LTR559_ALS_DATA_CH0_0, ®_ch0_data[0], I2C_DATA_LEN, I2C_OP_RETRIES);
644 if (unlikely(ret)) {
645 return -1;
646 }
647 ret = sensor_i2c_read(<r559_ctx, LTR559_ALS_DATA_CH0_1, ®_ch0_data[1], I2C_DATA_LEN, I2C_OP_RETRIES);
648 if (unlikely(ret)) {
649 return -1;
650 }
651
652 als_ch0_data = ((uint16_t) reg_ch0_data[1] << 8 | reg_ch0_data[0]);
653 als_ch1_data = ((uint16_t) reg_ch1_data[1] << 8 | reg_ch1_data[0]);
654
655 chRatio = (als_ch1_data * 100) / (als_ch0_data + als_ch1_data);
656 if (chRatio < 45)
657 {
658 tmpCalc = (1774 * als_ch0_data + 1106 * als_ch1_data);
659 }
660 else if (chRatio >= 45 && chRatio < 64)
661 {
662 tmpCalc = (4279 * als_ch0_data - 1955 * als_ch1_data);
663 }
664 else if (chRatio >= 64 && chRatio < 85)
665 {
666 tmpCalc = (593 * als_ch0_data + 119 * als_ch1_data);
667 }
668 else
669 {
670 tmpCalc = 0;
671 }
672
673 als_gain_val = drv_als_liteon_ltr559_get_gain_val(<r559_ctx);
674 als_integ_time_val = drv_als_liteon_ltr559_get_integ_time_val(<r559_ctx);
675 if ((als_gain_val != 0) && (als_integ_time_val != 0))
676 {
677 pdata->lux = tmpCalc / als_gain_val / als_integ_time_val / 100;
678 }
679 else
680 {
681 pdata->lux = 0;
682 }
683 pdata->timestamp = aos_now_ms();
684
685 return (int) size;
686 }
687
drv_ps_liteon_ltr559_read(void * buf,size_t len)688 static int drv_ps_liteon_ltr559_read(void *buf, size_t len)
689 {
690 int ret = 0;
691 size_t size;
692 uint8_t reg_data[2] = { 0 };
693 proximity_data_t * pdata = (proximity_data_t *) buf;
694
695 if (buf == NULL) {
696 return -1;
697 }
698
699 size = sizeof(proximity_data_t);
700 if (len < size) {
701 return -1;
702 }
703
704 ret = sensor_i2c_read(<r559_ctx, LTR559_PS_DATA_0, ®_data[0], I2C_DATA_LEN, I2C_OP_RETRIES);
705 if (unlikely(ret)) {
706 return -1;
707 }
708 ret = sensor_i2c_read(<r559_ctx, LTR559_PS_DATA_1, ®_data[1], I2C_DATA_LEN, I2C_OP_RETRIES);
709 if (unlikely(ret)) {
710 return -1;
711 }
712
713 pdata->present = (((uint32_t) (reg_data[1] & 0x07) << 8) | reg_data[0]);
714 pdata->timestamp = aos_now_ms();
715
716 return (int) size;
717 }
718
drv_als_liteon_ltr559_write(const void * buf,size_t len)719 static int drv_als_liteon_ltr559_write(const void *buf, size_t len)
720 {
721 (void) buf;
722 (void) len;
723
724 return 0;
725 }
726
drv_ps_liteon_ltr559_write(const void * buf,size_t len)727 static int drv_ps_liteon_ltr559_write(const void *buf, size_t len)
728 {
729 (void) buf;
730 (void) len;
731
732 return 0;
733 }
734
drv_als_liteon_ltr559_ioctl(int cmd,unsigned long arg)735 static int drv_als_liteon_ltr559_ioctl(int cmd, unsigned long arg)
736 {
737 int ret = 0;
738
739 switch (cmd) {
740 case SENSOR_IOCTL_SET_POWER: {
741 ret = drv_als_liteon_ltr559_set_power_mode(<r559_ctx, arg);
742 if (unlikely(ret)) {
743 return -1;
744 }
745 } break;
746 case SENSOR_IOCTL_GET_INFO: {
747 /* fill the dev info here */
748 dev_sensor_info_t *info = (dev_sensor_info_t *) arg;
749 info->vendor = DEV_SENSOR_VENDOR_LITEON;
750 info->model = "LTR559";
751 info->unit = lux;
752 } break;
753 default:
754 return -1;
755 }
756
757 LOG("%s %s successfully \n", SENSOR_STR, __func__);
758 return 0;
759 }
760
drv_ps_liteon_ltr559_ioctl(int cmd,unsigned long arg)761 static int drv_ps_liteon_ltr559_ioctl(int cmd, unsigned long arg)
762 {
763 int ret = 0;
764
765 switch (cmd) {
766 case SENSOR_IOCTL_SET_POWER: {
767 ret = drv_ps_liteon_ltr559_set_power_mode(<r559_ctx, arg);
768 if (unlikely(ret)) {
769 return -1;
770 }
771 } break;
772 case SENSOR_IOCTL_GET_INFO: {
773 /* fill the dev info here */
774 dev_sensor_info_t *info = (dev_sensor_info_t *) arg;
775 info->vendor = DEV_SENSOR_VENDOR_LITEON;
776 info->model = "LTR559";
777 info->unit = cm;
778 } break;
779 default:
780 return -1;
781 }
782
783 LOG("%s %s successfully \n", SENSOR_STR, __func__);
784 return 0;
785 }
786
drv_als_liteon_ltr559_init(void)787 int drv_als_liteon_ltr559_init(void)
788 {
789 int ret = 0;
790 sensor_obj_t sensor_als;
791
792 memset(&sensor_als, 0, sizeof(sensor_als));
793
794 if (!g_init_bitwise) {
795 ret = drv_als_ps_liteon_ltr559_validate_id(<r559_ctx, LTR559_PART_ID_VAL, LTR559_MANUFAC_ID_VAL);
796 if (unlikely(ret)) {
797 return -1;
798 }
799 }
800
801 if (!(g_init_bitwise & (1 << LTR559_FLAG_INIT_ALS))) {
802 /* fill the sensor_ps obj parameters here */
803 sensor_als.tag = TAG_DEV_ALS;
804 sensor_als.path = dev_als_path;
805 sensor_als.io_port = I2C_PORT;
806 sensor_als.mode = DEV_POLLING;
807 sensor_als.power = DEV_POWER_OFF;
808 sensor_als.open = drv_als_liteon_ltr559_open;
809 sensor_als.close = drv_als_liteon_ltr559_close;
810 sensor_als.read = drv_als_liteon_ltr559_read;
811 sensor_als.write = drv_als_liteon_ltr559_write;
812 sensor_als.ioctl = drv_als_liteon_ltr559_ioctl;
813 sensor_als.irq_handle = drv_als_liteon_ltr559_irq_handle;
814
815 ret = sensor_create_obj(&sensor_als);
816 if (unlikely(ret)) {
817 return -1;
818 }
819
820 ret = drv_ps_liteon_ltr559_set_default_config(<r559_ctx);
821 if (unlikely(ret)) {
822 return -1;
823 }
824
825 g_init_bitwise |= 1 << LTR559_FLAG_INIT_ALS;
826 }
827
828 LOG("%s %s successfully \n", SENSOR_STR, __func__);
829 return 0;
830 }
831
drv_ps_liteon_ltr559_init(void)832 int drv_ps_liteon_ltr559_init(void)
833 {
834 int ret = 0;
835 sensor_obj_t sensor_ps;
836
837 memset(&sensor_ps, 0, sizeof(sensor_ps));
838
839 if (!g_init_bitwise) {
840 ret = drv_als_ps_liteon_ltr559_validate_id(<r559_ctx, LTR559_PART_ID_VAL, LTR559_MANUFAC_ID_VAL);
841 if (unlikely(ret)) {
842 return -1;
843 }
844 }
845
846 if (!(g_init_bitwise & (1 << LTR559_FLAG_INIT_PS))) {
847 /* fill the sensor_ps obj parameters here */
848 sensor_ps.tag = TAG_DEV_PS;
849 sensor_ps.path = dev_ps_path;
850 sensor_ps.io_port = I2C_PORT;
851 sensor_ps.mode = DEV_POLLING;
852 sensor_ps.power = DEV_POWER_OFF;
853 sensor_ps.open = drv_ps_liteon_ltr559_open;
854 sensor_ps.close = drv_ps_liteon_ltr559_close;
855 sensor_ps.read = drv_ps_liteon_ltr559_read;
856 sensor_ps.write = drv_ps_liteon_ltr559_write;
857 sensor_ps.ioctl = drv_ps_liteon_ltr559_ioctl;
858 sensor_ps.irq_handle = drv_ps_liteon_ltr559_irq_handle;
859
860 ret = sensor_create_obj(&sensor_ps);
861 if (unlikely(ret)) {
862 return -1;
863 }
864
865 ret = drv_ps_liteon_ltr559_set_default_config(<r559_ctx);
866 if (unlikely(ret)) {
867 return -1;
868 }
869
870 g_init_bitwise |= 1 << LTR559_FLAG_INIT_PS;
871 }
872
873 LOG("%s %s successfully \n", SENSOR_STR, __func__);
874 return 0;
875 }
876
877 SENSOR_DRV_ADD(drv_als_liteon_ltr559_init);
878 SENSOR_DRV_ADD(drv_ps_liteon_ltr559_init);
879
880
881