summaryrefslogtreecommitdiff
path: root/drivers/input/sensor/us5182_lpsensor/us5182.c
blob: b251fb83180f02230175607a7090ed480d07bf3e (plain)
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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
/*
 * us5182.c - us5182 ALS & Proximity Driver
 *
 * By Intersil Corp
 * Michael DiGioia
 *
 * Based on isl29011.c
 *	by Mike DiGioia <mdigioia@intersil.com>
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/hwmon.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/sysfs.h>
#include <linux/pm_runtime.h>
#include <linux/input-polldev.h>
#include <linux/miscdevice.h>
#include <asm/uaccess.h>
//#include <linux/earlysuspend.h>
#include <linux/types.h>
#include "../sensor.h"
#include "us5182.h"
/* Insmod parameters */
//I2C_CLIENT_INSMOD_1(us5182);

#define MODULE_NAME	"us5182"

#undef dbg
#define dbg(fmt, args...) 

#undef errlog
#undef klog
#define errlog(fmt, args...) printk(KERN_ERR "[%s]: " fmt, __FUNCTION__, ## args)
#define klog(fmt, args...) printk(KERN_ALERT "[%s]: " fmt, __FUNCTION__, ## args)

struct us_device {
	struct input_polled_dev* input_poll_devl;
	struct input_polled_dev* input_poll_devp;
	struct i2c_client* client;
	struct class* class;
	struct device *lsdev;
	dev_t devno;
#ifdef CONFIG_HAS_EARLYSUSPEND
	struct early_suspend earlysuspend;
#endif
	u8     enable_id;

};
static int psh_l8th = 90;
static int psh_h8th = 0;
	
static int psl_l8th = 50;
static int psl_h8th = 0;

static struct i2c_client *this_client = NULL;
/*=====Global variable===============================*/
static u8  error_flag, debounces;
static int previous_value, this_value;
static struct i2c_client *gclient = NULL;
/*===================================================*/
static u8 reg_cache[us5182_NUM_CACHABLE_REGS];

static struct us_device* l_sensorconfig = NULL;
static int l_enable = 0; // 0:don't report data
static int p_enable = 0; // 0:don't report data

extern int wmt_getsyspara(char *varname, unsigned char *varval, int *varlen);
static int no_adc_map = 1;

static DEFINE_MUTEX(mutex);

static int us5182_i2c_read(struct i2c_client *client,u8 reg) 
{
#if 0
	int val;
        val = i2c_smbus_read_byte_data(client, reg);
        if (val < 0)
                printk("%s %d i2c transfer error\n", __func__, __LINE__);
        return val;
#endif
//in default our i2c controller, will not send repeatStart signal in read process.(stop-start)
//well this sensor must have the repeatStart signal to work normally
//so we have to pass I2C_M_NOSTART flag to controller 2013-7-5	
	char rdData[2] = {0};
	
	struct i2c_msg msgs[2] = 
	{
		{.addr = client->addr, .flags = 0|I2C_M_NOSTART, .len = 1, .buf = rdData,}, 
		{.addr = client->addr, .flags = I2C_M_RD, .len = 1, .buf = rdData,},
	};
	rdData[0] = reg;
	
	if (i2c_transfer(client->adapter, msgs, 2) < 0) {
		printk( "%s: transfer failed.", __func__);
		return -EIO;
	}
	
	return rdData[0];
}

static int get_als_resolution(struct i2c_client *client)
{		
        return (us5182_i2c_read(client,REGS_CR01) & 0x18) >> 3;
}


static int isl_get_lux_datal(struct i2c_client* client)
{
	
        int lsb, msb, bitdepth;

        mutex_lock(&mutex);
        lsb = us5182_i2c_read(client, REGS_LSB_SENSOR);//

        if (lsb < 0) {
                mutex_unlock(&mutex);
                return lsb;
        }

        msb = us5182_i2c_read(client, REGS_MSB_SENSOR);//
        mutex_unlock(&mutex);

        if (msb < 0)
                return msb;

	bitdepth = get_als_resolution(client);//?????
	switch(bitdepth){
	case 0:	
		lsb &= 0xF0; // 12bit??
		lsb >>= 4;  //add
		return ((msb << 4) | lsb);
		break;
	case 1:
		lsb &= 0xFC;  //?? 14bit
		lsb >>= 2;
		return ((msb << 6) | lsb);
		break;
	}	
	
	return ((msb << 8) | lsb);
}


