1 /*
2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3 *
4 *
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "aos/kernel.h"
12 #include "ulog/ulog.h"
13 #include "sensor_drv_api.h"
14 #include "sensor_hal.h"
15
16 #define TCS3400_ENABLE 0x80
17 #define TCS3400_ALS_TIME 0x81
18 #define TCS3400_WAIT_TIME 0x83
19 #define TCS3400_ALS_MINTHRESHLO 0x84
20 #define TCS3400_ALS_MINTHRESHHI 0x85
21 #define TCS3400_ALS_MAXTHRESHLO 0x86
22 #define TCS3400_ALS_MAXTHRESHHI 0x87
23 #define TCS3400_PERSISTENCE 0x8C
24 #define TCS3400_CONFIG 0x8D
25 #define TCS3400_CONTROL 0x8F
26 #define TCS3400_REG_AUX 0x90
27 #define TCS3400_REVID 0x91
28 #define TCS3400_CHIPID 0x92
29 #define TCS3400_STATUS 0x93
30 #define TCS3400_CLR_CHANLO 0x94
31 #define TCS3400_CLR_CHANHI 0x95
32 #define TCS3400_RED_CHANLO 0x96
33 #define TCS3400_RED_CHANHI 0x97
34 #define TCS3400_GRN_CHANLO 0x98
35 #define TCS3400_GRN_CHANHI 0x99
36 #define TCS3400_BLU_CHANLO 0x9A
37 #define TCS3400_BLU_CHANHI 0x9B
38 #define TCS3400_IR_TOGGLE 0xC0
39 #define TCS3400_IFORCE 0xE4
40 #define TCS3400_CLCLEAR 0xE6
41 #define TCS3400_AICLEAR 0xE7
42
43 #define TCS3400_I2C_SLAVE_ADDR 0x29
44 #define TCS3400_ADDR_TRANS(n) ((n) << 1)
45 #define TCS3400_I2C_ADDR TCS3400_ADDR_TRANS(TCS3400_I2C_SLAVE_ADDR)
46 #define TCS3400_CHIPID_VALUE 0x90 // 0x90 or 0x93
47
48 #define MAX_REGS 256
49 #define INTEGRATION_CYCLE 2780
50
51 #define TCS3400_MASK_AGAIN 0x03
52 #define TCS3400_MAX_LUX 0xffff
53 #define TCS3400_MAX_ALS_VALUE 0xffff
54 #define TCS3400_MIN_ALS_VALUE 3
55
56 #define DGF 783
57 #define R_Coef 50
58 #define G_Coef 530
59 #define B_Coef -230
60
61 #define CT_Coef 3647
62 #define CT_Offset 1319
63
64 #define TCS3400_CMD_ALS_INT_CLR 0xE6
65 #define TCS3400_CMD_ALL_INT_CLR 0xE7
66
67 #define GAIN1 0
68 #define GAIN4 1
69 #define GAIN16 2
70 #define GAIN64 3
71
72 #define ALS_PERSIST(p) (((p)&0xf) << 3)
73 #define AW_TIME_MS(p) \
74 ((((p)*1000) + (INTEGRATION_CYCLE - 1)) / INTEGRATION_CYCLE)
75 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
76
77 struct tcs3400_als_info
78 {
79 uint32_t sat;
80 uint32_t cpl;
81 uint16_t clear;
82 uint16_t red;
83 uint16_t green;
84 uint16_t blue;
85 int16_t ir_data;
86 uint16_t lux;
87 uint16_t cct;
88 };
89
90 enum tcs3400_pwr_state
91 {
92 POWER_ON,
93 POWER_OFF,
94 POWER_STANDBY,
95 };
96
97 enum tcs3400_ctrl_reg
98 {
99 AGAIN_1 = (0 << 0),
100 AGAIN_4 = (1 << 0),
101 AGAIN_16 = (2 << 0),
102 AGAIN_64 = (3 << 0),
103 };
104
105 enum tcs3400_en_reg
106 {
107 TCS3400_EN_PWR_ON = (1 << 0),
108 TCS3400_EN_ALS = (1 << 1),
109 TCS3400_EN_WAIT = (1 << 3),
110 TCS3400_EN_ALS_IRQ = (1 << 4),
111 TCS3400_EN_ALS_SAT_IRQ = (1 << 5),
112 TCS3400_EN_IRQ_PWRDN = (1 << 6),
113 };
114
115 // 0x93 Register
116 enum tcs3400_status
117 {
118 TCS3400_ST_ALS_VALID = (1 << 0),
119 TCS3400_ST_ALS_IRQ = (1 << 4),
120 TCS3400_ST_ALS_SAT = (1 << 7),
121 };
122
123 enum
124 {
125 TCS3400_ALS_GAIN_MASK = (3 << 0),
126 TCS3400_ATIME_DEFAULT_MS = 50,
127 MAX_ALS_VALUE = 0xffff,
128 MIN_ALS_VALUE = 1,
129 };
130
131 static uint8_t const regs[] = {
132 TCS3400_ENABLE, TCS3400_ALS_TIME, TCS3400_ALS_MINTHRESHLO,
133 TCS3400_ALS_MINTHRESHHI, TCS3400_ALS_MAXTHRESHLO, TCS3400_ALS_MAXTHRESHHI,
134 TCS3400_PERSISTENCE, TCS3400_CONFIG, TCS3400_CONTROL,
135 TCS3400_REG_AUX,
136 };
137 static uint8_t const als_gains[] = { 1, 4, 16, 64 };
138
139 struct tcs3400_chips
140 {
141 bool als_enabled;
142 uint8_t atime;
143 uint8_t again;
144 uint8_t persist;
145 uint8_t in_asat;
146 uint8_t shadow[MAX_REGS];
147 struct tcs3400_als_info als_inf;
148 };
149
150 i2c_dev_t tcs3400_ctx = {
151 .port = 3,
152 .config.address_width = 8,
153 .config.freq = 400000,
154 .config.dev_addr = TCS3400_I2C_ADDR,
155 };
156
157 static struct tcs3400_chips tcs3400_chip;
158
drv_als_ams_tcs3400_validate_id(i2c_dev_t * drv,uint8_t id_value)159 static int drv_als_ams_tcs3400_validate_id(i2c_dev_t *drv, uint8_t id_value)
160 {
161 int ret = 0;
162 uint8_t chipid_value;
163 ret = sensor_i2c_read(drv, TCS3400_CHIPID, &chipid_value, I2C_DATA_LEN,
164 I2C_OP_RETRIES);
165 if (unlikely(ret)) {
166 LOG("%s %s Sensor_i2c_read failure \n", SENSOR_STR, __func__);
167 return ret;
168 }
169
170 if (!((chipid_value == 0x90) || (chipid_value == 0x93)))
171 return -1;
172
173 return 0;
174 }
175
drv_als_ams_tcs3400_set_power_mode(i2c_dev_t * drv,dev_power_mode_e mode)176 static int drv_als_ams_tcs3400_set_power_mode(i2c_dev_t * drv,
177 dev_power_mode_e mode)
178 {
179 int ret = 0;
180 uint8_t temp_zero = 0;
181 switch (mode) {
182 case DEV_POWER_ON: {
183 if (!(tcs3400_chip.als_enabled)) {
184 ret = sensor_i2c_write(drv, TCS3400_CMD_ALL_INT_CLR, &temp_zero,
185 I2C_DATA_LEN, I2C_OP_RETRIES);
186 if (unlikely(ret)) {
187 return ret;
188 }
189 tcs3400_chip.shadow[TCS3400_ENABLE] |=
190 (TCS3400_EN_PWR_ON | TCS3400_EN_ALS | TCS3400_EN_ALS_IRQ);
191 ret = sensor_i2c_write(drv, TCS3400_ENABLE,
192 &(tcs3400_chip.shadow[TCS3400_ENABLE]),
193 I2C_DATA_LEN, I2C_OP_RETRIES);
194 if (unlikely(ret)) {
195 LOG("%s %s PowerEnable failure \n", SENSOR_STR, __func__);
196 return ret;
197 }
198 tcs3400_chip.shadow[TCS3400_REG_AUX] |= TCS3400_EN_ALS_SAT_IRQ;
199 sensor_i2c_write(drv, TCS3400_REG_AUX,
200 &(tcs3400_chip.shadow[TCS3400_REG_AUX]),
201 I2C_DATA_LEN, I2C_OP_RETRIES);
202 tcs3400_chip.als_enabled = 1;
203 }
204 } break;
205
206 case DEV_POWER_OFF:
207
208 case DEV_SLEEP: {
209 if (tcs3400_chip.als_enabled) {
210 ret = sensor_i2c_write(drv, TCS3400_CMD_ALL_INT_CLR, &temp_zero,
211 I2C_DATA_LEN, I2C_OP_RETRIES);
212 if (unlikely(ret)) {
213 return ret;
214 }
215 tcs3400_chip.shadow[TCS3400_ENABLE] = 0x00;
216 ret = sensor_i2c_write(drv, TCS3400_ENABLE,
217 &(tcs3400_chip.shadow[TCS3400_ENABLE]),
218 I2C_DATA_LEN, I2C_OP_RETRIES);
219 if (unlikely(ret)) {
220 LOG("%s %s PowerOFF or SLEEP failure \n", SENSOR_STR,
221 __func__);
222 return ret;
223 }
224 tcs3400_chip.als_enabled = 0;
225 }
226 } break;
227
228 default:
229 break;
230 }
231 return 0;
232 }
233
tcs3400_reset_regs(i2c_dev_t * drv)234 static int tcs3400_reset_regs(i2c_dev_t *drv)
235 {
236 int i;
237 int ret = 0;
238 uint8_t reg;
239 uint8_t temp = 0;
240 uint8_t temp_zero = 0;
241 // clear interrupt
242 ret = sensor_i2c_write(drv, TCS3400_CMD_ALL_INT_CLR, &temp_zero,
243 I2C_DATA_LEN, I2C_OP_RETRIES);
244 if (unlikely(ret)) {
245 LOG("%s %s reset_regs INT_CLR failure \n", SENSOR_STR, __func__);
246 return -1;
247 }
248 sensor_i2c_read(drv, TCS3400_ENABLE, &temp, I2C_DATA_LEN, I2C_OP_RETRIES);
249 sensor_i2c_write(drv, TCS3400_ENABLE, &temp_zero, I2C_DATA_LEN,
250 I2C_OP_RETRIES);
251 for (i = 0; i < ARRAY_SIZE(regs); i++) {
252 reg = regs[i];
253 ret = sensor_i2c_write(drv, reg, &(tcs3400_chip.shadow[reg]),
254 I2C_DATA_LEN, I2C_OP_RETRIES);
255 if (unlikely(ret)) {
256 LOG("%s %s reset_regs failure \n", SENSOR_STR, __func__);
257 return ret;
258 }
259 }
260 sensor_i2c_write(drv, TCS3400_ENABLE, &temp, I2C_DATA_LEN, I2C_OP_RETRIES);
261 return ret;
262 }
263
drv_als_ams_tcs3400_set_default_config(i2c_dev_t * drv)264 static int drv_als_ams_tcs3400_set_default_config(i2c_dev_t *drv)
265 {
266 int ret;
267 tcs3400_chip.again = AGAIN_16;
268 tcs3400_chip.atime = 0xee;
269 tcs3400_chip.als_enabled = 0;
270 tcs3400_chip.persist = ALS_PERSIST(0);
271 tcs3400_chip.shadow[TCS3400_CONTROL] &= ~0x3;
272 tcs3400_chip.shadow[TCS3400_CONTROL] |= tcs3400_chip.again;
273 tcs3400_chip.shadow[TCS3400_ALS_TIME] |= tcs3400_chip.atime;
274 tcs3400_chip.shadow[TCS3400_PERSISTENCE] = tcs3400_chip.persist;
275 tcs3400_chip.shadow[TCS3400_CONFIG] = 0x40;
276 ret = tcs3400_reset_regs(&tcs3400_ctx);
277 if (unlikely(ret)) {
278 LOG("%s %s set_default_config failure \n", SENSOR_STR, __func__);
279 return ret;
280 }
281 return 0;
282 }
283
drv_als_ams_tcs3400_irq_handle(void)284 static void drv_als_ams_tcs3400_irq_handle(void)
285 {
286 /* no handle so far */
287 }
288
drv_als_ams_tcs3400_open(void)289 static int drv_als_ams_tcs3400_open(void)
290 {
291 int ret = 0;
292 ret = drv_als_ams_tcs3400_set_power_mode(&tcs3400_ctx, DEV_POWER_ON);
293 if (unlikely(ret)) {
294 return -1;
295 }
296 return 0;
297 }
298
drv_als_ams_tcs3400_close(void)299 static int drv_als_ams_tcs3400_close(void)
300 {
301 int ret = 0;
302 ret = drv_als_ams_tcs3400_set_power_mode(&tcs3400_ctx, DEV_POWER_OFF);
303 if (unlikely(ret)) {
304 return -1;
305 }
306 return 0;
307 }
308
tcs3400_read_als_data()309 static int tcs3400_read_als_data()
310 {
311 int ret;
312 uint8_t *buf;
313 int16_t ir_data;
314 ret = sensor_i2c_read(&tcs3400_ctx, TCS3400_CLR_CHANLO,
315 &tcs3400_chip.shadow[TCS3400_CLR_CHANLO], 8,
316 I2C_OP_RETRIES);
317 if (unlikely(ret)) {
318 LOG("%s %s TCS3400_READ_all_channel data failure \n", SENSOR_STR,
319 __func__);
320 return ret;
321 }
322 buf = &tcs3400_chip.shadow[TCS3400_CLR_CHANLO];
323 tcs3400_chip.als_inf.clear = (uint16_t)((buf[1] << 8) | buf[0]);
324 tcs3400_chip.als_inf.red = (uint16_t)((buf[3] << 8) | buf[2]);
325 tcs3400_chip.als_inf.green = (uint16_t)((buf[5] << 8) | buf[4]);
326 tcs3400_chip.als_inf.blue = (uint16_t)((buf[7] << 8) | buf[6]);
327
328 ir_data = (tcs3400_chip.als_inf.red + tcs3400_chip.als_inf.green +
329 tcs3400_chip.als_inf.blue - tcs3400_chip.als_inf.clear + 1) /
330 2;
331 if (ir_data < 0)
332 ir_data = 0;
333 tcs3400_chip.als_inf.ir_data = ir_data;
334 // LOG("clear is %d ,red is %d, green is %d, blue is %d, ir_data is %d,atime
335 // is %d, again is %d \n",
336 // tcs3400_chip.als_inf.clear,tcs3400_chip.als_inf.red,tcs3400_chip.als_inf.green,tcs3400_chip.als_inf.blue,tcs3400_chip.als_inf.ir_data,tcs3400_chip.atime,tcs3400_chip.again);
337 return 0;
338 }
339
tcs3400_max_als_value()340 static int tcs3400_max_als_value()
341 {
342 int val;
343 val = 256 - tcs3400_chip.shadow[TCS3400_ALS_TIME];
344 val = ((val)*1024);
345 val = val > MAX_ALS_VALUE ? MAX_ALS_VALUE : val;
346 return val;
347 }
348
tcs3400_cal_cpl()349 static int tcs3400_cal_cpl()
350 {
351 uint32_t CPL = 0;
352 uint8_t atime = 256 - tcs3400_chip.shadow[TCS3400_ALS_TIME];
353 int32_t dgf = DGF;
354 uint32_t sat;
355 uint8_t gain = als_gains[(tcs3400_chip.shadow[TCS3400_CONTROL] & 0x3)];
356
357 CPL = (atime * gain);
358 CPL *= INTEGRATION_CYCLE;
359
360 CPL /= dgf;
361 if (CPL < 1)
362 CPL = 1;
363
364 sat = (int32_t)(atime << 10);
365 if (sat > TCS3400_MAX_ALS_VALUE)
366 sat = TCS3400_MAX_ALS_VALUE;
367
368 sat = sat * 8 / 10;
369 tcs3400_chip.als_inf.sat = sat;
370 tcs3400_chip.als_inf.cpl = (uint32_t)CPL;
371 return 1;
372 }
373
tcs3400_als_enable(int on)374 static int tcs3400_als_enable(int on)
375 {
376 int ret = 0;
377 uint8_t temp_zero = 0;
378 if (on) {
379 tcs3400_cal_cpl();
380 ret = sensor_i2c_write(&tcs3400_ctx, TCS3400_CMD_ALL_INT_CLR,
381 &temp_zero, I2C_DATA_LEN, I2C_OP_RETRIES);
382 if (unlikely(ret)) {
383 return ret;
384 }
385 tcs3400_chip.shadow[TCS3400_ENABLE] |=
386 (TCS3400_EN_PWR_ON | TCS3400_EN_ALS | TCS3400_EN_ALS_IRQ);
387 ret = sensor_i2c_write(&tcs3400_ctx, TCS3400_ENABLE,
388 &(tcs3400_chip.shadow[TCS3400_ENABLE]),
389 I2C_DATA_LEN, I2C_OP_RETRIES);
390 if (unlikely(ret)) {
391 LOG("%s %s als_enable failure \n", SENSOR_STR, __func__);
392 return ret;
393 }
394 tcs3400_chip.shadow[TCS3400_REG_AUX] |= TCS3400_EN_ALS_SAT_IRQ;
395 sensor_i2c_write(&tcs3400_ctx, TCS3400_REG_AUX,
396 &(tcs3400_chip.shadow[TCS3400_REG_AUX]), I2C_DATA_LEN,
397 I2C_OP_RETRIES);
398 tcs3400_chip.als_enabled = 1;
399 } else {
400 ret = sensor_i2c_write(&tcs3400_ctx, TCS3400_CMD_ALL_INT_CLR,
401 &temp_zero, I2C_DATA_LEN, I2C_OP_RETRIES);
402 if (unlikely(ret)) {
403 return ret;
404 }
405 tcs3400_chip.shadow[TCS3400_ENABLE] = 0x00;
406 ret = sensor_i2c_write(&tcs3400_ctx, TCS3400_ENABLE,
407 &(tcs3400_chip.shadow[TCS3400_ENABLE]),
408 I2C_DATA_LEN, I2C_OP_RETRIES);
409 if (unlikely(ret)) {
410 LOG("%s %s als_enable failure \n", SENSOR_STR, __func__);
411 return ret;
412 }
413 tcs3400_chip.als_enabled = 0;
414 }
415 return 0;
416 }
417
tcs3400_set_als_gain(int gain)418 static int tcs3400_set_als_gain(int gain)
419 {
420 int ret;
421 uint8_t ctrl_reg;
422 bool current_state = tcs3400_chip.als_enabled;
423 tcs3400_als_enable(0);
424 switch (gain) {
425 case 1:
426 ctrl_reg = AGAIN_1;
427 break;
428 case 4:
429 ctrl_reg = AGAIN_4;
430 break;
431 case 16:
432 ctrl_reg = AGAIN_16;
433 break;
434 case 64:
435 ctrl_reg = AGAIN_64;
436 break;
437 default:
438 LOG("set als_gain wrong gain data \n");
439 return -1;
440 }
441 tcs3400_chip.again = ctrl_reg;
442 tcs3400_chip.shadow[TCS3400_CONTROL] &= ~0x3;
443 tcs3400_chip.shadow[TCS3400_CONTROL] |= ctrl_reg;
444 ret = tcs3400_reset_regs(&tcs3400_ctx);
445 if (unlikely(ret)) {
446 LOG("%s %s tcs3400_reset_regs failure \n", SENSOR_STR, __func__);
447 return ret;
448 }
449 tcs3400_cal_cpl();
450 if (current_state)
451 tcs3400_als_enable(1);
452
453 return 0;
454 }
455
tcs3400_dec_gain()456 static void tcs3400_dec_gain()
457 {
458 int ret;
459 uint8_t gain = (tcs3400_chip.shadow[TCS3400_CONTROL]) & 0x3;
460 uint8_t idx;
461
462 if ((gain <= 0))
463 return;
464 for (idx = (ARRAY_SIZE(als_gains) - 1); idx >= 0; idx--) {
465 if ((als_gains[idx] == 0) || (idx >= gain))
466 continue;
467 else if (idx < gain) {
468 gain = idx;
469 break;
470 }
471 }
472 LOG("%s %s in the dec_gain the alg_gain value is %d \n", SENSOR_STR,
473 __func__, als_gains[gain]);
474 ret = tcs3400_set_als_gain(als_gains[gain]);
475 if (ret == 0)
476 tcs3400_cal_cpl();
477 }
478
tcs3400_inc_gain()479 static void tcs3400_inc_gain()
480 {
481 int ret;
482 uint8_t gain = (tcs3400_chip.shadow[TCS3400_CONTROL]) & 0x3;
483 uint8_t idx;
484
485 LOG("kavin print gain value is %d\n", gain);
486 if ((gain >= (ARRAY_SIZE(als_gains) - 1)))
487 return;
488 for (idx = 0; idx <= (ARRAY_SIZE(als_gains) - 1); idx++) {
489 if ((als_gains[idx] == 0) || (idx <= gain))
490 continue;
491 else if (idx > gain) {
492 gain = idx;
493 break;
494 }
495 }
496
497 LOG("%s %s in the inc_gain the alg_gain value is %d \n", SENSOR_STR,
498 __func__, als_gains[gain]);
499 ret = tcs3400_set_als_gain(als_gains[gain]);
500 if (ret == 0)
501 tcs3400_cal_cpl();
502 }
503
504
tcs3400_get_lux_cct()505 static int tcs3400_get_lux_cct()
506 {
507 int32_t rp1 = 0, gp1 = 0, bp1 = 0, cp1 = 0;
508 int32_t rp2 = 0, gp2 = 0, bp2 = 0;
509 uint32_t lux, temp_lux, CPL = 0;
510 int32_t ir, cct = 0, r_coef = R_Coef, g_coef = G_Coef,
511 b_coef = B_Coef;
512
513 int32_t quintile = (tcs3400_max_als_value() / 5);
514 tcs3400_cal_cpl();
515
516 rp1 = (tcs3400_chip.als_inf.red);
517 gp1 = (tcs3400_chip.als_inf.green);
518 bp1 = (tcs3400_chip.als_inf.blue);
519 cp1 = (tcs3400_chip.als_inf.clear);
520 ir = (tcs3400_chip.als_inf.ir_data);
521 CPL = (tcs3400_chip.als_inf.cpl);
522 /*if(tcs3400_chip.shadow[TCS3400_CONTROL]==0x03)
523 ir=0; */
524 /* remove ir from counts */
525 rp1 -= ir;
526 gp1 -= ir;
527 bp1 -= ir;
528 cp1 -= ir;
529
530 rp2 = r_coef * rp1;
531 gp2 = g_coef * gp1;
532 bp2 = b_coef * bp1;
533
534 lux = (rp2 + gp2 + bp2);
535
536 temp_lux = lux / CPL;
537 // avoid div err;
538 if (rp1 == 0)
539 rp1 = 1;
540 cct = ((CT_Coef * (uint32_t)bp1) / (uint32_t)rp1) + CT_Offset;
541
542 if ((tcs3400_chip.als_inf.clear >= tcs3400_chip.als_inf.sat) ||
543 tcs3400_chip.in_asat) {
544 LOG("%s %s enter dec_gain", SENSOR_STR, __func__);
545 tcs3400_dec_gain();
546 tcs3400_reset_regs(&tcs3400_ctx);
547 } else if (tcs3400_chip.als_inf.clear < quintile) {
548 LOG("%s %s enter inc_gain", SENSOR_STR, __func__);
549 tcs3400_inc_gain();
550 tcs3400_reset_regs(&tcs3400_ctx);
551 }
552
553 if (temp_lux > 0) {
554 tcs3400_chip.als_inf.lux = (uint16_t)temp_lux;
555 tcs3400_chip.als_inf.cct = (uint16_t)cct;
556 }
557
558 // LOG("%s %s tcs3400_get_lux_cct lux is %d, cct is %d \n", SENSOR_STR,
559 // __func__, temp_lux, cct);
560 return 0;
561 }
562
drv_als_ams_tcs3400_read(void * buf,size_t len)563 static int drv_als_ams_tcs3400_read(void *buf, size_t len)
564 {
565 // LOG("%s %s drv_als_ams_tcs3400_read enter \n", SENSOR_STR, __func__);
566 int ret;
567 size_t size;
568 uint8_t temp_zero = 0;
569 uint8_t status = 0;
570 als_data_t *sensordata = (als_data_t *)buf;
571
572 if (buf == NULL) {
573 return -1;
574 }
575 size = sizeof(als_data_t);
576 if (len < size) {
577 return -1;
578 }
579 // LOG("%s %s drv_als_ams_tcs3400_read begin \n", SENSOR_STR, __func__);
580 ret = sensor_i2c_read(&tcs3400_ctx, TCS3400_STATUS,
581 &(tcs3400_chip.shadow[TCS3400_STATUS]), I2C_DATA_LEN,
582 I2C_OP_RETRIES);
583 if (unlikely(ret)) {
584 LOG("%s %s TCS3400_READ_STATUS failure \n", SENSOR_STR, __func__);
585 return ret;
586 } else {
587 status = tcs3400_chip.shadow[TCS3400_STATUS];
588 if (status & TCS3400_ST_ALS_SAT)
589 tcs3400_chip.in_asat = true;
590 else
591 tcs3400_chip.in_asat = false;
592 if (((status & TCS3400_ST_ALS_IRQ) &&
593 (status & TCS3400_ST_ALS_VALID)) ||
594 (tcs3400_chip.in_asat)) {
595 // clear interrupt
596 sensor_i2c_write(&tcs3400_ctx, TCS3400_CMD_ALL_INT_CLR, &temp_zero,
597 I2C_DATA_LEN, I2C_OP_RETRIES);
598 LOG("%s %s drv_als_ams_tcs3400_read enter read_als_data \n",
599 SENSOR_STR, __func__);
600 tcs3400_read_als_data();
601 tcs3400_get_lux_cct();
602 }
603 sensordata->lux = (uint32_t)(tcs3400_chip.als_inf.lux);
604 sensordata->timestamp = aos_now_ms();
605 return (int)size;
606 }
607 return 0;
608 }
609
drv_als_ams_tcs3400_write(const void * buf,size_t len)610 static int drv_als_ams_tcs3400_write(const void *buf, size_t len)
611 {
612 // no handle so far
613 return 0;
614 }
615
drv_als_ams_tcs3400_ioctl(int cmd,unsigned long arg)616 static int drv_als_ams_tcs3400_ioctl(int cmd, unsigned long arg)
617 {
618 int ret = 0;
619
620 switch (cmd) {
621 case SENSOR_IOCTL_SET_POWER: {
622 ret = drv_als_ams_tcs3400_set_power_mode(&tcs3400_ctx, arg);
623 if (unlikely(ret)) {
624 return -1;
625 }
626 } break;
627
628 case SENSOR_IOCTL_GET_INFO: {
629 /* fill the dev info here */
630 dev_sensor_info_t *info = (dev_sensor_info_t *)arg;
631 info->model = "TCS3400";
632 info->unit = lux;
633 } break;
634
635 default:
636 break;
637 }
638
639 return 0;
640 }
641
drv_als_ams_tcs3400_init(void)642 int drv_als_ams_tcs3400_init(void)
643 {
644 int ret = 0;
645 sensor_obj_t sensor_als;
646 memset(&sensor_als, 0, sizeof(sensor_als));
647
648 /* fill the sensor obj parameters here */
649 sensor_als.tag = TAG_DEV_ALS;
650 sensor_als.path = dev_als_path;
651 sensor_als.io_port = I2C_PORT;
652 sensor_als.open = drv_als_ams_tcs3400_open;
653 sensor_als.close = drv_als_ams_tcs3400_close;
654 sensor_als.read = drv_als_ams_tcs3400_read;
655 sensor_als.write = drv_als_ams_tcs3400_write;
656 sensor_als.ioctl = drv_als_ams_tcs3400_ioctl;
657 sensor_als.irq_handle = drv_als_ams_tcs3400_irq_handle;
658
659 ret = sensor_create_obj(&sensor_als);
660 if (unlikely(ret)) {
661 return -1;
662 }
663
664 ret = drv_als_ams_tcs3400_validate_id(&tcs3400_ctx, TCS3400_CHIPID_VALUE);
665 if (unlikely(ret)) {
666 return -1;
667 }
668
669 ret = drv_als_ams_tcs3400_set_default_config(&tcs3400_ctx);
670 if (unlikely(ret)) {
671 return -1;
672 }
673 return 0;
674 }
675
676 SENSOR_DRV_ADD(drv_als_ams_tcs3400_init);
677
678