aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/mediatek/accelerometer/accel.c
blob: fb7d603aebcc8618aaffc1eb1d4af86675e47310 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762

#include "accel.h"
#include "accel_factory.h"

struct acc_context *acc_context_obj = NULL;


static struct acc_init_info *gsensor_init_list[MAX_CHOOSE_G_NUM] = { 0 };	/* modified */
#if defined(CONFIG_HAS_EARLYSUSPEND)
static void acc_early_suspend(struct early_suspend *h);
static void acc_late_resume(struct early_suspend *h);
#endif

static void acc_work_func(struct work_struct *work)
{

	struct acc_context *cxt = NULL;
	/* hwm_sensor_data sensor_data; */
	int x, y, z, status;
	int64_t nt;
	struct timespec time;
	int err;

	cxt = acc_context_obj;

	if (NULL == cxt->acc_data.get_data) {
        ACC_ERR("acc driver not register data path\n");
        return;
	}


	time.tv_sec = time.tv_nsec = 0;
	time = get_monotonic_coarse();
	nt = time.tv_sec * 1000000000LL + time.tv_nsec;

	err = cxt->acc_data.get_data(&x,&y,&z,&status);

	if (err) {
		ACC_ERR("get acc data fails!!\n");
		goto acc_loop;
	} else {
			if (0 == x && 0 == y && 0 == z) {
				goto acc_loop;
			}

			cxt->drv_data.acc_data.values[0] = x;
			cxt->drv_data.acc_data.values[1] = y;
			cxt->drv_data.acc_data.values[2] = z;
			cxt->drv_data.acc_data.status = status;
			cxt->drv_data.acc_data.time = nt;

	}

	if (true == cxt->is_first_data_after_enable) {
		cxt->is_first_data_after_enable = false;
		/* filter -1 value */
		if (ACC_INVALID_VALUE == cxt->drv_data.acc_data.values[0] ||
		    ACC_INVALID_VALUE == cxt->drv_data.acc_data.values[1] ||
		    ACC_INVALID_VALUE == cxt->drv_data.acc_data.values[2]) {
			ACC_LOG(" read invalid data\n");
			goto acc_loop;

		}
	}
	/* report data to input device */
	/* printk("new acc work run....\n"); */
	/* ACC_LOG("acc data[%d,%d,%d] \n" ,cxt->drv_data.acc_data.values[0], */
	/* cxt->drv_data.acc_data.values[1],cxt->drv_data.acc_data.values[2]); */

	acc_data_report(cxt->drv_data.acc_data.values[0],
			cxt->drv_data.acc_data.values[1], cxt->drv_data.acc_data.values[2],
			cxt->drv_data.acc_data.status);

 acc_loop:
	if (true == cxt->is_polling_run) {
		mod_timer(&cxt->timer, jiffies + atomic_read(&cxt->delay) / (1000 / HZ));
	}
}

static void acc_poll(unsigned long data)
{
	struct acc_context *obj = (struct acc_context *)data;
	if (obj != NULL) {
		schedule_work(&obj->report);
	}
}

static struct acc_context *acc_context_alloc_object(void)
{

	struct acc_context *obj = kzalloc(sizeof(*obj), GFP_KERNEL);
	ACC_LOG("acc_context_alloc_object++++\n");
	if (!obj) {
		ACC_ERR("Alloc accel object error!\n");
		return NULL;
	}
	atomic_set(&obj->delay, 200);	/*5Hz *//*  set work queue delay time 200ms */
	atomic_set(&obj->wake, 0);
	INIT_WORK(&obj->report, acc_work_func);
	init_timer(&obj->timer);
	obj->timer.expires = jiffies + atomic_read(&obj->delay) / (1000 / HZ);
	obj->timer.function = acc_poll;
	obj->timer.data = (unsigned long)obj;
	obj->is_first_data_after_enable = false;
	obj->is_polling_run = false;
	mutex_init(&obj->acc_op_mutex);
	obj->is_batch_enable = false;//for batch mode init
	obj->cali_sw[ACC_AXIS_X]=0;
	obj->cali_sw[ACC_AXIS_Y]=0;
	obj->cali_sw[ACC_AXIS_Z]=0;
	ACC_LOG("acc_context_alloc_object----\n");
	return obj;
}