static int get_ps_resolution(struct i2c_client *client)
{
        u8 data;

	data = (us5182_i2c_read(client,REGS_CR02) & 0x18) >> 3;

	return data;
}

static int isl_get_lux_datap(struct i2c_client* client)
{
	
        int lsb, msb, bitdepth;

        mutex_lock(&mutex);
        lsb = us5182_i2c_read(client, REGS_LSB_SENSOR_PS);

        if (lsb < 0) {
                mutex_unlock(&mutex);
                return lsb;
        }

        msb = us5182_i2c_read(client, REGS_MSB_SENSOR_PS);
        mutex_unlock(&mutex);

        if (msb < 0)
                return msb;

	bitdepth = get_ps_resolution(client);
	switch(bitdepth){
	case 0:	
		lsb &= 0xF0; // 12bit ??
		lsb >>= 4;
		return ((msb << 4) | lsb);
		break;
	case 1:
		lsb &= 0xFC; // 14bit ??
		lsb >>= 2;
		return ((msb << 6) | lsb);
		break;
	}	
	
	return ((msb << 8) | lsb); //we use 16bit now
}



/* Return 0 if detection is successful, -ENODEV otherwise */
static int us5182_detect(struct i2c_client *client/*, int kind,
                          struct i2c_board_info *info*/)
{	
	
	char rxData[2] = {0xb2, 0};
	int ret = 0;

#if 1	
	//rxData[0] = 0xb2;
	
	struct i2c_msg msgs[2] = {
	
			{.addr = client->addr, .flags = 0|I2C_M_NOSTART, .len = 1, .buf = rxData,},
			{.addr = client->addr, .flags = I2C_M_RD, .len = 1, .buf = rxData,}
		};
	ret = i2c_transfer(client->adapter, msgs, 2);
	if (ret < 0){
		printk(KERN_ERR "%s i2c_transfer error!\n", __FUNCTION__);
		return -EIO;
	}
			
#endif 	

	if(0x26 == rxData[0])
	{
		printk(KERN_ALERT "us5182 detected OK\n");
		return 0;
	}
	else
		return -1;
}

int isl_input_open(struct input_dev* input)
{
	return 0;
}

void isl_input_close(struct input_dev* input)
{
}

//Fixme plan to transfer the adc value to the config.xml lux 2013-5-10
static __u16 uadc[8] = {2, 8, 100, 400, 900, 1000, 1500, 1900};//customize
static __u16 ulux[9] = {128, 200, 1300, 2000, 3000, 4000, 5000, 6000, 7000};
static __u16 adc_to_lux(__u16 adc)
{
	static long long var = 0;
	int i = 0; //length of array is 8,9
	for (i=0; i<8; i++) {
		if ( adc < uadc[i]){
			break;
		}
	}
	if ( i<9)
	{
		var++;
		if (var%2)
			return ulux[i]+0;
		else
			return ulux[i]-1;
	}
	return ulux[4];	
}

static void isl_input_lux_poll_l(struct input_polled_dev *dev)
{
	struct us_device* idev = dev->private;
	struct input_dev* input = idev->input_poll_devl->input;
	struct i2c_client* client = idev->client;
	int ret_val = 0;
	
	if (client == NULL){
		printk("%s client NULL!\n", __FUNCTION__);
		return;
	}
	
	//printk("%s\n", __FUNCTION__);
	if (l_enable != 0)
	{
		//mutex_lock(&mutex); //dead lock!! 2013-7-9!!!
		//printk(KERN_ALERT "by flashchen val is %x",val);
		ret_val = isl_get_lux_datal(client); //adc
		if (ret_val < 0)
			return;
		if (!no_adc_map)
		ret_val = adc_to_lux(ret_val);
		
		input_report_abs(input, ABS_MISC, ret_val);
		//printk("%s %d\n", __FUNCTION__, ret_val);
		input_sync(input);
		//mutex_unlock(&mutex);
	}
}

