Commit | Line | Data |
---|---|---|
6fa3eb70 S |
1 | |
2 | #include "alsps.h" | |
3 | ||
4 | static struct alsps_context *alsps_context_obj = NULL; | |
5 | ||
6 | ||
7 | static struct alsps_init_info* alsps_init_list[MAX_CHOOSE_ALSPS_NUM]= {0}; //modified | |
8 | static void alsps_early_suspend(struct early_suspend *h); | |
9 | static void alsps_late_resume(struct early_suspend *h); | |
10 | ||
11 | ||
12 | int als_data_report(struct input_dev *dev, int value, int status) | |
13 | { | |
14 | //ALSPS_LOG("+als_data_report! %d, %d\n",value,status); | |
15 | input_report_abs(dev, EVENT_TYPE_ALS_VALUE, value); | |
16 | input_report_abs(dev, EVENT_TYPE_ALS_STATUS, status); | |
17 | input_sync(dev); | |
18 | return 0; | |
19 | } | |
20 | ||
21 | int ps_data_report(struct input_dev *dev, int value,int status) | |
22 | { | |
23 | //ALSPS_LOG("+ps_data_report! %d, %d\n",value,status); | |
24 | input_report_abs(dev, EVENT_TYPE_PS_VALUE, value); | |
25 | input_report_abs(dev, EVENT_TYPE_PS_STATUS, status); | |
26 | input_sync(dev); | |
27 | return 0; | |
28 | } | |
29 | ||
30 | static void als_work_func(struct work_struct *work) | |
31 | { | |
32 | ||
33 | struct alsps_context *cxt = NULL; | |
34 | int out_size; | |
35 | //hwm_sensor_data sensor_data; | |
36 | int value,status,div; | |
37 | int64_t nt; | |
38 | struct timespec time; | |
39 | int err, idx; | |
40 | ||
41 | cxt = alsps_context_obj; | |
42 | ||
43 | if(NULL == cxt->als_data.get_data) | |
44 | { | |
45 | ALSPS_LOG("alsps driver not register data path\n"); | |
46 | } | |
47 | ||
48 | ||
49 | time.tv_sec = time.tv_nsec = 0; | |
50 | time = get_monotonic_coarse(); | |
51 | nt = time.tv_sec*1000000000LL+time.tv_nsec; | |
52 | ||
53 | //add wake lock to make sure data can be read before system suspend | |
54 | err = cxt->als_data.get_data(&value,&status); | |
55 | ||
56 | if(err) | |
57 | { | |
58 | ALSPS_ERR("get alsps data fails!!\n" ); | |
59 | goto als_loop; | |
60 | } | |
61 | else | |
62 | { | |
63 | { | |
64 | cxt->drv_data.als_data.values[0] = value; | |
65 | cxt->drv_data.als_data.status = status; | |
66 | cxt->drv_data.als_data.time = nt; | |
67 | ||
68 | } | |
69 | } | |
70 | ||
71 | if(true == cxt->is_als_first_data_after_enable) | |
72 | { | |
73 | cxt->is_als_first_data_after_enable = false; | |
74 | //filter -1 value | |
75 | if(ALSPS_INVALID_VALUE == cxt->drv_data.als_data.values[0] ) | |
76 | { | |
77 | ALSPS_LOG(" read invalid data \n"); | |
78 | goto als_loop; | |
79 | ||
80 | } | |
81 | } | |
82 | //report data to input device | |
83 | //printk("new alsps work run....\n"); | |
84 | //ALSPS_LOG("als data[%d] \n" ,cxt->drv_data.als_data.values[0]); | |
85 | ||
86 | als_data_report(cxt->idev, | |
87 | cxt->drv_data.als_data.values[0], | |
88 | cxt->drv_data.als_data.status); | |
89 | ||
90 | als_loop: | |
91 | if(true == cxt->is_als_polling_run) | |
92 | { | |
93 | { | |
94 | mod_timer(&cxt->timer_als, jiffies + atomic_read(&cxt->delay_als)/(1000/HZ)); | |
95 | } | |
96 | ||
97 | } | |
98 | } | |
99 | ||
100 | static void ps_work_func(struct work_struct *work) | |
101 | { | |
102 | ||
103 | struct alsps_context *cxt = NULL; | |
104 | int out_size; | |
105 | //hwm_sensor_data sensor_data; | |
106 | int value,status,div; | |
107 | int64_t nt; | |
108 | struct timespec time; | |
109 | int err, idx; | |
110 | ||
111 | cxt = alsps_context_obj; | |
112 | ||
113 | if(NULL == cxt->ps_data.get_data) | |
114 | { | |
115 | ALSPS_LOG("alsps driver not register data path\n"); | |
116 | } | |
117 | ||
118 | ||
119 | time.tv_sec = time.tv_nsec = 0; | |
120 | time = get_monotonic_coarse(); | |
121 | nt = time.tv_sec*1000000000LL+time.tv_nsec; | |
122 | ||
123 | //add wake lock to make sure data can be read before system suspend | |
124 | err = cxt->ps_data.get_data(&value,&status); | |
125 | ||
126 | if(err) | |
127 | { | |
128 | ALSPS_ERR("get alsps data fails!!\n" ); | |
129 | goto ps_loop; | |
130 | } | |
131 | else | |
132 | { | |
133 | { | |
134 | cxt->drv_data.ps_data.values[0] = value; | |
135 | cxt->drv_data.ps_data.status = status; | |
136 | cxt->drv_data.ps_data.time = nt; | |
137 | ||
138 | } | |
139 | } | |
140 | ||
141 | if(true == cxt->is_ps_first_data_after_enable) | |
142 | { | |
143 | cxt->is_ps_first_data_after_enable = false; | |
144 | //filter -1 value | |
145 | if(ALSPS_INVALID_VALUE == cxt->drv_data.ps_data.values[0]) | |
146 | { | |
147 | ALSPS_LOG(" read invalid data \n"); | |
148 | goto ps_loop; | |
149 | ||
150 | } | |
151 | } | |
152 | //report data to input device | |
153 | //printk("new alsps work run....\n"); | |
154 | //ALSPS_LOG("alsps data[%d,%d,%d] \n" ,cxt->drv_data.alsps_data.values[0], | |
155 | //cxt->drv_data.alsps_data.values[1],cxt->drv_data.alsps_data.values[2]); | |
156 | ||
157 | ps_data_report(cxt->idev, | |
158 | cxt->drv_data.ps_data.values[0], | |
159 | cxt->drv_data.ps_data.status); | |
160 | ||
161 | ps_loop: | |
162 | if(true == cxt->is_ps_polling_run) | |
163 | { | |
164 | { | |
165 | mod_timer(&cxt->timer_ps, jiffies + atomic_read(&cxt->delay_ps)/(1000/HZ)); | |
166 | } | |
167 | ||
168 | } | |
169 | } | |
170 | ||
171 | static void als_poll(unsigned long data) | |
172 | { | |
173 | struct alsps_context *obj = (struct alsps_context *)data; | |
174 | if(obj != NULL) | |
175 | { | |
176 | schedule_work(&obj->report_als); | |
177 | } | |
178 | } | |
179 | ||
180 | static void ps_poll(unsigned long data) | |
181 | { | |
182 | struct alsps_context *obj = (struct alsps_context *)data; | |
183 | if(obj != NULL) | |
184 | { | |
185 | if(obj->ps_ctl.is_polling_mode) | |
186 | schedule_work(&obj->report_ps); | |
187 | } | |
188 | } | |
189 | ||
190 | static struct alsps_context *alsps_context_alloc_object(void) | |
191 | { | |
192 | ||
193 | struct alsps_context *obj = kzalloc(sizeof(*obj), GFP_KERNEL); | |
194 | ALSPS_LOG("alsps_context_alloc_object++++\n"); | |
195 | if(!obj) | |
196 | { | |
197 | ALSPS_ERR("Alloc alsps object error!\n"); | |
198 | return NULL; | |
199 | } | |
200 | atomic_set(&obj->delay_als, 200); /*5Hz*/// set work queue delay time 200ms | |
201 | atomic_set(&obj->delay_ps, 200); /*5Hz*/// set work queue delay time 200ms | |
202 | atomic_set(&obj->wake, 0); | |
203 | INIT_WORK(&obj->report_als, als_work_func); | |
204 | INIT_WORK(&obj->report_ps, ps_work_func); | |
205 | init_timer(&obj->timer_als); | |
206 | init_timer(&obj->timer_ps); | |
207 | obj->timer_als.expires = jiffies + atomic_read(&obj->delay_als)/(1000/HZ); | |
208 | obj->timer_als.function = als_poll; | |
209 | obj->timer_als.data = (unsigned long)obj; | |
210 | ||
211 | obj->timer_ps.expires = jiffies + atomic_read(&obj->delay_ps)/(1000/HZ); | |
212 | obj->timer_ps.function = ps_poll; | |
213 | obj->timer_ps.data = (unsigned long)obj; | |
214 | ||
215 | obj->is_als_first_data_after_enable = false; | |
216 | obj->is_als_polling_run = false; | |
217 | obj->is_ps_first_data_after_enable = false; | |
218 | obj->is_ps_polling_run = false; | |
219 | mutex_init(&obj->alsps_op_mutex); | |
220 | obj->is_als_batch_enable = false;//for batch mode init | |
221 | obj->is_ps_batch_enable = false;//for batch mode init | |
222 | ||
223 | ALSPS_LOG("alsps_context_alloc_object----\n"); | |
224 | return obj; | |
225 | } | |
226 | ||
227 | static int als_real_enable(int enable) | |
228 | { | |
229 | int err =0; | |
230 | struct alsps_context *cxt = NULL; | |
231 | cxt = alsps_context_obj; | |
232 | if(1==enable) | |
233 | { | |
234 | ||
235 | if(true==cxt->is_als_active_data || true ==cxt->is_als_active_nodata) | |
236 | { | |
237 | err = cxt->als_ctl.enable_nodata(1); | |
238 | if(err) | |
239 | { | |
240 | err = cxt->als_ctl.enable_nodata(1); | |
241 | if(err) | |
242 | { | |
243 | err = cxt->als_ctl.enable_nodata(1); | |
244 | if(err) | |
245 | ALSPS_ERR("alsps enable(%d) err 3 timers = %d\n", enable, err); | |
246 | } | |
247 | } | |
248 | ALSPS_LOG("alsps real enable \n" ); | |
249 | } | |
250 | ||
251 | } | |
252 | if(0==enable) | |
253 | { | |
254 | if(false==cxt->is_als_active_data && false ==cxt->is_als_active_nodata) | |
255 | { | |
256 | err = cxt->als_ctl.enable_nodata(0); | |
257 | if(err) | |
258 | { | |
259 | ALSPS_ERR("alsps enable(%d) err = %d\n", enable, err); | |
260 | } | |
261 | ALSPS_LOG("alsps real disable \n" ); | |
262 | } | |
263 | ||
264 | } | |
265 | ||
266 | return err; | |
267 | } | |
268 | static int als_enable_data(int enable) | |
269 | { | |
270 | struct alsps_context *cxt = NULL; | |
271 | int err =0; | |
272 | cxt = alsps_context_obj; | |
273 | if(NULL == cxt->als_ctl.open_report_data) | |
274 | { | |
275 | ALSPS_ERR("no als control path\n"); | |
276 | return -1; | |
277 | } | |
278 | ||
279 | if(1 == enable) | |
280 | { | |
281 | ALSPS_LOG("ALSPS enable data\n"); | |
282 | cxt->is_als_active_data =true; | |
283 | cxt->is_als_first_data_after_enable = true; | |
284 | cxt->als_ctl.open_report_data(1); | |
285 | if(false == cxt->is_als_polling_run && cxt->is_als_batch_enable == false) | |
286 | { | |
287 | if(false == cxt->als_ctl.is_report_input_direct) | |
288 | { | |
289 | mod_timer(&cxt->timer_als, jiffies + atomic_read(&cxt->delay_als)/(1000/HZ)); | |
290 | cxt->is_als_polling_run = true; | |
291 | } | |
292 | } | |
293 | } | |
294 | if(0 == enable) | |
295 | { | |
296 | ALSPS_LOG("ALSPS disable \n"); | |
297 | cxt->is_als_active_data =false; | |
298 | cxt->als_ctl.open_report_data(0); | |
299 | if(true == cxt->is_als_polling_run) | |
300 | { | |
301 | if(false == cxt->als_ctl.is_report_input_direct ) | |
302 | { | |
303 | cxt->is_als_polling_run = false; | |
304 | del_timer_sync(&cxt->timer_als); | |
305 | cancel_work_sync(&cxt->report_als); | |
306 | cxt->drv_data.als_data.values[0] = ALSPS_INVALID_VALUE; | |
307 | } | |
308 | } | |
309 | } | |
310 | als_real_enable(enable); | |
311 | return 0; | |
312 | } | |
313 | ||
314 | static int ps_real_enable(int enable) | |
315 | { | |
316 | int err =0; | |
317 | struct alsps_context *cxt = NULL; | |
318 | cxt = alsps_context_obj; | |
319 | if(1==enable) | |
320 | { | |
321 | ||
322 | if(true==cxt->is_ps_active_data || true ==cxt->is_ps_active_nodata) | |
323 | { | |
324 | err = cxt->ps_ctl.enable_nodata(1); | |
325 | if(err) | |
326 | { | |
327 | err = cxt->ps_ctl.enable_nodata(1); | |
328 | if(err) | |
329 | { | |
330 | err = cxt->ps_ctl.enable_nodata(1); | |
331 | if(err) | |
332 | ALSPS_ERR("ps enable(%d) err 3 timers = %d\n", enable, err); | |
333 | } | |
334 | } | |
335 | ALSPS_LOG("ps real enable \n" ); | |
336 | } | |
337 | ||
338 | } | |
339 | if(0==enable) | |
340 | { | |
341 | if(false==cxt->is_ps_active_data && false ==cxt->is_ps_active_nodata) | |
342 | { | |
343 | err = cxt->ps_ctl.enable_nodata(0); | |
344 | if(err) | |
345 | { | |
346 | ALSPS_ERR("ps enable(%d) err = %d\n", enable, err); | |
347 | } | |
348 | ALSPS_LOG("ps real disable \n" ); | |
349 | } | |
350 | ||
351 | } | |
352 | ||
353 | return err; | |
354 | } | |
355 | static int ps_enable_data(int enable) | |
356 | { | |
357 | struct alsps_context *cxt = NULL; | |
358 | int err =0; | |
359 | cxt = alsps_context_obj; | |
360 | if(NULL == cxt->ps_ctl.open_report_data) | |
361 | { | |
362 | ALSPS_ERR("no ps control path\n"); | |
363 | return -1; | |
364 | } | |
365 | ||
366 | if(1 == enable) | |
367 | { | |
368 | ALSPS_LOG("PS enable data\n"); | |
369 | cxt->is_ps_active_data =true; | |
370 | cxt->is_ps_first_data_after_enable = true; | |
371 | cxt->ps_ctl.open_report_data(1); | |
372 | if(false == cxt->is_ps_polling_run && cxt->is_ps_batch_enable == false) | |
373 | { | |
374 | if(false == cxt->ps_ctl.is_report_input_direct) | |
375 | { | |
376 | mod_timer(&cxt->timer_ps, jiffies + atomic_read(&cxt->delay_ps)/(1000/HZ)); | |
377 | cxt->is_ps_polling_run = true; | |
378 | } | |
379 | } | |
380 | } | |
381 | if(0 == enable) | |
382 | { | |
383 | ALSPS_LOG("PS disable \n"); | |
384 | cxt->is_ps_active_data =false; | |
385 | cxt->ps_ctl.open_report_data(0); | |
386 | if(true == cxt->is_ps_polling_run) | |
387 | { | |
388 | if(false == cxt->ps_ctl.is_report_input_direct ) | |
389 | { | |
390 | cxt->is_ps_polling_run = false; | |
391 | del_timer_sync(&cxt->timer_ps); | |
392 | cancel_work_sync(&cxt->report_ps); | |
393 | cxt->drv_data.ps_data.values[0] = ALSPS_INVALID_VALUE; | |
394 | } | |
395 | } | |
396 | } | |
397 | ps_real_enable(enable); | |
398 | return 0; | |
399 | } | |
400 | ||
401 | static ssize_t als_store_active(struct device* dev, struct device_attribute *attr, | |
402 | const char *buf, size_t count) | |
403 | { | |
404 | ALSPS_LOG("als_store_active buf=%s\n",buf); | |
405 | mutex_lock(&alsps_context_obj->alsps_op_mutex); | |
406 | struct alsps_context *cxt = NULL; | |
407 | int err =0; | |
408 | cxt = alsps_context_obj; | |
409 | ||
410 | if (!strncmp(buf, "1", 1)) | |
411 | { | |
412 | als_enable_data(1); | |
413 | } | |
414 | else if (!strncmp(buf, "0", 1)) | |
415 | { | |
416 | als_enable_data(0); | |
417 | } | |
418 | else | |
419 | { | |
420 | ALSPS_ERR(" alsps_store_active error !!\n"); | |
421 | } | |
422 | mutex_unlock(&alsps_context_obj->alsps_op_mutex); | |
423 | ALSPS_LOG(" alsps_store_active done\n"); | |
424 | return count; | |
425 | } | |
426 | /*----------------------------------------------------------------------------*/ | |
427 | static ssize_t als_show_active(struct device* dev, | |
428 | struct device_attribute *attr, char *buf) | |
429 | { | |
430 | struct alsps_context *cxt = NULL; | |
431 | cxt = alsps_context_obj; | |
432 | int div=cxt->als_data.vender_div; | |
433 | ALSPS_LOG("als vender_div value: %d\n", div); | |
434 | return snprintf(buf, PAGE_SIZE, "%d\n", div); | |
435 | } | |
436 | ||
437 | static ssize_t als_store_delay(struct device* dev, struct device_attribute *attr, | |
438 | const char *buf, size_t count) | |
439 | ||
440 | { | |
441 | mutex_lock(&alsps_context_obj->alsps_op_mutex); | |
442 | struct alsps_context *devobj = (struct alsps_context*)dev_get_drvdata(dev); | |
443 | int delay; | |
444 | int mdelay=0; | |
445 | struct alsps_context *cxt = NULL; | |
446 | int err =0; | |
447 | cxt = alsps_context_obj; | |
448 | if(NULL == cxt->als_ctl.set_delay) | |
449 | { | |
450 | ALSPS_LOG("als_ctl set_delay NULL\n"); | |
451 | mutex_unlock(&alsps_context_obj->alsps_op_mutex); | |
452 | return count; | |
453 | } | |
454 | ||
455 | if (1 != sscanf(buf, "%d", &delay)) { | |
456 | ALSPS_ERR("invalid format!!\n"); | |
457 | mutex_unlock(&alsps_context_obj->alsps_op_mutex); | |
458 | return count; | |
459 | } | |
460 | ||
461 | if(false == cxt->als_ctl.is_report_input_direct) | |
462 | { | |
463 | mdelay = (int)delay/1000/1000; | |
464 | atomic_set(&alsps_context_obj->delay_als, mdelay); | |
465 | } | |
466 | cxt->als_ctl.set_delay(delay); | |
467 | ALSPS_LOG(" als_delay %d ns\n",delay); | |
468 | mutex_unlock(&alsps_context_obj->alsps_op_mutex); | |
469 | return count; | |
470 | ||
471 | } | |
472 | ||
473 | static ssize_t als_show_delay(struct device* dev, | |
474 | struct device_attribute *attr, char *buf) | |
475 | { | |
476 | int len = 0; | |
477 | ALSPS_LOG(" not support now\n"); | |
478 | return len; | |
479 | } | |
480 | ||
481 | ||
482 | static ssize_t als_store_batch(struct device* dev, struct device_attribute *attr, | |
483 | const char *buf, size_t count) | |
484 | { | |
485 | ALSPS_LOG("als_store_batch buf=%s\n",buf); | |
486 | mutex_lock(&alsps_context_obj->alsps_op_mutex); | |
487 | struct alsps_context *cxt = NULL; | |
488 | int err =0; | |
489 | cxt = alsps_context_obj; | |
490 | if(cxt->als_ctl.is_support_batch){ | |
491 | if (!strncmp(buf, "1", 1)) | |
492 | { | |
493 | cxt->is_als_batch_enable = true; | |
494 | } | |
495 | else if (!strncmp(buf, "0", 1)) | |
496 | { | |
497 | cxt->is_als_batch_enable = false; | |
498 | } | |
499 | else | |
500 | { | |
501 | ALSPS_ERR(" als_store_batch error !!\n"); | |
502 | } | |
503 | }else{ | |
504 | ALSPS_LOG(" als_store_batch not supported\n"); | |
505 | } | |
506 | mutex_unlock(&alsps_context_obj->alsps_op_mutex); | |
507 | ALSPS_LOG(" als_store_batch done: %d\n", cxt->is_als_batch_enable); | |
508 | return count; | |
509 | ||
510 | } | |
511 | ||
512 | static ssize_t als_show_batch(struct device* dev, | |
513 | struct device_attribute *attr, char *buf) | |
514 | { | |
515 | return snprintf(buf, PAGE_SIZE, "%d\n", 0); | |
516 | } | |
517 | ||
518 | static ssize_t als_store_flush(struct device* dev, struct device_attribute *attr, | |
519 | const char *buf, size_t count) | |
520 | { | |
521 | mutex_lock(&alsps_context_obj->alsps_op_mutex); | |
522 | struct alsps_context *devobj = (struct alsps_context*)dev_get_drvdata(dev); | |
523 | //do read FIFO data function and report data immediately | |
524 | mutex_unlock(&alsps_context_obj->alsps_op_mutex); | |
525 | return count; | |
526 | } | |
527 | ||
528 | static ssize_t als_show_flush(struct device* dev, | |
529 | struct device_attribute *attr, char *buf) | |
530 | { | |
531 | return snprintf(buf, PAGE_SIZE, "%d\n", 0); | |
532 | } | |
533 | ||
534 | static ssize_t als_show_devnum(struct device* dev, | |
535 | struct device_attribute *attr, char *buf) | |
536 | { | |
537 | char *devname =NULL; | |
538 | devname = dev_name(&alsps_context_obj->idev->dev); | |
539 | return snprintf(buf, PAGE_SIZE, "%s\n", devname+5); | |
540 | } | |
541 | static ssize_t ps_store_active(struct device* dev, struct device_attribute *attr, | |
542 | const char *buf, size_t count) | |
543 | { | |
544 | ALSPS_LOG("ps_store_active buf=%s\n",buf); | |
545 | mutex_lock(&alsps_context_obj->alsps_op_mutex); | |
546 | struct alsps_context *cxt = NULL; | |
547 | int err =0; | |
548 | cxt = alsps_context_obj; | |
549 | ||
550 | if (!strncmp(buf, "1", 1)) | |
551 | { | |
552 | ps_enable_data(1); | |
553 | } | |
554 | else if (!strncmp(buf, "0", 1)) | |
555 | { | |
556 | ps_enable_data(0); | |
557 | } | |
558 | else | |
559 | { | |
560 | ALSPS_ERR(" ps_store_active error !!\n"); | |
561 | } | |
562 | mutex_unlock(&alsps_context_obj->alsps_op_mutex); | |
563 | ALSPS_LOG(" ps_store_active done\n"); | |
564 | return count; | |
565 | } | |
566 | /*----------------------------------------------------------------------------*/ | |
567 | static ssize_t ps_show_active(struct device* dev, | |
568 | struct device_attribute *attr, char *buf) | |
569 | { | |
570 | struct alsps_context *cxt = NULL; | |
571 | cxt = alsps_context_obj; | |
572 | int div=cxt->ps_data.vender_div; | |
573 | ALSPS_LOG("ps vender_div value: %d\n", div); | |
574 | return snprintf(buf, PAGE_SIZE, "%d\n", div); | |
575 | } | |
576 | ||
577 | static ssize_t ps_store_delay(struct device* dev, struct device_attribute *attr, | |
578 | const char *buf, size_t count) | |
579 | ||
580 | { | |
581 | mutex_lock(&alsps_context_obj->alsps_op_mutex); | |
582 | struct alsps_context *devobj = (struct alsps_context*)dev_get_drvdata(dev); | |
583 | int delay; | |
584 | int mdelay=0; | |
585 | struct alsps_context *cxt = NULL; | |
586 | int err =0; | |
587 | cxt = alsps_context_obj; | |
588 | if(NULL == cxt->ps_ctl.set_delay) | |
589 | { | |
590 | ALSPS_LOG("ps_ctl set_delay NULL\n"); | |
591 | mutex_unlock(&alsps_context_obj->alsps_op_mutex); | |
592 | return count; | |
593 | } | |
594 | ||
595 | if (1 != sscanf(buf, "%d", &delay)) { | |
596 | ALSPS_ERR("invalid format!!\n"); | |
597 | mutex_unlock(&alsps_context_obj->alsps_op_mutex); | |
598 | return count; | |
599 | } | |
600 | ||
601 | if(false == cxt->ps_ctl.is_report_input_direct) | |
602 | { | |
603 | mdelay = (int)delay/1000/1000; | |
604 | atomic_set(&alsps_context_obj->delay_ps, mdelay); | |
605 | } | |
606 | cxt->ps_ctl.set_delay(delay); | |
607 | ALSPS_LOG(" ps_delay %d ns\n",delay); | |
608 | mutex_unlock(&alsps_context_obj->alsps_op_mutex); | |
609 | return count; | |
610 | ||
611 | } | |
612 | ||
613 | static ssize_t ps_show_delay(struct device* dev, | |
614 | struct device_attribute *attr, char *buf) | |
615 | { | |
616 | int len = 0; | |
617 | ALSPS_LOG(" not support now\n"); | |
618 | return len; | |
619 | } | |
620 | ||
621 | ||
622 | static ssize_t ps_store_batch(struct device* dev, struct device_attribute *attr, | |
623 | const char *buf, size_t count) | |
624 | { | |
625 | ALSPS_LOG("ps_store_batch buf=%s\n",buf); | |
626 | mutex_lock(&alsps_context_obj->alsps_op_mutex); | |
627 | struct alsps_context *cxt = NULL; | |
628 | int err =0; | |
629 | cxt = alsps_context_obj; | |
630 | if(cxt->ps_ctl.is_support_batch){ | |
631 | if (!strncmp(buf, "1", 1)) | |
632 | { | |
633 | cxt->is_ps_batch_enable = true; | |
634 | } | |
635 | else if (!strncmp(buf, "0", 1)) | |
636 | { | |
637 | cxt->is_ps_batch_enable = false; | |
638 | } | |
639 | else | |
640 | { | |
641 | ALSPS_ERR(" ps_store_batch error !!\n"); | |
642 | } | |
643 | }else{ | |
644 | ALSPS_LOG(" ps_store_batch not supported\n"); | |
645 | } | |
646 | mutex_unlock(&alsps_context_obj->alsps_op_mutex); | |
647 | ALSPS_LOG(" ps_store_batch done: %d\n", cxt->is_ps_batch_enable); | |
648 | return count; | |
649 | ||
650 | } | |
651 | ||
652 | static ssize_t ps_show_batch(struct device* dev, | |
653 | struct device_attribute *attr, char *buf) | |
654 | { | |
655 | return snprintf(buf, PAGE_SIZE, "%d\n", 0); | |
656 | } | |
657 | ||
658 | static ssize_t ps_store_flush(struct device* dev, struct device_attribute *attr, | |
659 | const char *buf, size_t count) | |
660 | { | |
661 | mutex_lock(&alsps_context_obj->alsps_op_mutex); | |
662 | struct alsps_context *devobj = (struct alsps_context*)dev_get_drvdata(dev); | |
663 | //do read FIFO data function and report data immediately | |
664 | mutex_unlock(&alsps_context_obj->alsps_op_mutex); | |
665 | return count; | |
666 | } | |
667 | ||
668 | static ssize_t ps_show_flush(struct device* dev, | |
669 | struct device_attribute *attr, char *buf) | |
670 | { | |
671 | return snprintf(buf, PAGE_SIZE, "%d\n", 0); | |
672 | } | |
673 | ||
674 | static ssize_t ps_show_devnum(struct device* dev, | |
675 | struct device_attribute *attr, char *buf) | |
676 | { | |
677 | char *devname =NULL; | |
678 | devname = dev_name(&alsps_context_obj->idev->dev); | |
679 | return snprintf(buf, PAGE_SIZE, "%s\n", devname+5); | |
680 | } | |
681 | static int als_ps_remove(struct platform_device *pdev) | |
682 | { | |
683 | ALSPS_LOG("als_ps_remove\n"); | |
684 | return 0; | |
685 | } | |
686 | ||
687 | static int als_ps_probe(struct platform_device *pdev) | |
688 | { | |
689 | ALSPS_LOG("als_ps_probe\n"); | |
690 | return 0; | |
691 | } | |
692 | ||
693 | static struct platform_driver als_ps_driver = { | |
694 | .probe = als_ps_probe, | |
695 | .remove = als_ps_remove, | |
696 | .driver = | |
697 | { | |
698 | .name = "als_ps", | |
699 | } | |
700 | }; | |
701 | ||
702 | static int alsps_real_driver_init(void) | |
703 | { | |
704 | int i =0; | |
705 | int err=0; | |
706 | ALSPS_LOG(" alsps_real_driver_init +\n"); | |
707 | for(i = 0; i < MAX_CHOOSE_ALSPS_NUM; i++) | |
708 | { | |
709 | ALSPS_LOG("alsps_real_driver_init i=%d\n",i); | |
710 | if(0 != alsps_init_list[i]) | |
711 | { | |
712 | ALSPS_LOG(" alsps try to init driver %s\n", alsps_init_list[i]->name); | |
713 | err = alsps_init_list[i]->init(); | |
714 | if(0 == err) | |
715 | { | |
716 | ALSPS_LOG(" alsps real driver %s probe ok\n", alsps_init_list[i]->name); | |
717 | break; | |
718 | } | |
719 | } | |
720 | } | |
721 | ||
722 | if(i == MAX_CHOOSE_ALSPS_NUM) | |
723 | { | |
724 | ALSPS_LOG(" alsps_real_driver_init fail\n"); | |
725 | err=-1; | |
726 | } | |
727 | return err; | |
728 | } | |
729 | ||
730 | int alsps_driver_add(struct alsps_init_info* obj) | |
731 | { | |
732 | int err=0; | |
733 | int i =0; | |
734 | ||
735 | ALSPS_FUN(); | |
736 | ||
737 | for(i =0; i < MAX_CHOOSE_ALSPS_NUM; i++ ) | |
738 | { | |
739 | if(i == 0){ | |
740 | ALSPS_LOG("register alsps driver for the first time\n"); | |
741 | if(platform_driver_register(&als_ps_driver)) | |
742 | { | |
743 | ALSPS_ERR("failed to register gensor driver already exist\n"); | |
744 | } | |
745 | } | |
746 | ||
747 | if(NULL == alsps_init_list[i]) | |
748 | { | |
749 | obj->platform_diver_addr = &als_ps_driver; | |
750 | alsps_init_list[i] = obj; | |
751 | break; | |
752 | } | |
753 | } | |
754 | if(NULL==alsps_init_list[i]) | |
755 | { | |
756 | ALSPS_ERR("ALSPS driver add err \n"); | |
757 | err=-1; | |
758 | } | |
759 | ||
760 | return err; | |
761 | } | |
762 | EXPORT_SYMBOL_GPL(alsps_driver_add); | |
763 | ||
764 | int ps_report_interrupt_data(int value) | |
765 | { | |
766 | struct alsps_context *cxt = NULL; | |
767 | int err =0; | |
768 | cxt = alsps_context_obj; | |
769 | ps_data_report(cxt->idev,value,3); | |
770 | ||
771 | return 0; | |
772 | } | |
773 | /*----------------------------------------------------------------------------*/ | |
774 | EXPORT_SYMBOL_GPL(ps_report_interrupt_data); | |
775 | ||
776 | static int alsps_misc_init(struct alsps_context *cxt) | |
777 | { | |
778 | ||
779 | int err=0; | |
780 | cxt->mdev.minor = MISC_DYNAMIC_MINOR; | |
781 | cxt->mdev.name = ALSPS_MISC_DEV_NAME; | |
782 | if((err = misc_register(&cxt->mdev))) | |
783 | { | |
784 | ALSPS_ERR("unable to register alsps misc device!!\n"); | |
785 | } | |
786 | return err; | |
787 | } | |
788 | ||
789 | static void alsps_input_destroy(struct alsps_context *cxt) | |
790 | { | |
791 | struct input_dev *dev = cxt->idev; | |
792 | ||
793 | input_unregister_device(dev); | |
794 | input_free_device(dev); | |
795 | } | |
796 | ||
797 | static int alsps_input_init(struct alsps_context *cxt) | |
798 | { | |
799 | struct input_dev *dev; | |
800 | int err = 0; | |
801 | ||
802 | dev = input_allocate_device(); | |
803 | if (NULL == dev) | |
804 | return -ENOMEM; | |
805 | ||
806 | dev->name = ALSPS_INPUTDEV_NAME; | |
807 | ||
808 | input_set_capability(dev, EV_ABS, EVENT_TYPE_ALS_VALUE); | |
809 | input_set_capability(dev, EV_ABS, EVENT_TYPE_PS_VALUE); | |
810 | input_set_capability(dev, EV_ABS, EVENT_TYPE_ALS_STATUS); | |
811 | input_set_capability(dev, EV_ABS, EVENT_TYPE_PS_STATUS); | |
812 | ||
813 | input_set_abs_params(dev, EVENT_TYPE_ALS_VALUE, ALSPS_VALUE_MIN, ALSPS_VALUE_MAX, 0, 0); | |
814 | input_set_abs_params(dev, EVENT_TYPE_PS_VALUE, ALSPS_VALUE_MIN, ALSPS_VALUE_MAX, 0, 0); | |
815 | input_set_abs_params(dev, EVENT_TYPE_ALS_STATUS, ALSPS_STATUS_MIN, ALSPS_STATUS_MAX, 0, 0); | |
816 | input_set_abs_params(dev, EVENT_TYPE_PS_STATUS, ALSPS_STATUS_MIN, ALSPS_STATUS_MAX, 0, 0); | |
817 | input_set_drvdata(dev, cxt); | |
818 | ||
819 | err = input_register_device(dev); | |
820 | if (err < 0) { | |
821 | input_free_device(dev); | |
822 | return err; | |
823 | } | |
824 | cxt->idev= dev; | |
825 | ||
826 | return 0; | |
827 | } | |
828 | ||
829 | ||
830 | DEVICE_ATTR(alsactive, S_IWUSR | S_IRUGO, als_show_active, als_store_active); | |
831 | DEVICE_ATTR(alsdelay, S_IWUSR | S_IRUGO, als_show_delay, als_store_delay); | |
832 | DEVICE_ATTR(alsbatch, S_IWUSR | S_IRUGO, als_show_batch, als_store_batch); | |
833 | DEVICE_ATTR(alsflush, S_IWUSR | S_IRUGO, als_show_flush, als_store_flush); | |
834 | DEVICE_ATTR(alsdevnum, S_IWUSR | S_IRUGO, als_show_devnum, NULL); | |
835 | DEVICE_ATTR(psactive, S_IWUSR | S_IRUGO, ps_show_active, ps_store_active); | |
836 | DEVICE_ATTR(psdelay, S_IWUSR | S_IRUGO, ps_show_delay, ps_store_delay); | |
837 | DEVICE_ATTR(psbatch, S_IWUSR | S_IRUGO, ps_show_batch, ps_store_batch); | |
838 | DEVICE_ATTR(psflush, S_IWUSR | S_IRUGO, ps_show_flush, ps_store_flush); | |
839 | DEVICE_ATTR(psdevnum, S_IWUSR | S_IRUGO, ps_show_devnum, NULL); | |
840 | ||
841 | ||
842 | static struct attribute *alsps_attributes[] = { | |
843 | &dev_attr_alsactive.attr, | |
844 | &dev_attr_alsdelay.attr, | |
845 | &dev_attr_alsbatch.attr, | |
846 | &dev_attr_alsflush.attr, | |
847 | &dev_attr_alsdevnum.attr, | |
848 | &dev_attr_psactive.attr, | |
849 | &dev_attr_psdelay.attr, | |
850 | &dev_attr_psbatch.attr, | |
851 | &dev_attr_psflush.attr, | |
852 | &dev_attr_psdevnum.attr, | |
853 | NULL | |
854 | }; | |
855 | ||
856 | static struct attribute_group alsps_attribute_group = { | |
857 | .attrs = alsps_attributes | |
858 | }; | |
859 | ||
860 | int als_register_data_path(struct als_data_path *data) | |
861 | { | |
862 | struct alsps_context *cxt = NULL; | |
863 | int err =0; | |
864 | cxt = alsps_context_obj; | |
865 | cxt->als_data.get_data = data->get_data; | |
866 | cxt->als_data.vender_div = data->vender_div; | |
867 | ALSPS_LOG("alsps register data path vender_div: %d\n", cxt->als_data.vender_div); | |
868 | if(NULL == cxt->als_data.get_data) | |
869 | { | |
870 | ALSPS_LOG("alsps register data path fail \n"); | |
871 | return -1; | |
872 | } | |
873 | return 0; | |
874 | } | |
875 | ||
876 | int ps_register_data_path(struct ps_data_path *data) | |
877 | { | |
878 | struct alsps_context *cxt = NULL; | |
879 | int err =0; | |
880 | cxt = alsps_context_obj; | |
881 | cxt->ps_data.get_data = data->get_data; | |
882 | cxt->ps_data.vender_div = data->vender_div; | |
883 | ALSPS_LOG("alsps register data path vender_div: %d\n", cxt->ps_data.vender_div); | |
884 | if(NULL == cxt->ps_data.get_data) | |
885 | { | |
886 | ALSPS_LOG("alsps register data path fail \n"); | |
887 | return -1; | |
888 | } | |
889 | return 0; | |
890 | } | |
891 | ||
892 | int als_register_control_path(struct als_control_path *ctl) | |
893 | { | |
894 | struct alsps_context *cxt = NULL; | |
895 | int err =0; | |
896 | cxt = alsps_context_obj; | |
897 | cxt->als_ctl.set_delay = ctl->set_delay; | |
898 | cxt->als_ctl.open_report_data= ctl->open_report_data; | |
899 | cxt->als_ctl.enable_nodata = ctl->enable_nodata; | |
900 | cxt->als_ctl.is_support_batch = ctl->is_support_batch; | |
901 | cxt->als_ctl.is_report_input_direct= ctl->is_report_input_direct; | |
902 | ||
903 | if(NULL==cxt->als_ctl.set_delay || NULL==cxt->als_ctl.open_report_data | |
904 | || NULL==cxt->als_ctl.enable_nodata) | |
905 | { | |
906 | ALSPS_LOG("alsps register control path fail \n"); | |
907 | return -1; | |
908 | } | |
909 | ||
910 | return 0; | |
911 | } | |
912 | ||
913 | int ps_register_control_path(struct ps_control_path *ctl) | |
914 | { | |
915 | struct alsps_context *cxt = NULL; | |
916 | int err =0; | |
917 | cxt = alsps_context_obj; | |
918 | cxt->ps_ctl.set_delay = ctl->set_delay; | |
919 | cxt->ps_ctl.open_report_data= ctl->open_report_data; | |
920 | cxt->ps_ctl.enable_nodata = ctl->enable_nodata; | |
921 | cxt->ps_ctl.is_support_batch = ctl->is_support_batch; | |
922 | cxt->ps_ctl.is_report_input_direct= ctl->is_report_input_direct; | |
923 | ||
924 | if(NULL==cxt->ps_ctl.set_delay || NULL==cxt->ps_ctl.open_report_data | |
925 | || NULL==cxt->ps_ctl.enable_nodata) | |
926 | { | |
927 | ALSPS_LOG("ps register control path fail \n"); | |
928 | return -1; | |
929 | } | |
930 | ||
931 | //add misc dev for sensor hal control cmd | |
932 | err = alsps_misc_init(alsps_context_obj); | |
933 | if(err) | |
934 | { | |
935 | ALSPS_ERR("unable to register alsps misc device!!\n"); | |
936 | return -2; | |
937 | } | |
938 | err = sysfs_create_group(&alsps_context_obj->mdev.this_device->kobj, | |
939 | &alsps_attribute_group); | |
940 | if (err < 0) | |
941 | { | |
942 | ALSPS_ERR("unable to create alsps attribute file\n"); | |
943 | return -3; | |
944 | } | |
945 | ||
946 | ||
947 | kobject_uevent(&alsps_context_obj->mdev.this_device->kobj, KOBJ_ADD); | |
948 | ||
949 | return 0; | |
950 | } | |
951 | ||
952 | static int alsps_probe(struct platform_device *pdev) | |
953 | { | |
954 | ||
955 | int err; | |
956 | ALSPS_LOG("+++++++++++++alsps_probe!!\n"); | |
957 | ||
958 | alsps_context_obj = alsps_context_alloc_object(); | |
959 | if (!alsps_context_obj) | |
960 | { | |
961 | err = -ENOMEM; | |
962 | ALSPS_ERR("unable to allocate devobj!\n"); | |
963 | goto exit_alloc_data_failed; | |
964 | } | |
965 | ||
966 | //init real alspseleration driver | |
967 | err = alsps_real_driver_init(); | |
968 | if(err) | |
969 | { | |
970 | ALSPS_ERR("alsps real driver init fail\n"); | |
971 | goto real_driver_init_fail; | |
972 | } | |
973 | ||
974 | //init input dev | |
975 | err = alsps_input_init(alsps_context_obj); | |
976 | if(err) | |
977 | { | |
978 | ALSPS_ERR("unable to register alsps input device!\n"); | |
979 | goto exit_alloc_input_dev_failed; | |
980 | } | |
981 | ||
982 | atomic_set(&(alsps_context_obj->early_suspend), 0); | |
983 | alsps_context_obj->early_drv.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING - 1, | |
984 | alsps_context_obj->early_drv.suspend = alsps_early_suspend, | |
985 | alsps_context_obj->early_drv.resume = alsps_late_resume, | |
986 | register_early_suspend(&alsps_context_obj->early_drv); | |
987 | ||
988 | ||
989 | ALSPS_LOG("----alsps_probe OK !!\n"); | |
990 | return 0; | |
991 | ||
992 | exit_hwmsen_create_attr_failed: | |
993 | exit_misc_register_failed: | |
994 | ||
995 | exit_err_sysfs: | |
996 | ||
997 | if (err) | |
998 | { | |
999 | ALSPS_ERR("sysfs node creation error \n"); | |
1000 | alsps_input_destroy(alsps_context_obj); | |
1001 | } | |
1002 | ||
1003 | real_driver_init_fail: | |
1004 | exit_alloc_input_dev_failed: | |
1005 | kfree(alsps_context_obj); | |
1006 | ||
1007 | exit_alloc_data_failed: | |
1008 | ||
1009 | ||
1010 | ALSPS_LOG("----alsps_probe fail !!!\n"); | |
1011 | return err; | |
1012 | } | |
1013 | ||
1014 | ||
1015 | ||
1016 | static int alsps_remove(struct platform_device *pdev) | |
1017 | { | |
1018 | ALSPS_FUN(f); | |
1019 | int err=0; | |
1020 | input_unregister_device(alsps_context_obj->idev); | |
1021 | sysfs_remove_group(&alsps_context_obj->idev->dev.kobj, | |
1022 | &alsps_attribute_group); | |
1023 | ||
1024 | if((err = misc_deregister(&alsps_context_obj->mdev))) | |
1025 | { | |
1026 | ALSPS_ERR("misc_deregister fail: %d\n", err); | |
1027 | } | |
1028 | kfree(alsps_context_obj); | |
1029 | ||
1030 | return 0; | |
1031 | } | |
1032 | ||
1033 | static void alsps_early_suspend(struct early_suspend *h) | |
1034 | { | |
1035 | atomic_set(&(alsps_context_obj->early_suspend), 1); | |
1036 | ALSPS_LOG(" alsps_early_suspend ok------->hwm_obj->early_suspend=%d \n",atomic_read(&(alsps_context_obj->early_suspend))); | |
1037 | return ; | |
1038 | } | |
1039 | /*----------------------------------------------------------------------------*/ | |
1040 | static void alsps_late_resume(struct early_suspend *h) | |
1041 | { | |
1042 | atomic_set(&(alsps_context_obj->early_suspend), 0); | |
1043 | ALSPS_LOG(" alsps_late_resume ok------->hwm_obj->early_suspend=%d \n",atomic_read(&(alsps_context_obj->early_suspend))); | |
1044 | return ; | |
1045 | } | |
1046 | ||
1047 | static int alsps_suspend(struct platform_device *dev, pm_message_t state) | |
1048 | { | |
1049 | return 0; | |
1050 | } | |
1051 | /*----------------------------------------------------------------------------*/ | |
1052 | static int alsps_resume(struct platform_device *dev) | |
1053 | { | |
1054 | return 0; | |
1055 | } | |
1056 | ||
1057 | static struct platform_driver alsps_driver = | |
1058 | { | |
1059 | .probe = alsps_probe, | |
1060 | .remove = alsps_remove, | |
1061 | .suspend = alsps_suspend, | |
1062 | .resume = alsps_resume, | |
1063 | .driver = | |
1064 | { | |
1065 | .name = ALSPS_PL_DEV_NAME, | |
1066 | } | |
1067 | }; | |
1068 | ||
1069 | static int __init alsps_init(void) | |
1070 | { | |
1071 | ALSPS_FUN(); | |
1072 | ||
1073 | if(platform_driver_register(&alsps_driver)) | |
1074 | { | |
1075 | ALSPS_ERR("failed to register alsps driver\n"); | |
1076 | return -ENODEV; | |
1077 | } | |
1078 | ||
1079 | return 0; | |
1080 | } | |
1081 | ||
1082 | static void __exit alsps_exit(void) | |
1083 | { | |
1084 | platform_driver_unregister(&alsps_driver); | |
1085 | platform_driver_unregister(&als_ps_driver); | |
1086 | ||
1087 | } | |
1088 | ||
1089 | module_init(alsps_init); | |
1090 | module_exit(alsps_exit); | |
1091 | MODULE_LICENSE("GPL"); | |
1092 | MODULE_DESCRIPTION("ALSPS device driver"); | |
1093 | MODULE_AUTHOR("Mediatek"); | |
1094 |