static int acc_real_enable(int enable)
{
	int err = 0;
	struct acc_context *cxt = NULL;
	cxt = acc_context_obj;
	if (1 == enable) {

		if (true == cxt->is_active_data || true == cxt->is_active_nodata) {
			err = cxt->acc_ctl.enable_nodata(1);
			if (err) {
				err = cxt->acc_ctl.enable_nodata(1);
				if (err) {
					err = cxt->acc_ctl.enable_nodata(1);
					if (err)
						ACC_ERR("acc enable(%d) err 3 timers = %d\n",
							enable, err);
				}
			}
			ACC_LOG("acc real enable \n");
		}

	}
	if (0 == enable) {
		if (false == cxt->is_active_data && false == cxt->is_active_nodata) {
			err = cxt->acc_ctl.enable_nodata(0);
			if (err) {
				ACC_ERR("acc enable(%d) err = %d\n", enable, err);
			}
			ACC_LOG("acc real disable \n");
		}

	}

	return err;
}

static int acc_enable_data(int enable)
{
	struct acc_context *cxt = NULL;
	cxt = acc_context_obj;
	if (NULL == cxt->acc_ctl.open_report_data) {
		ACC_ERR("no acc control path\n");
		return -1;
	}

	if (1 == enable) {
		ACC_LOG("ACC enable data\n");
		cxt->is_active_data = true;
		cxt->is_first_data_after_enable = true;
		cxt->acc_ctl.open_report_data(1);
        acc_real_enable(enable);
		if (false == cxt->is_polling_run && cxt->is_batch_enable == false) {
			if (false == cxt->acc_ctl.is_report_input_direct) {
				mod_timer(&cxt->timer,
					  jiffies + atomic_read(&cxt->delay) / (1000 / HZ));
				cxt->is_polling_run = true;
			}
		}
	}
	if (0 == enable) {
		ACC_LOG("ACC disable\n");

		cxt->is_active_data = false;
		cxt->acc_ctl.open_report_data(0);
		if (true == cxt->is_polling_run) {
			if (false == cxt->acc_ctl.is_report_input_direct) {
				cxt->is_polling_run = false;
                smp_mb();
				del_timer_sync(&cxt->timer);
                smp_mb();
				cancel_work_sync(&cxt->report);
				cxt->drv_data.acc_data.values[0] = ACC_INVALID_VALUE;
				cxt->drv_data.acc_data.values[1] = ACC_INVALID_VALUE;
				cxt->drv_data.acc_data.values[2] = ACC_INVALID_VALUE;
			}
		}
        acc_real_enable(enable);
	}
	return 0;
}



int acc_enable_nodata(int enable)
{
	struct acc_context *cxt = NULL;
	cxt = acc_context_obj;
	if (NULL == cxt->acc_ctl.enable_nodata) {
		ACC_ERR("acc_enable_nodata:acc ctl path is NULL\n");
		return -1;
	}

	if (1 == enable) {
		cxt->is_active_nodata = true;
	}

	if (0 == enable) {
		cxt->is_active_nodata = false;
	}
	acc_real_enable(enable);
	return 0;
}


static ssize_t acc_show_enable_nodata(struct device *dev, struct device_attribute *attr, char *buf)
{
	int len = 0;
	ACC_LOG(" not support now\n");
	return len;
}

static ssize_t acc_store_enable_nodata(struct device *dev, struct device_attribute *attr,
				       const char *buf, size_t count)
{
	struct acc_context *cxt = NULL;
	ACC_LOG("acc_store_enable nodata buf=%s\n", buf);
	mutex_lock(&acc_context_obj->acc_op_mutex);
	cxt = acc_context_obj;
	if (NULL == cxt->acc_ctl.enable_nodata) {
		ACC_LOG("acc_ctl enable nodata NULL\n");
		mutex_unlock(&acc_context_obj->acc_op_mutex);
		return count;
	}
	if (!strncmp(buf, "1", 1)) {
		/* cxt->acc_ctl.enable_nodata(1); */
		acc_enable_nodata(1);
	} else if (!strncmp(buf, "0", 1)) {
		/* cxt->acc_ctl.enable_nodata(0); */
		acc_enable_nodata(0);
	} else {
		ACC_ERR(" acc_store enable nodata cmd error !!\n");
	}
	mutex_unlock(&acc_context_obj->acc_op_mutex);
	return count;
}

