summaryrefstree
path: root/kernel/drivers/sensorhub/brcm/factory/proximity.c
blob: 788884d710a98c245d3bc0aac6bf6fe400ba2ae9 (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
#include "../ssp.h"
#include "sensors.h"

#define PROX_ADC_BITS_NUM		14
#define THRESHOLD_HIGH			0
#define THRESHOLD_LOW			1
#define THRESHOLD_DETECT_HIGH	2
#define THRESHOLD_DETECT_LOW	3

#define PROX_CAL_FILE_PATH	"/efs/FactoryApp/gyro_cal_data"

struct sensor_manager prox_manager;
#define get_prox(dev) ((prox *)get_sensor_ptr(dev_get_drvdata(dev), &prox_manager, PROXIMITY_SENSOR))

struct proximity_t prox_default = {
	.name = " ",
	.vendor = " ",
	.get_prox_position = sensor_default_show
};

/*************************************************************************/
/* Functions                                                             */
/*************************************************************************/
static s16 get_proximity_rawdata(struct ssp_data *data)
{
	s16 uRowdata = 0;
	char chTempbuf[4] = { 0 };

	s32 dMsDelay = 20;

	memcpy(&chTempbuf[0], &dMsDelay, 4);

	if (data->bProximityRawEnabled == false) {
		send_instruction(data, ADD_SENSOR, PROXIMITY_RAW, chTempbuf, 4);
		msleep(200);
		uRowdata = data->buf[PROXIMITY_RAW].prox_raw[0];
		send_instruction(data, REMOVE_SENSOR, PROXIMITY_RAW,
			chTempbuf, 4);
	} else {
		uRowdata = data->buf[PROXIMITY_RAW].prox_raw[0];
	}

	return uRowdata;
}

static u16 get_proximity_all_threshold(struct ssp_data *data, int threshold_type)
{
	int iRet = 0;
	struct ssp_msg *msg;
	u8 buffer[8] = {0, };
	u16 threshold_high = 0;
	u16 threshold_low = 0;
	u16 threshold_detect_high = 0;
	u16 threshold_detect_low = 0;
	u16 threshold = 0;

	msg = kzalloc(sizeof(*msg), GFP_KERNEL);
	msg->cmd = MSG2SSP_AP_PROX_GET_THRESHOLD;
	msg->length = 8;
	msg->options = AP2HUB_READ;
	msg->buffer = buffer;
	msg->free_buffer = 0;

	iRet = ssp_spi_sync(data, msg, 1000);
	if (iRet != SUCCESS) {
		pr_err("[SSP] %s fail %d\n", __func__, iRet);
		return FAIL;
	}

	threshold_high = ((((u16) buffer[1]) << 8) & 0xff00) | ((((u16) buffer[0])) & 0x00ff);
	threshold_low = ((((u16) buffer[3]) << 8) & 0xff00) | ((((u16) buffer[2])) & 0x00ff);
	threshold_detect_high = ((((u16) buffer[5]) << 8) & 0xff00) | ((((u16) buffer[4])) & 0x00ff);
	threshold_detect_low = ((((u16) buffer[7]) << 8) & 0xff00) | ((((u16) buffer[6])) & 0x00ff);

	pr_info("[SSP] %s - %d, %d, %d, %d\n", __func__,
		threshold_high, threshold_low, threshold_detect_high, threshold_detect_low);

	if(threshold_type == THRESHOLD_HIGH) {
		threshold = threshold_high;
	}
	else if(threshold_type == THRESHOLD_LOW) {
		threshold = threshold_low;
	}
	else if(threshold_type == THRESHOLD_DETECT_HIGH) {
		threshold = threshold_detect_high;
	}
	else {
		threshold = threshold_detect_low;
	}

	return threshold;
	
}

/*************************************************************************/
/* factory Sysfs                                                         */
/*************************************************************************/

static ssize_t proximity_vendor_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "%s\n",  get_prox(dev)->vendor);
}

static ssize_t proximity_name_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "%s\n",  get_prox(dev)->name);
}