static void isl_input_lux_poll_p(struct input_polled_dev *dev)
{
	struct us_device* idev = dev->private;
	struct input_dev* input = idev->input_poll_devp->input;
	struct i2c_client* client = idev->client;
	
	int tmp_val = 0, debounce = 0;
	int ret_val = 0;
	
	//printk("%s\n", __FUNCTION__);
	if (p_enable != 0)
	{
		//mutex_lock(&mutex);
		//printk(KERN_ALERT "by flashchen val is %x",val);
		
	#if 0 //just read raw data out	2013-7-18
		for (debounce=0; debounce<10; debounce++){	
			ret_val = isl_get_lux_datap(client);
			if (ret_val < 0)
				return;
			
			tmp_val += ret_val;
			msleep(1);
		}
		tmp_val /= 10;
	//add for near/far detection!	
		if (tmp_val > 0x00ff)
			tmp_val = 6;
		else
			tmp_val = 0;
		input_report_abs(input, ABS_MISC, tmp_val);
		input_sync(input);	
	#endif	
		
		tmp_val = us5182_i2c_read(client, REGS_CR00); 
		
		if (tmp_val & CR0_PROX_MASK)  //approach
			input_report_abs(input, ABS_MISC, 0);
		else
			input_report_abs(input, ABS_MISC, 6);
			
		input_sync(input);
		//printk("%s %d\n", __FUNCTION__, tmp_val);
		//mutex_unlock(&mutex);
	}
}

#if 0
static struct i2c_device_id us5182_id[] = {
	{"us5182", 0},
	{}
};

MODULE_DEVICE_TABLE(i2c, us5182_id);
#endif // 2013-7-9

static int mmad_open(struct inode *inode, struct file *file)
{
	dbg("Open the l-sensor node...\n");
	return 0;
}

static int mmad_release(struct inode *inode, struct file *file)
{
	dbg("Close the l-sensor node...\n");
	return 0;
}

static ssize_t mmadl_read(struct file *fl, char __user *buf, size_t cnt, loff_t *lf)
{
	int lux_data = 0;
	//printk("%s try to mutex_lock \n", __FUNCTION__);
	//mutex_lock(&mutex);
	//printk("lock ok!\n");
	lux_data = isl_get_lux_datal(l_sensorconfig->client);
	//mutex_unlock(&mutex);
	if (lux_data < 0)
	{
		printk("Failed to read lux data!\n");
		return -1;
	}
	printk(KERN_ALERT "lux_data is %x\n",lux_data); 
	//return 0;
	copy_to_user(buf, &lux_data, sizeof(lux_data));
	return sizeof(lux_data);
}


static ssize_t mmadp_read(struct file *fl, char __user *buf, size_t cnt, loff_t *lf)
{
	int lux_data = 0;
	
	//mutex_lock(&mutex);
	lux_data = isl_get_lux_datap(l_sensorconfig->client);
	//mutex_unlock(&mutex);
	if (lux_data < 0)
	{
		errlog("Failed to read lux data!\n");
		return -1;
	}
	printk(KERN_ALERT "lux_data is %x\n",lux_data); 
	//return 0;
	copy_to_user(buf, &lux_data, sizeof(lux_data));
	return sizeof(lux_data);
}

static long
mmadl_ioctl(/*struct inode *inode,*/ struct file *file, unsigned int cmd,
	   unsigned long arg)
{
	void __user *argp = (void __user *)arg;
	//char rwbuf[5];
	short enable;  //amsr = -1;
	unsigned int uval;

	//printk("l-sensor ioctr... cmd 0x%x arg %d\n", cmd, arg);
	//memset(rwbuf, 0, sizeof(rwbuf));
	switch (cmd) {
		case LIGHT_IOCTL_SET_ENABLE:
			// enable/disable sensor
			if (copy_from_user(&enable, argp, sizeof(short)))
			{
				printk(KERN_ERR "Can't get enable flag!!!\n");
				return -EFAULT;
			}
			dbg("enable=%d\n",enable);
			if ((enable >=0) && (enable <=1))
			{
				dbg("driver: disable/enable(%d) gsensor.\n", enable);
				
				//l_sensorconfig.sensor_enable = enable;
				dbg("Should to implement d/e the light sensor!\n");
				l_enable = enable;
				
			} else {
				printk(KERN_ERR "Wrong enable argument in %s !!!\n", __FUNCTION__);
				return -EINVAL;
			}
			break;
		case WMT_IOCTL_SENSOR_GET_DRVID:
#define DRVID 0
			uval = DRVID ;
			if (copy_to_user((unsigned int*)arg, &uval, sizeof(unsigned int)))
			{
				return -EFAULT;
			}
			dbg("us5182_driver_id:%d\n",uval);
		default:
			break;
	}

	return 0;
}