static ssize_t acc_store_active(struct device *dev, struct device_attribute *attr,
				const char *buf, size_t count)
{
	struct acc_context *cxt = NULL;
	ACC_LOG("acc_store_active buf=%s\n", buf);
	mutex_lock(&acc_context_obj->acc_op_mutex);
	cxt = acc_context_obj;
	if (NULL == cxt->acc_ctl.open_report_data) {
		ACC_LOG("acc_ctl enable NULL\n");
		mutex_unlock(&acc_context_obj->acc_op_mutex);
		return count;
	}
	if (!strncmp(buf, "1", 1)) {
		/* cxt->acc_ctl.enable(1); */
		acc_enable_data(1);

	} else if (!strncmp(buf, "0", 1)) {

		/* cxt->acc_ctl.enable(0); */
		acc_enable_data(0);
	} else {
		ACC_ERR(" acc_store_active error !!\n");
	}
	mutex_unlock(&acc_context_obj->acc_op_mutex);
	ACC_LOG(" acc_store_active done\n");
	return count;
}

/*----------------------------------------------------------------------------*/
static ssize_t acc_show_active(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct acc_context *cxt = NULL;
	int div = 0;
	cxt = acc_context_obj;
	div=cxt->acc_data.vender_div;
	ACC_LOG("acc vender_div value: %d\n", div);
	return snprintf(buf, PAGE_SIZE, "%d\n", div);
}

static ssize_t acc_store_delay(struct device *dev, struct device_attribute *attr,
			       const char *buf, size_t count)
{
    int delay = 0;
	int mdelay = 0;
	struct acc_context *cxt = NULL;
	mutex_lock(&acc_context_obj->acc_op_mutex);
	cxt = acc_context_obj;
	if (NULL == cxt->acc_ctl.set_delay) {
		ACC_LOG("acc_ctl set_delay NULL\n");
		mutex_unlock(&acc_context_obj->acc_op_mutex);
		return count;
	}

	if (1 != sscanf(buf, "%d", &delay)) {
		ACC_ERR("invalid format!!\n");
		mutex_unlock(&acc_context_obj->acc_op_mutex);
		return count;
	}

	if (false == cxt->acc_ctl.is_report_input_direct) {
		mdelay = (int)delay / 1000 / 1000;
		atomic_set(&acc_context_obj->delay, mdelay);
	}
	cxt->acc_ctl.set_delay(delay);
	ACC_LOG(" acc_delay %d ns\n", delay);
	mutex_unlock(&acc_context_obj->acc_op_mutex);
	return count;
}

static ssize_t acc_show_delay(struct device *dev, struct device_attribute *attr, char *buf)
{
	int len = 0;
	ACC_LOG(" not support now\n");
	return len;
}

static ssize_t acc_show_sensordevnum(struct device* dev, 
                                 struct device_attribute *attr, char *buf) 
{
	struct acc_context *cxt = NULL;
	const char *devname = NULL;
	cxt = acc_context_obj;
	devname = dev_name(&cxt->idev->dev);
	return snprintf(buf, PAGE_SIZE, "%s\n", devname+5); 
}

static ssize_t acc_store_batch(struct device* dev, struct device_attribute *attr,
                                  const char *buf, size_t count)
{
	struct acc_context *cxt = NULL;
	ACC_LOG("acc_store_batch buf=%s\n",buf);
	mutex_lock(&acc_context_obj->acc_op_mutex);
	cxt = acc_context_obj;
	if(cxt->acc_ctl.is_support_batch){
	    	if (!strncmp(buf, "1", 1)) 
		{
	    		cxt->is_batch_enable = true;
			if(true == cxt->is_polling_run)
			{
				cxt->is_polling_run = false;
				del_timer_sync(&cxt->timer);
				cancel_work_sync(&cxt->report);
				cxt->drv_data.acc_data.values[0] = ACC_INVALID_VALUE;
				cxt->drv_data.acc_data.values[1] = ACC_INVALID_VALUE;
				cxt->drv_data.acc_data.values[2] = ACC_INVALID_VALUE;
			}
	    	} 
		else if (!strncmp(buf, "0", 1))
		{
			cxt->is_batch_enable = false;
			if(false == cxt->is_polling_run)
			{
				if(false == cxt->acc_ctl.is_report_input_direct)
				{
					mod_timer(&cxt->timer, jiffies + atomic_read(&cxt->delay)/(1000/HZ));
					cxt->is_polling_run = true;
				}
			}
	    	}
		else
		{
			ACC_ERR(" acc_store_batch error !!\n");
		}
	}else{
		ACC_LOG(" acc_store_batch mot supported\n");
	}
	mutex_unlock(&acc_context_obj->acc_op_mutex);
	ACC_LOG(" acc_store_batch done: %d\n", cxt->is_batch_enable);
    	return count;

}
static ssize_t acc_show_batch(struct device* dev, 
                                 struct device_attribute *attr, char *buf) 
{
	return snprintf(buf, PAGE_SIZE, "%d\n", 0); 
}
static ssize_t acc_store_flush(struct device* dev, struct device_attribute *attr,
                                  const char *buf, size_t count)
{
	//mutex_lock(&acc_context_obj->acc_op_mutex);
   // struct acc_context *devobj = (struct acc_context*)dev_get_drvdata(dev);
	//do read FIFO data function and report data immediately
	//mutex_unlock(&acc_context_obj->acc_op_mutex);
    return count;
}

