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 LTR91100_I2C_SLAVE_ADDR                           0x23
9 
10 #define LTR91100_CTRL                                     0x80 /* SW reset control */
11 #define LTR91100_PS_CTRL                                  0x81 /* PS operation mode */
12 #define LTR91100_GS_CTRL                                  0x82 /* GS operation mode */
13 #define LTR91100_PS_LED                                   0x88 /* LED pulse freq, current duty, peak current */
14 #define LTR91100_PS_MEAS_RATE                             0x89 /* measurement rate*/
15 #define LTR91100_PS_XTALK_NE_LSB                          0x8B /* Crosstalk correction on PS north-east detector, lower byte */
16 #define LTR91100_PS_XTALK_NE_MSB                          0xCB /* Crosstalk correction on PS north-east detector, upper byte */
17 #define LTR91100_PS_XTALK_SW_LSB                          0x8C /* Crosstalk correction on PS south-west detector, lower byte */
18 #define LTR91100_PS_XTALK_SW_MSB                          0xCC /* Crosstalk correction on PS south-west detector, upper byte */
19 #define LTR91100_PS_THRES_UP_LSB                          0x8D /* PS interrupt upper threshold, lower byte */
20 #define LTR91100_PS_THRES_UP_MSB                          0xCD /* PS interrupt upper threshold, upper byte */
21 #define LTR91100_PS_THRES_LOW_LSB                         0x8E /* PS interrupt lower threshold, lower byte */
22 #define LTR91100_PS_THRES_LOW_MSB                         0xCE /* PS interrupt lower threshold, upper byte */
23 #define LTR91100_INTR_PRST                                0x8F /* PS interrupt persist setting */
24 #define LTR91100_GS_LED                                   0x90 /* GS LED setting */
25 #define LTR91100_GS_WAIT                                  0x91 /* GS wait time setting */
26 #define LTR91100_GS_PRST                                  0x92 /* GS interrupt persist setting */
27 #define LTR91100_GS_ENTRY_LSB                             0x93 /* GS entry threshold value (1st entry), lower byte */
28 #define LTR91100_GS_ENTRY_MSB                             0xC3 /* GS entry threshold value (1st entry), upper byte */
29 #define LTR91100_GS_EXIT                                  0x94 /* GS exit threshold value */
30 #define LTR91100_GS_GATE                                  0x95 /* GS gate after the 1st entry */
31 #define LTR91100_GS_XTALK_N                               0x96 /* GS crosstalk correction on north detector */
32 #define LTR91100_GS_XTALK_S                               0x97 /* GS crosstalk correction on south detector */
33 #define LTR91100_GS_XTALK_E                               0x98 /* GS crosstalk correction on east detector */
34 #define LTR91100_GS_XTALK_W                               0x99 /* GS crosstalk correction on west detector */
35 #define LTR91100_PART_ID                                  0x9A
36 #define LTR91100_MANUFAC_ID                               0x9B
37 #define LTR91100_PS_STATUS                                0x9D
38 #define LTR91100_GS_STATUS                                0x9E
39 #define LTR91100_PS_DATA_LSB                              0xB0
40 #define LTR91100_PS_DATA_MSB                              0xB1
41 #define LTR91100_GS_FIFO_ADDR                             0xB2 /* GS FIFO address pointer */
42 #define LTR91100_GS_FIFO_ACCESS_N                         0xB3 /* GS north data */
43 #define LTR91100_GS_FIFO_ACCESS_S                         0xB4 /* GS south data */
44 #define LTR91100_GS_FIFO_ACCESS_E                         0xB5 /* GS east data */
45 #define LTR91100_GS_FIFO_ACCESS_W                         0xB6 /* GS west data */
46 
47 #define LTR91100_ADDR_TRANS(n)                            ((n) << 1)
48 #define LTR91100_I2C_ADDR                                 LTR91100_ADDR_TRANS(LTR91100_I2C_SLAVE_ADDR)
49 #define LTR91100_PART_ID_VAL                              0xC1
50 #define LTR91100_MANUFAC_ID_VAL                           0x05
51 
52 #define LTR91100_CTRL_REG_SW_RESET__POS                   (1)
53 #define LTR91100_CTRL_REG_SW_RESET__MSK                   (0x02)
54 #define LTR91100_CTRL_REG_SW_RESET__REG                   (LTR91100_CTRL)
55 
56 #define LTR91100_PS_CTRL_REG_PS_INT__POS                  (0)
57 #define LTR91100_PS_CTRL_REG_PS_INT__MSK                  (0x01)
58 #define LTR91100_PS_CTRL_REG_PS_INT__REG                  (LTR91100_PS_CTRL)
59 
60 #define LTR91100_PS_CTRL_REG_PS_MODE__POS                 (1)
61 #define LTR91100_PS_CTRL_REG_PS_MODE__MSK                 (0x02)
62 #define LTR91100_PS_CTRL_REG_PS_MODE__REG                 (LTR91100_PS_CTRL)
63 
64 #define LTR91100_PS_CTRL_REG_PS_GAIN__POS                 (2)
65 #define LTR91100_PS_CTRL_REG_PS_GAIN__MSK                 (0x0C)
66 #define LTR91100_PS_CTRL_REG_PS_GAIN__REG                 (LTR91100_PS_CTRL)
67 
68 #define LTR91100_PS_CTRL_REG_PS_OFFSET_EN__POS            (4)
69 #define LTR91100_PS_CTRL_REG_PS_OFFSET_EN__MSK            (0x10)
70 #define LTR91100_PS_CTRL_REG_PS_OFFSET_EN__REG            (LTR91100_PS_CTRL)
71 
72 #define LTR91100_PS_CTRL_REG_NEAR_FAR_STATUS_EN__POS      (7)
73 #define LTR91100_PS_CTRL_REG_NEAR_FAR_STATUS_EN__MSK      (0x80)
74 #define LTR91100_PS_CTRL_REG_NEAR_FAR_STATUS_EN__REG      (LTR91100_PS_CTRL)
75 
76 #define LTR91100_GS_CTRL_REG_GS_INT__POS                  (0)
77 #define LTR91100_GS_CTRL_REG_GS_INT__MSK                  (0x01)
78 #define LTR91100_GS_CTRL_REG_GS_INT__REG                  (LTR91100_GS_CTRL)
79 
80 #define LTR91100_GS_CTRL_REG_GS_MODE__POS                 (1)
81 #define LTR91100_GS_CTRL_REG_GS_MODE__MSK                 (0x02)
82 #define LTR91100_GS_CTRL_REG_GS_MODE__REG                 (LTR91100_GS_CTRL)
83 
84 #define LTR91100_GS_CTRL_REG_GS_GAIN__POS                 (2)
85 #define LTR91100_GS_CTRL_REG_GS_GAIN__MSK                 (0x0C)
86 #define LTR91100_GS_CTRL_REG_GS_GAIN__REG                 (LTR91100_GS_CTRL)
87 
88 #define LTR91100_GS_CTRL_REG_GS_OFFSET_EN__POS            (4)
89 #define LTR91100_GS_CTRL_REG_GS_OFFSET_EN__MSK            (0x10)
90 #define LTR91100_GS_CTRL_REG_GS_OFFSET_EN__REG            (LTR91100_GS_CTRL)
91 
92 #define LTR91100_GS_CTRL_REG_GS_FIFO_RESET__POS           (5)
93 #define LTR91100_GS_CTRL_REG_GS_FIFO_RESET__MSK           (0x20)
94 #define LTR91100_GS_CTRL_REG_GS_FIFO_RESET__REG           (LTR91100_GS_CTRL)
95 
96 #define LTR91100_GS_CTRL_REG_GS_FORCE_START__POS          (6)
97 #define LTR91100_GS_CTRL_REG_GS_FORCE_START__MSK          (0x40)
98 #define LTR91100_GS_CTRL_REG_GS_FORCE_START__REG          (LTR91100_GS_CTRL)
99 
100 #define LTR91100_PS_LED_REG_PLED_DRIVE__POS               (0)
101 #define LTR91100_PS_LED_REG_PLED_DRIVE__MSK               (0x03)
102 #define LTR91100_PS_LED_REG_PLED_DRIVE__REG               (LTR91100_PS_LED)
103 
104 #define LTR91100_PS_LED_REG_PLED_BOOST__POS               (2)
105 #define LTR91100_PS_LED_REG_PLED_BOOST__MSK               (0x0C)
106 #define LTR91100_PS_LED_REG_PLED_BOOST__REG               (LTR91100_PS_LED)
107 
108 #define LTR91100_PS_MEAS_RATE_REG_PULSES__POS             (0)
109 #define LTR91100_PS_MEAS_RATE_REG_PULSES__MSK             (0x0F)
110 #define LTR91100_PS_MEAS_RATE_REG_PULSES__REG             (LTR91100_PS_MEAS_RATE)
111 
112 #define LTR91100_PS_MEAS_RATE_REG_MEAS_RATE__POS          (5)
113 #define LTR91100_PS_MEAS_RATE_REG_MEAS_RATE__MSK          (0x0E)
114 #define LTR91100_PS_MEAS_RATE_REG_MEAS_RATE__REG          (LTR91100_PS_MEAS_RATE)
115 
116 #define LTR91100_INTR_PRST_REG_PS_PERSIST__POS            (4)
117 #define LTR91100_INTR_PRST_REG_PS_PERSIST__MSK            (0xF0)
118 #define LTR91100_INTR_PRST_REG_PS_PERSIST__REG            (LTR91100_INTR_PRST)
119 
120 #define LTR91100_GS_LED_REG_GLED_DRIVE__POS               (0)
121 #define LTR91100_GS_LED_REG_GLED_DRIVE__MSK               (0x03)
122 #define LTR91100_GS_LED_REG_GLED_DRIVE__REG               (LTR91100_GS_LED)
123 
124 #define LTR91100_GS_LED_REG_GLED_BOOST__POS               (2)
125 #define LTR91100_GS_LED_REG_GLED_BOOST__MSK               (0x0C)
126 #define LTR91100_GS_LED_REG_GLED_BOOST__REG               (LTR91100_GS_LED)
127 
128 #define LTR91100_GS_WAIT_REG_PULSES__POS                  (0)
129 #define LTR91100_GS_WAIT_REG_PULSES__MSK                  (0x0F)
130 #define LTR91100_GS_WAIT_REG_PULSES__REG                  (LTR91100_GS_WAIT)
131 
132 #define LTR91100_GS_WAIT_REG_WAIT_TIME__POS               (4)
133 #define LTR91100_GS_WAIT_REG_WAIT_TIME__MSK               (0x70)
134 #define LTR91100_GS_WAIT_REG_WAIT_TIME__REG               (LTR91100_GS_WAIT)
135 
136 #define LTR91100_GS_PRST_REG_GS_PERSIST__POS              (2)
137 #define LTR91100_GS_PRST_REG_GS_PERSIST__MSK              (0x0C)
138 #define LTR91100_GS_PRST_REG_GS_PERSIST__REG              (LTR91100_GS_PRST)
139 
140 #define LTR91100_PS_STATUS_REG_PS_DATA_STATUS__POS        (0)
141 #define LTR91100_PS_STATUS_REG_PS_DATA_STATUS__MSK        (0x01)
142 #define LTR91100_PS_STATUS_REG_PS_DATA_STATUS__REG        (LTR91100_PS_STATUS)
143 
144 #define LTR91100_PS_STATUS_REG_PS_INT_STATUS__POS         (1)
145 #define LTR91100_PS_STATUS_REG_PS_INT_STATUS__MSK         (0x02)
146 #define LTR91100_PS_STATUS_REG_PS_INT_STATUS__REG         (LTR91100_PS_STATUS)
147 
148 #define LTR91100_PS_STATUS_REG_PS_DATA_VALIDITY__POS      (2)
149 #define LTR91100_PS_STATUS_REG_PS_DATA_VALIDITY__MSK      (0x04)
150 #define LTR91100_PS_STATUS_REG_PS_DATA_VALIDITY__REG      (LTR91100_PS_STATUS)
151 
152 #define LTR91100_PS_STATUS_REG_FAR_STATUS__POS            (3)
153 #define LTR91100_PS_STATUS_REG_FAR_STATUS__MSK            (0x08)
154 #define LTR91100_PS_STATUS_REG_FAR_STATUS__REG            (LTR91100_PS_STATUS)
155 
156 #define LTR91100_PS_STATUS_REG_NEAR_STATUS__POS           (4)
157 #define LTR91100_PS_STATUS_REG_NEAR_STATUS__MSK           (0x10)
158 #define LTR91100_PS_STATUS_REG_NEAR_STATUS__REG           (LTR91100_PS_STATUS)
159 
160 #define LTR91100_GS_STATUS_REG_GS_DATA_STATUS__POS        (3)
161 #define LTR91100_GS_STATUS_REG_GS_DATA_STATUS__MSK        (0x08)
162 #define LTR91100_GS_STATUS_REG_GS_DATA_STATUS__REG        (LTR91100_GS_STATUS)
163 
164 #define LTR91100_GS_STATUS_REG_GS_INT_STATUS__POS         (4)
165 #define LTR91100_GS_STATUS_REG_GS_INT_STATUS__MSK         (0x10)
166 #define LTR91100_GS_STATUS_REG_GS_INT_STATUS__REG         (LTR91100_GS_STATUS)
167 
168 #define LTR91100_GS_STATUS_REG_GS_DATA_VALIDITY__POS      (5)
169 #define LTR91100_GS_STATUS_REG_GS_DATA_VALIDITY__MSK      (0x20)
170 #define LTR91100_GS_STATUS_REG_GS_DATA_VALIDITY__REG      (LTR91100_GS_STATUS)
171 
172 #define LTR91100_GS_STATUS_REG_GS_FIFO_FULL__POS          (6)
173 #define LTR91100_GS_STATUS_REG_GS_FIFO_FULL__MSK          (0x40)
174 #define LTR91100_GS_STATUS_REG_GS_FIFO_FULL__REG          (LTR91100_GS_STATUS)
175 
176 #define LTR91100_GS_STATUS_REG_GS_FIFO_EMPTY__POS         (7)
177 #define LTR91100_GS_STATUS_REG_GS_FIFO_EMPTY__MSK         (0x80)
178 #define LTR91100_GS_STATUS_REG_GS_FIFO_EMPTY__REG         (LTR91100_GS_STATUS)
179 
180 #define LTR91100_GET_BITSLICE(regvar, bitname)            ((regvar & LTR91100_##bitname##__MSK) >> LTR91100_##bitname##__POS)
181 #define LTR91100_SET_BITSLICE(regvar, bitname, val)       ((regvar & ~LTR91100_##bitname##__MSK) | ((val<<LTR91100_##bitname##__POS)&LTR91100_##bitname##__MSK))
182 
183 #define LTR91100_WAIT_TIME_PER_CHECK                      (10)
184 #define LTR91100_WAIT_TIME_TOTAL                          (100)
185 
186 typedef enum {
187     LTR91100_SW_RESET_FALSE = 0x00,
188     LTR91100_SW_RESET_TRUE = 0x01,
189 } LTR91100_CFG_SW_RESET;
190 
191 typedef enum {
192     LTR91100_PS_INT_DISABLE = 0x00,
193     LTR91100_PS_INT_ENABLE = 0x01,
194 } LTR91100_CFG_PS_INT;
195 
196 typedef enum {
197     LTR91100_PS_STANDBY = 0x00,
198     LTR91100_PS_ACTIVE = 0x01,
199 } LTR91100_CFG_PS_MODE;
200 
201 typedef enum {
202     LTR91100_PS_GAIN_1X = 0x00,
203 } LTR91100_CFG_PS_GAIN;
204 
205 typedef enum {
206     LTR91100_PS_OFFSET_DISABLE = 0x00,
207     LTR91100_PS_OFFSET_ENABLE = 0x01,
208 } LTR91100_CFG_PS_OFFSET_EN;
209 
210 typedef enum {
211     LTR91100_NEAR_FAR_STATUS_DISABLE = 0x00,
212     LTR91100_NEAR_FAR_STATUS_ENABLE = 0x01,
213 } LTR91100_CFG_NEAR_FAR_STATUS_EN;
214 
215 typedef enum {
216     LTR91100_GS_INT_DISABLE = 0x00,
217     LTR91100_GS_INT_ENABLE = 0x01,
218 } LTR91100_CFG_GS_INT;
219 
220 typedef enum {
221     LTR91100_GS_STANDBY = 0x00,
222     LTR91100_GS_ACTIVE = 0x01,
223 } LTR91100_CFG_GS_MODE;
224 
225 typedef enum {
226     LTR91100_GS_GAIN_1X = 0x00,
227 } LTR91100_CFG_GS_GAIN;
228 
229 typedef enum {
230     LTR91100_GS_OFFSET_DISABLE = 0x00,
231     LTR91100_GS_OFFSET_ENABLE = 0x01,
232 } LTR91100_CFG_GS_OFFSET_EN;
233 
234 typedef enum {
235     LTR91100_GS_FIFO_RESET_FALSE = 0x00,
236     LTR91100_GS_FIFO_RESET_TRUE = 0x01,
237 } LTR91100_CFG_GS_FIFO_RESET;
238 
239 typedef enum {
240     LTR91100_GS_FORCE_START_DISABLE = 0x00,
241     LTR91100_GS_FORCE_START_ENABLE = 0x01,
242 } LTR91100_CFG_GS_FORCE_START;
243 
244 typedef enum {
245     LTR91100_PLED_DRIVE_100mA = 0x00,
246     LTR91100_PLED_DRIVE_50mA = 0x01,
247     LTR91100_PLED_DRIVE_25mA = 0x02,
248     LTR91100_PLED_DRIVE_12_5mA = 0x03,
249 } LTR91100_CFG_PLED_DRIVE;
250 
251 typedef enum {
252     LTR91100_PLED_BOOST_1X = 0x00,
253     LTR91100_PLED_BOOST_1_5X = 0x01,
254     LTR91100_PLED_BOOST_2X = 0x02,
255     LTR91100_PLED_BOOST_3X = 0x03,
256 } LTR91100_CFG_PLED_BOOST;
257 
258 typedef enum {
259     LTR91100_PS_MEAS_RATE_6_125 = 0x00,                   /* GS Measurement Repeat Rate = 6.125ms */
260     LTR91100_PS_MEAS_RATE_50 = 0x01,                      /* GS Measurement Repeat Rate = 50ms */
261     LTR91100_PS_MEAS_RATE_100 = 0x02,                     /* GS Measurement Repeat Rate = 100ms (default) */
262     LTR91100_PS_MEAS_RATE_200 = 0x03,                     /* GS Measurement Repeat Rate = 200ms */
263     LTR91100_PS_MEAS_RATE_400 = 0x04,                     /* GS Measurement Repeat Rate = 400ms */
264     LTR91100_PS_MEAS_RATE_800 = 0x05,                     /* GS Measurement Repeat Rate = 800ms */
265     LTR91100_PS_MEAS_RATE_12_5 = 0x06,                    /* GS Measurement Repeat Rate = 12.5ms */
266     LTR91100_PS_MEAS_RATE_25 = 0x07,                      /* GS Measurement Repeat Rate = 25ms */
267 } LTR91100_CFG_PS_MEAS_RATE;
268 
269 typedef enum {
270     LTR91100_GLED_DRIVE_100mA = 0x00,
271     LTR91100_GLED_DRIVE_50mA = 0x01,
272     LTR91100_GLED_DRIVE_25mA = 0x02,
273     LTR91100_GLED_DRIVE_12_5mA = 0x03,
274 } LTR91100_CFG_GLED_DRIVE;
275 
276 typedef enum {
277     LTR91100_GLED_BOOST_1X = 0x00,
278     LTR91100_GLED_BOOST_1_5X = 0x01,
279     LTR91100_GLED_BOOST_2X = 0x02,
280     LTR91100_GLED_BOOST_3X = 0x03,
281 } LTR91100_CFG_GLED_BOOST;
282 
283 typedef enum {
284     LTR91100_GS_WAIT_TIME_0ms = 0x00,
285     LTR91100_GS_WAIT_TIME_2ms = 0x01,
286     LTR91100_GS_WAIT_TIME_4ms = 0x02,
287     LTR91100_GS_WAIT_TIME_6ms = 0x03,
288     LTR91100_GS_WAIT_TIME_10ms = 0x04,
289     LTR91100_GS_WAIT_TIME_14ms = 0x05,
290     LTR91100_GS_WAIT_TIME_18ms = 0x06,
291     LTR91100_GS_WAIT_TIME_22ms = 0x07,
292 } LTR91100_CFG_GS_WAIT_TIME;
293 
294 typedef enum {
295     LTR91100_GS_PERSIST_1st = 0x00,                       /* 1st gesture exit occurrence will exit gesture detections */
296     LTR91100_GS_PERSIST_2nd = 0x01,                       /* 2nd gesture exit occurrence will exit gesture detections */
297     LTR91100_GS_PERSIST_3rd = 0x02,                       /* 3rd gesture exit occurrence will exit gesture detections */
298     LTR91100_GS_PERSIST_4th = 0x03,                       /* 4th gesture exit occurrence will exit gesture detections */
299 } LTR91100_CFG_GS_PERSIST;
300 
301 typedef enum {
302     LTR91100_PS_DATA_STATUS_OLD = 0x00,
303     LTR91100_PS_DATA_STATUS_NEW = 0x01,
304 } LTR91100_CFG_PS_DATA_STATUS;
305 
306 typedef enum {
307     LTR91100_PS_INT_STATUS_INACTIVE = 0x00,
308     LTR91100_PS_INT_STATUS_ACTIVE = 0x01,
309 } LTR91100_CFG_PS_INT_STATUS;
310 
311 typedef enum {
312     LTR91100_PS_DATA_VALID = 0x00,
313     LTR91100_PS_DATA_INVALID = 0x01,
314 } LTR91100_CFG_PS_DATA_VALIDITY;
315 
316 typedef enum {
317     LTR91100_PS_FAR_STATUS_FALSE = 0x00,
318     LTR91100_PS_FAR_STATUS_TRUE = 0x01,
319 } LTR91100_CFG_PS_FAR_STATUS;
320 
321 typedef enum {
322     LTR91100_PS_NEAR_STATUS_FALSE = 0x00,
323     LTR91100_PS_NEAR_STATUS_TRUE = 0x01,
324 } LTR91100_CFG_PS_NEAR_STATUS;
325 
326 typedef enum {
327     LTR91100_GS_DATA_STATUS_OLD = 0x00,
328     LTR91100_GS_DATA_STATUS_NEW = 0x01,
329 } LTR91100_CFG_GS_DATA_STATUS;
330 
331 typedef enum {
332     LTR91100_GS_INT_STATUS_INACTIVE = 0x00,
333     LTR91100_GS_INT_STATUS_ACTIVE = 0x01,
334 } LTR91100_CFG_GS_INT_STATUS;
335 
336 typedef enum {
337     LTR91100_GS_DATA_VALID = 0x00,
338     LTR91100_GS_DATA_INVALID = 0x01,
339 } LTR91100_CFG_GS_DATA_VALIDITY;
340 
341 typedef enum {
342     LTR91100_GS_FIFO_FULL_FALSE = 0x00,
343     LTR91100_GS_FIFO_FULL_TRUE = 0x01,
344 } LTR91100_CFG_GS_FIFO_FULL;
345 
346 typedef enum {
347     LTR91100_GS_FIFO_EMPTY_TRUE = 0x00,
348     LTR91100_GS_FIFO_EMPTY_FALSE = 0x01,
349 } LTR91100_CFG_GS_FIFO_EMPTY;
350 
351 i2c_dev_t ltr91100_ctx = {
352     .port = 3,
353     .config.address_width = 8,
354     .config.freq = 100000,
355     .config.dev_addr = LTR91100_I2C_ADDR,
356 };
357 
358 static uint8_t g_init_bitwise = 0;
359 
drv_gs_liteon_ltr91100_validate_id(i2c_dev_t * drv,uint8_t part_id,uint8_t manufac_id)360 static int drv_gs_liteon_ltr91100_validate_id(i2c_dev_t* drv, uint8_t part_id, uint8_t manufac_id)
361 {
362     int     ret = 0;
363     uint8_t part_id_value = 0;
364     uint8_t manufac_id_value = 0;
365 
366     if (drv == NULL) {
367         return -1;
368     }
369 
370     ret = sensor_i2c_read(drv, LTR91100_PART_ID, &part_id_value, I2C_DATA_LEN, I2C_OP_RETRIES);
371     if (unlikely(ret)) {
372         return ret;
373     }
374 
375     ret = sensor_i2c_read(drv, LTR91100_MANUFAC_ID, &manufac_id_value, I2C_DATA_LEN, I2C_OP_RETRIES);
376     if (unlikely(ret)) {
377         return ret;
378     }
379 
380     if (part_id_value != part_id || manufac_id_value != manufac_id) {
381         return -1;
382     }
383 
384     return 0;
385 }
386 
drv_gs_liteon_ltr91100_set_power_mode(i2c_dev_t * drv,dev_power_mode_e mode)387 static int drv_gs_liteon_ltr91100_set_power_mode(i2c_dev_t* drv, dev_power_mode_e mode)
388 {
389     int     ret = 0;
390     uint8_t ps_dev_mode = 0, gs_dev_mode = 0;
391     uint8_t ps_value = 0, gs_value = 0;
392 
393     ret = sensor_i2c_read(drv, LTR91100_PS_CTRL, &ps_value, I2C_DATA_LEN, I2C_OP_RETRIES);
394     if (unlikely(ret)) {
395         return ret;
396     }
397     ret = sensor_i2c_read(drv, LTR91100_GS_CTRL, &gs_value, I2C_DATA_LEN, I2C_OP_RETRIES);
398     if (unlikely(ret)) {
399         return ret;
400     }
401 
402     switch (mode) {
403         case DEV_POWER_OFF:
404         case DEV_SLEEP:
405             ps_dev_mode = LTR91100_SET_BITSLICE(ps_value, PS_CTRL_REG_PS_MODE, LTR91100_PS_STANDBY);
406             gs_dev_mode = LTR91100_SET_BITSLICE(gs_value, GS_CTRL_REG_GS_MODE, LTR91100_GS_STANDBY);
407             break;
408         case DEV_POWER_ON:
409             ps_dev_mode = LTR91100_SET_BITSLICE(ps_value, PS_CTRL_REG_PS_MODE, LTR91100_PS_ACTIVE);
410             gs_dev_mode = LTR91100_SET_BITSLICE(gs_value, GS_CTRL_REG_GS_MODE, LTR91100_GS_ACTIVE);
411             break;
412         default:
413             return -1;
414     }
415 
416     ret = sensor_i2c_write(drv, LTR91100_PS_CTRL, &ps_dev_mode, I2C_DATA_LEN, I2C_OP_RETRIES);
417     if (unlikely(ret)) {
418         return ret;
419     }
420     ret = sensor_i2c_write(drv, LTR91100_GS_CTRL, &gs_dev_mode, I2C_DATA_LEN, I2C_OP_RETRIES);
421     if (unlikely(ret)) {
422         return ret;
423     }
424 
425     return 0;
426 }
427 
drv_gs_liteon_ltr91100_is_ready(i2c_dev_t * drv)428 UNUSED static int drv_gs_liteon_ltr91100_is_ready(i2c_dev_t* drv)
429 {
430     int     ret = 0;
431     uint8_t value = 0;
432 
433     ret = sensor_i2c_read(drv, LTR91100_GS_STATUS, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
434     if (unlikely(ret)) {
435         return 0;
436     }
437 
438     ret = (LTR91100_GET_BITSLICE(value, GS_STATUS_REG_GS_DATA_STATUS) == LTR91100_GS_DATA_STATUS_NEW) ? 1 : 0;
439 
440     return ret;
441 }
442 
drv_gs_liteon_ltr91100_set_default_config(i2c_dev_t * drv)443 static int drv_gs_liteon_ltr91100_set_default_config(i2c_dev_t* drv)
444 {
445     int     ret = 0;
446     uint8_t value = 0;
447 
448     ret = sensor_i2c_read(drv, LTR91100_PS_CTRL, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
449     if (unlikely(ret)) {
450         return ret;
451     }
452     value = LTR91100_SET_BITSLICE(value, PS_CTRL_REG_PS_INT, LTR91100_PS_INT_DISABLE);
453     value = LTR91100_SET_BITSLICE(value, PS_CTRL_REG_PS_GAIN, LTR91100_PS_GAIN_1X);
454     value = LTR91100_SET_BITSLICE(value, PS_CTRL_REG_PS_OFFSET_EN, LTR91100_PS_OFFSET_DISABLE);
455     value = LTR91100_SET_BITSLICE(value, PS_CTRL_REG_NEAR_FAR_STATUS_EN, LTR91100_NEAR_FAR_STATUS_DISABLE);
456     ret = sensor_i2c_write(drv, LTR91100_PS_CTRL, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
457     if (unlikely(ret)) {
458         return ret;
459     }
460 
461     ret = sensor_i2c_read(drv, LTR91100_GS_CTRL, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
462     if (unlikely(ret)) {
463         return ret;
464     }
465     value = LTR91100_SET_BITSLICE(value, GS_CTRL_REG_GS_INT, LTR91100_GS_INT_DISABLE);
466     value = LTR91100_SET_BITSLICE(value, GS_CTRL_REG_GS_GAIN, LTR91100_GS_GAIN_1X);
467     value = LTR91100_SET_BITSLICE(value, GS_CTRL_REG_GS_OFFSET_EN, LTR91100_GS_OFFSET_DISABLE);
468     value = LTR91100_SET_BITSLICE(value, GS_CTRL_REG_GS_FIFO_RESET, LTR91100_GS_FIFO_RESET_FALSE);
469     value = LTR91100_SET_BITSLICE(value, GS_CTRL_REG_GS_FORCE_START, LTR91100_GS_FORCE_START_ENABLE);
470     ret = sensor_i2c_write(drv, LTR91100_GS_CTRL, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
471     if (unlikely(ret)) {
472         return ret;
473     }
474 
475     value = 0;
476     value = LTR91100_SET_BITSLICE(value, PS_LED_REG_PLED_DRIVE, LTR91100_PLED_DRIVE_100mA);
477     value = LTR91100_SET_BITSLICE(value, PS_LED_REG_PLED_BOOST, LTR91100_PLED_BOOST_1X);
478     ret = sensor_i2c_write(drv, LTR91100_PS_LED, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
479     if (unlikely(ret)) {
480         return ret;
481     }
482 
483     value = 0;
484     value = LTR91100_SET_BITSLICE(value, PS_MEAS_RATE_REG_PULSES, 10);
485     value = LTR91100_SET_BITSLICE(value, PS_MEAS_RATE_REG_MEAS_RATE, LTR91100_PS_MEAS_RATE_100);
486     ret = sensor_i2c_write(drv, LTR91100_PS_MEAS_RATE, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
487     if (unlikely(ret)) {
488         return ret;
489     }
490 
491     value = 0;
492     value = LTR91100_SET_BITSLICE(value, GS_LED_REG_GLED_DRIVE, LTR91100_GLED_DRIVE_100mA);
493     value = LTR91100_SET_BITSLICE(value, GS_LED_REG_GLED_BOOST, LTR91100_GLED_BOOST_1X);
494     ret = sensor_i2c_write(drv, LTR91100_GS_LED, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
495     if (unlikely(ret)) {
496         return ret;
497     }
498 
499     value = 0;
500     value = LTR91100_SET_BITSLICE(value, GS_WAIT_REG_PULSES, 10);
501     value = LTR91100_SET_BITSLICE(value, GS_WAIT_REG_WAIT_TIME, LTR91100_GS_WAIT_TIME_0ms);
502     ret = sensor_i2c_write(drv, LTR91100_GS_WAIT, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
503     if (unlikely(ret)) {
504         return ret;
505     }
506 
507     return 0;
508 }
509 
drv_gs_liteon_ltr91100_irq_handle(void)510 static void drv_gs_liteon_ltr91100_irq_handle(void)
511 {
512     /* no handle so far */
513 }
514 
drv_gs_liteon_ltr91100_open(void)515 static int drv_gs_liteon_ltr91100_open(void)
516 {
517     int ret = 0;
518 
519     ret = drv_gs_liteon_ltr91100_set_power_mode(&ltr91100_ctx, DEV_POWER_ON);
520     if (unlikely(ret)) {
521         return -1;
522     }
523 
524     LOG("%s %s successfully \n", SENSOR_STR, __func__);
525     return 0;
526 
527 }
528 
drv_gs_liteon_ltr91100_close(void)529 static int drv_gs_liteon_ltr91100_close(void)
530 {
531     int ret = 0;
532 
533     ret = drv_gs_liteon_ltr91100_set_power_mode(&ltr91100_ctx, DEV_POWER_OFF);
534     if (unlikely(ret)) {
535         return -1;
536     }
537 
538     LOG("%s %s successfully \n", SENSOR_STR, __func__);
539     return 0;
540 }
541 
drv_gs_liteon_ltr91100_read(void * buf,size_t len)542 static int drv_gs_liteon_ltr91100_read(void *buf, size_t len)
543 {
544     int ret = 0;
545     size_t size;
546     uint8_t gs_ndata = 0, gs_sdata = 0, gs_edata = 0, gs_wdata = 0;
547     uint8_t ps_data[2] = { 0 };
548     proximity_data_t * pdata = (proximity_data_t *) buf;
549 
550     if (buf == NULL) {
551         return -1;
552     }
553 
554     size = sizeof(proximity_data_t);
555     if (len < size) {
556         return -1;
557     }
558 
559     ret = sensor_i2c_read(&ltr91100_ctx, LTR91100_PS_DATA_LSB, &ps_data[0], I2C_DATA_LEN, I2C_OP_RETRIES);
560     if (unlikely(ret)) {
561         return -1;
562     }
563     ret = sensor_i2c_read(&ltr91100_ctx, LTR91100_PS_DATA_MSB, &ps_data[1], I2C_DATA_LEN, I2C_OP_RETRIES);
564     if (unlikely(ret)) {
565         return -1;
566     }
567 
568     ret = sensor_i2c_read(&ltr91100_ctx, LTR91100_GS_FIFO_ACCESS_N, &gs_ndata, I2C_DATA_LEN, I2C_OP_RETRIES);
569     if (unlikely(ret)) {
570         return -1;
571     }
572     ret = sensor_i2c_read(&ltr91100_ctx, LTR91100_GS_FIFO_ACCESS_S, &gs_sdata, I2C_DATA_LEN, I2C_OP_RETRIES);
573     if (unlikely(ret)) {
574         return -1;
575     }
576     ret = sensor_i2c_read(&ltr91100_ctx, LTR91100_GS_FIFO_ACCESS_E, &gs_edata, I2C_DATA_LEN, I2C_OP_RETRIES);
577     if (unlikely(ret)) {
578         return -1;
579     }
580     ret = sensor_i2c_read(&ltr91100_ctx, LTR91100_GS_FIFO_ACCESS_W, &gs_wdata, I2C_DATA_LEN, I2C_OP_RETRIES);
581     if (unlikely(ret)) {
582         return -1;
583     }
584 
585     pdata->present = gs_ndata;
586     pdata->timestamp = aos_now_ms();
587 
588     return (int) size;
589 }
590 
drv_gs_liteon_ltr91100_write(const void * buf,size_t len)591 static int drv_gs_liteon_ltr91100_write(const void *buf, size_t len)
592 {
593     (void) buf;
594     (void) len;
595 
596     return 0;
597 }
598 
drv_gs_liteon_ltr91100_ioctl(int cmd,unsigned long arg)599 static int drv_gs_liteon_ltr91100_ioctl(int cmd, unsigned long arg)
600 {
601     int ret = 0;
602 
603     switch (cmd) {
604         case SENSOR_IOCTL_SET_POWER: {
605             ret = drv_gs_liteon_ltr91100_set_power_mode(&ltr91100_ctx, arg);
606             if (unlikely(ret)) {
607                 return -1;
608             }
609         } break;
610         case SENSOR_IOCTL_GET_INFO: {
611             /* fill the dev info here */
612             dev_sensor_info_t *info = (dev_sensor_info_t *) arg;
613             info->vendor = DEV_SENSOR_VENDOR_LITEON;
614             info->model = "LTR91100";
615             info->unit = cm;
616         } break;
617         default:
618             return -1;
619     }
620 
621     LOG("%s %s successfully \n", SENSOR_STR, __func__);
622     return 0;
623 }
624 
drv_gs_liteon_ltr91100_init(void)625 int drv_gs_liteon_ltr91100_init(void)
626 {
627     int ret = 0;
628     sensor_obj_t sensor_gs;
629     memset(&sensor_gs, 0, sizeof(sensor_gs));
630 
631     if (!g_init_bitwise) {
632         ret = drv_gs_liteon_ltr91100_validate_id(&ltr91100_ctx, LTR91100_PART_ID_VAL, LTR91100_MANUFAC_ID_VAL);
633         if (unlikely(ret)) {
634             return -1;
635         }
636     }
637 
638     if (!g_init_bitwise) {
639         /* fill the sensor_gs obj parameters here */
640         sensor_gs.tag = TAG_DEV_GS;
641         sensor_gs.path = dev_gs_path;
642         sensor_gs.io_port = I2C_PORT;
643         sensor_gs.mode = DEV_POLLING;
644         sensor_gs.power = DEV_POWER_OFF;
645         sensor_gs.open = drv_gs_liteon_ltr91100_open;
646         sensor_gs.close = drv_gs_liteon_ltr91100_close;
647         sensor_gs.read = drv_gs_liteon_ltr91100_read;
648         sensor_gs.write = drv_gs_liteon_ltr91100_write;
649         sensor_gs.ioctl = drv_gs_liteon_ltr91100_ioctl;
650         sensor_gs.irq_handle = drv_gs_liteon_ltr91100_irq_handle;
651 
652         ret = sensor_create_obj(&sensor_gs);
653         if (unlikely(ret)) {
654             return -1;
655         }
656 
657         ret = drv_gs_liteon_ltr91100_set_default_config(&ltr91100_ctx);
658         if (unlikely(ret)) {
659             return -1;
660         }
661 
662         g_init_bitwise = 1;
663     }
664 
665     LOG("%s %s successfully \n", SENSOR_STR, __func__);
666     return 0;
667 }
668 
669 SENSOR_DRV_ADD(drv_gs_liteon_ltr91100_init);
670 
671