static long
mmadp_ioctl(/*struct inode *inode,*/ struct file *file, unsigned int cmd,
	   unsigned long arg)
{
	void __user *argp = (void __user *)arg;
	//char rwbuf[5];
	short enable;  //amsr = -1;
	unsigned int uval;
	unsigned char regval;

	dbg("l-sensor ioctr...\n");
	//memset(rwbuf, 0, sizeof(rwbuf));
	switch (cmd) {
		case LIGHT_IOCTL_SET_ENABLE:
			// enable/disable sensor
			if (copy_from_user(&enable, argp, sizeof(short)))
			{
				printk(KERN_ERR "Can't get enable flag!!!\n");
				return -EFAULT;
			}
			dbg("enable=%d\n",enable);
			if ((enable >=0) && (enable <=1))
			{
				dbg("driver: disable/enable(%d) gsensor.\n", enable);
				
				//l_sensorconfig.sensor_enable = enable;
				dbg("Should to implement d/e the light sensor!\n");
				p_enable = enable;
			#if 1	
				if(p_enable)
				{
					regval = us5182_i2c_read(l_sensorconfig->client, 0);
					regval &= ~(3 << 4);
					i2c_smbus_write_byte_data(l_sensorconfig->client, 0, regval);
				}
				else
				{
					regval = us5182_i2c_read(l_sensorconfig->client, 0);
					regval &= ~(3 << 4);
					regval |= (1 << 4);
					i2c_smbus_write_byte_data(l_sensorconfig->client, 0, regval);
				}
			#endif	
				
			} else {
				printk(KERN_ERR "Wrong enable argument in %s !!!\n", __FUNCTION__);
				return -EINVAL;
			}
			break;
		case WMT_IOCTL_SENSOR_GET_DRVID:
#define DRVID 0
			uval = DRVID ;
			if (copy_to_user((unsigned int*)arg, &uval, sizeof(unsigned int)))
			{
				return -EFAULT;
			}
			dbg("us5182_driver_id:%d\n",uval);
		default:
			break;
	}

	return 0;
}


static struct file_operations mmadl_fops = {
	.owner = THIS_MODULE,
	.open = mmad_open,
	.release = mmad_release,
	.read = mmadl_read,
	.unlocked_ioctl = mmadl_ioctl,
};

static struct miscdevice mmadl_device = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "lsensor_ctrl",
	.fops = &mmadl_fops,
};

static struct file_operations mmadp_fops = {
	.owner = THIS_MODULE,
	.open = mmad_open,
	.release = mmad_release,
	.read = mmadp_read,
	.unlocked_ioctl = mmadp_ioctl,
};

static struct miscdevice mmadp_device = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "psensor_ctrl",
	.fops = &mmadp_fops,
};

#ifdef CONFIG_HAS_EARLYSUSPEND
static void us5182_early_suspend(struct early_suspend *h)
{
	dbg("start\n");
	mutex_lock(&mutex);
	//pm_runtime_get_sync(dev);
	//isl_set_mod(client, ISL_MOD_POWERDOWN);
	//pm_runtime_put_sync(dev);
	mutex_unlock(&mutex);
	dbg("exit\n");
}

static void us5182_late_resume(struct early_suspend *h)
{
	struct i2c_client *client = l_sensorconfig->client;

	dbg("start\n");
	mutex_lock(&mutex);
	//pm_runtime_get_sync(dev);
	//isl_set_mod(client, last_mod);
	//isl_set_default_config(client);
	//pm_runtime_put_sync(dev);
	mutex_unlock(&mutex);	
	dbg("exit\n");
}
#endif

int us5182_i2c_write(struct i2c_client *client, u8 reg,u8 mask, u8 shift, int val ) {
        //struct us5182_data *data = i2c_get_clientdata(client);
	int err;
        u8 tmp;
	mutex_lock(&mutex);

	tmp = reg_cache[reg];
        tmp &= ~mask;
        tmp |= val << shift;

        err = i2c_smbus_write_byte_data(client, reg, tmp);
        if (!err)
                reg_cache[reg] = tmp;

        mutex_unlock(&mutex);
        if (err >= 0) return 0;

        printk("%s %d i2c transfer error\n", __func__, __LINE__);
        return err;
}


static int set_word_mode(struct i2c_client *client, int mode)
{
	//
	return us5182_i2c_write(client, REGS_CR00,
                CR0_WORD_MASK, CR0_WORD_SHIFT, mode);
}


