1 /*
2 * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3 *
4 *
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "aos/kernel.h"
11 #include "sensor_drv_api.h"
12 #include "sensor_hal.h"
13 //#include "soc_init.h"
14
15
16 #define PAJ7620_SLAVE_ADDRESS (0x73 << 1)
17
18 #define BIT(x) 1 << x
19
20 // REGISTER DESCRIPTION
21 #define PAJ7620_VAL(val, maskbit) ( val << maskbit )
22 #define PAJ7620_ADDR_BASE 0x00
23
24 // REGISTER BANK SELECT
25 #define PAJ7620_REGITER_BANK_SEL (PAJ7620_ADDR_BASE + 0xEF) //W
26
27 // REGISTER BANK 0
28 #define PAJ7620_ADDR_SUSPEND_CMD (PAJ7620_ADDR_BASE + 0x3) //W
29 #define PAJ7620_ADDR_GES_PS_DET_MASK_0 (PAJ7620_ADDR_BASE + 0x41) //RW
30 #define PAJ7620_ADDR_GES_PS_DET_MASK_1 (PAJ7620_ADDR_BASE + 0x42) //RW
31 #define PAJ7620_ADDR_GES_PS_DET_FLAG_0 (PAJ7620_ADDR_BASE + 0x43) //R
32 #define PAJ7620_ADDR_GES_PS_DET_FLAG_1 (PAJ7620_ADDR_BASE + 0x44) //R
33 #define PAJ7620_ADDR_STATE_INDICATOR (PAJ7620_ADDR_BASE + 0x45) //R
34 #define PAJ7620_ADDR_PS_HIGH_THRESHOLD (PAJ7620_ADDR_BASE + 0x69) //RW
35 #define PAJ7620_ADDR_PS_LOW_THRESHOLD (PAJ7620_ADDR_BASE + 0x6A) //RW
36 #define PAJ7620_ADDR_PS_APPROACH_STATE (PAJ7620_ADDR_BASE + 0x6B) //R
37 #define PAJ7620_ADDR_PS_RAW_DATA (PAJ7620_ADDR_BASE + 0x6C) //R
38
39 // REGISTER BANK 1
40 #define PAJ7620_ADDR_PS_GAIN (PAJ7620_ADDR_BASE + 0x44) //RW
41 #define PAJ7620_ADDR_IDLE_S1_STEP_0 (PAJ7620_ADDR_BASE + 0x67) //RW
42 #define PAJ7620_ADDR_IDLE_S1_STEP_1 (PAJ7620_ADDR_BASE + 0x68) //RW
43 #define PAJ7620_ADDR_IDLE_S2_STEP_0 (PAJ7620_ADDR_BASE + 0x69) //RW
44 #define PAJ7620_ADDR_IDLE_S2_STEP_1 (PAJ7620_ADDR_BASE + 0x6A) //RW
45 #define PAJ7620_ADDR_OP_TO_S1_STEP_0 (PAJ7620_ADDR_BASE + 0x6B) //RW
46 #define PAJ7620_ADDR_OP_TO_S1_STEP_1 (PAJ7620_ADDR_BASE + 0x6C) //RW
47 #define PAJ7620_ADDR_OP_TO_S2_STEP_0 (PAJ7620_ADDR_BASE + 0x6D) //RW
48 #define PAJ7620_ADDR_OP_TO_S2_STEP_1 (PAJ7620_ADDR_BASE + 0x6E) //RW
49 #define PAJ7620_ADDR_OPERATION_ENABLE (PAJ7620_ADDR_BASE + 0x72) //RW
50
51 // PAJ7620_REGITER_BANK_SEL
52 #define PAJ7620_BANK0 PAJ7620_VAL(0,0)
53 #define PAJ7620_BANK1 PAJ7620_VAL(1,0)
54
55 // PAJ7620_ADDR_SUSPEND_CMD
56 #define PAJ7620_I2C_WAKEUP PAJ7620_VAL(1,0)
57 #define PAJ7620_I2C_SUSPEND PAJ7620_VAL(0,0)
58
59 // PAJ7620_ADDR_OPERATION_ENABLE
60 #define PAJ7620_ENABLE PAJ7620_VAL(1,0)
61 #define PAJ7620_DISABLE PAJ7620_VAL(0,0)
62
63 enum {
64 BANK0 = 0,
65 BANK1,
66 };
67
68 #define GES_RIGHT_FLAG PAJ7620_VAL(1,0)
69 #define GES_LEFT_FLAG PAJ7620_VAL(1,1)
70 #define GES_UP_FLAG PAJ7620_VAL(1,2)
71 #define GES_DOWN_FLAG PAJ7620_VAL(1,3)
72 #define GES_FORWARD_FLAG PAJ7620_VAL(1,4)
73 #define GES_BACKWARD_FLAG PAJ7620_VAL(1,5)
74 #define GES_CLOCKWISE_FLAG PAJ7620_VAL(1,6)
75 #define GES_COUNT_CLOCKWISE_FLAG PAJ7620_VAL(1,7)
76 #define GES_WAVE_FLAG PAJ7620_VAL(1,0)
77
78
79 enum {
80 GESTURE_INVALID = 0,
81 GESTURE_UP,
82 GESTURE_DOWN,
83 GESTURE_LEFT,
84 GESTURE_RIGHT,
85 GESTURE_FORWARD,
86 GESTURE_BACKWARD,
87 GESTURE_CLOCKWISE,
88 GESTURE_ANTICLOCKWISE,
89 GESTURE_WAVE,
90 };
91
92 static unsigned char paj7620_init_regs[][2] = {
93 {0xEF,0x00},
94 {0x32,0x29},
95 {0x33,0x01},
96 {0x34,0x00},
97 {0x35,0x01},
98 {0x36,0x00},
99 {0x37,0x07},
100 {0x38,0x17},
101 {0x39,0x06},
102 {0x3A,0x12},
103 {0x3F,0x00},
104 {0x40,0x02},
105 {0x41,0xFF},
106 {0x42,0x01},
107 {0x46,0x2D},
108 {0x47,0x0F},
109 {0x48,0x3C},
110 {0x49,0x00},
111 {0x4A,0x1E},
112 {0x4B,0x00},
113 {0x4C,0x20},
114 {0x4D,0x00},
115 {0x4E,0x1A},
116 {0x4F,0x14},
117 {0x50,0x00},
118 {0x51,0x10},
119 {0x52,0x00},
120 {0x5C,0x02},
121 {0x5D,0x00},
122 {0x5E,0x10},
123 {0x5F,0x3F},
124 {0x60,0x27},
125 {0x61,0x28},
126 {0x62,0x00},
127 {0x63,0x03},
128 {0x64,0xF7},
129 {0x65,0x03},
130 {0x66,0xD9},
131 {0x67,0x03},
132 {0x68,0x01},
133 {0x69,0xC8},
134 {0x6A,0x40},
135 {0x6D,0x04},
136 {0x6E,0x00},
137 {0x6F,0x00},
138 {0x70,0x80},
139 {0x71,0x00},
140 {0x72,0x00},
141 {0x73,0x00},
142 {0x74,0xF0},
143 {0x75,0x00},
144 {0x80,0x42},
145 {0x81,0x44},
146 {0x82,0x04},
147 {0x83,0x20},
148 {0x84,0x20},
149 {0x85,0x00},
150 {0x86,0x10},
151 {0x87,0x00},
152 {0x88,0x05},
153 {0x89,0x18},
154 {0x8A,0x10},
155 {0x8B,0x01},
156 {0x8C,0x37},
157 {0x8D,0x00},
158 {0x8E,0xF0},
159 {0x8F,0x81},
160 {0x90,0x06},
161 {0x91,0x06},
162 {0x92,0x1E},
163 {0x93,0x0D},
164 {0x94,0x0A},
165 {0x95,0x0A},
166 {0x96,0x0C},
167 {0x97,0x05},
168 {0x98,0x0A},
169 {0x99,0x41},
170 {0x9A,0x14},
171 {0x9B,0x0A},
172 {0x9C,0x3F},
173 {0x9D,0x33},
174 {0x9E,0xAE},
175 {0x9F,0xF9},
176 {0xA0,0x48},
177 {0xA1,0x13},
178 {0xA2,0x10},
179 {0xA3,0x08},
180 {0xA4,0x30},
181 {0xA5,0x19},
182 {0xA6,0x10},
183 {0xA7,0x08},
184 {0xA8,0x24},
185 {0xA9,0x04},
186 {0xAA,0x1E},
187 {0xAB,0x1E},
188 {0xCC,0x19},
189 {0xCD,0x0B},
190 {0xCE,0x13},
191 {0xCF,0x64},
192 {0xD0,0x21},
193 {0xD1,0x0F},
194 {0xD2,0x88},
195 {0xE0,0x01},
196 {0xE1,0x04},
197 {0xE2,0x41},
198 {0xE3,0xD6},
199 {0xE4,0x00},
200 {0xE5,0x0C},
201 {0xE6,0x0A},
202 {0xE7,0x00},
203 {0xE8,0x00},
204 {0xE9,0x00},
205 {0xEE,0x07},
206 {0xEF,0x01},
207 {0x00,0x1E},
208 {0x01,0x1E},
209 {0x02,0x0F},
210 {0x03,0x10},
211 {0x04,0x02},
212 {0x05,0x00},
213 {0x06,0xB0},
214 {0x07,0x04},
215 {0x08,0x0D},
216 {0x09,0x0E},
217 {0x0A,0x9C},
218 {0x0B,0x04},
219 {0x0C,0x05},
220 {0x0D,0x0F},
221 {0x0E,0x02},
222 {0x0F,0x12},
223 {0x10,0x02},
224 {0x11,0x02},
225 {0x12,0x00},
226 {0x13,0x01},
227 {0x14,0x05},
228 {0x15,0x07},
229 {0x16,0x05},
230 {0x17,0x07},
231 {0x18,0x01},
232 {0x19,0x04},
233 {0x1A,0x05},
234 {0x1B,0x0C},
235 {0x1C,0x2A},
236 {0x1D,0x01},
237 {0x1E,0x00},
238 {0x21,0x00},
239 {0x22,0x00},
240 {0x23,0x00},
241 {0x25,0x01},
242 {0x26,0x00},
243 {0x27,0x39},
244 {0x28,0x7F},
245 {0x29,0x08},
246 {0x30,0x03},
247 {0x31,0x00},
248 {0x32,0x1A},
249 {0x33,0x1A},
250 {0x34,0x07},
251 {0x35,0x07},
252 {0x36,0x01},
253 {0x37,0xFF},
254 {0x38,0x36},
255 {0x39,0x07},
256 {0x3A,0x00},
257 {0x3E,0xFF},
258 {0x3F,0x00},
259 {0x40,0x77},
260 {0x41,0x40},
261 {0x42,0x00},
262 {0x43,0x30},
263 {0x44,0xA0},
264 {0x45,0x5C},
265 {0x46,0x00},
266 {0x47,0x00},
267 {0x48,0x58},
268 {0x4A,0x1E},
269 {0x4B,0x1E},
270 {0x4C,0x00},
271 {0x4D,0x00},
272 {0x4E,0xA0},
273 {0x4F,0x80},
274 {0x50,0x00},
275 {0x51,0x00},
276 {0x52,0x00},
277 {0x53,0x00},
278 {0x54,0x00},
279 {0x57,0x80},
280 {0x59,0x10},
281 {0x5A,0x08},
282 {0x5B,0x94},
283 {0x5C,0xE8},
284 {0x5D,0x08},
285 {0x5E,0x3D},
286 {0x5F,0x99},
287 {0x60,0x45},
288 {0x61,0x40},
289 {0x63,0x2D},
290 {0x64,0x02},
291 {0x65,0x96},
292 {0x66,0x00},
293 {0x67,0x97},
294 {0x68,0x01},
295 {0x69,0xCD},
296 {0x6A,0x01},
297 {0x6B,0xB0},
298 {0x6C,0x04},
299 {0x6D,0x2C},
300 {0x6E,0x01},
301 {0x6F,0x32},
302 {0x71,0x00},
303 {0x72,0x01},
304 {0x73,0x35},
305 {0x74,0x00},
306 {0x75,0x33},
307 {0x76,0x31},
308 {0x77,0x01},
309 {0x7C,0x84},
310 {0x7D,0x03},
311 {0x7E,0x01},
312 };
313
314 static i2c_dev_t paj7620_client = {
315 .port = 3,
316 .config.dev_addr = PAJ7620_SLAVE_ADDRESS,
317 };
318
319 uint8_t gesture_action;
320
paj7620_reg_write(uint8_t reg,uint8_t val)321 static int paj7620_reg_write(uint8_t reg, uint8_t val)
322 {
323 if (sensor_i2c_write(&paj7620_client, reg, &val, 1, 200)) {
324 printf("%s: write failed !\n", __func__);
325 return 1;
326 }
327 return 0;
328 }
329
paj7620_reg_read(uint8_t reg,uint8_t * val)330 static int paj7620_reg_read(uint8_t reg, uint8_t *val)
331 {
332 if (sensor_i2c_read(&paj7620_client, reg, val, 1, 200)) {
333 //printf("%s: read failed !\n", __func__);
334 return 1;
335 }
336 return 0;
337 }
338
paj7620_bank_select(int bank)339 static int paj7620_bank_select(int bank)
340 {
341 switch (bank) {
342 case BANK0:
343 paj7620_reg_write(PAJ7620_REGITER_BANK_SEL, PAJ7620_BANK0);
344 break;
345 case BANK1:
346 paj7620_reg_write(PAJ7620_REGITER_BANK_SEL, PAJ7620_BANK1);
347 break;
348 default:
349 break;
350 }
351 return 0;
352 }
353
paj7620_gesture(uint8_t gesture)354 static uint8_t paj7620_gesture(uint8_t gesture)
355 {
356 uint8_t data = 0;
357 uint8_t data1 = 0;
358 uint8_t rc = 0;
359
360 switch (gesture) {
361 case GES_RIGHT_FLAG:
362 aos_msleep(1);
363 paj7620_reg_read(PAJ7620_ADDR_GES_PS_DET_FLAG_0, &data);
364 if (data == GES_FORWARD_FLAG) {
365 rc = GESTURE_FORWARD;
366 aos_msleep(1);
367 } else if (data == GES_BACKWARD_FLAG) {
368 rc = GESTURE_BACKWARD;
369 aos_msleep(1);
370 } else {
371 rc = GESTURE_RIGHT;
372 }
373 break;
374 case GES_LEFT_FLAG:
375 aos_msleep(1);
376 paj7620_reg_read(PAJ7620_ADDR_GES_PS_DET_FLAG_0, &data);
377 if (data == GES_FORWARD_FLAG) {
378 rc = GESTURE_FORWARD;
379 aos_msleep(1);
380 } else if(data == GES_BACKWARD_FLAG) {
381 rc = GESTURE_BACKWARD;
382 aos_msleep(1);
383 } else {
384 rc = GESTURE_LEFT;
385 }
386 break;
387 case GES_UP_FLAG:
388 aos_msleep(1);
389 paj7620_reg_read(PAJ7620_ADDR_GES_PS_DET_FLAG_0, &data);
390 if (data == GES_FORWARD_FLAG) {
391 rc = GESTURE_FORWARD;
392 aos_msleep(1);
393 } else if(data == GES_BACKWARD_FLAG) {
394 rc = GESTURE_BACKWARD;
395 aos_msleep(1);
396 } else {
397 rc = GESTURE_UP;
398 }
399 break;
400 case GES_DOWN_FLAG:
401 aos_msleep(1);
402 paj7620_reg_read(PAJ7620_ADDR_GES_PS_DET_FLAG_0, &data);
403 if(data == GES_FORWARD_FLAG) {
404 rc = GESTURE_FORWARD;
405 aos_msleep(1);
406 } else if(data == GES_BACKWARD_FLAG) {
407 rc = GESTURE_BACKWARD;
408 aos_msleep(1);
409 } else {
410 rc = GESTURE_DOWN;
411 }
412 break;
413 case GES_FORWARD_FLAG:
414 rc = GESTURE_FORWARD;
415 aos_msleep(1);
416 break;
417 case GES_BACKWARD_FLAG:
418 rc = GESTURE_BACKWARD;
419 aos_msleep(1);
420 break;
421 case GES_CLOCKWISE_FLAG:
422 rc = GESTURE_CLOCKWISE;
423 break;
424 case GES_COUNT_CLOCKWISE_FLAG:
425 rc = GESTURE_ANTICLOCKWISE;
426 break;
427 default:
428 paj7620_reg_read(PAJ7620_ADDR_GES_PS_DET_FLAG_1, &data1);
429 if (data1 == GES_WAVE_FLAG)
430 rc = GESTURE_WAVE;
431 break;
432 }
433 return rc;
434 }
435
paj7620_poll(void)436 int paj7620_poll(void)
437 {
438 uint8_t gesture = 0;
439 while (1) {
440 aos_msleep(5);
441 paj7620_reg_read(PAJ7620_ADDR_GES_PS_DET_FLAG_0, &gesture);
442 paj7620_gesture(gesture);
443 }
444 return 0;
445 }
446
paj7620_gesture_get(uint8_t gesture)447 static uint8_t paj7620_gesture_get(uint8_t gesture)
448 {
449 uint8_t data = 0;
450 uint8_t data1 = 0;
451 uint8_t rc = 0;
452
453 switch (gesture) {
454 case GES_RIGHT_FLAG:
455 aos_msleep(1);
456 paj7620_reg_read(PAJ7620_ADDR_GES_PS_DET_FLAG_0, &data);
457 if (data == GES_FORWARD_FLAG) {
458 rc = GS_SENSOR_INVALID;
459 aos_msleep(1);
460 } else if (data == GES_BACKWARD_FLAG) {
461 rc = GS_SENSOR_INVALID;
462 aos_msleep(1);
463 } else {
464 rc = GS_SENSOR_RIGHT;
465 }
466 break;
467 case GES_LEFT_FLAG:
468 aos_msleep(1);
469 paj7620_reg_read(PAJ7620_ADDR_GES_PS_DET_FLAG_0, &data);
470 if (data == GES_FORWARD_FLAG) {
471 rc = GS_SENSOR_INVALID;
472 aos_msleep(1);
473 } else if(data == GES_BACKWARD_FLAG) {
474 rc = GS_SENSOR_INVALID;
475 aos_msleep(1);
476 } else {
477 rc = GS_SENSOR_LEFT;
478 }
479 break;
480 case GES_UP_FLAG:
481 aos_msleep(1);
482 paj7620_reg_read(PAJ7620_ADDR_GES_PS_DET_FLAG_0, &data);
483 if (data == GES_FORWARD_FLAG) {
484 rc = GS_SENSOR_INVALID;
485 aos_msleep(1);
486 } else if(data == GES_BACKWARD_FLAG) {
487 rc = GS_SENSOR_INVALID;
488 aos_msleep(1);
489 } else {
490 rc = GS_SENSOR_UP;
491 }
492 break;
493 case GES_DOWN_FLAG:
494 aos_msleep(1);
495 paj7620_reg_read(PAJ7620_ADDR_GES_PS_DET_FLAG_0, &data);
496 if(data == GES_FORWARD_FLAG) {
497 rc = GS_SENSOR_INVALID;
498 aos_msleep(1);
499 } else if(data == GES_BACKWARD_FLAG) {
500 rc = GS_SENSOR_INVALID;
501 aos_msleep(1);
502 } else {
503 rc = GS_SENSOR_DOWN;
504 }
505 break;
506 case GES_FORWARD_FLAG:
507 rc = GS_SENSOR_INVALID;
508 aos_msleep(1);
509 break;
510 case GES_BACKWARD_FLAG:
511 rc = GS_SENSOR_INVALID;
512 aos_msleep(1);
513 break;
514 case GES_CLOCKWISE_FLAG:
515 rc = GS_SENSOR_CLOCKWISE;
516 break;
517 case GES_COUNT_CLOCKWISE_FLAG:
518 rc = GS_SENSOR_ANTICLOCKWISE;
519 break;
520 default:
521 paj7620_reg_read(PAJ7620_ADDR_GES_PS_DET_FLAG_1, &data1);
522 if (data1 == GES_WAVE_FLAG)
523 rc = GS_SENSOR_WAVE;
524 break;
525 }
526 return rc;
527 }
528
529
drv_gs_pixart_paj7620_open(void)530 static int drv_gs_pixart_paj7620_open(void)
531 {
532 LOG("%s %s successfully \n", SENSOR_STR, __func__);
533 return 0;
534 }
535
drv_gs_pixart_paj7620_close(void)536 static int drv_gs_pixart_paj7620_close(void)
537 {
538 LOG("%s %s successfully \n", SENSOR_STR, __func__);
539 return 0;
540 }
541
drv_gs_pixart_paj7620_read(void * buf,size_t len)542 static int drv_gs_pixart_paj7620_read(void *buf, size_t len)
543 {
544 uint8_t gesture = 0;
545 size_t size = sizeof(gs_data_t);
546 gs_data_t* gs_data = (gs_data_t*)buf;
547 if(NULL == buf){
548 return -1;
549 }
550
551 if(len <= size){
552 return -1;
553 }
554
555 paj7620_reg_read(PAJ7620_ADDR_GES_PS_DET_FLAG_0, &gesture);
556 gs_data->gs_type = (gs_type_e)paj7620_gesture_get(gesture);
557 gs_data->timestamp = aos_now_ms();
558
559 return (int) size;
560 }
561
drv_gs_pixart_paj7620_write(const void * buf,size_t len)562 static int drv_gs_pixart_paj7620_write(const void *buf, size_t len)
563 {
564 (void) buf;
565 (void) len;
566
567 return 0;
568 }
569
drv_gs_pixart_paj7620_ioctl(int cmd,unsigned long arg)570 static int drv_gs_pixart_paj7620_ioctl(int cmd, unsigned long arg)
571 {
572
573 switch (cmd) {
574 case SENSOR_IOCTL_SET_POWER: {
575 } break;
576 case SENSOR_IOCTL_GET_INFO: {
577 /* fill the dev info here */
578 dev_sensor_info_t *info = (dev_sensor_info_t *) arg;
579 info->model = "paj7620";
580 } break;
581 default:
582 return -1;
583 }
584
585 LOG("%s %s successfully \n", SENSOR_STR, __func__);
586 return 0;
587 }
588
589
drv_gs_pixart_paj7620_init(void)590 int drv_gs_pixart_paj7620_init(void)
591 {
592 int ret;
593 uint8_t data0 = 1;
594 uint8_t data1 = 2;
595 int i;
596
597 sensor_obj_t sensor;
598 memset(&sensor, 0, sizeof(sensor));
599
600 /* fill the sensor_gs obj parameters here */
601 sensor.tag = TAG_DEV_GS;
602 sensor.path = dev_gs_path;
603 sensor.io_port = I2C_PORT;
604 sensor.mode = DEV_POLLING;
605 sensor.power = DEV_POWER_OFF;
606 sensor.open = drv_gs_pixart_paj7620_open;
607 sensor.close = drv_gs_pixart_paj7620_close;
608 sensor.read = drv_gs_pixart_paj7620_read;
609 sensor.write = drv_gs_pixart_paj7620_write;
610 sensor.ioctl = drv_gs_pixart_paj7620_ioctl;
611 sensor.irq_handle = NULL;
612
613 ret = sensor_create_obj(&sensor);
614 if (unlikely(ret)) {
615 return -1;
616 }
617
618 aos_msleep(10);
619 paj7620_bank_select(BANK0);
620 paj7620_bank_select(BANK0);
621
622 paj7620_reg_read(0, &data0);
623 paj7620_reg_read(1, &data1);
624
625 if (data0 != 0x20 || data1 != 0x76) {
626 printf("%s: paj7620 check failed!\n", __func__);
627 return 1;
628 }
629 if (data0 == 0x20)
630 printf("%s: paj7620 wakeup\n", __func__);
631
632 for (i = 0; i < sizeof(paj7620_init_regs)/sizeof(paj7620_init_regs[0]); i++) {
633 paj7620_reg_write(paj7620_init_regs[i][0], paj7620_init_regs[i][1]);
634 }
635
636 paj7620_bank_select(BANK0);
637 aos_msleep(100);
638 uint8_t gesture;
639 paj7620_reg_read(PAJ7620_ADDR_GES_PS_DET_FLAG_0, &gesture);
640 paj7620_reg_read(PAJ7620_ADDR_GES_PS_DET_FLAG_1, &gesture);
641 printf("%s: paj7620 init success\n", __func__);
642
643 return 0;
644 }
645
646 SENSOR_DRV_ADD(drv_gs_pixart_paj7620_init);
647
648