static ssize_t acc_show_flush(struct device* dev, 
                                 struct device_attribute *attr, char *buf) 
{
	return snprintf(buf, PAGE_SIZE, "%d\n", 0); 
}
static int gsensor_remove(struct platform_device *pdev)
{
	ACC_LOG("gsensor_remove\n");
	return 0;
}

static int gsensor_probe(struct platform_device *pdev)
{
	ACC_LOG("gsensor_probe\n");
	return 0;
}

#ifdef CONFIG_OF
static const struct of_device_id gsensor_of_match[] = {
	{ .compatible = "mediatek,gsensor", },
	{},
};
#endif
static struct platform_driver gsensor_driver = {
	.probe = gsensor_probe,
	.remove = gsensor_remove,
	.driver = {
		   .name = "gsensor",
        #ifdef CONFIG_OF
		.of_match_table = gsensor_of_match,
		#endif
		   }
};

static int acc_real_driver_init(void)
{
	int i = 0;
	int err = 0;
	ACC_LOG(" acc_real_driver_init +\n");
	for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
		ACC_LOG(" i=%d\n", i);
		if (0 != gsensor_init_list[i]) {
			ACC_LOG(" acc try to init driver %s\n", gsensor_init_list[i]->name);
			err = gsensor_init_list[i]->init();
			if (0 == err) {
				ACC_LOG(" acc real driver %s probe ok\n",
					gsensor_init_list[i]->name);
				break;
			}
		}
	}

	if (i == MAX_CHOOSE_G_NUM) {
		ACC_LOG(" acc_real_driver_init fail\n");
		err = -1;
	}
	return err;
}

int acc_driver_add(struct acc_init_info *obj)
{
	int err = 0;
	int i = 0;

	ACC_FUN();
	if (!obj) {
		ACC_ERR("ACC driver add fail, acc_init_info is NULL \n");
		return -1;
	}
	for (i = 0; i < MAX_CHOOSE_G_NUM; i++) {
        if ((i == 0) && (NULL == gsensor_init_list[0])) {
			ACC_LOG("register gensor driver for the first time\n");
			if (platform_driver_register(&gsensor_driver)) {
				ACC_ERR("failed to register gensor driver already exist\n");
			}
		}

		if (NULL == gsensor_init_list[i]) {
			obj->platform_diver_addr = &gsensor_driver;
			gsensor_init_list[i] = obj;
			break;
		}
	}
	if (i >= MAX_CHOOSE_G_NUM) {
		ACC_ERR("ACC driver add err\n");
		err = -1;
	}

	return err;
}
EXPORT_SYMBOL_GPL(acc_driver_add);

static int acc_misc_init(struct acc_context *cxt)
{

	int err = 0;
	cxt->mdev.minor = MISC_DYNAMIC_MINOR;
	cxt->mdev.name = ACC_MISC_DEV_NAME;
	if ((err = misc_register(&cxt->mdev))) {
		ACC_ERR("unable to register acc misc device!!\n");
	}
	/* dev_set_drvdata(cxt->mdev.this_device, cxt); */
	return err;
}