static ssize_t proximity_probe_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct ssp_data *data = dev_get_drvdata(dev);
	bool probe_pass_fail = FAIL;

	if (data->uSensorState & (1 << PROXIMITY_SENSOR))
		probe_pass_fail = SUCCESS;
	else
		probe_pass_fail = FAIL;

	pr_info("[SSP]: %s - All sensor 0x%llx, prox_sensor %d\n",
		__func__, data->uSensorState, probe_pass_fail);

	return snprintf(buf, PAGE_SIZE, "%d\n", probe_pass_fail);
}

static ssize_t proximity_thresh_high_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct ssp_data *data = dev_get_drvdata(dev);
	u16 threshold_high = get_proximity_all_threshold(data, THRESHOLD_HIGH);

	return snprintf(buf, PAGE_SIZE, "%d\n", threshold_high);
}

static ssize_t proximity_thresh_high_store(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t size)
{
	u16 uNewThresh;
	int iRet, i = 0;
	u16 prox_bits_mask = 0, prox_non_bits_mask = 0;
	struct ssp_data *data = dev_get_drvdata(dev);

	while (i < PROX_ADC_BITS_NUM)
		prox_bits_mask += (1 << i++);

	while (i < 16)
		prox_non_bits_mask += (1 << i++);

	iRet = kstrtou16(buf, 10, &uNewThresh);
	if (iRet < 0)
		pr_err("[SSP]: %s - kstrto16 failed.(%d)\n", __func__, iRet);
	else {
		if (uNewThresh & prox_non_bits_mask)
			pr_err("[SSP]: %s - allow %ubits.(%d)\n", __func__, PROX_ADC_BITS_NUM, uNewThresh);
		else {
			uNewThresh &= prox_bits_mask;
			data->uProxHiThresh = uNewThresh;
		}
	}

	ssp_dbg("[SSP]: %s - new prox threshold : hi - %u\n", __func__, data->uProxHiThresh);

	return size;
}

static ssize_t proximity_thresh_low_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct ssp_data *data = dev_get_drvdata(dev);
	u16 threshold_low = get_proximity_all_threshold(data, THRESHOLD_LOW);

	return snprintf(buf, PAGE_SIZE, "%d\n", threshold_low);
}

static ssize_t proximity_thresh_low_store(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t size)
{
	u16 uNewThresh;
	int iRet, i = 0;
	u16 prox_bits_mask = 0, prox_non_bits_mask = 0;
	struct ssp_data *data = dev_get_drvdata(dev);

	while (i < PROX_ADC_BITS_NUM)
		prox_bits_mask += (1 << i++);

	while (i < 16)
		prox_non_bits_mask += (1 << i++);

	iRet = kstrtou16(buf, 10, &uNewThresh);
	if (iRet < 0)
		pr_err("[SSP]: %s - kstrto16 failed.(%d)\n", __func__, iRet);
	else {
		if (uNewThresh & prox_non_bits_mask)
			pr_err("[SSP]: %s - allow %ubits.(%d)\n", __func__, PROX_ADC_BITS_NUM, uNewThresh);
		else {
			uNewThresh &= prox_bits_mask;
			data->uProxLoThresh = uNewThresh;
		}
	}

	ssp_dbg("[SSP]: %s - new prox threshold : lo - %u\n", __func__, data->uProxLoThresh);

	return size;
}

static ssize_t proximity_thresh_detect_high_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct ssp_data *data = dev_get_drvdata(dev);
	u16 threshold_detect_high = get_proximity_all_threshold(data, THRESHOLD_DETECT_HIGH);

	return snprintf(buf, PAGE_SIZE, "%d\n", threshold_detect_high);
}

static ssize_t proximity_thresh_detect_high_store(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t size)
{
	u16 uNewThresh;
	int iRet, i = 0;
	u16 prox_bits_mask = 0, prox_non_bits_mask = 0;
	struct ssp_data *data = dev_get_drvdata(dev);

	while (i < PROX_ADC_BITS_NUM)
		prox_bits_mask += (1 << i++);

	while (i < 16)
		prox_non_bits_mask += (1 << i++);

	iRet = kstrtou16(buf, 10, &uNewThresh);
	if (iRet < 0)
		pr_err("[SSP]: %s - kstrto16 failed.(%d)\n", __func__, iRet);
	else {
		if (uNewThresh & prox_non_bits_mask)
			pr_err("[SSP]: %s - allow %ubits.(%d)\n", __func__, PROX_ADC_BITS_NUM, uNewThresh);
		else {
			uNewThresh &= prox_bits_mask;
			data->uProxHiThresh_detect = uNewThresh;
		}
	}

	ssp_dbg("[SSP]: %s - new prox threshold : hidetect - %u\n", __func__, data->uProxHiThresh_detect);

	return size;
}