static int set_oneshotmode(struct i2c_client *client, int mode)
{
        return us5182_i2c_write(client,REGS_CR00,CR0_ONESHOT_MASK, CR0_ONESHOT_SHIFT, mode);
}


static int set_opmode(struct i2c_client *client, int mode)
{
        return us5182_i2c_write(client,REGS_CR00,CR0_OPMODE_MASK, 
		CR0_OPMODE_SHIFT, mode);
}

/* power_status */
static int set_power_status(struct i2c_client *client, int status)
{
	if(status == CR0_SHUTDOWN_EN )
                return us5182_i2c_write(client,REGS_CR00,CR0_SHUTDOWN_MASK, 
                	CR0_SHUTDOWN_SHIFT,CR0_SHUTDOWN_EN);
	else
		return us5182_i2c_write(client,REGS_CR00,CR0_SHUTDOWN_MASK, 
			CR0_SHUTDOWN_SHIFT, CR0_OPERATION);
}


static int us5182_init_client(struct i2c_client *client)
{
        
        struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
	int i = 0;
	int v = -1;
	if ( !i2c_check_functionality(adapter,I2C_FUNC_SMBUS_BYTE_DATA) ) {
                printk(KERN_INFO "byte op is not permited.\n");
                return -EIO;
        }

        /* read all the registers once to fill the cache.
         * if one of the reads fails, we consider the init failed */
	
        for (i = 0; i < ARRAY_SIZE(reg_cache); i++) {
                v = us5182_i2c_read(client, i);
		printk("reg 0x%x value 0x%x \n", i, v);
                if (v < 0)
                        return -ENODEV;
                reg_cache[i] = v;
        }

	/*Set Default*/
	set_word_mode(client, 0);//word enable? //just byte one time
	set_power_status(client,CR0_OPERATION); //power on?
	set_opmode(client,CR0_OPMODE_ALSONLY); //CR0_OPMODE_ALSANDPS CR0_OPMODE_ALSONLY
	set_oneshotmode(client, CR0_ONESHOT_DIS);
	
	us5182_i2c_write(client, REGS_CR03, CR3_LEDDR_MASK, CR3_LEDDR_SHIFT, CR3_LEDDR_50);
	
	//set als gain
	us5182_i2c_write(client, REGS_CR01, CR1_ALS_GAIN_MASK, CR1_ALS_GAIN_SHIFT, CR1_ALS_GAIN_X8);
	
	//set ps threshold --> lth, hth
	us5182_i2c_write(client, REGS_INT_LSB_TH_HI_PS, 0xff, 0, psh_l8th);  //
	us5182_i2c_write(client, REGS_INT_MSB_TH_HI_PS, 0xff, 0, psh_h8th); //0 default
	
	us5182_i2c_write(client, REGS_INT_LSB_TH_LO_PS, 0xff, 0, psl_l8th); //
	us5182_i2c_write(client, REGS_INT_MSB_TH_LO_PS, 0xff, 0, psl_h8th); // 0 default
	//set_resolution(client,us5182_RES_16);
	//set_range(client,3);
	//set_int_ht(client,0x3E8); //1000 lux
	//set_int_lt(client,0x8); //8 lux
	//dev_info(&data->client->dev, "us5182 ver. %s found.\n",DRIVER_VERSION);
	
	/*init global variable*/
	error_flag  = 0;
	previous_value  = 0;
	this_value = 0;
	debounces = 2;//debounces 5 times
	return 0;
}
//******add for sys debug devic_attribute
static ssize_t reg_show(struct device *dev, struct device_attribute *attr, char *buf) {

	int i = 0, val = 0;
	printk("<<<<<<<<<reg dump\n");
	
	for (i=0; i<=0x1f; i++) {
		val = us5182_i2c_read(gclient, i);
		printk("reg 0x%x: val 0x%x \n", i, val);
			
	}
	printk("\n");
	printk("<<<<<<<<<<<<<<<<<<<<dump end\n");
	return 0;
}

