summaryrefslogtreecommitdiff
path: root/patches.tizen/0267-iio-Add-driver-for-the-LPS331AP-barometer-sensor.patch
blob: bdac1c02613a8745966716e470a9b20c9881f3f0 (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
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
From 527ea6d2bb301d25b4fd54ce4b8d1e2c7fac9193 Mon Sep 17 00:00:00 2001
From: Jacek Anaszewski <j.anaszewski@samsung.com>
Date: Fri, 21 Jun 2013 10:52:31 +0200
Subject: [PATCH 0267/1302] iio: Add driver for the LPS331AP barometer sensor

Add new driver for the barometer device. The driver is
compliant with IIO framework, and exposes two channels
for reading the pressure and the temperature. The output
data can be read either in 'one shot' mode or by exploiting
IIO events.

Signed-off-by: Jacek Anaszewski <j.anaszewski@samsung.com>
Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
Signed-off-by: MyungJoo Ham <myungjoo.ham@samsung.com>
---
 .../devicetree/bindings/iio/barometer/lps331ap.txt |   40 +
 drivers/iio/Kconfig                                |    1 +
 drivers/iio/Makefile                               |    1 +
 drivers/iio/barometer/Kconfig                      |   12 +
 drivers/iio/barometer/Makefile                     |    7 +
 drivers/iio/barometer/lps331ap.c                   | 1252 ++++++++++++++++++++
 6 files changed, 1313 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/barometer/lps331ap.txt
 create mode 100644 drivers/iio/barometer/Kconfig
 create mode 100644 drivers/iio/barometer/Makefile
 create mode 100644 drivers/iio/barometer/lps331ap.c

diff --git a/Documentation/devicetree/bindings/iio/barometer/lps331ap.txt b/Documentation/devicetree/bindings/iio/barometer/lps331ap.txt
new file mode 100644
index 0000000..f0e44bc
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/barometer/lps331ap.txt
@@ -0,0 +1,40 @@
+* STMicroelectronics LPS331AP barometer sensor
+
+Required properties:
+
+  - compatible : should be "st,lps331ap"
+  - reg : the I2C address of the barometer
+
+Optional properties:
+
+  - interrupt-parent : phandle to the interrupt map subnode
+  - interrupts : interrupt mapping for LPS331AP interrupt sources:
+		 2 sources: 0 - INT1, 1 - INT2
+  - irq-map : irq sub-node defining interrupt map
+              (all properties listed below are required):
+      - #interrupt-cells : should be 1
+      - #address-cells : should be 0
+      - #size-cells : should be 0
+      - interrupt-map : table of entries consisting of three child elements:
+          - unit_interrupt_specifier - 0 : INT1, 1 : INT2
+          - interrupt parent phandle
+          - parent unit interrupt specifier consisiting of two elements:
+              - index of the interrupt within the controller
+              - flags : should be 0
+
+Example:
+
+lps331ap@5d {
+	compatible = "lps331ap";
+	reg = <0x5d>;
+	interrupt-parent = <&irq_map>;
+	interrupts = <0>, <1>;
+
+	irq_map: irq-map {
+		#interrupt-cells = <1>;
+		#address-cells = <0>;
+		#size-cells = <0>;
+		interrupt-map = <0 &gpf0 5 0>,
+				<1 &gpx0 3 0>;
+	};
+};
diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
index b2f963b..edf69ff 100644
--- a/drivers/iio/Kconfig
+++ b/drivers/iio/Kconfig
@@ -63,6 +63,7 @@ config IIO_CONSUMERS_PER_TRIGGER
 source "drivers/iio/accel/Kconfig"
 source "drivers/iio/adc/Kconfig"
 source "drivers/iio/amplifiers/Kconfig"
+source "drivers/iio/barometer/Kconfig"
 source "drivers/iio/common/Kconfig"
 source "drivers/iio/dac/Kconfig"
 source "drivers/iio/frequency/Kconfig"
diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
index a0e8cdd..cfcc812 100644
--- a/drivers/iio/Makefile
+++ b/drivers/iio/Makefile
@@ -14,6 +14,7 @@ obj-$(CONFIG_IIO_KFIFO_BUF) += kfifo_buf.o
 obj-y += accel/
 obj-y += adc/
 obj-y += amplifiers/
+obj-y += barometer/
 obj-y += common/
 obj-y += dac/
 obj-y += gyro/
diff --git a/drivers/iio/barometer/Kconfig b/drivers/iio/barometer/Kconfig
new file mode 100644
index 0000000..927c06c
--- /dev/null
+++ b/drivers/iio/barometer/Kconfig
@@ -0,0 +1,12 @@
+#
+# Magnetometer sensors
+#
+menu "Barometer sensors"
+
+config SENSORS_LPS331
+        tristate "STMicro LPS331 driver"
+        depends on I2C
+        help
+          Driver for STMicro LPS331
+
+endmenu
diff --git a/drivers/iio/barometer/Makefile b/drivers/iio/barometer/Makefile
new file mode 100644
index 0000000..8beba1c
--- /dev/null
+++ b/drivers/iio/barometer/Makefile
@@ -0,0 +1,7 @@
+#
+# Makefile for the sensors drivers.
+#
+
+# Each configuration option enables a list of files.
+
+obj-$(CONFIG_SENSORS_LPS331)            += lps331ap.o
diff --git a/drivers/iio/barometer/lps331ap.c b/drivers/iio/barometer/lps331ap.c
new file mode 100644
index 0000000..1e7cf07
--- /dev/null
+++ b/drivers/iio/barometer/lps331ap.c
@@ -0,0 +1,1252 @@
+/*
+* STMicroelectronics LPS331AP Pressure / Temperature Sensor module driver
+*
+* Copyright (C) 2013 Samsung Electronics Co., Ltd.
+* Author: Jacek Anaszewski <j.anaszewski@samsung.com>
+*
+* Copyright (C) 2010 STMicroelectronics- MSH - Motion Mems BU - Application Team
+* Authors:
+*	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.
+*
+* The device can be controlled by IIO sysfs interface from the location
+* /sys/bus/iio/devices/iio:deviceN, where N is the variable IIO device
+* number. The LPS331AP device can be identified by reading the 'name'
+* attribute.
+* Output data from the device can be read in two ways - on demand and by
+* initiating IIO events for the device. The former mode allows for power
+* saving as it instructs the device to do a 'one shot' measurement and return
+* to the power down mode, whereas the latter enables cyclic measurements
+* with the period managed through the 'odr' attribute, where possible settings
+* are: 40ms, 80ms, 143ms and 1000ms. The value has to be given in miliseconds.
+* Every integer value is accepted - the driver will round it to the nearest
+* bottom odr setting.
+*
+* Initialization of 'one shot' measurement:
+*	- read in_pressure_raw or in_temp_raw sysfs attribute
+* Managing cyclic measurements:
+*	1) enable:
+*		- write 1 to the events/in_pressure_mag_either_en and/or
+*		  events/in_temp_mag_either_en attribute
+*	2) read:
+*		- perform periodic read of events/in_pressure_mag_either_value
+*		  and/or events/in_temp_mag_either_value atrributes
+*	3) alter output data rate:
+*		- write value of 40, 80, 143 or 1000 to the 'odr' attribute
+*	1) disable:
+*		- write 0 to the events/in_pressure_mag_either_en and/or
+*		  events/in_temp_mag_either_en attribute
+*
+* Read pressures and temperatures output can be converted in units of
+* measurement by dividing them respectively by in_pressure_scale and
+* in_temp_scale. Temperature values must then be added by the constant float
+* in_temp_offset expressed as Celsius degrees.
+*
+* Obtained values are then expessed as
+* mbar (=0.1 kPa) and Celsius degrees.
+*
+* 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.
+*
+* 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., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA
+*/
+
+#include <linux/module.h>
+#include <linux/ratelimit.h>
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/gpio.h>
+#include <linux/of_gpio.h>
+#include <linux/of_irq.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/events.h>
+#include <linux/bitops.h>
+
+#define LPS331AP_VENDOR		"STM"
+#define LPS331AP_CHIP_ID		"LPS331"
+
+#define PR_ABS_MAX	8388607	/* 24 bit 2'compl */
+#define PR_ABS_MIN	-8388608
+
+#define WHOAMI_LPS331AP_PRS	0xBB	/* Expected content for WAI */
+
+/* Control registers */
+#define REF_P_XL	0x08	/*      pressure reference      */
+#define REF_P_L		0x09	/*      pressure reference      */
+#define REF_P_H		0x0a	/*      pressure reference      */
+#define REF_T_L		0x0b	/*      temperature reference   */
+#define REF_T_H		0x0c	/*      temperature reference   */
+
+#define WHO_AM_I	0x0f	/*      WhoAmI register         */
+#define TP_RESOL	0x10	/*      Pres Temp resolution set */
+#define DGAIN_L		0x18	/*      Dig Gain (3 regs)       */
+
+#define CTRL_REG1	0x20	/*      power / ODR control reg */
+#define CTRL_REG2	0x21	/*      boot reg                */
+#define CTRL_REG3	0x22	/*      interrupt control reg   */
+#define INT_CFG_REG	0x23	/*      2nterrupt config reg    */
+#define INT_SRC_REG	0x24	/*      interrupt source reg    */
+#define THS_P_L		0x25	/*      pressure threshold      */
+#define THS_P_H		0x26	/*      pressure threshold      */
+#define STATUS_REG	0X27	/*      status reg              */
+
+#define PRESS_OUT_XL	0x28	/*      press output (3 regs)   */
+#define TEMP_OUT_L	0x2b	/*      temper output (2 regs)  */
+#define DELTAREG_1	0x3c	/*      deltaPressure reg1       */
+#define DELTAREG_2	0x3d	/*      deltaPressure reg2       */
+#define DELTAREG_3	0x3e	/*      deltaPressure reg3       */
+
+/* Register aliases */
+#define P_REF_INDATA_REG	REF_P_XL
+#define T_REF_INDATA_REG	REF_T_L
+#define P_THS_INDATA_REG	THS_P_L
+#define P_OUTDATA_REG		PRESS_OUT_XL
+#define T_OUTDATA_REG		TEMP_OUT_L
+#define OUTDATA_REG		PRESS_OUT_XL
+
+/* Register masks */
+#define LPS331AP_PRS_ENABLE_MASK	0x80	/*  ctrl_reg1 */
+#define LPS331AP_PRS_ODR_MASK		0x70	/*  ctrl_reg1 */
+#define LPS331AP_PRS_DIFF_MASK		0x08	/*  ctrl_reg1 */
+#define LPS331AP_PRS_BDU_MASK		0x04	/*  ctrl_reg1 */
+#define LPS331AP_PRS_DELTA_EN_MASK	0x02	/*  ctrl_reg1 */
+#define LPS331AP_PRS_BOOT_MASK		0x80	/*  ctrl_reg2 */
+#define LPS331AP_PRS_SWRESET_MASK	0x04	/*  ctrl_reg2 */
+#define LPS331AP_PRS_AUTOZ_MASK		0x02	/*  ctrl_reg2 */
+#define LPS331AP_PRS_ONE_SHOT		0x01	/*  ctrl_reg2 */
+#define LPS331AP_PRS_INT1_MASK		0x07	/*  ctrl_reg3 */
+#define LPS331AP_PRS_INT2_MASK		0x38	/*  ctrl_reg3 */
+#define LPS331AP_PRS_PP_OD_MASK		0x40	/*  ctrl_reg3 */
+
+#define LPS331AP_PRS_PM_NORMAL		0x80	/* Power Normal Mode */
+#define LPS331AP_PRS_PM_OFF		0x00	/* Power Down */
+
+#define LPS331AP_PRS_DIFF_ON		0x08	/* En Difference circuitry */
+#define LPS331AP_PRS_DIFF_OFF		0x00	/* Dis Difference circuitry */
+
+#define LPS331AP_PRS_AUTOZ_ON		0x02	/* En AutoZero Function */
+#define LPS331AP_PRS_AUTOZ_OFF		0x00	/* Dis Difference Function */
+
+#define LPS331AP_PRS_BDU_ON		0x04	/* En BDU Block Data Upd */
+#define LPS331AP_PRS_DELTA_EN_ON	0x02	/* En Delta Press registers */
+#define LPS331AP_PRS_DIFF_EN		0x08	/* En diff pressure computing */
+#define LPS331AP_PRS_RES_AVGTEMP_064	0x60
+#define LPS331AP_PRS_RES_AVGTEMP_128	0x70
+#define LPS331AP_PRS_RES_AVGPRES_512	0x0a
+#define LPS331AP_PRS_RES_AVGPRES_384	0x09
+
+#define LPS331AP_PRS_RES_MAX		(LPS331AP_PRS_RES_AVGTEMP_128  | \
+						LPS331AP_PRS_RES_AVGPRES_512)
+						/* Max Resol. for 1/7/12,5Hz */
+
+#define LPS331AP_PRS_RES_25HZ		(LPS331AP_PRS_RES_AVGTEMP_128  | \
+						LPS331AP_PRS_RES_AVGPRES_384)
+						/* Max Resol. for 25Hz */
+#define LPS331AP_PRS_DRDY_INT1		0x04	/* En INT1 'data ready' */
+#define LPS331AP_PRS_DRDY_INT2		0x20	/* En INT2 'data ready' */
+#define LPS331AP_PRS_P_HIGH_INT1	0x01	/* En INT1 P_high */
+#define LPS331AP_PRS_P_HIGH_INT2	0x08	/* En INT2 P_high */
+#define LPS331AP_PRS_P_LOW_INT1		0x02	/* En INT1 P_low */
+#define LPS331AP_PRS_P_LOW_INT2		0x10	/* En INT2 P_low */
+#define LPS331AP_PRS_PH_E		0x01	/* En prs high evt interrupt */
+#define LPS331AP_PRS_PL_E		0x02	/* En prs low evt interrupt */
+
+#define LPS331AP_PRS_INT_FLAG_PH	0x01	/* Diff press high int flag */
+#define LPS331AP_PRS_INT_FLAG_PL	0x02	/* Diff press low int flag */
+#define LPS331AP_PRS_INT_FLAG_IA	0x04	/* Any int flag */
+
+#define LPS331AP_PRESS_SCALE		4096
+#define LPS331AP_TEMP_SCALE		480
+#define LPS331AP_TEMP_OFFSET_INT	42
+#define LPS331AP_TEMP_OFFSET_FRACT	500000
+
+#define I2C_AUTO_INCREMENT		0x80
+
+/* Register cache indices */
+#define LPS331AP_RES_REF_P_XL		0
+#define LPS331AP_RES_REF_P_L		1
+#define LPS331AP_RES_REF_P_H		2
+#define LPS331AP_RES_REF_T_L		3
+#define LPS331AP_RES_REF_T_H		4
+#define LPS331AP_RES_TP_RESOL		5
+#define LPS331AP_RES_CTRL_REG1		6
+#define LPS331AP_RES_CTRL_REG2		7
+#define LPS331AP_RES_CTRL_REG3		8
+#define LPS331AP_RES_INT_CFG_REG	9
+#define LPS331AP_RES_THS_P_L		10
+#define LPS331AP_RES_THS_P_H		11
+#define REG_ENTRIES			12
+
+/* Poll delays */
+#define LPS331AP_ODR_DELAY_DEFAULT	200
+#define LPS331AP_ODR_DELAY_MINIMUM	40
+#define LPS331AP_DATA_READY_TIMEOUT	(HZ * 2)
+#define LPS331AP_DATA_READY_POLL_TIME	10
+
+#define LPS331AP_PRS_DEV_NAME		"lps331ap"
+
+/* Barometer and Termometer output data rate (ODR) */
+#define LPS331AP_PRS_ODR_ONESH	0x00	/* one shot both                */
+#define LPS331AP_PRS_ODR_1_1	0x10	/*  1  Hz baro,  1  Hz term ODR */
+#define LPS331AP_PRS_ODR_7_7	0x50	/*  7  Hz baro,  7  Hz term ODR */
+#define LPS331AP_PRS_ODR_12_12	0x60	/* 12.5Hz baro, 12.5Hz term ODR */
+#define LPS331AP_PRS_ODR_25_25	0x70	/* 25  Hz baro, 25  Hz term ODR */
+
+enum {
+	LPS331AP_LPS331AP_VENDOR,
+	LPS331AP_NAME,
+	LPS331AP_ODR,
+	LPS331AP_PRESSURE_REF_LEVEL,
+};
+
+enum {
+	LPS331AP_INTERRUPT_SRC_NONE,
+	LPS331AP_INTERRUPT_SRC_INT1,
+	LPS331AP_INTERRUPT_SRC_INT2
+};
+
+enum prs_state {
+	FL_HW_ENABLED,
+	FL_HW_INITIALIZED,
+	FL_PRESS_EV_ENABLED,
+	FL_TEMP_EV_ENABLED
+};
+
+static const struct {
+	unsigned int cutoff_ms;
+	unsigned int mask;
+} lps331ap_odr_table[] = {
+	{ 40, LPS331AP_PRS_ODR_25_25 },
+	{ 80, LPS331AP_PRS_ODR_12_12 },
+	{ 143, LPS331AP_PRS_ODR_7_7 },
+	{ 1000, LPS331AP_PRS_ODR_1_1 }
+};
+
+struct outputdata {
+	unsigned int press;
+	int temperature;
+};
+
+struct lps331ap_data {
+	struct i2c_client *client;
+	struct mutex lock;
+	struct outputdata press_temp;
+
+	unsigned int output_data_rate;
+	unsigned long flags;
+	u8 reg_cache[REG_ENTRIES];
+
+	int lps_irq;
+	int lps_irq_src;
+};
+
+static int lps331ap_i2c_read(struct lps331ap_data *prs,
+						u8 *buf, int len)
+{
+	int err;
+	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,
+		},
+	};
+
+	err = i2c_transfer(prs->client->adapter, msgs, 2);
+	if (err != 2) {
+		dev_err(&prs->client->dev, "read transfer error = %d\n", err);
+		err = -EIO;
+	}
+	return err;
+}
+
+static int lps331ap_i2c_write(struct lps331ap_data *prs,
+						u8 *buf, int len)
+{
+	int err;
+	struct i2c_msg msgs[] = {
+		{
+			.addr = prs->client->addr,
+			.flags = prs->client->flags & I2C_M_TEN,
+			.len = len + 1,
+			.buf = buf,
+		},
+	};
+
+	err = i2c_transfer(prs->client->adapter, msgs, 1);
+	if (err != 1) {
+		dev_err(&prs->client->dev, "write transfer error\n");
+		err = -EIO;
+	}
+	return err;
+}
+
+static int lps331ap_hw_init(struct lps331ap_data *prs)
+{
+	int err;
+	u8 buf[6];
+
+	dev_dbg(&prs->client->dev, "hw init start\n");
+
+	buf[0] = WHO_AM_I;
+	err = lps331ap_i2c_read(prs, buf, 1);
+	if (err < 0) {
+		dev_err(&prs->client->dev,
+		"error reading WHO_AM_I: is device available/working?\n");
+		goto err_hw_init;
+	}
+
+	if (buf[0] != WHOAMI_LPS331AP_PRS) {
+		err = -ENODEV;
+		dev_err(&prs->client->dev,
+			"device unknown. Expected: 0x%02x, Replies: 0x%02x\n",
+			WHOAMI_LPS331AP_PRS, buf[0]);
+		goto err_hw_init;
+	}
+
+	buf[0] = (I2C_AUTO_INCREMENT | P_REF_INDATA_REG);
+	buf[1] = prs->reg_cache[LPS331AP_RES_REF_P_XL];
+	buf[2] = prs->reg_cache[LPS331AP_RES_REF_P_L];
+	buf[3] = prs->reg_cache[LPS331AP_RES_REF_P_H];
+	buf[4] = prs->reg_cache[LPS331AP_RES_REF_T_L];
+	buf[5] = prs->reg_cache[LPS331AP_RES_REF_T_H];
+	err = lps331ap_i2c_write(prs, buf, 5);
+	if (err < 0)
+		goto err_hw_init;
+
+	buf[0] = TP_RESOL;
+	buf[1] = prs->reg_cache[LPS331AP_RES_TP_RESOL];
+	err = lps331ap_i2c_write(prs, buf, 1);
+	if (err < 0)
+		goto err_hw_init;
+
+	buf[0] = (I2C_AUTO_INCREMENT | P_THS_INDATA_REG);
+	buf[1] = prs->reg_cache[LPS331AP_RES_THS_P_L];
+	buf[2] = prs->reg_cache[LPS331AP_RES_THS_P_H];
+	err = lps331ap_i2c_write(prs, buf, 2);
+	if (err < 0)
+		goto err_hw_init;
+
+	/* clear INT_ACK flag */
+	buf[0] = INT_SRC_REG;
+	err = lps331ap_i2c_read(prs, buf, 1);
+	if (err < 0)
+		return err;
+
+	/* CTRL_REG3 register has to be initialized before powering on */
+	buf[0] = CTRL_REG3;
+	buf[1] = prs->reg_cache[LPS331AP_RES_CTRL_REG3];
+	err = lps331ap_i2c_write(prs, buf, 1);
+	if (err < 0)
+		goto err_hw_init;
+
+	buf[0] = (I2C_AUTO_INCREMENT | CTRL_REG1);
+	buf[1] = prs->reg_cache[LPS331AP_RES_CTRL_REG1];
+	buf[2] = prs->reg_cache[LPS331AP_RES_CTRL_REG2];
+	err = lps331ap_i2c_write(prs, buf, 2);
+	if (err < 0)
+		goto err_hw_init;
+
+	set_bit(FL_HW_INITIALIZED, &prs->flags);
+	dev_dbg(&prs->client->dev, "hw init done\n");
+
+	return 0;
+
+err_hw_init:
+	clear_bit(FL_HW_INITIALIZED, &prs->flags);
+	dev_err(&prs->client->dev, "hw init error 0x%02x,0x%02x: %d\n",
+		buf[0], buf[1], err);
+	return err;
+}
+
+static int lps331ap_device_power_off(struct lps331ap_data *prs)
+{
+	int err;
+	u8 buf[2];
+
+	prs->reg_cache[LPS331AP_RES_CTRL_REG1]
+	    &= ~LPS331AP_PRS_PM_NORMAL;
+
+	buf[0] = CTRL_REG1;
+	buf[1] = prs->reg_cache[LPS331AP_RES_CTRL_REG1];
+
+	err = lps331ap_i2c_write(prs, buf, 1);
+	if (err < 0)
+		return err;
+
+	return 0;
+}
+
+static int lps331ap_device_power_on(struct lps331ap_data *prs)
+{
+	u8 buf[2];
+	int err;
+
+	prs->reg_cache[LPS331AP_RES_CTRL_REG1] |= LPS331AP_PRS_PM_NORMAL;
+
+	if (!test_bit(FL_HW_INITIALIZED, &prs->flags)) {
+		err = lps331ap_hw_init(prs);
+		lps331ap_device_power_off(prs);
+	} else {
+		buf[0] = CTRL_REG1;
+		buf[1] = prs->reg_cache[LPS331AP_RES_CTRL_REG1];
+		err = lps331ap_i2c_write(prs, buf, 1);
+	}
+
+	return err;
+}
+
+static int lps331ap_update_odr(struct lps331ap_data *prs, int delay_ms)
+{
+	int err = -1;
+	int i;
+
+	u8 buf[2];
+	u8 init_val, updated_val;
+	u8 curr_val, new_val;
+	u8 mask = LPS331AP_PRS_ODR_MASK;
+	u8 resol = LPS331AP_PRS_RES_MAX;
+
+	/*
+	 * Following, looks for the longest possible odr interval scrolling the
+	 * odr_table vector from the end (longest period) backward (shortest
+	 * period), to support the poll_interval requested by the system.
+	 * It must be the longest period shorter then the set poll period.
+	 */
+	for (i = ARRAY_SIZE(lps331ap_odr_table) - 1; i >= 0; i--) {
+		if ((lps331ap_odr_table[i].cutoff_ms <= delay_ms)
+		    || (i == 0))
+			break;
+	}
+
+	prs->output_data_rate = lps331ap_odr_table[i].cutoff_ms;
+
+	new_val = lps331ap_odr_table[i].mask;
+	if (new_val == LPS331AP_PRS_ODR_25_25)
+		resol = LPS331AP_PRS_RES_25HZ;
+
+	if (!test_bit(FL_HW_ENABLED, &prs->flags))
+		return 0;
+
+	buf[0] = CTRL_REG1;
+	err = lps331ap_i2c_read(prs, buf, 1);
+	if (err < 0)
+		goto error;
+	/* work on all but ENABLE bits */
+	init_val = buf[0];
+	prs->reg_cache[LPS331AP_RES_CTRL_REG1] = init_val;
+
+	/* disable */
+	curr_val = ((LPS331AP_PRS_ENABLE_MASK & LPS331AP_PRS_PM_OFF)
+		    | ((~LPS331AP_PRS_ENABLE_MASK) & init_val));
+	buf[0] = CTRL_REG1;
+	buf[1] = curr_val;
+	err = lps331ap_i2c_write(prs, buf, 1);
+	if (err < 0)
+		goto error;
+
+	buf[0] = CTRL_REG1;
+	updated_val = ((mask & new_val) | ((~mask) & curr_val));
+
+	buf[0] = CTRL_REG1;
+	buf[1] = updated_val;
+	err = lps331ap_i2c_write(prs, buf, 1);
+	if (err < 0)
+		goto error;
+
+	/* enable */
+	curr_val = ((LPS331AP_PRS_ENABLE_MASK & LPS331AP_PRS_PM_NORMAL)
+		    | ((~LPS331AP_PRS_ENABLE_MASK) & updated_val));
+	buf[0] = CTRL_REG1;
+	buf[1] = curr_val;
+	err = lps331ap_i2c_write(prs, buf, 1);
+	if (err < 0)
+		goto error;
+
+	buf[0] = TP_RESOL;
+	buf[1] = resol;
+	err = lps331ap_i2c_write(prs, buf, 1);
+	if (err < 0)
+		goto error;
+
+	prs->reg_cache[LPS331AP_RES_CTRL_REG1] = curr_val;
+	prs->reg_cache[LPS331AP_RES_TP_RESOL] = resol;
+
+	return err;
+
+error:
+	dev_err(&prs->client->dev, "update odr failed 0x%02x,0x%02x: %d\n",
+		buf[0], buf[1], err);
+
+	return err;
+}
+
+static int lps331ap_set_press_reference(struct lps331ap_data *prs,
+						 s32 new_reference)
+{
+	u8 buf[4], *ref;
+	__le32 new_ref;
+	int err;
+
+	new_ref = cpu_to_le32(new_reference);
+	ref = (u8 *)&new_ref;
+
+	buf[0] = (I2C_AUTO_INCREMENT | P_REF_INDATA_REG);
+	buf[1] = ref[0];
+	buf[2] = ref[1];
+	buf[3] = ref[2];
+
+	err = lps331ap_i2c_write(prs, buf, 3);
+	if (err < 0)
+		return err;
+
+	prs->reg_cache[LPS331AP_RES_REF_P_XL] = ref[0];
+	prs->reg_cache[LPS331AP_RES_REF_P_L] = ref[1];
+	prs->reg_cache[LPS331AP_RES_REF_P_H] = ref[2];
+
+	return 0;
+}
+
+static int lps331ap_get_press_reference(struct lps331ap_data *prs,
+							s32 *reference)
+{
+	u8 buf[4];
+	int err;
+
+	memset(buf, 0, sizeof(buf));
+	buf[0] = (I2C_AUTO_INCREMENT | P_REF_INDATA_REG);
+	err = lps331ap_i2c_read(prs, buf, 3);
+
+	if (err < 0)
+		return err;
+
+	*reference = le32_to_cpup((__le32 *) buf);
+
+	return 0;
+}
+
+static int lps331ap_enable(struct lps331ap_data *prs)
+{
+	int err;
+
+	if (!test_bit(FL_HW_ENABLED, &prs->flags)) {
+		err = lps331ap_device_power_on(prs);
+		if (err < 0) {
+			clear_bit(FL_HW_ENABLED, &prs->flags);
+			return err;
+		}
+		set_bit(FL_HW_ENABLED, &prs->flags);
+	}
+
+	return 0;
+}
+
+static int lps331ap_disable(struct lps331ap_data *prs)
+{
+	int err;
+
+	if (!test_bit(FL_HW_ENABLED, &prs->flags))
+		return 0;
+
+	err = lps331ap_device_power_off(prs);
+	clear_bit(FL_HW_ENABLED, &prs->flags);
+
+	return err;
+}
+
+static int lps331ap_get_presstemp_data(struct lps331ap_data *prs,
+						struct outputdata *out)
+{
+	/*
+	 * Data bytes from hardware:
+	 * PRESS_OUT_XL, PRESS_OUT_L, PRESS_OUT_H,
+	 * TEMP_OUT_L, TEMP_OUT_H.
+	 */
+	u8 prs_data[5] = { 0, };
+	int err = 0;
+
+	s32 pressure = 0;
+	s16 temperature = 0;
+
+	int reg_to_read = 5;
+
+	prs_data[0] = (I2C_AUTO_INCREMENT | OUTDATA_REG);
+	err = lps331ap_i2c_read(prs, prs_data, reg_to_read);
+	if (err < 0)
+		return err;
+
+	dev_dbg(&prs->client->dev,
+		"temp out tH = 0x%02x, tL = 0x%02x",
+		prs_data[4], prs_data[3]);
+	dev_dbg(&prs->client->dev,
+		"press_out: pH = 0x%02x, pL = 0x%02x, pXL= 0x%02x\n",
+		prs_data[2], prs_data[1], prs_data[0]);
+
+	pressure = (s32) ((((s8) prs_data[2]) << 16) |
+			  (prs_data[1] << 8) | (prs_data[0]));
+	temperature = (s16) ((((s8) prs_data[4]) << 8) | (prs_data[3]));
+
+	out->press = pressure;
+	out->temperature = temperature;
+
+	return 0;
+}
+
+static ssize_t lps331ap_get_press_ref(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	struct iio_dev *dev_info = dev_to_iio_dev(dev);
+	struct lps331ap_data *prs = iio_priv(dev_info);
+	s32 val;
+	int err;
+
+	mutex_lock(&prs->lock);
+	err = lps331ap_get_press_reference(prs, &val);
+	mutex_unlock(&prs->lock);
+	if (err < 0)
+		return err;
+
+	return sprintf(buf, "%d\n", val);
+}
+
+static ssize_t lps331ap_set_press_ref(struct device *dev,
+		       struct device_attribute *attr,
+		       const char *buf, size_t size)
+{
+	struct iio_dev *dev_info = dev_to_iio_dev(dev);
+	struct lps331ap_data *prs = iio_priv(dev_info);
+	long val;
+	int err;
+
+	if (kstrtol(buf, 10, &val))
+		return -EINVAL;
+
+	if (val < PR_ABS_MIN || val > PR_ABS_MAX)
+		return -EINVAL;
+
+	mutex_lock(&prs->lock);
+	err = lps331ap_set_press_reference(prs, val);
+	mutex_unlock(&prs->lock);
+
+	return err < 0 ? err : size;
+}
+static ssize_t lps331ap_get_odr(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	int val;
+	struct iio_dev *dev_info = dev_to_iio_dev(dev);
+	struct lps331ap_data *prs = iio_priv(dev_info);
+
+	mutex_lock(&prs->lock);
+	val = prs->output_data_rate;
+	mutex_unlock(&prs->lock);
+
+	return sprintf(buf, "%d\n", val);
+}
+
+static ssize_t lps331ap_set_odr(struct device *dev,
+		 struct device_attribute *attr, const char *buf, size_t size)
+{
+	struct iio_dev *dev_info = dev_to_iio_dev(dev);
+	struct lps331ap_data *prs = iio_priv(dev_info);
+	unsigned long delay_ms = 0;
+	unsigned int delay_min = LPS331AP_ODR_DELAY_MINIMUM;
+
+	if (kstrtoul(buf, 10, &delay_ms))
+		return -EINVAL;
+	if (!delay_ms)
+		return -EINVAL;
+
+	dev_dbg(&prs->client->dev, "delay_ms passed = %ld\n", delay_ms);
+	delay_ms = max_t(unsigned int, (unsigned int)delay_ms, delay_min);
+
+	mutex_lock(&prs->lock);
+	prs->output_data_rate = delay_ms;
+	lps331ap_update_odr(prs, delay_ms);
+
+	if (delay_ms == LPS331AP_ODR_DELAY_MINIMUM)
+		dev_dbg(&prs->client->dev, "delay limited to 40ms\n");
+
+	mutex_unlock(&prs->lock);
+
+	return size;
+}
+
+static ssize_t lps331_vendor_show(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%s\n", LPS331AP_VENDOR);
+}
+
+static ssize_t lps331_name_show(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%s\n", LPS331AP_CHIP_ID);
+}
+
+static int lps331ap_wait_one_shot_ready_polled(struct iio_dev *indio_dev)
+{
+	struct lps331ap_data *prs = iio_priv(indio_dev);
+	u32 timeout_ms = LPS331AP_DATA_READY_TIMEOUT;
+	u8 buf;
+	int err;
+
+	/* Wait for the conversion to complete. */
+	while (timeout_ms) {
+		msleep(LPS331AP_DATA_READY_POLL_TIME);
+		buf = CTRL_REG2;
+		err = lps331ap_i2c_read(prs, &buf, 1);
+		if (err < 0)
+			return err;
+		if (!(buf & LPS331AP_PRS_ONE_SHOT))
+			break;
+		timeout_ms -= LPS331AP_DATA_READY_POLL_TIME;
+	}
+	if (!timeout_ms) {
+		dev_err(&prs->client->dev, "Conversion timeout occurred.\n");
+		return -ETIME;
+	}
+
+	return err;
+}
+
+static u8 get_drdy_reg_mask(struct lps331ap_data *prs)
+{
+	if (prs->lps_irq_src == LPS331AP_INTERRUPT_SRC_INT1)
+		return LPS331AP_PRS_DRDY_INT1;
+	else
+		return LPS331AP_PRS_DRDY_INT2;
+}
+
+static int lps331ap_read_presstemp(struct iio_dev *indio_dev, int index,
+					int *val)
+{
+	struct lps331ap_data *prs = iio_priv(indio_dev);
+	u8 drdy_reg_mask;
+	int enabled;
+	char buf[2], curr_odr;
+	int err;
+
+	mutex_lock(&prs->lock);
+
+	drdy_reg_mask = get_drdy_reg_mask(prs);
+	/*
+	 * Set 'one shot' mode if either device is in power down mode
+	 * or it is powered up but 'data ready' interrupt isn't enabled.
+	 * If this condition evaluates to false then it means that the recent
+	 * output is being cached periodically by 'data ready' interrupt
+	 * handler, so we can return the cached value.
+	 */
+	enabled = test_bit(FL_HW_ENABLED, &prs->flags);
+	if (!enabled ||
+	    !(prs->reg_cache[LPS331AP_RES_CTRL_REG3] & drdy_reg_mask)) {
+		/* ensure device is in power down mode */
+		lps331ap_disable(prs);
+
+		/* set ODR configuration to 'one shot' */
+		curr_odr = prs->reg_cache[LPS331AP_RES_CTRL_REG1]
+		    & LPS331AP_PRS_ODR_MASK;
+		prs->reg_cache[LPS331AP_RES_CTRL_REG1]
+		    &= ~LPS331AP_PRS_ODR_MASK;
+		buf[0] = CTRL_REG1;
+		buf[1] = prs->reg_cache[LPS331AP_RES_CTRL_REG1];
+		err = lps331ap_i2c_write(prs, buf, 1);
+		if (err < 0)
+			goto exit;
+
+		/* power on the device */
+		err = lps331ap_enable(prs);
+		if (err < 0)
+			goto exit;
+
+		/* set ONE_SHOT mode */
+		buf[0] = CTRL_REG2;
+		buf[1] = prs->reg_cache[LPS331AP_RES_CTRL_REG2]
+		    | LPS331AP_PRS_ONE_SHOT;
+		err = lps331ap_i2c_write(prs, buf, 1);
+
+		if (err < 0)
+			goto exit;
+
+		/* wait for the end of coversion */
+		err = lps331ap_wait_one_shot_ready_polled(indio_dev);
+		if (err < 0)
+			goto exit;
+
+		/* read output data */
+		err = lps331ap_get_presstemp_data(prs, &prs->press_temp);
+		if (err < 0)
+			goto exit;
+
+		/* bring back previous ODR settings */
+		prs->reg_cache[LPS331AP_RES_CTRL_REG1]
+		    |= curr_odr;
+		buf[0] = CTRL_REG1;
+		buf[1] = prs->reg_cache[LPS331AP_RES_CTRL_REG1];
+		err = lps331ap_i2c_write(prs, buf, 1);
+
+		if (!enabled)
+			lps331ap_disable(prs);
+	}
+
+	*val = index == 0 ? prs->press_temp.press : prs->press_temp.temperature;
+
+	mutex_unlock(&prs->lock);
+
+	return IIO_VAL_INT;
+
+exit:
+	mutex_unlock(&prs->lock);
+	return -EINVAL;
+}
+
+static irqreturn_t lps331ap_irq_handler(int irq, void *data)
+{
+	struct iio_dev *indio_dev = data;
+	struct lps331ap_data *prs = iio_priv(indio_dev);
+	int err;
+
+	if (test_bit(FL_PRESS_EV_ENABLED, &prs->flags))
+		iio_push_event(indio_dev,
+			       IIO_UNMOD_EVENT_CODE(IIO_PRESSURE, 0,
+						    IIO_EV_TYPE_MAG,
+						    IIO_EV_DIR_EITHER),
+			       iio_get_time_ns());
+	if (test_bit(FL_TEMP_EV_ENABLED, &prs->flags))
+		iio_push_event(indio_dev,
+			       IIO_UNMOD_EVENT_CODE(IIO_TEMP, 1,
+						    IIO_EV_TYPE_MAG,
+						    IIO_EV_DIR_EITHER),
+			       iio_get_time_ns());
+
+	err = lps331ap_get_presstemp_data(prs, &prs->press_temp);
+	if (err < 0)
+		dev_err_ratelimited(&prs->client->dev,
+				    "get_presstemp_data failed\n");
+
+	return IRQ_HANDLED;
+}
+
+static int lps331ap_setup_irq(struct iio_dev *indio_dev)
+{
+	struct lps331ap_data *prs = iio_priv(indio_dev);
+	struct i2c_client *client = prs->client;
+	int err;
+
+	err = request_threaded_irq(prs->lps_irq, NULL,
+				   lps331ap_irq_handler,
+				   IRQF_TRIGGER_RISING | IRQF_ONESHOT,
+				   dev_name(&client->dev), indio_dev);
+	if (err < 0) {
+		dev_err(&client->dev,
+			"irq %d request failed: %d\n",
+			prs->lps_irq, err);
+		return err;
+	}
+
+	return 0;
+}
+
+static int lps331ap_read_raw(struct iio_dev *indio_dev,
+		  struct iio_chan_spec const *chan,
+		  int *val, int *val2, long mask)
+{
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		return lps331ap_read_presstemp(indio_dev, chan->address, val);
+	case IIO_CHAN_INFO_SCALE:
+		switch (chan->type) {
+		case IIO_PRESSURE:
+			*val = LPS331AP_PRESS_SCALE;
+			return IIO_VAL_INT;
+		case IIO_TEMP:
+			*val = LPS331AP_TEMP_SCALE;
+			return IIO_VAL_INT;
+		default:
+			return -EINVAL;
+		}
+	case IIO_CHAN_INFO_OFFSET:
+		/* Only the temperature channel has an offset. */
+		*val = LPS331AP_TEMP_OFFSET_INT;
+		*val2 = LPS331AP_TEMP_OFFSET_FRACT;
+		return IIO_VAL_INT_PLUS_MICRO;
+	}
+
+	return -EINVAL;
+}
+
+static int lps331ap_read_event_val(struct iio_dev *indio_dev, u64 event_code,
+					int *val)
+{
+	struct lps331ap_data *prs = iio_priv(indio_dev);
+	int chan_type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code);
+	int event_type = IIO_EVENT_CODE_EXTRACT_TYPE(event_code);
+	u8 drdy_reg_mask;
+
+	drdy_reg_mask = get_drdy_reg_mask(prs);
+
+	if (event_type == IIO_EV_TYPE_MAG) {
+		/*
+		 * If events are enabled the output data is being
+		 * cached in the interrupt handler and thus we
+		 * can return it here.
+		 */
+		if (prs->reg_cache[LPS331AP_RES_CTRL_REG3]
+		    & drdy_reg_mask)
+			switch (chan_type) {
+			case IIO_PRESSURE:
+				*val = prs->press_temp.press;
+				break;
+			case IIO_TEMP:
+				*val = prs->press_temp.temperature;
+				break;
+		} else
+			return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int lps331ap_write_event_config(struct iio_dev *indio_dev,
+			    u64 event_code, int state)
+{
+	struct lps331ap_data *prs = iio_priv(indio_dev);
+	int event_type = IIO_EVENT_CODE_EXTRACT_TYPE(event_code);
+	int chan_type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code);
+	u8 ctrl_reg3 = prs->reg_cache[LPS331AP_RES_CTRL_REG3];
+	u8 drdy_reg_mask;
+	char buf[2];
+	enum prs_state event_state = (chan_type == IIO_PRESSURE) ?
+	    FL_PRESS_EV_ENABLED : FL_TEMP_EV_ENABLED;
+	int err = 0;
+
+	if (prs->lps_irq_src == LPS331AP_INTERRUPT_SRC_NONE) {
+		dev_err(&prs->client->dev,
+			"current platform doesn't support LPS331 interrupts\n");
+		return -EINVAL;
+	}
+
+	mutex_lock(&prs->lock);
+
+	drdy_reg_mask = get_drdy_reg_mask(prs);
+
+	if (state) {
+		/* Don't do anything if the event is already enabled. */
+		if (test_bit(event_state, &prs->flags))
+			goto done;
+		switch (event_type) {
+		case IIO_EV_TYPE_MAG:
+			ctrl_reg3 |= drdy_reg_mask;
+			set_bit(event_state, &prs->flags);
+			break;
+		default:
+			goto err_unsupported_event_type;
+		}
+	} else {
+		switch (event_type) {
+		case IIO_EV_TYPE_MAG:
+			clear_bit(event_state, &prs->flags);
+			/*
+			 * Disable 'data ready' interrupt only if
+			 * there is no enabled event.
+			 */
+			if (!test_bit(FL_PRESS_EV_ENABLED, &prs->flags) &&
+			    !test_bit(FL_TEMP_EV_ENABLED, &prs->flags))
+				ctrl_reg3 &= ~drdy_reg_mask;
+			break;
+		default:
+			goto err_unsupported_event_type;
+		}
+	}
+
+	/* Setup new interrupt configuration. */
+	prs->reg_cache[LPS331AP_RES_CTRL_REG3] = ctrl_reg3;
+
+	buf[0] = CTRL_REG3;
+	buf[1] = prs->reg_cache[LPS331AP_RES_CTRL_REG3];
+	err = lps331ap_i2c_write(prs, buf, 1);
+	if (err < 0)
+		goto err_write_event_config;
+
+	/*
+	 * The device should be powered on only
+	 * if at least one event is enabled.
+	 */
+	if (ctrl_reg3 & drdy_reg_mask) {
+		err = lps331ap_enable(prs);
+		if (err < 0)
+			goto err_write_event_config;
+	} else {
+		err = lps331ap_disable(prs);
+		if (err < 0)
+			goto err_write_event_config;
+	}
+
+	/* clear INT_ACK flag */
+	buf[0] = INT_SRC_REG;
+	err = lps331ap_i2c_read(prs, buf, 1);
+	if (err < 0)
+		goto err_write_event_config;
+done:
+	mutex_unlock(&prs->lock);
+
+	return 0;
+
+err_unsupported_event_type:
+	err = -EINVAL;
+	dev_err(&prs->client->dev, "unsupported event type\n");
+err_write_event_config:
+	clear_bit(event_state, &prs->flags);
+	mutex_unlock(&prs->lock);
+	dev_err(&prs->client->dev, "writing event config failed\n");
+
+	return err;
+}
+
+static int lps331ap_read_event_config(struct iio_dev *indio_dev,
+					u64 event_code)
+{
+	struct lps331ap_data *prs = iio_priv(indio_dev);
+	int event_type = IIO_EVENT_CODE_EXTRACT_TYPE(event_code);
+	int chan_type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code);
+	enum prs_state event_state = (chan_type == IIO_PRESSURE) ?
+	    FL_PRESS_EV_ENABLED : FL_TEMP_EV_ENABLED;
+	int val;
+
+	if (prs->lps_irq_src == LPS331AP_INTERRUPT_SRC_NONE)
+		return -EINVAL;
+
+	switch (event_type) {
+	case IIO_EV_TYPE_MAG:
+		val = test_bit(event_state, &prs->flags);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return val;
+}
+
+static IIO_DEVICE_ATTR(odr, 0664,
+		       lps331ap_get_odr, lps331ap_set_odr, LPS331AP_ODR);
+static IIO_DEVICE_ATTR(pressure_reference_level, 0664,
+		       lps331ap_get_press_ref,
+		       lps331ap_set_press_ref, LPS331AP_PRESSURE_REF_LEVEL);
+static IIO_DEVICE_ATTR(vendor, 0644, lps331_vendor_show, NULL,
+		       LPS331AP_LPS331AP_VENDOR);
+static IIO_DEVICE_ATTR(name, 0644, lps331_name_show, NULL, LPS331AP_NAME);
+
+static struct attribute *lps331ap_attributes[] = {
+	&iio_dev_attr_vendor.dev_attr.attr,
+	&iio_dev_attr_name.dev_attr.attr,
+	&iio_dev_attr_odr.dev_attr.attr,
+	&iio_dev_attr_pressure_reference_level.dev_attr.attr,
+	NULL
+};
+
+static struct attribute_group lps331ap_attribute_group = {
+	.attrs = lps331ap_attributes,
+};
+
+#define LPS331AP_PRESSURE_CHANNEL(index)			\
+	{							\
+		.channel = index,				\
+		.address = index,				\
+		.type = IIO_PRESSURE,				\
+		.modified = 0,					\
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
+			     BIT(IIO_CHAN_INFO_SCALE),		\
+		.event_mask = IIO_EV_BIT(IIO_EV_TYPE_MAG,	\
+					IIO_EV_DIR_EITHER),	\
+	}
+
+#define LPS331AP_TEMP_CHANNEL(index)				\
+	{							\
+		.channel = index,				\
+		.address = index,				\
+		.type = IIO_TEMP,				\
+		.modified = 0,					\
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
+			     BIT(IIO_CHAN_INFO_SCALE) |		\
+			     BIT(IIO_CHAN_INFO_OFFSET),		\
+		.event_mask = IIO_EV_BIT(IIO_EV_TYPE_MAG,	\
+					IIO_EV_DIR_EITHER),	\
+	}
+
+static const struct iio_chan_spec lps331ap_channels[] = {
+	LPS331AP_PRESSURE_CHANNEL(0), LPS331AP_TEMP_CHANNEL(1),
+};
+
+static const struct iio_info lps331ap_info = {
+	.attrs = &lps331ap_attribute_group,
+	.read_raw = &lps331ap_read_raw,
+	.read_event_value = &lps331ap_read_event_val,
+	.read_event_config = &lps331ap_read_event_config,
+	.write_event_config = &lps331ap_write_event_config,
+	.driver_module = THIS_MODULE,
+};
+
+static int lps331ap_probe(struct i2c_client *client,
+					const struct i2c_device_id *id)
+{
+	struct lps331ap_data *prs;
+	struct iio_dev *indio_dev;
+	int int1_src, int2_src;
+	int err = -EINVAL;
+
+	indio_dev = iio_device_alloc(sizeof(struct lps331ap_data));
+	if (indio_dev == NULL)
+		return -ENOMEM;
+
+	prs = iio_priv(indio_dev);
+
+	if (client->dev.of_node) {
+		int1_src = irq_of_parse_and_map(client->dev.of_node, 0);
+		int2_src = irq_of_parse_and_map(client->dev.of_node, 1);
+
+		if (int1_src) {
+			prs->lps_irq = int1_src;
+			prs->lps_irq_src = LPS331AP_INTERRUPT_SRC_INT1;
+		} else if (int2_src) {
+			prs->lps_irq = int2_src;
+			prs->lps_irq_src = LPS331AP_INTERRUPT_SRC_INT2;
+		} else {
+			prs->lps_irq_src = LPS331AP_INTERRUPT_SRC_NONE;
+		}
+	} else {
+		prs->lps_irq = -EINVAL;
+	}
+
+	prs->output_data_rate = LPS331AP_ODR_DELAY_DEFAULT;
+	prs->client = client;
+	mutex_init(&prs->lock);
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->channels = lps331ap_channels;
+	indio_dev->num_channels = ARRAY_SIZE(lps331ap_channels);
+	indio_dev->info = &lps331ap_info;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+
+	if (prs->lps_irq) {
+		err = lps331ap_setup_irq(indio_dev);
+		if (err < 0) {
+			dev_err(&client->dev,
+				"Error setting data ready interrupt\n");
+			goto err_free_data;
+		}
+	}
+
+	i2c_set_clientdata(client, indio_dev);
+
+	memset(prs->reg_cache, 0, sizeof(prs->reg_cache));
+
+	/* Do not update output registers until MSB and LSB reading. */
+	prs->reg_cache[LPS331AP_RES_CTRL_REG1] = LPS331AP_PRS_BDU_ON;
+
+	/* Perform hw init. */
+	err = lps331ap_device_power_on(prs);
+	if (err < 0) {
+		dev_err(&client->dev, "power on failed: %d\n", err);
+		goto err_free_data;
+	}
+
+	set_bit(FL_HW_ENABLED, &prs->flags);
+
+	err = lps331ap_update_odr(prs, prs->output_data_rate);
+	if (err < 0) {
+		dev_err(&client->dev, "update_odr failed\n");
+		goto err_power_off;
+	}
+
+	lps331ap_device_power_off(prs);
+
+	clear_bit(FL_HW_ENABLED, &prs->flags);
+
+	err = iio_device_register(indio_dev);
+	if (err < 0)
+		goto err_free_data;
+
+	return 0;
+
+err_power_off:
+	lps331ap_device_power_off(prs);
+err_free_data:
+	if (prs->lps_irq)
+		free_irq(prs->lps_irq, indio_dev);
+	iio_device_free(indio_dev);
+
+	return err;
+}
+
+static int lps331ap_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct lps331ap_data *prs = iio_priv(indio_dev);
+
+	if (prs->lps_irq)
+		free_irq(prs->lps_irq, indio_dev);
+
+	lps331ap_device_power_off(prs);
+
+	iio_device_unregister(indio_dev);
+	iio_device_free(indio_dev);
+
+	return 0;
+}
+
+static const struct i2c_device_id lps331ap_id[] = {
+	{ LPS331AP_PRS_DEV_NAME, 0 },
+	{ },
+};
+
+MODULE_DEVICE_TABLE(i2c, lps331ap_id);
+
+#ifdef CONFIG_OF
+static const struct of_device_id lps331ap_of_match[] = {
+	{ .compatible = "st,lps331ap" },
+	{ .compatible = "lps331ap" },
+	{ }
+};
+#endif
+
+static struct i2c_driver lps331ap_driver = {
+	.driver = {
+		.name = LPS331AP_PRS_DEV_NAME,
+		.of_match_table = of_match_ptr(lps331ap_of_match),
+	},
+	.probe = lps331ap_probe,
+	.remove = lps331ap_remove,
+	.id_table = lps331ap_id,
+};
+
+module_i2c_driver(lps331ap_driver);
+
+MODULE_DESCRIPTION("STMicrolectronics LPS331AP pressure sensor IIO driver");
+MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
+MODULE_AUTHOR("Matteo Dameno, STMicroelectronic <matteo.dameno@st.com>");
+MODULE_AUTHOR("Carmine Iascone, STMicroelectronic <carmine.iascone@st.com>");
+MODULE_LICENSE("GPL");
-- 
1.8.3.2