static ssize_t proximity_thresh_detect_low_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct ssp_data *data = dev_get_drvdata(dev);
	u16 threshold_detect_low = get_proximity_all_threshold(data, THRESHOLD_DETECT_LOW);

	return snprintf(buf, PAGE_SIZE, "%d\n", threshold_detect_low);
}

static ssize_t proximity_thresh_detect_low_store(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t size)
{
	u16 uNewThresh;
	int iRet, i = 0;
	u16 prox_bits_mask = 0, prox_non_bits_mask = 0;
	struct ssp_data *data = dev_get_drvdata(dev);

	while (i < PROX_ADC_BITS_NUM)
		prox_bits_mask += (1 << i++);

	while (i < 16)
		prox_non_bits_mask += (1 << i++);

	iRet = kstrtou16(buf, 10, &uNewThresh);
	if (iRet < 0)
		pr_err("[SSP]: %s - kstrto16 failed.(%d)\n", __func__, iRet);
	else {
		if (uNewThresh & prox_non_bits_mask)
			pr_err("[SSP]: %s - allow %ubits.(%d)\n", __func__, PROX_ADC_BITS_NUM, uNewThresh);
		else {
			uNewThresh &= prox_bits_mask;
			data->uProxLoThresh_detect = uNewThresh;
		}
	}

	ssp_dbg("[SSP]: %s - new prox threshold : lodetect - %u\n", __func__, data->uProxLoThresh_detect);

	return size;
}


static ssize_t proximity_raw_data_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct ssp_data *data = dev_get_drvdata(dev);

	return sprintf(buf, "%d\n", get_proximity_rawdata(data));
}

int load_prox_cal_from_nvm(int *cal_data, int size)
{
	int ret = 0;
	mm_segment_t old_fs;
	struct file *cal_filp = NULL;

	if (size < 2 * sizeof(int)) {
		pr_err("[SSP]: %s - invalid size(%d)", __func__, size);
		return -EIO;
	}
	pr_info("[SSP] %s ", __func__);


	old_fs = get_fs();
	set_fs(KERNEL_DS);

	cal_filp = filp_open(PROX_CAL_FILE_PATH, O_RDONLY, 0660);
	if (IS_ERR(cal_filp)) {
		ret = PTR_ERR(cal_filp);
		if (ret != -ENOENT)
			pr_err("[SSP]: %s - Can't open cancellation file\n",
				__func__);
		goto exit;
	}

	cal_filp->f_pos = PROX_CAL_FILE_INDEX;
	ret = vfs_read(cal_filp, (u8 *)cal_data, size, &cal_filp->f_pos);
	if (ret != size) {
		memset(cal_data, 0, size);
		pr_err("[SSP]: %s - Can't read the cancel data\n", __func__);
		ret = -EIO;
	}	

	filp_close(cal_filp, current->files);
exit:
	set_fs(old_fs);

	pr_info("[SSP] %s: flag %d, gain %d\n", __func__, cal_data[0], cal_data[1]);

	return ret;
}

static ssize_t proximity_default_trim_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	int ret = 0;
	int prox_cal[2] = {0, };

	pr_info("[SSP] %s ", __func__);

	ret = load_prox_cal_from_nvm(prox_cal, sizeof(prox_cal));

	// prox_cal[1] : moving sum offset
	// 1: no offset
	// 2: moving sum type2 
	// 3: moving sum type3
	if (ret != sizeof(prox_cal)) {
		ret = 1;
	} else {
		ret = prox_cal[1];
	}

	return snprintf(buf, PAGE_SIZE, "%d\n", ret);
}

