summaryrefslogtreecommitdiff
path: root/drivers/input/misc/lps001wp_prs.c
blob: 9ec96ba3863c79cd201ba3ab4befd7696e9cfa83 (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
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276

/******************** (C) COPYRIGHT 2010 STMicroelectronics ********************
*
* File Name	: lps001wp_prs.c
* Authors	: MSH - Motion Mems BU - Application Team
*		: Matteo Dameno (matteo.dameno@st.com)
*		: Carmine Iascone (carmine.iascone@st.com)
*		: Both authors are willing to be considered the contact
*		: and update points for the driver.
* Version	: V 1.1.1
* Date		: 2010/11/22
* Description	: LPS001WP pressure temperature sensor driver
*
********************************************************************************
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* THE PRESENT SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, FOR THE SOLE
* PURPOSE TO SUPPORT YOUR APPLICATION DEVELOPMENT.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
******************************************************************************

 Revision 0.9.0 01/10/2010:
	first beta release
 Revision 1.1.0 05/11/2010:
	add sysfs management
 Revision 1.1.1 22/11/2010:
	moved to input/misc
******************************************************************************/

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/device.h>

#include <linux/input/lps001wp.h>



#define	DEBUG	1


#define	PR_ABS_MAX	 0xffff
#define	PR_ABS_MIN	 0x0000
#define	PR_DLT_MAX	 0x7ffff
#define	PR_DLT_MIN	-0x80000 /* 16-bit signed value */
#define	TEMP_MAX	 0x7fff
#define TEMP_MIN	-0x80000 /* 16-bit signed value */


#define	SENSITIVITY_T_SHIFT	6	/** =	64 LSB/degrC	*/
#define	SENSITIVITY_P_SHIFT	4	/** =	16 LSB/mbar	*/


#define	OUTDATA_REG	0x28
#define	INDATA_REG	0X30

#define	WHOAMI_LPS001WP_PRS	0xBA	/*	Expctd content for WAI	*/

/*	CONTROL REGISTERS	*/
#define	WHO_AM_I	0x0F		/*	WhoAmI register		*/
#define	CTRL_REG1	0x20		/*	power / ODR control reg	*/
#define	CTRL_REG2	0x21		/*	boot reg		*/
#define	CTRL_REG3	0x22		/*	interrupt control reg	*/

#define	STATUS_REG	0X27		/*	status reg		*/

#define PRESS_OUT_L	OUTDATA_REG


#define	REF_P_L		INDATA_REG	/*	pressure reference	*/
#define	REF_P_H		0x31		/*	pressure reference	*/
#define	THS_P_L		0x32		/*	pressure threshold	*/
#define	THS_P_H		0x33		/*	pressure threshold	*/

#define	INT_CFG		0x34		/*	interrupt config	*/
#define	INT_SRC		0x35		/*	interrupt source	*/
#define	INT_ACK		0x36		/*	interrupt acknoledge	*/
/*	end CONTROL REGISTRES	*/


/* Barometer and Termometer output data rate ODR */
#define	LPS001WP_PRS_ODR_MASK	0x30	/* Mask to access odr bits only	*/
#define	LPS001WP_PRS_ODR_7_1	0x00	/* 7Hz baro and 1Hz term ODR	*/
#define	LPS001WP_PRS_ODR_7_7	0x01	/* 7Hz baro and 7Hz term ODR	*/
#define	LPS001WP_PRS_ODR_12_12	0x11	/* 12.5Hz baro and 12.5Hz term ODR */


#define	LPS001WP_PRS_ENABLE_MASK	0x40	/*  */
#define	LPS001WP_PRS_DIFF_MASK		0x08
#define LPS001WP_PRS_LPOW_MASK		0x80

#define	LPS001WP_PRS_DIFF_ON		0x08
#define	LPS001WP_PRS_DIFF_OFF		0x00

#define	LPS001WP_PRS_LPOW_ON		0x80
#define	LPS001WP_PRS_LPOW_OFF		0x00

#define	FUZZ			0
#define	FLAT			0
#define	I2C_RETRY_DELAY		5
#define	I2C_RETRIES		5
#define	I2C_AUTO_INCREMENT	0x80

/* RESUME STATE INDICES */
#define	RES_CTRL_REG1		0
#define	RES_CTRL_REG2		1
#define	RES_CTRL_REG3		2
#define	RES_REF_P_L		3
#define	RES_REF_P_H		4
#define	RES_THS_P_L		5
#define	RES_THS_P_H		6
#define	RES_INT_CFG		7

#define	RESUME_ENTRIES		8
/* end RESUME STATE INDICES */

/* Pressure Sensor Operating Mode */
#define	LPS001WP_PRS_DIFF_ENABLE	1
#define LPS001WP_PRS_DIFF_DISABLE	0
#define	LPS001WP_PRS_LPOWER_EN		1
#define	LPS001WP_PRS_LPOWER_DIS		0

static const struct {
	unsigned int cutoff_ms;
	unsigned int mask;
} lps001wp_prs_odr_table[] = {
		{80,	LPS001WP_PRS_ODR_12_12 },
		{143,	LPS001WP_PRS_ODR_7_7 },
		{1000,	LPS001WP_PRS_ODR_7_1 },
};

struct lps001wp_prs_data {
	struct i2c_client *client;
	struct lps001wp_prs_platform_data *pdata;

	struct mutex lock;
	struct delayed_work input_work;

	struct input_dev *input_dev;

	int hw_initialized;
	/* hw_working=-1 means not tested yet */
	int hw_working;
	u8 diff_enabled;
	u8 lpowmode_enabled ;

	atomic_t enabled;
	int on_before_suspend;

	u8 resume_state[RESUME_ENTRIES];

#ifdef DEBUG
	u8 reg_addr;
#endif
};

struct outputdata {
	u16 abspress;
	s16 temperature;
	s16 deltapress;
};


static int lps001wp_prs_i2c_read(struct lps001wp_prs_data *prs,
				  u8 *buf, int len)
{
	int err;
	int tries = 0;

	struct i2c_msg msgs[] = {
		{
		 .addr = prs->client->addr,
		 .flags = prs->client->flags & I2C_M_TEN,
		 .len = 1,
		 .buf = buf,
		 },
		{
		 .addr = prs->client->addr,
		 .flags = (prs->client->flags & I2C_M_TEN) | I2C_M_RD,
		 .len = len,
		 .buf = buf,
		 },
	};

	do {
		err = i2c_transfer(prs->client->adapter, msgs, 2);
		if (err != 2)
			msleep_interruptible(I2C_RETRY_DELAY);
	} while ((err != 2) && (++tries < I2C_RETRIES));

	if (err != 2) {
		dev_err(&prs->client->dev, "read transfer error\n");
		err = -EIO;
	} else {
		err = 0;
	}

	return err;
}

static int lps001wp_prs_i2c_write(struct lps001wp_prs_data *prs,
				   u8 *buf, int len)
{
	int err;
	int tries = 0;
	struct i2c_msg msgs[] = {
		{
		 .addr = prs->client->addr,
		 .flags = prs->client->flags & I2C_M_TEN,
		 .len = len + 1,
		 .buf = buf,
		 },
	};

	do {
		err = i2c_transfer(prs->client->adapter, msgs, 1);
		if (err != 1)
			msleep_interruptible(I2C_RETRY_DELAY);
	} while ((err != 1) && (++tries < I2C_RETRIES));

	if (err != 1) {
		dev_err(&prs->client->dev, "write transfer error\n");
		err = -EIO;
	} else {
		err = 0;
	}

	return err;
}

static int lps001wp_prs_i2c_update(struct lps001wp_prs_data *prs,
			u8 reg_address, u8 mask, u8 new_bit_values)
{
	int err = -1;
	u8 rdbuf[1] = { reg_address };
	u8 wrbuf[2] = { reg_address , 0x00 };

	u8 init_val;
	u8 updated_val;
	err = lps001wp_prs_i2c_read(prs, rdbuf, 1);
	if (!(err < 0)) {
		init_val = rdbuf[0];
		updated_val = ((mask & new_bit_values) | ((~mask) & init_val));
		wrbuf[1] = updated_val;
		err = lps001wp_prs_i2c_write(prs, wrbuf, 2);
	}
	return err;
}
/* */

static int lps001wp_prs_register_write(struct lps001wp_prs_data *prs, u8 *buf,
		u8 reg_address, u8 new_value)
{
	int err = -1;

	/* Sets configuration register at reg_address
	 *  NOTE: this is a straight overwrite  */
	buf[0] = reg_address;
	buf[1] = new_value;
	err = lps001wp_prs_i2c_write(prs, buf, 1);
	if (err < 0)
		return err;
	return err;
}

static int lps001wp_prs_register_read(struct lps001wp_prs_data *prs, u8 *buf,
		u8 reg_address)
{

	int err = -1;
	buf[0] = (reg_address);
	err = lps001wp_prs_i2c_read(prs, buf, 1);

	return err;
}

static int lps001wp_prs_register_update(struct lps001wp_prs_data *prs, u8 *buf,
		u8 reg_address, u8 mask, u8 new_bit_values)
{
	int err = -1;
	u8 init_val;
	u8 updated_val;
	err = lps001wp_prs_register_read(prs, buf, reg_address);
	if (!(err < 0)) {
		init_val = buf[0];
		updated_val = ((mask & new_bit_values) | ((~mask) & init_val));
		err = lps001wp_prs_register_write(prs, buf, reg_address,
				updated_val);
	}
	return err;
}

/* */


static int lps001wp_prs_hw_init(struct lps001wp_prs_data *prs)
{
	int err = -1;
	u8 buf[6];

	printk(KERN_DEBUG "%s: hw init start\n", LPS001WP_PRS_DEV_NAME);

	buf[0] = WHO_AM_I;
	err = lps001wp_prs_i2c_read(prs, buf, 1);
	if (err < 0)
		goto error_firstread;
	else
		prs->hw_working = 1;
	if (buf[0] != WHOAMI_LPS001WP_PRS) {
		err = -1; /* TODO:choose the right coded error */
		goto error_unknown_device;
	}


	buf[0] = (I2C_AUTO_INCREMENT | INDATA_REG);
	buf[1] = prs->resume_state[RES_REF_P_L];
	buf[2] = prs->resume_state[RES_REF_P_H];
	buf[3] = prs->resume_state[RES_THS_P_L];
	buf[4] = prs->resume_state[RES_THS_P_H];
	err = lps001wp_prs_i2c_write(prs, buf, 4);
	if (err < 0)
		goto error1;

	buf[0] = (I2C_AUTO_INCREMENT | CTRL_REG1);
	buf[1] = prs->resume_state[RES_CTRL_REG1];
	buf[2] = prs->resume_state[RES_CTRL_REG2];
	buf[3] = prs->resume_state[RES_CTRL_REG3];
	err = lps001wp_prs_i2c_write(prs, buf, 3);
	if (err < 0)
		goto error1;

	buf[0] = INT_CFG;
	buf[1] = prs->resume_state[RES_INT_CFG];
	err = lps001wp_prs_i2c_write(prs, buf, 1);
	if (err < 0)
		goto error1;


	prs->hw_initialized = 1;
	printk(KERN_DEBUG "%s: hw init done\n", LPS001WP_PRS_DEV_NAME);
	return 0;

error_firstread:
	prs->hw_working = 0;
	dev_warn(&prs->client->dev, "Error reading WHO_AM_I: is device "
		"available/working?\n");
	goto error1;
error_unknown_device:
	dev_err(&prs->client->dev,
		"device unknown. Expected: 0x%x,"
		" Replies: 0x%x\n", WHOAMI_LPS001WP_PRS, buf[0]);
error1:
	prs->hw_initialized = 0;
	dev_err(&prs->client->dev, "hw init error 0x%x,0x%x: %d\n", buf[0],
			buf[1], err);
	return err;
}

static void lps001wp_prs_device_power_off(struct lps001wp_prs_data *prs)
{
	int err;
	u8 buf[2] = { CTRL_REG1, LPS001WP_PRS_PM_OFF };

	err = lps001wp_prs_i2c_write(prs, buf, 1);
	if (err < 0)
		dev_err(&prs->client->dev, "soft power off failed: %d\n", err);

	if (prs->pdata->power_off) {
		prs->pdata->power_off();
		prs->hw_initialized = 0;
	}
	if (prs->hw_initialized) {
		prs->hw_initialized = 0;
	}

}

static int lps001wp_prs_device_power_on(struct lps001wp_prs_data *prs)
{
	int err = -1;

	if (prs->pdata->power_on) {
		err = prs->pdata->power_on();
		if (err < 0) {
			dev_err(&prs->client->dev,
					"power_on failed: %d\n", err);
			return err;
		}
	}

	if (!prs->hw_initialized) {
		err = lps001wp_prs_hw_init(prs);
		if (prs->hw_working == 1 && err < 0) {
			lps001wp_prs_device_power_off(prs);
			return err;
		}
	}

	return 0;
}



int lps001wp_prs_update_odr(struct lps001wp_prs_data *prs, int poll_interval_ms)
{
	int err = -1;
	int i;

	u8 buf[2];
	u8 updated_val;
	u8 init_val;
	u8 new_val;
	u8 mask = LPS001WP_PRS_ODR_MASK;

	/* Following, looks for the longest possible odr interval scrolling the
	 * odr_table vector from the end (shortest interval) backward (longest
	 * interval), to support the poll_interval requested by the system.
	 * It must be the longest interval lower then the poll interval.*/
	for (i = ARRAY_SIZE(lps001wp_prs_odr_table) - 1; i >= 0; i--) {
		if (lps001wp_prs_odr_table[i].cutoff_ms <= poll_interval_ms)
			break;
	}

	new_val = lps001wp_prs_odr_table[i].mask;

	/* If device is currently enabled, we need to write new
	 *  configuration out to it */
	if (atomic_read(&prs->enabled)) {
		buf[0] = CTRL_REG1;
		err = lps001wp_prs_i2c_read(prs, buf, 1);
		if (err < 0)
			goto error;
		init_val = buf[0];
		prs->resume_state[RES_CTRL_REG1] = init_val;

		buf[0] = CTRL_REG1;
		updated_val = ((mask & new_val) | ((~mask) & init_val));
		buf[1] = updated_val;
		buf[0] = CTRL_REG1;
		err = lps001wp_prs_i2c_write(prs, buf, 1);
		if (err < 0)
			goto error;
		prs->resume_state[RES_CTRL_REG1] = updated_val;
	}
	return err;

error:
	dev_err(&prs->client->dev, "update odr failed 0x%x,0x%x: %d\n",
			buf[0], buf[1], err);

	return err;
}

static int lps001wp_prs_set_press_reference(struct lps001wp_prs_data *prs,
				u16 new_reference)
{
	int err = -1;
	u8 const reg_addressL = REF_P_L;
	u8 const reg_addressH = REF_P_H;
	u8 bit_valuesL, bit_valuesH;
	u8 buf[2];

	bit_valuesL = (u8) (new_reference & 0x00FF);
	bit_valuesH = (u8)((new_reference & 0xFF00) >> 8);

	err = lps001wp_prs_register_write(prs, buf, reg_addressL,
			bit_valuesL);
	if (err < 0)
		return err;
	err = lps001wp_prs_register_write(prs, buf, reg_addressH,
			bit_valuesH);
	if (err < 0) {
		lps001wp_prs_register_write(prs, buf, reg_addressL,
				prs->resume_state[RES_REF_P_L]);
		return err;
	}
	prs->resume_state[RES_REF_P_L] = bit_valuesL;
	prs->resume_state[RES_REF_P_H] = bit_valuesH;
	return err;
}

static int lps001wp_prs_get_press_reference(struct lps001wp_prs_data *prs,
		u16 *buf16)
{
	int err = -1;

	u8 bit_valuesL, bit_valuesH;
	u8 buf[2] = {0};
	u16 temp = 0;

	err = lps001wp_prs_register_read(prs, buf, REF_P_L);
	if (err < 0)
		return err;
	bit_valuesL = buf[0];
	err = lps001wp_prs_register_read(prs, buf, REF_P_H);
	if (err < 0)
		return err;
	bit_valuesH = buf[0];

	temp = (((u16) bit_valuesH) << 8);
	*buf16 = (temp | ((u16) bit_valuesL));

	return err;
}

static int lps001wp_prs_lpow_manage(struct lps001wp_prs_data *prs, u8 control)
{
	int err = -1;
	u8 buf[2] = {0x00, 0x00};
	u8 const mask = LPS001WP_PRS_LPOW_MASK;
	u8 bit_values = LPS001WP_PRS_LPOW_OFF;

	if (control >= LPS001WP_PRS_LPOWER_EN) {
		bit_values = LPS001WP_PRS_LPOW_ON;
	}

	err = lps001wp_prs_register_update(prs, buf, CTRL_REG1,
			mask, bit_values);

	if (err < 0)
		return err;
	prs->resume_state[RES_CTRL_REG1] = ((mask & bit_values) |
			(~mask & prs->resume_state[RES_CTRL_REG1]));
	if (bit_values == LPS001WP_PRS_LPOW_ON)
		prs->lpowmode_enabled = 1;
	else
		prs->lpowmode_enabled = 0;
	return err;
}

static int lps001wp_prs_diffen_manage(struct lps001wp_prs_data *prs, u8 control)
{
	int err = -1;
	u8 buf[2] = {0x00, 0x00};
	u8 const mask = LPS001WP_PRS_DIFF_MASK;
	u8 bit_values = LPS001WP_PRS_DIFF_OFF;

	if (control >= LPS001WP_PRS_DIFF_ENABLE) {
		bit_values = LPS001WP_PRS_DIFF_ON;
	}

	err = lps001wp_prs_register_update(prs, buf, CTRL_REG1,
			mask, bit_values);

	if (err < 0)
		return err;
	prs->resume_state[RES_CTRL_REG1] = ((mask & bit_values) |
			(~mask & prs->resume_state[RES_CTRL_REG1]));
	if (bit_values == LPS001WP_PRS_DIFF_ON)
		prs->diff_enabled = 1;
	else
		prs->diff_enabled = 0;
	return err;
}


static int lps001wp_prs_get_presstemp_data(struct lps001wp_prs_data *prs,
						struct outputdata *out)
{
	int err = -1;
	/* Data bytes from hardware	PRESS_OUT_L, PRESS_OUT_H,
	 *				TEMP_OUT_L, TEMP_OUT_H,
	 *				DELTA_L, DELTA_H */
	u8 prs_data[6];

	u16 abspr;
	s16 temperature, deltapr;
	int regToRead = 4;
	prs_data[4] = 0;
	prs_data[5] = 0;

	if (prs->diff_enabled)
		regToRead = 6;

	prs_data[0] = (I2C_AUTO_INCREMENT | OUTDATA_REG);
	err = lps001wp_prs_i2c_read(prs, prs_data, regToRead);
	if (err < 0)
		return err;

	abspr = ((((u16) prs_data[1] << 8) | ((u16) prs_data[0])));
	temperature = ((s16) (((u16) prs_data[3] << 8) | ((u16)prs_data[2])));

	out->abspress = abspr;
	out->temperature = temperature;

	deltapr = ((s16) (((u16) prs_data[5] << 8) | ((u16)prs_data[4])));
	out->deltapress = deltapr;

	return err;
}

static void lps001wp_prs_report_values(struct lps001wp_prs_data *prs,
					struct outputdata *out)
{
	input_report_abs(prs->input_dev, ABS_PR, out->abspress);
	input_report_abs(prs->input_dev, ABS_TEMP, out->temperature);
	input_report_abs(prs->input_dev, ABS_DLTPR, out->deltapress);
	input_sync(prs->input_dev);
}

static int lps001wp_prs_enable(struct lps001wp_prs_data *prs)
{
	int err;

	if (!atomic_cmpxchg(&prs->enabled, 0, 1)) {
		err = lps001wp_prs_device_power_on(prs);
		if (err < 0) {
			atomic_set(&prs->enabled, 0);
			return err;
		}
		schedule_delayed_work(&prs->input_work,
			msecs_to_jiffies(prs->pdata->poll_interval));
	}

	return 0;
}

static int lps001wp_prs_disable(struct lps001wp_prs_data *prs)
{
	if (atomic_cmpxchg(&prs->enabled, 1, 0)) {
		cancel_delayed_work_sync(&prs->input_work);
		lps001wp_prs_device_power_off(prs);
	}

	return 0;
}

static ssize_t read_single_reg(struct device *dev, char *buf, u8 reg)
{
	ssize_t ret;
	struct lps001wp_prs_data *prs = dev_get_drvdata(dev);
	int rc = 0;

	u8 data = reg;
	rc = lps001wp_prs_i2c_read(prs, &data, 1);
	/*TODO: error need to be managed */
	ret = sprintf(buf, "0x%02x\n", data);
	return ret;

}

static int write_reg(struct device *dev, const char *buf, u8 reg)
{
	int rc = 0;
	struct lps001wp_prs_data *prs = dev_get_drvdata(dev);
	u8 x[2];
	unsigned long val;

	if (strict_strtoul(buf, 16, &val))
		return -EINVAL;

	x[0] = reg;
	x[1] = val;
	rc = lps001wp_prs_i2c_write(prs, x, 1);
	/*TODO: error need to be managed */
	return rc;
}

static ssize_t attr_get_polling_rate(struct device *dev,
				     struct device_attribute *attr,
				     char *buf)
{
	int val;
	struct lps001wp_prs_data *prs = dev_get_drvdata(dev);
	mutex_lock(&prs->lock);
	val = prs->pdata->poll_interval;
	mutex_unlock(&prs->lock);
	return sprintf(buf, "%d\n", val);
}

static ssize_t attr_set_polling_rate(struct device *dev,
				     struct device_attribute *attr,
				     const char *buf, size_t size)
{
	struct lps001wp_prs_data *prs = dev_get_drvdata(dev);
	unsigned long interval_ms;

	if (strict_strtoul(buf, 10, &interval_ms))
		return -EINVAL;
	if (!interval_ms)
		return -EINVAL;
	mutex_lock(&prs->lock);
	prs->pdata->poll_interval = interval_ms;
	lps001wp_prs_update_odr(prs, interval_ms);
	mutex_unlock(&prs->lock);
	return size;
}

static ssize_t attr_get_diff_enable(struct device *dev,
				     struct device_attribute *attr,
				     char *buf)
{
	u8 val;
	struct lps001wp_prs_data *prs = dev_get_drvdata(dev);
	mutex_lock(&prs->lock);
	val = prs->diff_enabled;
	mutex_unlock(&prs->lock);
	return sprintf(buf, "%d\n", val);
}

static ssize_t attr_set_diff_enable(struct device *dev,
				     struct device_attribute *attr,
				     const char *buf, size_t size)
{
	struct lps001wp_prs_data *prs = dev_get_drvdata(dev);
	unsigned long val;

	if (strict_strtoul(buf, 10, &val))
		return -EINVAL;

	mutex_lock(&prs->lock);
	lps001wp_prs_diffen_manage(prs, (u8) val);
	mutex_unlock(&prs->lock);
	return size;
}

static ssize_t attr_get_enable(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct lps001wp_prs_data *prs = dev_get_drvdata(dev);
	int val = atomic_read(&prs->enabled);
	return sprintf(buf, "%d\n", val);
}

static ssize_t attr_set_enable(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t size)
{
	struct lps001wp_prs_data *prs = dev_get_drvdata(dev);
	unsigned long val;

	if (strict_strtoul(buf, 10, &val))
		return -EINVAL;

	if (val)
		lps001wp_prs_enable(prs);
	else
		lps001wp_prs_disable(prs);

	return size;
}

static ssize_t attr_get_press_ref(struct device *dev,
				  struct device_attribute *attr, char *buf)
{
	int err = -1;
	struct lps001wp_prs_data *prs = dev_get_drvdata(dev);
	u16 val = 0;

	mutex_lock(&prs->lock);
	err = lps001wp_prs_get_press_reference(prs, &val);
	mutex_unlock(&prs->lock);
	if (err < 0)
		return err;

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

static ssize_t attr_set_press_ref(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t size)
{
	int err = -1;
	struct lps001wp_prs_data *prs = dev_get_drvdata(dev);
	unsigned long val = 0;

	if (strict_strtoul(buf, 10, &val))
		return -EINVAL;

	if (val < PR_ABS_MIN || val > PR_ABS_MAX)
		return -EINVAL;

	mutex_lock(&prs->lock);
	err = lps001wp_prs_set_press_reference(prs, val);
	mutex_unlock(&prs->lock);
	if (err < 0)
		return err;
	return size;
}


static ssize_t attr_get_lowpowmode(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	u8 val;
	struct lps001wp_prs_data *prs = dev_get_drvdata(dev);
	mutex_lock(&prs->lock);
	val = prs->lpowmode_enabled;
	mutex_unlock(&prs->lock);
	return sprintf(buf, "%d\n", val);
}

static ssize_t attr_set_lowpowmode(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t size)
{
	int err = -1;
	struct lps001wp_prs_data *prs = dev_get_drvdata(dev);
	unsigned long val;

	if (strict_strtoul(buf, 10, &val))
		return -EINVAL;

	mutex_lock(&prs->lock);
	err = lps001wp_prs_lpow_manage(prs, (u8) val);
	mutex_unlock(&prs->lock);
	if (err < 0)
		return err;
	return size;
}


#ifdef DEBUG
static ssize_t attr_reg_set(struct device *dev, struct device_attribute *attr,
				const char *buf, size_t size)
{
	int rc;
	struct lps001wp_prs_data *prs = dev_get_drvdata(dev);
	u8 x[2];
	unsigned long val;

	if (strict_strtoul(buf, 16, &val))
		return -EINVAL;
	mutex_lock(&prs->lock);
	x[0] = prs->reg_addr;
	mutex_unlock(&prs->lock);
	x[1] = val;
	rc = lps001wp_prs_i2c_write(prs, x, 1);
	/*TODO: error need to be managed */
	return size;
}

static ssize_t attr_reg_get(struct device *dev, struct device_attribute *attr,
				char *buf)
{
	ssize_t ret;
	struct lps001wp_prs_data *prs = dev_get_drvdata(dev);
	int rc;
	u8 data;

	mutex_lock(&prs->lock);
	data = prs->reg_addr;
	mutex_unlock(&prs->lock);
	rc = lps001wp_prs_i2c_read(prs, &data, 1);
	/*TODO: error need to be managed */
	ret = sprintf(buf, "0x%02x\n", data);
	return ret;
}

static ssize_t attr_addr_set(struct device *dev, struct device_attribute *attr,
				const char *buf, size_t size)
{
	struct lps001wp_prs_data *prs = dev_get_drvdata(dev);
	unsigned long val;
	if (strict_strtoul(buf, 16, &val))
		return -EINVAL;
	mutex_lock(&prs->lock);
	prs->reg_addr = val;
	mutex_unlock(&prs->lock);
	return size;
}
#endif



static struct device_attribute attributes[] = {
	__ATTR(pollrate_ms, 0664, attr_get_polling_rate, attr_set_polling_rate),
	__ATTR(enable, 0664, attr_get_enable, attr_set_enable),
	__ATTR(diff_enable, 0664, attr_get_diff_enable, attr_set_diff_enable),
	__ATTR(press_reference, 0664, attr_get_press_ref, attr_set_press_ref),
	__ATTR(lowpow_enable, 0664, attr_get_lowpowmode, attr_set_lowpowmode),
#ifdef DEBUG
	__ATTR(reg_value, 0664, attr_reg_get, attr_reg_set),
	__ATTR(reg_addr, 0220, NULL, attr_addr_set),
#endif
};

static int create_sysfs_interfaces(struct device *dev)
{
	int i;
	for (i = 0; i < ARRAY_SIZE(attributes); i++)
		if (device_create_file(dev, attributes + i))
			goto error;
	return 0;

error:
	for ( ; i >= 0; i--)
		device_remove_file(dev, attributes + i);
	dev_err(dev, "%s:Unable to create interface\n", __func__);
	return -1;
}

static int remove_sysfs_interfaces(struct device *dev)
{
	int i;
	for (i = 0; i < ARRAY_SIZE(attributes); i++)
		device_remove_file(dev, attributes + i);
	return 0;
}


static void lps001wp_prs_input_work_func(struct work_struct *work)
{
	struct lps001wp_prs_data *prs;

	struct outputdata output;
	struct outputdata *out = &output;
	int err;

	prs = container_of((struct delayed_work *)work,
				struct lps001wp_prs_data, input_work);

	mutex_lock(&prs->lock);
	err = lps001wp_prs_get_presstemp_data(prs, out);
	if (err < 0)
		dev_err(&prs->client->dev, "get_pressure_data failed\n");
	else
		lps001wp_prs_report_values(prs, out);

	schedule_delayed_work(&prs->input_work,
				msecs_to_jiffies(prs->pdata->poll_interval));
	mutex_unlock(&prs->lock);
}

int lps001wp_prs_input_open(struct input_dev *input)
{
	struct lps001wp_prs_data *prs = input_get_drvdata(input);

	return lps001wp_prs_enable(prs);
}

void lps001wp_prs_input_close(struct input_dev *dev)
{
	struct lps001wp_prs_data *prs = input_get_drvdata(dev);

	lps001wp_prs_disable(prs);
}


static int lps001wp_prs_validate_pdata(struct lps001wp_prs_data *prs)
{
	prs->pdata->poll_interval = max(prs->pdata->poll_interval,
					prs->pdata->min_interval);

	/* Enforce minimum polling interval */
	if (prs->pdata->poll_interval < prs->pdata->min_interval) {
		dev_err(&prs->client->dev, "minimum poll interval violated\n");
		return -EINVAL;
	}

	return 0;
}

static int lps001wp_prs_input_init(struct lps001wp_prs_data *prs)
{
	int err;

	INIT_DELAYED_WORK(&prs->input_work, lps001wp_prs_input_work_func);
	prs->input_dev = input_allocate_device();
	if (!prs->input_dev) {
		err = -ENOMEM;
		dev_err(&prs->client->dev, "input device allocate failed\n");
		goto err0;
	}

	prs->input_dev->open = lps001wp_prs_input_open;
	prs->input_dev->close = lps001wp_prs_input_close;
	prs->input_dev->name = LPS001WP_PRS_DEV_NAME;
	prs->input_dev->id.bustype = BUS_I2C;
	prs->input_dev->dev.parent = &prs->client->dev;

	input_set_drvdata(prs->input_dev, prs);

	set_bit(EV_ABS, prs->input_dev->evbit);

	input_set_abs_params(prs->input_dev, ABS_PR,
			PR_ABS_MIN, PR_ABS_MAX, FUZZ, FLAT);
	input_set_abs_params(prs->input_dev, ABS_TEMP,
			PR_DLT_MIN, PR_DLT_MAX, FUZZ, FLAT);
	input_set_abs_params(prs->input_dev, ABS_DLTPR,
			TEMP_MIN, TEMP_MAX, FUZZ, FLAT);


	prs->input_dev->name = "LPS001WP barometer";

	err = input_register_device(prs->input_dev);
	if (err) {
		dev_err(&prs->client->dev,
			"unable to register input polled device %s\n",
			prs->input_dev->name);
		goto err1;
	}

	return 0;

err1:
	input_free_device(prs->input_dev);
err0:
	return err;
}

static void lps001wp_prs_input_cleanup(struct lps001wp_prs_data *prs)
{
	input_unregister_device(prs->input_dev);
	input_free_device(prs->input_dev);
}

static int lps001wp_prs_probe(struct i2c_client *client,
					const struct i2c_device_id *id)
{
	struct lps001wp_prs_data *prs;
	int err = -1;
	int tempvalue;

	pr_info("%s: probe start.\n", LPS001WP_PRS_DEV_NAME);

	if (client->dev.platform_data == NULL) {
		dev_err(&client->dev, "platform data is NULL. exiting.\n");
		err = -ENODEV;
		goto exit_check_functionality_failed;
	}

	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
		dev_err(&client->dev, "client not i2c capable\n");
		err = -ENODEV;
		goto exit_check_functionality_failed;
	}

	if (!i2c_check_functionality(client->adapter,
					I2C_FUNC_SMBUS_BYTE |
					I2C_FUNC_SMBUS_BYTE_DATA |
					I2C_FUNC_SMBUS_WORD_DATA)) {
		dev_err(&client->dev, "client not smb-i2c capable:2\n");
		err = -EIO;
		goto exit_check_functionality_failed;
	}


	if (!i2c_check_functionality(client->adapter,
						I2C_FUNC_SMBUS_I2C_BLOCK)){
		dev_err(&client->dev, "client not smb-i2c capable:3\n");
		err = -EIO;
		goto exit_check_functionality_failed;
	}


	prs = kzalloc(sizeof(struct lps001wp_prs_data), GFP_KERNEL);
	if (prs == NULL) {
		err = -ENOMEM;
		dev_err(&client->dev,
				"failed to allocate memory for module data: "
					"%d\n", err);
		goto exit_alloc_data_failed;
	}

	mutex_init(&prs->lock);
	mutex_lock(&prs->lock);

	prs->client = client;
	i2c_set_clientdata(client, prs);


	if (i2c_smbus_read_byte(client) < 0) {
		printk(KERN_ERR "i2c_smbus_read_byte error!!\n");
		goto err_mutexunlockfreedata;
	} else {
		printk(KERN_DEBUG "%s Device detected!\n",
							LPS001WP_PRS_DEV_NAME);
	}

	/* read chip id */
	tempvalue = i2c_smbus_read_word_data(client, WHO_AM_I);
	if ((tempvalue & 0x00FF) == WHOAMI_LPS001WP_PRS) {
		printk(KERN_DEBUG "%s I2C driver registered!\n",
							LPS001WP_PRS_DEV_NAME);
	} else {
		prs->client = NULL;
		printk(KERN_DEBUG "I2C driver not registered!"
				" Device unknown\n");
		goto err_mutexunlockfreedata;
	}

	prs->pdata = kmalloc(sizeof(*prs->pdata), GFP_KERNEL);
	if (prs->pdata == NULL) {
		err = -ENOMEM;
		dev_err(&client->dev,
				"failed to allocate memory for pdata: %d\n",
				err);
		goto err_mutexunlockfreedata;
	}

	memcpy(prs->pdata, client->dev.platform_data, sizeof(*prs->pdata));

	err = lps001wp_prs_validate_pdata(prs);
	if (err < 0) {
		dev_err(&client->dev, "failed to validate platform data\n");
		goto exit_kfree_pdata;
	}

	i2c_set_clientdata(client, prs);


	if (prs->pdata->init) {
		err = prs->pdata->init();
		if (err < 0) {
			dev_err(&client->dev, "init failed: %d\n", err);
			goto err2;
		}
	}

	memset(prs->resume_state, 0, ARRAY_SIZE(prs->resume_state));

	prs->resume_state[RES_CTRL_REG1] = LPS001WP_PRS_PM_NORMAL;
	prs->resume_state[RES_CTRL_REG2] = 0x00;
	prs->resume_state[RES_CTRL_REG3] = 0x00;
	prs->resume_state[RES_REF_P_L] = 0x00;
	prs->resume_state[RES_REF_P_H] = 0x00;
	prs->resume_state[RES_THS_P_L] = 0x00;
	prs->resume_state[RES_THS_P_H] = 0x00;
	prs->resume_state[RES_INT_CFG] = 0x00;

	err = lps001wp_prs_device_power_on(prs);
	if (err < 0) {
		dev_err(&client->dev, "power on failed: %d\n", err);
		goto err2;
	}

	prs->diff_enabled = 0;
	prs->lpowmode_enabled = 0;
	atomic_set(&prs->enabled, 1);

	err = lps001wp_prs_update_odr(prs, prs->pdata->poll_interval);
	if (err < 0) {
		dev_err(&client->dev, "update_odr failed\n");
		goto err_power_off;
	}

	err = lps001wp_prs_input_init(prs);
	if (err < 0) {
		dev_err(&client->dev, "input init failed\n");
		goto err_power_off;
	}


	err = create_sysfs_interfaces(&client->dev);
	if (err < 0) {
		dev_err(&client->dev,
			"device LPS001WP_PRS_DEV_NAME sysfs register failed\n");
		goto err_input_cleanup;
	}


	lps001wp_prs_device_power_off(prs);

	/* As default, do not report information */
	atomic_set(&prs->enabled, 0);


	mutex_unlock(&prs->lock);

	dev_info(&client->dev, "%s: probed\n", LPS001WP_PRS_DEV_NAME);

	return 0;

/*
remove_sysfs_int:
	remove_sysfs_interfaces(&client->dev);
*/
err_input_cleanup:
	lps001wp_prs_input_cleanup(prs);
err_power_off:
	lps001wp_prs_device_power_off(prs);
err2:
	if (prs->pdata->exit)
		prs->pdata->exit();
exit_kfree_pdata:
	kfree(prs->pdata);

err_mutexunlockfreedata:
	mutex_unlock(&prs->lock);
	kfree(prs);
exit_alloc_data_failed:
exit_check_functionality_failed:
	printk(KERN_ERR "%s: Driver Init failed\n", LPS001WP_PRS_DEV_NAME);
	return err;
}

static int __devexit lps001wp_prs_remove(struct i2c_client *client)
{
	struct lps001wp_prs_data *prs = i2c_get_clientdata(client);

	lps001wp_prs_input_cleanup(prs);
	lps001wp_prs_device_power_off(prs);
	remove_sysfs_interfaces(&client->dev);

	if (prs->pdata->exit)
		prs->pdata->exit();
	kfree(prs->pdata);
	kfree(prs);

	return 0;
}

static int lps001wp_prs_resume(struct i2c_client *client)
{
	struct lps001wp_prs_data *prs = i2c_get_clientdata(client);

	if (prs->on_before_suspend)
		return lps001wp_prs_enable(prs);
	return 0;
}

static int lps001wp_prs_suspend(struct i2c_client *client, pm_message_t mesg)
{
	struct lps001wp_prs_data *prs = i2c_get_clientdata(client);

	prs->on_before_suspend = atomic_read(&prs->enabled);
	return lps001wp_prs_disable(prs);
}

static const struct i2c_device_id lps001wp_prs_id[]
		= { { LPS001WP_PRS_DEV_NAME, 0}, { },};

MODULE_DEVICE_TABLE(i2c, lps001wp_prs_id);

static struct i2c_driver lps001wp_prs_driver = {
	.driver = {
			.name = LPS001WP_PRS_DEV_NAME,
			.owner = THIS_MODULE,
	},
	.probe = lps001wp_prs_probe,
	.remove = __devexit_p(lps001wp_prs_remove),
	.id_table = lps001wp_prs_id,
	.resume = lps001wp_prs_resume,
	.suspend = lps001wp_prs_suspend,
};

static int __init lps001wp_prs_init(void)
{
	printk(KERN_DEBUG "%s barometer driver: init\n",
						LPS001WP_PRS_DEV_NAME);
	return i2c_add_driver(&lps001wp_prs_driver);
}

static void __exit lps001wp_prs_exit(void)
{
	#if DEBUG
	printk(KERN_DEBUG "%s barometer driver exit\n",
						LPS001WP_PRS_DEV_NAME);
	#endif
	i2c_del_driver(&lps001wp_prs_driver);
	return;
}

module_init(lps001wp_prs_init);
module_exit(lps001wp_prs_exit);

MODULE_DESCRIPTION("STMicrolelectronics lps001wp pressure sensor sysfs driver");
MODULE_AUTHOR("Matteo Dameno, Carmine Iascone, STMicroelectronics");
MODULE_LICENSE("GPL");