static ssize_t reg_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) {

	int reg = 0, val = 0;
	int ret = 0;
	int res = 0;
	
	printk("buf is %s\n", buf);
	
	ret = sscanf(buf, "%x:%x", &reg, &val);
	printk("reg:val is 0x%x:0x%x\n", reg, val);
	
	if (reg<0 || val<0) {
		printk("param error!\n");
		return -1;
	}		
	
	res = us5182_i2c_read(gclient, reg);
	i2c_smbus_write_byte_data(gclient, reg, val);
	printk(KERN_ERR "reg 0x%x 0x%x -->0x%x\n", reg, res, val);
	
	return count;
}
//struct device_attribute dev_attr_reg = __ATTR(reg, 0644, reg_show, reg_store);
//DEVICE_ATTR(reg, 0644, reg_show, reg_store);

static ssize_t adc_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	
	int i;
	int size = sizeof(uadc)/sizeof(uadc[0]);
	printk("<<<%s\n", __FUNCTION__);
	for (i=0; i<size; i++)
	{
		printk(" %5d  ", uadc[i]);
		//sprintf(buf, "%d " &uadc[i]);
	}
	printk("\n");
	
	return sizeof(uadc);
	//printk(" %s \n", buf);
}

static ssize_t adc_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
	__u32 tmp;
	int index;
	int n;
	int size = sizeof(uadc)/sizeof(uadc[0]);
	
	printk("<<<%s\n", __FUNCTION__);
	printk("<< %s >>>\n", buf);
	n = sscanf(buf, "%d:%d", &index, &tmp);
	printk("<<<<int n %d %d:%d \n", n, index, tmp);
	if ( n==2 && index>=0 && index<size){
	
		uadc[index] = tmp;
		no_adc_map = 0;
	}	
	else {
		printk("<<<param error!\n");
		no_adc_map = 1;
	}	
	return count;
}

static struct device_attribute us5182_attr[] = 
{
	__ATTR(reg, 0644, reg_show, reg_store),
	__ATTR(adc, 0644, adc_show, adc_store),
	__ATTR_NULL,
};
  
static int device_create_attribute(struct device *dev, struct device_attribute *attr)
{
	int err = 0, i;
	for (i=0; NULL != attr[i].attr.name; i++)
	{
		err = device_create_file(dev, &attr[i]); //&attr[i].attr
		if (err)
			break;
	}
	if (err)
	{
		for (; i>=0; i--)
		device_remove_file(dev, &attr[i]);//&attr[i].attr
	}
	return err;
}

static void device_remove_attribute(struct device *dev, struct device_attribute *attr)
{
	int i;
	for (i=0; attr[i].attr.name != NULL; i++)
		device_remove_file(dev, &attr[i]); //&attr[i].attr
}
//add end
static int
us5182_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	int res=0;

	struct us_device* idev = kzalloc(sizeof(struct us_device), GFP_KERNEL);
	if(!idev)
		return -ENOMEM;

	l_sensorconfig = idev;
	
	/*initial enable device id*/
	idev->enable_id = 0x00;
	
	gclient = client;
	/* initialize the us5182 chip */
        res = us5182_init_client(client);//
	
        if (res != 0)
              goto err_input_allocate_device;
/* last mod is ALS continuous */
	//pm_runtime_enable(&client->dev);	
	idev->input_poll_devl = input_allocate_polled_device();
	if(!idev->input_poll_devl)
	{
		res = -ENOMEM;
		goto err_input_allocate_device;
	}
	idev->input_poll_devp = input_allocate_polled_device();
	if(!idev->input_poll_devp)
	{
		res = -ENOMEM;
		goto err_input_allocate_device;
	}
	idev->client = client;

	idev->input_poll_devl->private = idev;
	idev->input_poll_devl->poll = isl_input_lux_poll_l;
	idev->input_poll_devl->poll_interval = 100;//50;
	idev->input_poll_devl->input->open = isl_input_open;
	idev->input_poll_devl->input->close = isl_input_close;
	idev->input_poll_devl->input->name = "lsensor_lux";
	idev->input_poll_devl->input->id.bustype = BUS_I2C;
	idev->input_poll_devl->input->dev.parent = &client->dev;

	input_set_drvdata(idev->input_poll_devl->input, idev);
	input_set_capability(idev->input_poll_devl->input, EV_ABS, ABS_MISC);
	input_set_abs_params(idev->input_poll_devl->input, ABS_MISC, 0, 16000, 0, 0);

	idev->input_poll_devp->private = idev;
	idev->input_poll_devp->poll = isl_input_lux_poll_p;
	idev->input_poll_devp->poll_interval = 10;//100; 50ms
	idev->input_poll_devp->input->open = isl_input_open;
	idev->input_poll_devp->input->close = isl_input_close;
	idev->input_poll_devp->input->name = "psensor_lux";
	idev->input_poll_devp->input->id.bustype = BUS_I2C;
	idev->input_poll_devp->input->dev.parent = &client->dev;

	input_set_drvdata(idev->input_poll_devp->input, idev);
	input_set_capability(idev->input_poll_devp->input, EV_ABS, ABS_MISC);
	input_set_abs_params(idev->input_poll_devp->input, ABS_MISC, 0, 16000, 0, 0);
	i2c_set_clientdata(client, idev);
	/* set default config after set_clientdata */
	//res = isl_set_default_config(client);	
	res = misc_register(&mmadl_device);
	if (res) {
		errlog("mmad_device register failed\n");
		goto err_misc_registerl;
	}
	res = misc_register(&mmadp_device);
	if (res) {
		errlog("mmad_device register failed\n");
		goto err_misc_registerp;
	}
	res = input_register_polled_device(idev->input_poll_devl);
	if(res < 0)
		goto err_input_register_devicel;
	res = input_register_polled_device(idev->input_poll_devp);
	if(res < 0)
		goto err_input_register_devicep;
	// suspend/resume register