static ssize_t proximity_avg_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct ssp_data *data = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n",
		data->buf[PROXIMITY_RAW].prox_raw[1],
		data->buf[PROXIMITY_RAW].prox_raw[2],
		data->buf[PROXIMITY_RAW].prox_raw[3]);
}

/*
 *	Proximity Raw Sensor Register/Unregister
 */
static ssize_t proximity_avg_store(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t size)
{
	char chTempbuf[4] = { 0 };
	int iRet;
	int64_t dEnable;
	struct ssp_data *data = dev_get_drvdata(dev);

	s32 dMsDelay = 20;

	memcpy(&chTempbuf[0], &dMsDelay, 4);

	iRet = kstrtoll(buf, 10, &dEnable);
	if (iRet < 0)
		return iRet;
	if (atomic64_read(&data->aSensorEnable) & (1 << PROXIMITY_SENSOR)) {
		if (dEnable) {
			data->buf[PROXIMITY_RAW].prox_raw[0] = -1;
			data->buf[PROXIMITY_RAW].prox_raw[1] = 0;
			data->buf[PROXIMITY_RAW].prox_raw[2] = 0;
			data->buf[PROXIMITY_RAW].prox_raw[3] = 0;
			data->uFactoryProxAvg[0] = 0;
			data->uFactoryProxAvg[1] = 0;
			data->uFactoryProxAvg[2] = 0;
			data->uFactoryProxAvg[3] = 0;
			send_instruction(data, ADD_SENSOR, PROXIMITY_RAW, chTempbuf, 4);
			data->bProximityRawEnabled = true;
		} else {
			send_instruction(data, REMOVE_SENSOR, PROXIMITY_RAW,
				chTempbuf, 4);
			data->bProximityRawEnabled = false;
		}
	}

	return size;
}

static ssize_t barcode_emul_enable_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct ssp_data *data = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%u\n", data->bBarcodeEnabled);
}

static ssize_t barcode_emul_enable_store(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t size)
{
	int iRet;
	int64_t dEnable;
	struct ssp_data *data = dev_get_drvdata(dev);

	iRet = kstrtoll(buf, 10, &dEnable);
	if (iRet < 0)
		return iRet;

	if (dEnable)
		set_proximity_barcode_enable(data, true);
	else
		set_proximity_barcode_enable(data, false);

	return size;
}

#ifdef CONFIG_SENSORS_SSP_PROX_SETTING
static ssize_t proximity_setting_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%d\n", 1);
}

static ssize_t proximity_setting_store(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t size)
{
	int iRet;
	u8 val[2];
	char *token;
	char *str;
	struct ssp_msg *msg;
	struct ssp_data *data = dev_get_drvdata(dev);

	pr_info("[SSP] %s - %s\n", __func__, buf);

	//parsing
	str = (char *)buf;
	token = strsep(&str, "\n");
	if (token == NULL) {
		pr_err("[SSP] %s : too few arguments (2 needed)", __func__);
			return -EINVAL;
	}

	iRet = kstrtou8(token, 10, &val[0]);
	if (iRet < 0) {
		pr_err("[SSP] %s : kstrtou8 error %d", __func__, iRet);
		return iRet;
	}

	token = strsep(&str, "\n");
	if (token == NULL) {
		pr_err("[SSP] %s : too few arguments (2 needed)", __func__);
			return -EINVAL;
	}

	iRet = kstrtou8(token, 16, &val[1]);
	if (iRet < 0) {
		pr_err("[SSP] %s : kstrtou8 error %d", __func__, iRet);
		return iRet;
	}

	pr_info("[SSP] %s - index = %d value = 0x%x\n", __func__, val[0], val[1]);

	msg = kzalloc(sizeof(*msg), GFP_KERNEL);
	msg->cmd = MSG2SSP_AP_SET_PROX_SETTING;
	msg->length = 2;
	msg->options = AP2HUB_WRITE;
	msg->buffer = kzalloc(2, GFP_KERNEL);

	msg->free_buffer = 1;
	memcpy(msg->buffer, val, 2);

	iRet = ssp_spi_async(data, msg);

	if (iRet != SUCCESS) {
		pr_err("[SSP]: %s - fail %d\n", __func__, iRet);
		iRet = ERROR;
	}

	return size;
}
#endif