static int acc_input_init(struct acc_context *cxt)
{
	struct input_dev *dev;
	int err = 0;

	dev = input_allocate_device();
	if (NULL == dev)
		return -ENOMEM;

	dev->name = ACC_INPUTDEV_NAME;

	input_set_capability(dev, EV_ABS, EVENT_TYPE_ACCEL_X);
	input_set_capability(dev, EV_ABS, EVENT_TYPE_ACCEL_Y);
	input_set_capability(dev, EV_ABS, EVENT_TYPE_ACCEL_Z);
	input_set_capability(dev, EV_ABS, EVENT_TYPE_ACCEL_STATUS);
	input_set_capability(dev, EV_REL, EVENT_TYPE_ACCEL_UPDATE);

	input_set_abs_params(dev, EVENT_TYPE_ACCEL_X, ACC_VALUE_MIN, ACC_VALUE_MAX, 0, 0);
	input_set_abs_params(dev, EVENT_TYPE_ACCEL_Y, ACC_VALUE_MIN, ACC_VALUE_MAX, 0, 0);
	input_set_abs_params(dev, EVENT_TYPE_ACCEL_Z, ACC_VALUE_MIN, ACC_VALUE_MAX, 0, 0);
	input_set_abs_params(dev, EVENT_TYPE_ACCEL_STATUS, ACC_STATUS_MIN, ACC_STATUS_MAX, 0, 0);
	input_set_drvdata(dev, cxt);

	err = input_register_device(dev);
	if (err < 0) {
		input_free_device(dev);
		return err;
	}
	cxt->idev = dev;

	return 0;
}

DEVICE_ATTR(accenablenodata, S_IWUSR | S_IRUGO, acc_show_enable_nodata, acc_store_enable_nodata);
DEVICE_ATTR(accactive, S_IWUSR | S_IRUGO, acc_show_active, acc_store_active);
DEVICE_ATTR(accdelay, S_IWUSR | S_IRUGO, acc_show_delay, acc_store_delay);
DEVICE_ATTR(accbatch,      		S_IWUSR | S_IRUGO, acc_show_batch,  acc_store_batch);
DEVICE_ATTR(accflush,      		S_IWUSR | S_IRUGO, acc_show_flush,  acc_store_flush);
DEVICE_ATTR(accdevnum,      		S_IWUSR | S_IRUGO, acc_show_sensordevnum,  NULL);

static struct attribute *acc_attributes[] = {
	&dev_attr_accenablenodata.attr,
	&dev_attr_accactive.attr,
	&dev_attr_accdelay.attr,
	&dev_attr_accbatch.attr,
	&dev_attr_accflush.attr,
	&dev_attr_accdevnum.attr,
	NULL
};

static struct attribute_group acc_attribute_group = {
	.attrs = acc_attributes
};

int acc_register_data_path(struct acc_data_path *data)
{
	struct acc_context *cxt = NULL;
	cxt = acc_context_obj;
	cxt->acc_data.get_data = data->get_data;
	cxt->acc_data.get_raw_data = data->get_raw_data;
	cxt->acc_data.vender_div = data->vender_div;
	ACC_LOG("acc register data path vender_div: %d\n", cxt->acc_data.vender_div);
	if (NULL == cxt->acc_data.get_data) {
		ACC_LOG("acc register data path fail\n");
		return -1;
	}
	return 0;
}

int acc_register_control_path(struct acc_control_path *ctl)
{
	struct acc_context *cxt = NULL;
	int err = 0;
	cxt = acc_context_obj;
	cxt->acc_ctl.set_delay = ctl->set_delay;
	cxt->acc_ctl.open_report_data = ctl->open_report_data;
	cxt->acc_ctl.enable_nodata = ctl->enable_nodata;
	cxt->acc_ctl.is_support_batch = ctl->is_support_batch;
	cxt->acc_ctl.is_report_input_direct= ctl->is_report_input_direct;
	cxt->acc_ctl.acc_calibration = ctl->acc_calibration;

	if (NULL == cxt->acc_ctl.set_delay || NULL == cxt->acc_ctl.open_report_data
	    || NULL == cxt->acc_ctl.enable_nodata) {
		ACC_LOG("acc register control path fail\n");
		return -1;
	}
	/* add misc dev for sensor hal control cmd */
	err = acc_misc_init(acc_context_obj);
	if (err) {
		ACC_ERR("unable to register acc misc device!!\n");
		return -2;
	}
	err = sysfs_create_group(&acc_context_obj->mdev.this_device->kobj, &acc_attribute_group);
	if (err < 0) {
		ACC_ERR("unable to create acc attribute file\n");
		return -3;
	}

	kobject_uevent(&acc_context_obj->mdev.this_device->kobj, KOBJ_ADD);
	return 0;
}

int acc_data_report(int x, int y, int z, int status)
{
	/* ACC_LOG("+acc_data_report! %d, %d, %d, %d\n",x,y,z,status); */
	struct acc_context *cxt = NULL;
	int err = 0;
	cxt = acc_context_obj;
	input_report_abs(cxt->idev, EVENT_TYPE_ACCEL_X, x);
	input_report_abs(cxt->idev, EVENT_TYPE_ACCEL_Y, y);
	input_report_abs(cxt->idev, EVENT_TYPE_ACCEL_Z, z);
	input_report_abs(cxt->idev, EVENT_TYPE_ACCEL_STATUS, status);
	input_report_rel(cxt->idev, EVENT_TYPE_ACCEL_UPDATE, 1);
	input_sync(cxt->idev);
	return err;
}