#ifdef CONFIG_HAS_EARLYSUSPEND
        idev->earlysuspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
        idev->earlysuspend.suspend = us5182_early_suspend;
        idev->earlysuspend.resume = us5182_late_resume;
        register_early_suspend(&(idev->earlysuspend));
#endif

	dbg("us5182 probe succeed!\n");
	res = alloc_chrdev_region(&idev->devno, 0, 1, "us5182");
  	if(res)
	{
		printk("can't allocate chrdev\n");
		return 0;
	}
	idev->class = class_create(THIS_MODULE, "us5182-lsensor");
	if (IS_ERR(idev->class)) {
		printk("<<< %s class_create() error!\n", __FUNCTION__);
		return 0;
	}
	idev->lsdev = device_create(idev->class, NULL, idev->devno, NULL, "us5182");
	if (IS_ERR(idev->lsdev)) {
		printk("<<< %s device_create() error!\n", __FUNCTION__);
		return 0;
	}
	res = device_create_attribute(idev->lsdev, us5182_attr);
	return 0;
err_input_register_devicep:
	input_free_polled_device(idev->input_poll_devp);
err_input_register_devicel:
	input_free_polled_device(idev->input_poll_devl);
err_misc_registerp:
	misc_deregister(&mmadp_device);
err_misc_registerl:
	misc_deregister(&mmadl_device);
err_input_allocate_device:
	//__pm_runtime_disable(&client->dev, false);
	kfree(idev);
	return res;
}

static int us5182_remove(struct i2c_client *client)
{
	int i = 0;
	struct us_device* idev = i2c_get_clientdata(client);
#if 1	
	//device_remove_file(idev->lsdev, &dev_attr_reg);
	device_remove_attribute(idev->lsdev, us5182_attr);
	unregister_chrdev_region(idev->devno, 1);
	device_destroy(idev->class, idev->devno);
	class_destroy(idev->class);
#endif	
	printk("%s %d\n", __FUNCTION__, i++); // 0
	//unregister_early_suspend(&(idev->earlysuspend));
	misc_deregister(&mmadl_device);
	printk("%s %d\n", __FUNCTION__, i++);
	misc_deregister(&mmadp_device);
	printk("%s %d\n", __FUNCTION__, i++);
	input_unregister_polled_device(idev->input_poll_devl);//here block??
	printk("%s %d\n", __FUNCTION__, i++);
	input_unregister_polled_device(idev->input_poll_devp);
	printk("%s %d\n", __FUNCTION__, i++);
	input_free_polled_device(idev->input_poll_devl);
	printk("%s %d\n", __FUNCTION__, i++);
	input_free_polled_device(idev->input_poll_devp);
	printk("%s %d\n", __FUNCTION__, i++);
	//__pm_runtime_disable(&client->dev, false);

	kfree(idev);
	printk(KERN_INFO MODULE_NAME ": %s us5182 remove call, \n", __func__);
	return 0;
}
static void us5182_shutdown(struct i2c_client *client)
{
	l_enable = 0;
	p_enable = 0;	
}