static ssize_t proximity_alert_thresh_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct ssp_data *data = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%u\n", data->uProxAlertHiThresh);
}

static ssize_t prox_light_get_dhr_sensor_info_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct ssp_data *data = dev_get_drvdata(dev);
	int iRet = 0;
	struct ssp_msg *msg;
	u8 buffer[18] = {0, };
	u8 chipId = 0;
	u16 threshold_high = 0;
	u16 threshold_low = 0;
	u16 threshold_detect_high = 0;
	u16 threshold_detect_low = 0;
	u8 p_drive_current = 0;
	u8 persistent_time = 0;
	u8 p_pulse = 0;
	u8 p_gain = 0;
	u8 p_time = 0;
	u8 p_pulse_length = 0;
	u8 l_atime = 0;
	int offset = 0;

	msg = kzalloc(sizeof(*msg), GFP_KERNEL);
	msg->cmd = MSG2SSP_AP_GET_PROXIMITY_LIGHT_DHR_SENSOR_INFO;
	msg->length = 18;
	msg->options = AP2HUB_READ;
	msg->buffer = buffer;
	msg->free_buffer = 0;

	iRet = ssp_spi_sync(data, msg, 1000);
	if (iRet != SUCCESS) {
		pr_err("[SSP] %s fail %d\n", __func__, iRet);
		return FAIL;
	}

	chipId = buffer[0];
	threshold_high = ((((u16) buffer[2]) << 8) & 0xff00) | ((((u16) buffer[1])) & 0x00ff);
	threshold_low = ((((u16) buffer[4]) << 8) & 0xff00) | ((((u16) buffer[3])) & 0x00ff);
	threshold_detect_high = ((((u16) buffer[6]) << 8) & 0xff00) | ((((u16) buffer[5])) & 0x00ff);
	threshold_detect_low = ((((u16) buffer[8]) << 8) & 0xff00) | ((((u16) buffer[7])) & 0x00ff);
	p_drive_current = buffer[9];
	persistent_time = buffer[10];
	p_pulse = buffer[11];
	p_gain = buffer[12];
	p_time = buffer[13];
	p_pulse_length = buffer[14];
	l_atime = buffer[15];

	if (buffer[17] == 0xff)
		offset = (0xff - buffer[16]) * (-1);
	else
		offset = buffer[16];

	pr_info("[SSP] %s - %02x, %d, %d, %d, %d, %02x, %02x, %02x, %02x, %02x, %02x, %02x, %d", __func__,
		chipId, threshold_high, threshold_low, threshold_detect_high, threshold_detect_low,
		p_drive_current, persistent_time, p_pulse, p_gain, p_time, p_pulse_length, l_atime, offset);

	return snprintf(buf, PAGE_SIZE, "\"THD\":\"%d %d %d %d\","\
		"\"PDRIVE_CURRENT\":\"%02x\","\
		"\"PERSIST_TIME\":\"%02x\","\
		"\"PPULSE\":\"%02x\","\
		"\"PGAIN\":\"%02x\","\
		"\"PTIME\":\"%02x\","\
		"\"PPLUSE_LEN\":\"%02x\","\
		"\"ATIME\":\"%02x\","\
		"\"POFFSET\":\"%d\"\n",
		threshold_high, threshold_low, threshold_detect_high, threshold_detect_low,
		p_drive_current, persistent_time, p_pulse, p_gain, p_time, p_pulse_length, l_atime, offset);
}