static int acc_probe(struct platform_device *pdev)
{

	int err;
	ACC_LOG("+++++++++++++accel_probe!!\n");

	acc_context_obj = acc_context_alloc_object();
	if (!acc_context_obj) {
		err = -ENOMEM;
		ACC_ERR("unable to allocate devobj!\n");
		goto exit_alloc_data_failed;
	}
	/* init real acceleration driver */
	err = acc_real_driver_init();
	if (err) {
		ACC_ERR("acc real driver init fail\n");
		goto real_driver_init_fail;
	}
	//init acc common factory mode misc device
	err = acc_factory_device_init();
	if(err)
	{
		ACC_ERR("acc factory device already registed\n");
	}
	/* init input dev */
	err = acc_input_init(acc_context_obj);
	if (err) {
		ACC_ERR("unable to register acc input device!\n");
		goto exit_alloc_input_dev_failed;
	}

#if defined(CONFIG_HAS_EARLYSUSPEND) && defined(CONFIG_EARLYSUSPEND)
	atomic_set(&(acc_context_obj->early_suspend), 0);
	acc_context_obj->early_drv.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING - 1,
	    acc_context_obj->early_drv.suspend = acc_early_suspend,
	    acc_context_obj->early_drv.resume = acc_late_resume,
	    register_early_suspend(&acc_context_obj->early_drv);
#endif

  
	ACC_LOG("----accel_probe OK !!\n");
	return 0;


 real_driver_init_fail:
 exit_alloc_input_dev_failed:
	kfree(acc_context_obj);

 exit_alloc_data_failed:


	ACC_LOG("----accel_probe fail !!!\n");
	return err;
}



static int acc_remove(struct platform_device *pdev)
{
	int err=0;
	ACC_FUN(f);
	input_unregister_device(acc_context_obj->idev);
	sysfs_remove_group(&acc_context_obj->idev->dev.kobj, &acc_attribute_group);

	if ((err = misc_deregister(&acc_context_obj->mdev))) {
		ACC_ERR("misc_deregister fail: %d\n", err);
	}
	kfree(acc_context_obj);

	return 0;
}
#if defined(CONFIG_HAS_EARLYSUSPEND)
static void acc_early_suspend(struct early_suspend *h)
{
	atomic_set(&(acc_context_obj->early_suspend), 1);
	ACC_LOG(" acc_early_suspend ok------->hwm_obj->early_suspend=%d\n",
		atomic_read(&(acc_context_obj->early_suspend)));
	return;
}

/*----------------------------------------------------------------------------*/
static void acc_late_resume(struct early_suspend *h)
{
	atomic_set(&(acc_context_obj->early_suspend), 0);
	ACC_LOG(" acc_late_resume ok------->hwm_obj->early_suspend=%d\n",
		atomic_read(&(acc_context_obj->early_suspend)));
	return;
}
#endif
static int acc_suspend(struct platform_device *dev, pm_message_t state)
{
	return 0;
}

/*----------------------------------------------------------------------------*/
static int acc_resume(struct platform_device *dev)
{
	return 0;
}

#ifdef CONFIG_OF
static const struct of_device_id m_acc_pl_of_match[] = {
	{ .compatible = "mediatek,m_acc_pl", },
	{},
};
#endif

static struct platform_driver acc_driver = {
	.probe = acc_probe,
	.remove = acc_remove,
	.suspend = acc_suspend,
	.resume = acc_resume,
	.driver = {
		   .name = ACC_PL_DEV_NAME,
        #ifdef CONFIG_OF
		.of_match_table = m_acc_pl_of_match,
		#endif
		   }
};

static int __init acc_init(void)
{
	ACC_FUN();

	if (platform_driver_register(&acc_driver)) {
		ACC_ERR("failed to register acc driver\n");
		return -ENODEV;
	}

	return 0;
}

static void __exit acc_exit(void)
{
	platform_driver_unregister(&acc_driver);
	platform_driver_unregister(&gsensor_driver);
}
late_initcall(acc_init);
//module_init(acc_init);
//module_exit(acc_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("ACCELEROMETER device driver");
MODULE_AUTHOR("Mediatek");