static int us5182_suspend(struct i2c_client *client, pm_message_t message)
{
	return 0;
}
static int us5182_resume(struct i2c_client *client)
{
	int res = 0;
	res = us5182_init_client(client);//
	return 0;
}

static const struct i2c_device_id us5182_id[] = {
	{ SENSOR_I2C_NAME , 0 },
	{},
};


static struct i2c_driver us5182_i2c_driver = 
{
	.probe		= us5182_probe,
	.remove		= us5182_remove,
	.suspend	= us5182_suspend,
	.resume 	= us5182_resume,
	.shutdown 	= us5182_shutdown,
	.driver 	= {
			.name	= SENSOR_I2C_NAME,
			.owner 	= THIS_MODULE,
			},
	.id_table	= us5182_id,	
};

static int get_adc_val(void)
{
	int i=0, varlen=0, n=0;
	__u32 buf[8] = {0};
	char varbuf[50] ={0};
	char *name = "wmt.io.lsensor";
	char *psth = "wmt.io.psensor";
	int thbuf[4] = {0};
	
	varlen = sizeof(varbuf);
	if (wmt_getsyspara(psth, varbuf, &varlen))
	{
		printk("<<<<fail to wmt_syspara %s\n", "wmt.io.psensor");
		
	}
	else
	{
		n = sscanf(varbuf, "%d:%d:%d:%d", &thbuf[0], &thbuf[1], &thbuf[2], &thbuf[3]);
		if (n == 4)
		{
			psh_h8th = thbuf[0];
			psh_l8th = thbuf[1];
			psl_h8th = thbuf[2];
			psl_l8th = thbuf[3];
		}
		else
			printk("wmt.io.psensor error!\n");
	}
	if (wmt_getsyspara(name, varbuf, &varlen))
	{
		printk("<<<<fail to wmt_syspara %s\n", "wmt.io.lsensor");
		no_adc_map = 1;
		return -1;
	}
	n = sscanf(varbuf, "%d:%d:%d:%d:%d:%d:%d:%d", &buf[0], &buf[1], &buf[2], &buf[3], &buf[4], &buf[5], \
	&buf[6], &buf[7]);
	//printk("<<< n %d \n", n);
	if (n != 8)
	{
		printk("<<<<<%s uboot env  wmt.io.lsensor param error!\n", __FUNCTION__);
		return -1;
	}
	for (i=0; i<8; i++)
	{
		//printk("<<<< %5d ", buf[i]);
		uadc[i] = buf[i];
	}
	no_adc_map = 0;
	//printk("\n");
	return 0;
}

static int __init sensor_us5182_init(void)
{
	 printk(KERN_INFO MODULE_NAME ": %s us5182 init call, \n", __func__);
	/*
	 * Force device to initialize: i2c-15 0x44
	 * If i2c_new_device is not called, even us5182_detect will not run
	 * TODO: rework to automatically initialize the device
	 */
	//i2c_new_device(i2c_get_adapter(15), &isl_info);
	//return i2c_add_driver(&us5182_driver);
	if (!(this_client = sensor_i2c_register_device(4, SENSOR_I2C_ADDR, SENSOR_I2C_NAME)))  
	{
		printk(KERN_EMERG"Can't register gsensor i2c device!\n");
		return -1;
	}
	if (us5182_detect(this_client)) //
	{
		errlog("Can't find light sensor us5182!\n");
		goto detect_fail;
	}
	
	get_adc_val();
/*	
	if(us5182_probe(this_client))
	{
		errlog("Erro for probe!\n");
		goto detect_fail;
	}
*/	
	if(i2c_add_driver(&us5182_i2c_driver) < 0)
	{
		errlog("Erro for i2c_add_driver()!\n");
		goto detect_fail;
	}
	return 0;

detect_fail:
	sensor_i2c_unregister_device(this_client);
	return -1;
}

static void __exit sensor_us5182_exit(void)
{
 	printk(KERN_INFO MODULE_NAME ": %s us5182 exit call \n", __func__);
 	//us5182_remove(this_client);
	i2c_del_driver(&us5182_i2c_driver);
 	sensor_i2c_unregister_device(this_client);
	
}

module_init(sensor_us5182_init);
module_exit(sensor_us5182_exit);

MODULE_AUTHOR("rambo");
MODULE_ALIAS("us5182 ALS");
MODULE_DESCRIPTION("us5182 Driver");
MODULE_LICENSE("GPL v2");