int proximity_save_calibration(int *cal_data, int size)
{
	int ret = 0;
	mm_segment_t old_fs;
	struct file *cal_filp = NULL;

	if (size < 2 * sizeof(int)) {
		pr_err("[SSP]: %s - invalid size(%d)", __func__, size);
		return -EIO;
	}

	pr_info("[SSP] %s ", __func__);

	old_fs = get_fs();
	set_fs(KERNEL_DS);

	cal_filp = filp_open(PROX_CAL_FILE_PATH, O_CREAT | O_WRONLY | O_NOFOLLOW | O_NONBLOCK, 0660);
	if (IS_ERR(cal_filp)) {
		pr_err("[SSP]: %s - Can't open calibration file\n", __func__);
		set_fs(old_fs);
		ret = PTR_ERR(cal_filp);
		return -EIO;
	}

	cal_filp->f_pos = PROX_CAL_FILE_INDEX;

	ret = vfs_write(cal_filp, (char *)cal_data, size, &cal_filp->f_pos);
	if (ret < 0) {
		pr_err("[SSP]: %s - Can't write prox cal to file\n", __func__);
		ret = -EIO;
	}
	
	pr_info("[SSP] %s: flag %d, gain %d\n", __func__, cal_data[0], cal_data[1]);

	filp_close(cal_filp, current->files);
	set_fs(old_fs);

	return ret;
}

int set_prox_cal_to_ssp(struct ssp_data *data)
{
	int ret;
	int prox_cal[2] = {0, };
	struct ssp_msg *msg;

	if (!(data->uSensorState & (1 << PROXIMITY_SENSOR))) {
		pr_info("[SSP] %s - Skip this function!!!, proximity sensor is not connected(0x%llx)\n",
			__func__, data->uSensorState);
		return -EIO;
	}

	pr_info("[SSP] %s ", __func__);

	ret = load_prox_cal_from_nvm(prox_cal, sizeof(prox_cal));
	
	msg = kzalloc(sizeof(*msg), GFP_KERNEL);
	msg->cmd = MSG2SSP_AP_MCU_SET_PROX_CAL;
	msg->length = sizeof(prox_cal);
	msg->options = AP2HUB_WRITE;
	msg->buffer = kzalloc(sizeof(prox_cal), GFP_KERNEL);
	msg->free_buffer = 1;
	memcpy(msg->buffer, &prox_cal, sizeof(prox_cal));

	ret = ssp_spi_async(data, msg);

	if (ret != SUCCESS) {
		pr_err("[SSP] %s - fail %d\n", __func__, ret);
	}

	pr_info("[SSP] %s : flag: %d, gain %d", __func__, prox_cal[0], prox_cal[1]);

	return ret;
}

static ssize_t proximity_cal_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	int cal_data[2] = {0, };
	
	load_prox_cal_from_nvm(cal_data, sizeof(cal_data));

	pr_info("[SSP] %s ", __func__);

	return snprintf(buf, PAGE_SIZE, "%d,%d\n", cal_data[0], cal_data[1]);
}

static ssize_t proximity_cal_store(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t size)
{
	int iRet = 0;
	int64_t enable = 0;
	struct ssp_data *data = dev_get_drvdata(dev);
	int cal_data[2] = {0, 1};
	

	if (!(data->uSensorState & (1 << PROXIMITY_SENSOR))) {
		pr_info("[SSP] %s - Skip this function!!!, proximity sensor is not connected(0x%llx)\n",
			__func__, data->uSensorState);
		return -EINVAL;
	}

	iRet = kstrtoll(buf, 10, &enable);

	if (iRet < 0)
		return iRet;

	pr_info("[SSP] %s - enable %d", __func__, enable);
	
	if (enable) {
		// ToDo: start proximity calibration
		iRet = ssp_send_cmd(data, MSG2SSP_AP_PROX_CAL_START, 0);

	} else {
		iRet = proximity_save_calibration(cal_data, sizeof(cal_data));
		if (iRet < 0)
			pr_err("[SSP] %s : initilaize fail", __func__);
		if (set_prox_cal_to_ssp(data) < 0)
			pr_err("[SSP]: %s - sending proximity calibration data failed\n", __func__);

	}

	return size;
}

static ssize_t proximity_offset_pass_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "%u\n", 1);
}

static ssize_t proximity_position_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	return get_prox(dev)->get_prox_position(dev, buf);
}

static ssize_t proximity_trim_check_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct ssp_data *data = dev_get_drvdata(dev);
	int iRet = 0, iRetries = 0;
	struct ssp_msg *msg;
	u8 buffer[8] = {0,};
	int trim;

	msg = kzalloc(sizeof(*msg), GFP_KERNEL);
	msg->cmd = MSG2SSP_AP_GET_PROX_TRIM;
	msg->length = 1;
	msg->options = AP2HUB_READ;
	msg->buffer = buffer;
	msg->free_buffer = 0;
