4 static struct acc_context
*acc_context_obj
= NULL
;
7 static struct acc_init_info
* gsensor_init_list
[MAX_CHOOSE_G_NUM
]= {0}; //modified
8 static void acc_early_suspend(struct early_suspend
*h
);
9 static void acc_late_resume(struct early_suspend
*h
);
11 static void acc_work_func(struct work_struct
*work
)
14 struct acc_context
*cxt
= NULL
;
16 //hwm_sensor_data sensor_data;
22 cxt
= acc_context_obj
;
24 if(NULL
== cxt
->acc_data
.get_data
)
26 ACC_LOG("acc driver not register data path\n");
30 time
.tv_sec
= time
.tv_nsec
= 0;
31 time
= get_monotonic_coarse();
32 nt
= time
.tv_sec
*1000000000LL+time
.tv_nsec
;
34 err
= cxt
->acc_data
.get_data(&x
,&y
,&z
,&status
);
38 ACC_ERR("get acc data fails!!\n" );
50 cxt
->drv_data
.acc_data
.values
[0] = x
;
51 cxt
->drv_data
.acc_data
.values
[1] = y
;
52 cxt
->drv_data
.acc_data
.values
[2] = z
;
53 cxt
->drv_data
.acc_data
.status
= status
;
54 cxt
->drv_data
.acc_data
.time
= nt
;
59 if(true == cxt
->is_first_data_after_enable
)
61 cxt
->is_first_data_after_enable
= false;
63 if(ACC_INVALID_VALUE
== cxt
->drv_data
.acc_data
.values
[0] ||
64 ACC_INVALID_VALUE
== cxt
->drv_data
.acc_data
.values
[1] ||
65 ACC_INVALID_VALUE
== cxt
->drv_data
.acc_data
.values
[2])
67 ACC_LOG(" read invalid data \n");
72 //report data to input device
73 //printk("new acc work run....\n");
74 //ACC_LOG("acc data[%d,%d,%d] \n" ,cxt->drv_data.acc_data.values[0],
75 //cxt->drv_data.acc_data.values[1],cxt->drv_data.acc_data.values[2]);
77 acc_data_report(cxt
->drv_data
.acc_data
.values
[0],
78 cxt
->drv_data
.acc_data
.values
[1],cxt
->drv_data
.acc_data
.values
[2],
79 cxt
->drv_data
.acc_data
.status
);
82 if(true == cxt
->is_polling_run
)
84 mod_timer(&cxt
->timer
, jiffies
+ atomic_read(&cxt
->delay
)/(1000/HZ
));
88 static void acc_poll(unsigned long data
)
90 struct acc_context
*obj
= (struct acc_context
*)data
;
93 schedule_work(&obj
->report
);
97 static struct acc_context
*acc_context_alloc_object(void)
100 struct acc_context
*obj
= kzalloc(sizeof(*obj
), GFP_KERNEL
);
101 ACC_LOG("acc_context_alloc_object++++\n");
104 ACC_ERR("Alloc accel object error!\n");
107 atomic_set(&obj
->delay
, 200); /*5Hz*/// set work queue delay time
200ms
108 atomic_set(&obj
->wake
, 0);
109 INIT_WORK(&obj
->report
, acc_work_func
);
110 init_timer(&obj
->timer
);
111 obj
->timer
.expires
= jiffies
+ atomic_read(&obj
->delay
)/(1000/HZ
);
112 obj
->timer
.function
= acc_poll
;
113 obj
->timer
.data
= (unsigned long)obj
;
114 obj
->is_first_data_after_enable
= false;
115 obj
->is_polling_run
= false;
116 mutex_init(&obj
->acc_op_mutex
);
117 obj
->is_batch_enable
= false;//for batch mode init
118 ACC_LOG("acc_context_alloc_object----\n");
122 static int acc_real_enable(int enable
)
125 struct acc_context
*cxt
= NULL
;
126 cxt
= acc_context_obj
;
130 if(true==cxt
->is_active_data
|| true ==cxt
->is_active_nodata
)
132 err
= cxt
->acc_ctl
.enable_nodata(1);
135 err
= cxt
->acc_ctl
.enable_nodata(1);
138 err
= cxt
->acc_ctl
.enable_nodata(1);
140 ACC_ERR("acc enable(%d) err 3 timers = %d\n", enable
, err
);
143 ACC_LOG("acc real enable \n" );
149 if(false==cxt
->is_active_data
&& false ==cxt
->is_active_nodata
)
151 err
= cxt
->acc_ctl
.enable_nodata(0);
154 ACC_ERR("acc enable(%d) err = %d\n", enable
, err
);
156 ACC_LOG("acc real disable \n" );
163 static int acc_enable_data(int enable
)
165 struct acc_context
*cxt
= NULL
;
167 cxt
= acc_context_obj
;
168 if(NULL
== cxt
->acc_ctl
.open_report_data
)
170 ACC_ERR("no acc control path\n");
176 ACC_LOG("ACC enable data\n");
177 cxt
->is_active_data
=true;
178 cxt
->is_first_data_after_enable
= true;
179 cxt
->acc_ctl
.open_report_data(1);
180 if(false == cxt
->is_polling_run
&& cxt
->is_batch_enable
== false)
182 if(false == cxt
->acc_ctl
.is_report_input_direct
)
184 mod_timer(&cxt
->timer
, jiffies
+ atomic_read(&cxt
->delay
)/(1000/HZ
));
185 cxt
->is_polling_run
= true;
191 ACC_LOG("ACC disable \n");
193 cxt
->is_active_data
=false;
194 cxt
->acc_ctl
.open_report_data(0);
195 if(true == cxt
->is_polling_run
)
197 if(false == cxt
->acc_ctl
.is_report_input_direct
)
199 cxt
->is_polling_run
= false;
200 del_timer_sync(&cxt
->timer
);
201 cancel_work_sync(&cxt
->report
);
202 cxt
->drv_data
.acc_data
.values
[0] = ACC_INVALID_VALUE
;
203 cxt
->drv_data
.acc_data
.values
[1] = ACC_INVALID_VALUE
;
204 cxt
->drv_data
.acc_data
.values
[2] = ACC_INVALID_VALUE
;
209 acc_real_enable(enable
);
215 int acc_enable_nodata(int enable
)
217 struct acc_context
*cxt
= NULL
;
219 cxt
= acc_context_obj
;
220 if(NULL
== cxt
->acc_ctl
.enable_nodata
)
222 ACC_ERR("acc_enable_nodata:acc ctl path is NULL\n");
228 cxt
->is_active_nodata
= true;
233 cxt
->is_active_nodata
= false;
235 acc_real_enable(enable
);
240 static ssize_t
acc_show_enable_nodata(struct device
* dev
,
241 struct device_attribute
*attr
, char *buf
)
244 ACC_LOG(" not support now\n");
248 static ssize_t
acc_store_enable_nodata(struct device
* dev
, struct device_attribute
*attr
,
249 const char *buf
, size_t count
)
251 ACC_LOG("acc_store_enable nodata buf=%s\n",buf
);
252 mutex_lock(&acc_context_obj
->acc_op_mutex
);
253 struct acc_context
*cxt
= NULL
;
255 cxt
= acc_context_obj
;
256 if(NULL
== cxt
->acc_ctl
.enable_nodata
)
258 ACC_LOG("acc_ctl enable nodata NULL\n");
259 mutex_unlock(&acc_context_obj
->acc_op_mutex
);
262 if (!strncmp(buf
, "1", 1))
264 //cxt->acc_ctl.enable_nodata(1);
265 acc_enable_nodata(1);
267 else if (!strncmp(buf
, "0", 1))
269 //cxt->acc_ctl.enable_nodata(0);
270 acc_enable_nodata(0);
274 ACC_ERR(" acc_store enable nodata cmd error !!\n");
276 mutex_unlock(&acc_context_obj
->acc_op_mutex
);
279 static ssize_t
acc_store_active(struct device
* dev
, struct device_attribute
*attr
,
280 const char *buf
, size_t count
)
282 ACC_LOG("acc_store_active buf=%s\n",buf
);
283 mutex_lock(&acc_context_obj
->acc_op_mutex
);
284 struct acc_context
*cxt
= NULL
;
286 cxt
= acc_context_obj
;
287 if(NULL
== cxt
->acc_ctl
.open_report_data
)
289 ACC_LOG("acc_ctl enable NULL\n");
290 mutex_unlock(&acc_context_obj
->acc_op_mutex
);
293 if (!strncmp(buf
, "1", 1))
295 // cxt->acc_ctl.enable(1);
299 else if (!strncmp(buf
, "0", 1))
302 //cxt->acc_ctl.enable(0);
307 ACC_ERR(" acc_store_active error !!\n");
309 mutex_unlock(&acc_context_obj
->acc_op_mutex
);
310 ACC_LOG(" acc_store_active done\n");
313 /*----------------------------------------------------------------------------*/
314 static ssize_t
acc_show_active(struct device
* dev
,
315 struct device_attribute
*attr
, char *buf
)
317 struct acc_context
*cxt
= NULL
;
318 cxt
= acc_context_obj
;
319 int div
=cxt
->acc_data
.vender_div
;
320 ACC_LOG("acc vender_div value: %d\n", div
);
321 return snprintf(buf
, PAGE_SIZE
, "%d\n", div
);
324 static ssize_t
acc_store_delay(struct device
* dev
, struct device_attribute
*attr
,
325 const char *buf
, size_t count
)
327 mutex_lock(&acc_context_obj
->acc_op_mutex
);
328 struct acc_context
*devobj
= (struct acc_context
*)dev_get_drvdata(dev
);
331 struct acc_context
*cxt
= NULL
;
333 cxt
= acc_context_obj
;
334 if(NULL
== cxt
->acc_ctl
.set_delay
)
336 ACC_LOG("acc_ctl set_delay NULL\n");
337 mutex_unlock(&acc_context_obj
->acc_op_mutex
);
341 if (1 != sscanf(buf
, "%d", &delay
)) {
342 ACC_ERR("invalid format!!\n");
343 mutex_unlock(&acc_context_obj
->acc_op_mutex
);
347 if(false == cxt
->acc_ctl
.is_report_input_direct
)
349 mdelay
= (int)delay
/1000/1000;
350 atomic_set(&acc_context_obj
->delay
, mdelay
);
352 cxt
->acc_ctl
.set_delay(delay
);
353 ACC_LOG(" acc_delay %d ns\n",delay
);
354 mutex_unlock(&acc_context_obj
->acc_op_mutex
);
358 static ssize_t
acc_show_delay(struct device
* dev
,
359 struct device_attribute
*attr
, char *buf
)
362 ACC_LOG(" not support now\n");
366 static ssize_t
acc_show_sensordevnum(struct device
* dev
,
367 struct device_attribute
*attr
, char *buf
)
369 struct acc_context
*cxt
= NULL
;
370 char *devname
= NULL
;
371 cxt
= acc_context_obj
;
372 devname
= dev_name(&cxt
->idev
->dev
);
373 return snprintf(buf
, PAGE_SIZE
, "%s\n", devname
+5);
377 static ssize_t
acc_store_batch(struct device
* dev
, struct device_attribute
*attr
,
378 const char *buf
, size_t count
)
380 ACC_LOG("acc_store_batch buf=%s\n",buf
);
381 mutex_lock(&acc_context_obj
->acc_op_mutex
);
382 struct acc_context
*cxt
= NULL
;
384 cxt
= acc_context_obj
;
385 if(cxt
->acc_ctl
.is_support_batch
){
386 if (!strncmp(buf
, "1", 1))
388 cxt
->is_batch_enable
= true;
390 else if (!strncmp(buf
, "0", 1))
392 cxt
->is_batch_enable
= false;
396 ACC_ERR(" acc_store_batch error !!\n");
399 ACC_LOG(" acc_store_batch mot supported\n");
401 mutex_unlock(&acc_context_obj
->acc_op_mutex
);
402 ACC_LOG(" acc_store_batch done: %d\n", cxt
->is_batch_enable
);
407 static ssize_t
acc_show_batch(struct device
* dev
,
408 struct device_attribute
*attr
, char *buf
)
410 return snprintf(buf
, PAGE_SIZE
, "%d\n", 0);
413 static ssize_t
acc_store_flush(struct device
* dev
, struct device_attribute
*attr
,
414 const char *buf
, size_t count
)
416 mutex_lock(&acc_context_obj
->acc_op_mutex
);
417 struct acc_context
*devobj
= (struct acc_context
*)dev_get_drvdata(dev
);
418 //do read FIFO data function and report data immediately
419 mutex_unlock(&acc_context_obj
->acc_op_mutex
);
423 static ssize_t
acc_show_flush(struct device
* dev
,
424 struct device_attribute
*attr
, char *buf
)
426 return snprintf(buf
, PAGE_SIZE
, "%d\n", 0);
429 static int gsensor_remove(struct platform_device
*pdev
)
431 ACC_LOG("gsensor_remove\n");
435 static int gsensor_probe(struct platform_device
*pdev
)
437 ACC_LOG("gsensor_probe\n");
441 static struct platform_driver gsensor_driver
= {
442 .probe
= gsensor_probe
,
443 .remove
= gsensor_remove
,
450 static int acc_real_driver_init(void)
454 ACC_LOG(" acc_real_driver_init +\n");
455 for(i
= 0; i
< MAX_CHOOSE_G_NUM
; i
++)
457 ACC_LOG(" i=%d\n",i
);
458 if(0 != gsensor_init_list
[i
])
460 ACC_LOG(" acc try to init driver %s\n", gsensor_init_list
[i
]->name
);
461 err
= gsensor_init_list
[i
]->init();
464 ACC_LOG(" acc real driver %s probe ok\n", gsensor_init_list
[i
]->name
);
470 if(i
== MAX_CHOOSE_G_NUM
)
472 ACC_LOG(" acc_real_driver_init fail\n");
478 int acc_driver_add(struct acc_init_info
* obj
)
485 for(i
=0; i
< MAX_CHOOSE_G_NUM
; i
++ )
488 ACC_LOG("register gensor driver for the first time\n");
489 if(platform_driver_register(&gsensor_driver
))
491 ACC_ERR("failed to register gensor driver already exist\n");
495 if(NULL
== gsensor_init_list
[i
])
497 obj
->platform_diver_addr
= &gsensor_driver
;
498 gsensor_init_list
[i
] = obj
;
502 if(NULL
==gsensor_init_list
[i
])
504 ACC_ERR("ACC driver add err \n");
510 EXPORT_SYMBOL_GPL(acc_driver_add
);
512 static int acc_misc_init(struct acc_context
*cxt
)
516 cxt
->mdev
.minor
= MISC_DYNAMIC_MINOR
;
517 cxt
->mdev
.name
= ACC_MISC_DEV_NAME
;
518 if((err
= misc_register(&cxt
->mdev
)))
520 ACC_ERR("unable to register acc misc device!!\n");
522 //dev_set_drvdata(cxt->mdev.this_device, cxt);
526 static void acc_input_destroy(struct acc_context
*cxt
)
528 struct input_dev
*dev
= cxt
->idev
;
530 input_unregister_device(dev
);
531 input_free_device(dev
);
534 static int acc_input_init(struct acc_context
*cxt
)
536 struct input_dev
*dev
;
539 dev
= input_allocate_device();
543 dev
->name
= ACC_INPUTDEV_NAME
;
545 input_set_capability(dev
, EV_ABS
, EVENT_TYPE_ACCEL_X
);
546 input_set_capability(dev
, EV_ABS
, EVENT_TYPE_ACCEL_Y
);
547 input_set_capability(dev
, EV_ABS
, EVENT_TYPE_ACCEL_Z
);
548 input_set_capability(dev
, EV_ABS
, EVENT_TYPE_ACCEL_STATUS
);
550 input_set_abs_params(dev
, EVENT_TYPE_ACCEL_X
, ACC_VALUE_MIN
, ACC_VALUE_MAX
, 0, 0);
551 input_set_abs_params(dev
, EVENT_TYPE_ACCEL_Y
, ACC_VALUE_MIN
, ACC_VALUE_MAX
, 0, 0);
552 input_set_abs_params(dev
, EVENT_TYPE_ACCEL_Z
, ACC_VALUE_MIN
, ACC_VALUE_MAX
, 0, 0);
553 input_set_abs_params(dev
, EVENT_TYPE_ACCEL_STATUS
, ACC_STATUS_MIN
, ACC_STATUS_MAX
, 0, 0);
554 input_set_drvdata(dev
, cxt
);
556 err
= input_register_device(dev
);
558 input_free_device(dev
);
566 DEVICE_ATTR(accenablenodata
, S_IWUSR
| S_IRUGO
, acc_show_enable_nodata
, acc_store_enable_nodata
);
567 DEVICE_ATTR(accactive
, S_IWUSR
| S_IRUGO
, acc_show_active
, acc_store_active
);
568 DEVICE_ATTR(accdelay
, S_IWUSR
| S_IRUGO
, acc_show_delay
, acc_store_delay
);
569 DEVICE_ATTR(accbatch
, S_IWUSR
| S_IRUGO
, acc_show_batch
, acc_store_batch
);
570 DEVICE_ATTR(accflush
, S_IWUSR
| S_IRUGO
, acc_show_flush
, acc_store_flush
);
571 DEVICE_ATTR(accdevnum
, S_IWUSR
| S_IRUGO
, acc_show_sensordevnum
, NULL
);
573 static struct attribute
*acc_attributes
[] = {
574 &dev_attr_accenablenodata
.attr
,
575 &dev_attr_accactive
.attr
,
576 &dev_attr_accdelay
.attr
,
577 &dev_attr_accbatch
.attr
,
578 &dev_attr_accflush
.attr
,
579 &dev_attr_accdevnum
.attr
,
583 static struct attribute_group acc_attribute_group
= {
584 .attrs
= acc_attributes
587 int acc_register_data_path(struct acc_data_path
*data
)
589 struct acc_context
*cxt
= NULL
;
591 cxt
= acc_context_obj
;
592 cxt
->acc_data
.get_data
= data
->get_data
;
593 cxt
->acc_data
.vender_div
= data
->vender_div
;
594 ACC_LOG("acc register data path vender_div: %d\n", cxt
->acc_data
.vender_div
);
595 if(NULL
== cxt
->acc_data
.get_data
)
597 ACC_LOG("acc register data path fail \n");
603 int acc_register_control_path(struct acc_control_path
*ctl
)
605 struct acc_context
*cxt
= NULL
;
607 cxt
= acc_context_obj
;
608 cxt
->acc_ctl
.set_delay
= ctl
->set_delay
;
609 cxt
->acc_ctl
.open_report_data
= ctl
->open_report_data
;
610 cxt
->acc_ctl
.enable_nodata
= ctl
->enable_nodata
;
611 cxt
->acc_ctl
.is_support_batch
= ctl
->is_support_batch
;
612 cxt
->acc_ctl
.is_report_input_direct
= ctl
->is_report_input_direct
;
614 if(NULL
==cxt
->acc_ctl
.set_delay
|| NULL
==cxt
->acc_ctl
.open_report_data
615 || NULL
==cxt
->acc_ctl
.enable_nodata
)
617 ACC_LOG("acc register control path fail \n");
621 //add misc dev for sensor hal control cmd
622 err
= acc_misc_init(acc_context_obj
);
625 ACC_ERR("unable to register acc misc device!!\n");
628 err
= sysfs_create_group(&acc_context_obj
->mdev
.this_device
->kobj
,
629 &acc_attribute_group
);
632 ACC_ERR("unable to create acc attribute file\n");
636 kobject_uevent(&acc_context_obj
->mdev
.this_device
->kobj
, KOBJ_ADD
);
641 int acc_data_report(int x
, int y
, int z
,int status
)
643 //ACC_LOG("+acc_data_report! %d, %d, %d, %d\n",x,y,z,status);
644 struct acc_context
*cxt
= NULL
;
646 cxt
= acc_context_obj
;
647 input_report_abs(cxt
->idev
, EVENT_TYPE_ACCEL_X
, x
);
648 input_report_abs(cxt
->idev
, EVENT_TYPE_ACCEL_Y
, y
);
649 input_report_abs(cxt
->idev
, EVENT_TYPE_ACCEL_Z
, z
);
650 input_report_abs(cxt
->idev
, EVENT_TYPE_ACCEL_STATUS
, status
);
651 input_sync(cxt
->idev
);
654 static int acc_probe(struct platform_device
*pdev
)
658 ACC_LOG("+++++++++++++accel_probe!!\n");
660 acc_context_obj
= acc_context_alloc_object();
661 if (!acc_context_obj
)
664 ACC_ERR("unable to allocate devobj!\n");
665 goto exit_alloc_data_failed
;
668 //init real acceleration driver
669 err
= acc_real_driver_init();
672 ACC_ERR("acc real driver init fail\n");
673 goto real_driver_init_fail
;
677 err
= acc_input_init(acc_context_obj
);
680 ACC_ERR("unable to register acc input device!\n");
681 goto exit_alloc_input_dev_failed
;
684 atomic_set(&(acc_context_obj
->early_suspend
), 0);
685 acc_context_obj
->early_drv
.level
= EARLY_SUSPEND_LEVEL_STOP_DRAWING
- 1,
686 acc_context_obj
->early_drv
.suspend
= acc_early_suspend
,
687 acc_context_obj
->early_drv
.resume
= acc_late_resume
,
688 register_early_suspend(&acc_context_obj
->early_drv
);
691 ACC_LOG("----accel_probe OK !!\n");
694 exit_hwmsen_create_attr_failed
:
695 exit_misc_register_failed
:
701 ACC_ERR("sysfs node creation error \n");
702 acc_input_destroy(acc_context_obj
);
705 real_driver_init_fail
:
706 exit_alloc_input_dev_failed
:
707 kfree(acc_context_obj
);
709 exit_alloc_data_failed
:
712 ACC_LOG("----accel_probe fail !!!\n");
718 static int acc_remove(struct platform_device
*pdev
)
722 input_unregister_device(acc_context_obj
->idev
);
723 sysfs_remove_group(&acc_context_obj
->idev
->dev
.kobj
,
724 &acc_attribute_group
);
726 if((err
= misc_deregister(&acc_context_obj
->mdev
)))
728 ACC_ERR("misc_deregister fail: %d\n", err
);
730 kfree(acc_context_obj
);
735 static void acc_early_suspend(struct early_suspend
*h
)
737 atomic_set(&(acc_context_obj
->early_suspend
), 1);
738 ACC_LOG(" acc_early_suspend ok------->hwm_obj->early_suspend=%d \n",atomic_read(&(acc_context_obj
->early_suspend
)));
741 /*----------------------------------------------------------------------------*/
742 static void acc_late_resume(struct early_suspend
*h
)
744 atomic_set(&(acc_context_obj
->early_suspend
), 0);
745 ACC_LOG(" acc_late_resume ok------->hwm_obj->early_suspend=%d \n",atomic_read(&(acc_context_obj
->early_suspend
)));
749 static int acc_suspend(struct platform_device
*dev
, pm_message_t state
)
753 /*----------------------------------------------------------------------------*/
754 static int acc_resume(struct platform_device
*dev
)
759 static struct platform_driver acc_driver
=
762 .remove
= acc_remove
,
763 .suspend
= acc_suspend
,
764 .resume
= acc_resume
,
767 .name
= ACC_PL_DEV_NAME
,
771 static int __init
acc_init(void)
775 if(platform_driver_register(&acc_driver
))
777 ACC_ERR("failed to register acc driver\n");
784 static void __exit
acc_exit(void)
786 platform_driver_unregister(&acc_driver
);
787 platform_driver_unregister(&gsensor_driver
);
790 module_init(acc_init
);
791 module_exit(acc_exit
);
792 MODULE_LICENSE("GPL");
793 MODULE_DESCRIPTION("ACCELEROMETER device driver");
794 MODULE_AUTHOR("Mediatek");