retries:
	iRet = ssp_spi_async(data, msg);
	if (iRet != SUCCESS) {
		pr_err("[SSP] %s fail %d\n", __func__, iRet);

		if (iRetries++ < 2) {
			pr_err("[SSP] %s fail, retry\n", __func__);
			mdelay(5);
			goto retries;
		}
		return snprintf(buf, PAGE_SIZE, "NG\n");
	}
	
	trim = (int)buffer[0];
	pr_info("[SSP] %s - %d\n", __func__, trim);

	switch(trim) {
		case 0:
			return snprintf(buf, PAGE_SIZE, "UNTRIM\n");
		case 1:
			return snprintf(buf, PAGE_SIZE, "TRIM\n");			
	}
	return snprintf(buf, PAGE_SIZE, "NG\n");
}

static DEVICE_ATTR(vendor, 0440, proximity_vendor_show, NULL);
static DEVICE_ATTR(name, 0440, proximity_name_show, NULL);
static DEVICE_ATTR(prox_probe, 0440, proximity_probe_show, NULL);
static DEVICE_ATTR(thresh_high, 0660, proximity_thresh_high_show, proximity_thresh_high_store);
static DEVICE_ATTR(thresh_low, 0660, proximity_thresh_low_show, proximity_thresh_low_store);
static DEVICE_ATTR(thresh_detect_high, 0660, proximity_thresh_detect_high_show, proximity_thresh_detect_high_store);
static DEVICE_ATTR(thresh_detect_low, 0660, proximity_thresh_detect_low_show, proximity_thresh_detect_low_store);
static DEVICE_ATTR(prox_trim, 0440, proximity_default_trim_show, NULL);
static DEVICE_ATTR(raw_data, 0440, proximity_raw_data_show, NULL);
static DEVICE_ATTR(prox_avg, 0660, proximity_avg_show, proximity_avg_store);
static DEVICE_ATTR(barcode_emul_en, 0660, barcode_emul_enable_show, barcode_emul_enable_store);
static DEVICE_ATTR(prox_alert_thresh, 0440, proximity_alert_thresh_show, NULL);
static DEVICE_ATTR(dhr_sensor_info, 0440, prox_light_get_dhr_sensor_info_show, NULL);
static DEVICE_ATTR(prox_cal, 0660, proximity_cal_show, proximity_cal_store);
static DEVICE_ATTR(prox_offset_pass, 0440, proximity_offset_pass_show, NULL);

static DEVICE_ATTR(prox_position, 0440, proximity_position_show, NULL);
static DEVICE_ATTR(trim_check, 0444, proximity_trim_check_show, NULL);

static struct device_attribute *prox_attrs[] = {
	&dev_attr_vendor,
	&dev_attr_name,
	&dev_attr_prox_probe,
	&dev_attr_thresh_high,
	&dev_attr_thresh_low,
	&dev_attr_thresh_detect_high,
	&dev_attr_thresh_detect_low,
	&dev_attr_prox_trim,
	&dev_attr_raw_data,
	&dev_attr_prox_avg,
	&dev_attr_barcode_emul_en,
	&dev_attr_prox_alert_thresh,
	&dev_attr_dhr_sensor_info,
	&dev_attr_prox_cal,
	&dev_attr_prox_offset_pass,
	&dev_attr_prox_position,
	&dev_attr_trim_check,
	NULL,
};

void initialize_prox_factorytest(struct ssp_data *data)
{
	memset(&prox_manager, 0, sizeof(prox_manager));
	push_back(&prox_manager, "DEFAULT", &prox_default);
#ifdef CONFIG_SENSORS_TMD4907
	push_back(&prox_manager, "TMD4907", get_prox_tmd4907());
#endif
#ifndef CONFIG_SENSORS_SSP_R8
	sensors_register(data->prox_device, data,
		prox_attrs, "proximity_sensor");
#endif
}

void remove_prox_factorytest(struct ssp_data *data)
{
	sensors_unregister(data->prox_device, prox_attrs);
}