Commit | Line | Data |
---|---|---|
217494e5 DC |
1 | /* |
2 | * STMicroelectronics pressures driver | |
3 | * | |
4 | * Copyright 2013 STMicroelectronics Inc. | |
5 | * | |
6 | * Denis Ciocca <denis.ciocca@st.com> | |
7 | * | |
8 | * Licensed under the GPL-2. | |
9 | */ | |
10 | ||
11 | #include <linux/kernel.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/errno.h> | |
15 | #include <linux/types.h> | |
16 | #include <linux/mutex.h> | |
17 | #include <linux/interrupt.h> | |
18 | #include <linux/i2c.h> | |
19 | #include <linux/gpio.h> | |
20 | #include <linux/irq.h> | |
21 | #include <linux/delay.h> | |
22 | #include <linux/iio/iio.h> | |
23 | #include <linux/iio/sysfs.h> | |
24 | #include <linux/iio/trigger.h> | |
25 | #include <linux/iio/buffer.h> | |
26 | #include <asm/unaligned.h> | |
27 | ||
28 | #include <linux/iio/common/st_sensors.h> | |
29 | #include "st_pressure.h" | |
30 | ||
d43a4115 GB |
31 | #define MCELSIUS_PER_CELSIUS 1000 |
32 | ||
33 | /* Default pressure sensitivity */ | |
67dbf54a JA |
34 | #define ST_PRESS_LSB_PER_MBAR 4096UL |
35 | #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \ | |
36 | ST_PRESS_LSB_PER_MBAR) | |
d43a4115 GB |
37 | |
38 | /* Default temperature sensitivity */ | |
1003eb67 | 39 | #define ST_PRESS_LSB_PER_CELSIUS 480UL |
d43a4115 GB |
40 | #define ST_PRESS_MILLI_CELSIUS_OFFSET 42500UL |
41 | ||
217494e5 DC |
42 | #define ST_PRESS_NUMBER_DATA_CHANNELS 1 |
43 | ||
217494e5 | 44 | /* FULLSCALE */ |
d43a4115 | 45 | #define ST_PRESS_FS_AVL_1100MB 1100 |
217494e5 DC |
46 | #define ST_PRESS_FS_AVL_1260MB 1260 |
47 | ||
93187840 DC |
48 | #define ST_PRESS_1_OUT_XL_ADDR 0x28 |
49 | #define ST_TEMP_1_OUT_L_ADDR 0x2b | |
50 | ||
302fbd50 LJ |
51 | /* CUSTOM VALUES FOR LPS331AP SENSOR */ |
52 | #define ST_PRESS_LPS331AP_WAI_EXP 0xbb | |
53 | #define ST_PRESS_LPS331AP_ODR_ADDR 0x20 | |
54 | #define ST_PRESS_LPS331AP_ODR_MASK 0x70 | |
55 | #define ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL 0x01 | |
56 | #define ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL 0x05 | |
57 | #define ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL 0x06 | |
58 | #define ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL 0x07 | |
59 | #define ST_PRESS_LPS331AP_PW_ADDR 0x20 | |
60 | #define ST_PRESS_LPS331AP_PW_MASK 0x80 | |
61 | #define ST_PRESS_LPS331AP_FS_ADDR 0x23 | |
62 | #define ST_PRESS_LPS331AP_FS_MASK 0x30 | |
302fbd50 LJ |
63 | #define ST_PRESS_LPS331AP_BDU_ADDR 0x20 |
64 | #define ST_PRESS_LPS331AP_BDU_MASK 0x04 | |
65 | #define ST_PRESS_LPS331AP_DRDY_IRQ_ADDR 0x22 | |
66 | #define ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK 0x04 | |
67 | #define ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK 0x20 | |
a9fd053b LW |
68 | #define ST_PRESS_LPS331AP_IHL_IRQ_ADDR 0x22 |
69 | #define ST_PRESS_LPS331AP_IHL_IRQ_MASK 0x80 | |
0e6f6871 LW |
70 | #define ST_PRESS_LPS331AP_OD_IRQ_ADDR 0x22 |
71 | #define ST_PRESS_LPS331AP_OD_IRQ_MASK 0x40 | |
302fbd50 | 72 | #define ST_PRESS_LPS331AP_MULTIREAD_BIT true |
217494e5 | 73 | |
7885a8ce | 74 | /* CUSTOM VALUES FOR LPS001WP SENSOR */ |
d43a4115 GB |
75 | |
76 | /* LPS001WP pressure resolution */ | |
77 | #define ST_PRESS_LPS001WP_LSB_PER_MBAR 16UL | |
78 | /* LPS001WP temperature resolution */ | |
79 | #define ST_PRESS_LPS001WP_LSB_PER_CELSIUS 64UL | |
80 | ||
7885a8ce LJ |
81 | #define ST_PRESS_LPS001WP_WAI_EXP 0xba |
82 | #define ST_PRESS_LPS001WP_ODR_ADDR 0x20 | |
83 | #define ST_PRESS_LPS001WP_ODR_MASK 0x30 | |
84 | #define ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL 0x01 | |
85 | #define ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL 0x02 | |
86 | #define ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL 0x03 | |
87 | #define ST_PRESS_LPS001WP_PW_ADDR 0x20 | |
88 | #define ST_PRESS_LPS001WP_PW_MASK 0x40 | |
d43a4115 GB |
89 | #define ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN \ |
90 | (100000000UL / ST_PRESS_LPS001WP_LSB_PER_MBAR) | |
7885a8ce LJ |
91 | #define ST_PRESS_LPS001WP_BDU_ADDR 0x20 |
92 | #define ST_PRESS_LPS001WP_BDU_MASK 0x04 | |
93 | #define ST_PRESS_LPS001WP_MULTIREAD_BIT true | |
94 | #define ST_PRESS_LPS001WP_OUT_L_ADDR 0x28 | |
95 | #define ST_TEMP_LPS001WP_OUT_L_ADDR 0x2a | |
96 | ||
93187840 DC |
97 | /* CUSTOM VALUES FOR LPS25H SENSOR */ |
98 | #define ST_PRESS_LPS25H_WAI_EXP 0xbd | |
99 | #define ST_PRESS_LPS25H_ODR_ADDR 0x20 | |
100 | #define ST_PRESS_LPS25H_ODR_MASK 0x70 | |
101 | #define ST_PRESS_LPS25H_ODR_AVL_1HZ_VAL 0x01 | |
102 | #define ST_PRESS_LPS25H_ODR_AVL_7HZ_VAL 0x02 | |
103 | #define ST_PRESS_LPS25H_ODR_AVL_13HZ_VAL 0x03 | |
104 | #define ST_PRESS_LPS25H_ODR_AVL_25HZ_VAL 0x04 | |
105 | #define ST_PRESS_LPS25H_PW_ADDR 0x20 | |
106 | #define ST_PRESS_LPS25H_PW_MASK 0x80 | |
93187840 DC |
107 | #define ST_PRESS_LPS25H_BDU_ADDR 0x20 |
108 | #define ST_PRESS_LPS25H_BDU_MASK 0x04 | |
109 | #define ST_PRESS_LPS25H_DRDY_IRQ_ADDR 0x23 | |
110 | #define ST_PRESS_LPS25H_DRDY_IRQ_INT1_MASK 0x01 | |
111 | #define ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK 0x10 | |
a9fd053b LW |
112 | #define ST_PRESS_LPS25H_IHL_IRQ_ADDR 0x22 |
113 | #define ST_PRESS_LPS25H_IHL_IRQ_MASK 0x80 | |
0e6f6871 LW |
114 | #define ST_PRESS_LPS25H_OD_IRQ_ADDR 0x22 |
115 | #define ST_PRESS_LPS25H_OD_IRQ_MASK 0x40 | |
93187840 | 116 | #define ST_PRESS_LPS25H_MULTIREAD_BIT true |
93187840 DC |
117 | #define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28 |
118 | #define ST_TEMP_LPS25H_OUT_L_ADDR 0x2b | |
119 | ||
120 | static const struct iio_chan_spec st_press_1_channels[] = { | |
2f5effcb LJ |
121 | { |
122 | .type = IIO_PRESSURE, | |
123 | .channel2 = IIO_NO_MOD, | |
93187840 | 124 | .address = ST_PRESS_1_OUT_XL_ADDR, |
2f5effcb LJ |
125 | .scan_index = ST_SENSORS_SCAN_X, |
126 | .scan_type = { | |
127 | .sign = 'u', | |
128 | .realbits = 24, | |
129 | .storagebits = 24, | |
130 | .endianness = IIO_LE, | |
131 | }, | |
132 | .info_mask_separate = | |
217494e5 | 133 | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), |
2f5effcb LJ |
134 | .modified = 0, |
135 | }, | |
136 | { | |
137 | .type = IIO_TEMP, | |
138 | .channel2 = IIO_NO_MOD, | |
93187840 | 139 | .address = ST_TEMP_1_OUT_L_ADDR, |
2f5effcb LJ |
140 | .scan_index = -1, |
141 | .scan_type = { | |
142 | .sign = 'u', | |
143 | .realbits = 16, | |
144 | .storagebits = 16, | |
145 | .endianness = IIO_LE, | |
146 | }, | |
147 | .info_mask_separate = | |
148 | BIT(IIO_CHAN_INFO_RAW) | | |
149 | BIT(IIO_CHAN_INFO_SCALE) | | |
150 | BIT(IIO_CHAN_INFO_OFFSET), | |
151 | .modified = 0, | |
152 | }, | |
217494e5 DC |
153 | IIO_CHAN_SOFT_TIMESTAMP(1) |
154 | }; | |
155 | ||
7885a8ce LJ |
156 | static const struct iio_chan_spec st_press_lps001wp_channels[] = { |
157 | { | |
158 | .type = IIO_PRESSURE, | |
159 | .channel2 = IIO_NO_MOD, | |
160 | .address = ST_PRESS_LPS001WP_OUT_L_ADDR, | |
161 | .scan_index = ST_SENSORS_SCAN_X, | |
162 | .scan_type = { | |
163 | .sign = 'u', | |
164 | .realbits = 16, | |
165 | .storagebits = 16, | |
166 | .endianness = IIO_LE, | |
167 | }, | |
d43a4115 GB |
168 | .info_mask_separate = |
169 | BIT(IIO_CHAN_INFO_RAW) | | |
170 | BIT(IIO_CHAN_INFO_SCALE), | |
7885a8ce LJ |
171 | .modified = 0, |
172 | }, | |
173 | { | |
174 | .type = IIO_TEMP, | |
175 | .channel2 = IIO_NO_MOD, | |
176 | .address = ST_TEMP_LPS001WP_OUT_L_ADDR, | |
177 | .scan_index = -1, | |
178 | .scan_type = { | |
179 | .sign = 'u', | |
180 | .realbits = 16, | |
181 | .storagebits = 16, | |
182 | .endianness = IIO_LE, | |
183 | }, | |
184 | .info_mask_separate = | |
185 | BIT(IIO_CHAN_INFO_RAW) | | |
d43a4115 | 186 | BIT(IIO_CHAN_INFO_SCALE), |
7885a8ce LJ |
187 | .modified = 0, |
188 | }, | |
189 | IIO_CHAN_SOFT_TIMESTAMP(1) | |
190 | }; | |
191 | ||
a7ee8839 | 192 | static const struct st_sensor_settings st_press_sensors_settings[] = { |
217494e5 | 193 | { |
302fbd50 | 194 | .wai = ST_PRESS_LPS331AP_WAI_EXP, |
bc27381e | 195 | .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, |
217494e5 DC |
196 | .sensors_supported = { |
197 | [0] = LPS331AP_PRESS_DEV_NAME, | |
198 | }, | |
93187840 DC |
199 | .ch = (struct iio_chan_spec *)st_press_1_channels, |
200 | .num_ch = ARRAY_SIZE(st_press_1_channels), | |
217494e5 | 201 | .odr = { |
302fbd50 LJ |
202 | .addr = ST_PRESS_LPS331AP_ODR_ADDR, |
203 | .mask = ST_PRESS_LPS331AP_ODR_MASK, | |
217494e5 | 204 | .odr_avl = { |
302fbd50 LJ |
205 | { 1, ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL, }, |
206 | { 7, ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL, }, | |
207 | { 13, ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL, }, | |
208 | { 25, ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL, }, | |
217494e5 DC |
209 | }, |
210 | }, | |
211 | .pw = { | |
302fbd50 LJ |
212 | .addr = ST_PRESS_LPS331AP_PW_ADDR, |
213 | .mask = ST_PRESS_LPS331AP_PW_MASK, | |
217494e5 DC |
214 | .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, |
215 | .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, | |
216 | }, | |
217 | .fs = { | |
302fbd50 LJ |
218 | .addr = ST_PRESS_LPS331AP_FS_ADDR, |
219 | .mask = ST_PRESS_LPS331AP_FS_MASK, | |
217494e5 | 220 | .fs_avl = { |
d43a4115 GB |
221 | /* |
222 | * Pressure and temperature sensitivity values | |
223 | * as defined in table 3 of LPS331AP datasheet. | |
224 | */ | |
217494e5 DC |
225 | [0] = { |
226 | .num = ST_PRESS_FS_AVL_1260MB, | |
d43a4115 GB |
227 | .gain = ST_PRESS_KPASCAL_NANO_SCALE, |
228 | .gain2 = ST_PRESS_LSB_PER_CELSIUS, | |
217494e5 DC |
229 | }, |
230 | }, | |
231 | }, | |
232 | .bdu = { | |
302fbd50 LJ |
233 | .addr = ST_PRESS_LPS331AP_BDU_ADDR, |
234 | .mask = ST_PRESS_LPS331AP_BDU_MASK, | |
217494e5 DC |
235 | }, |
236 | .drdy_irq = { | |
302fbd50 LJ |
237 | .addr = ST_PRESS_LPS331AP_DRDY_IRQ_ADDR, |
238 | .mask_int1 = ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK, | |
239 | .mask_int2 = ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK, | |
a9fd053b LW |
240 | .addr_ihl = ST_PRESS_LPS331AP_IHL_IRQ_ADDR, |
241 | .mask_ihl = ST_PRESS_LPS331AP_IHL_IRQ_MASK, | |
0e6f6871 LW |
242 | .addr_od = ST_PRESS_LPS331AP_OD_IRQ_ADDR, |
243 | .mask_od = ST_PRESS_LPS331AP_OD_IRQ_MASK, | |
97865fe4 | 244 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, |
217494e5 | 245 | }, |
302fbd50 | 246 | .multi_read_bit = ST_PRESS_LPS331AP_MULTIREAD_BIT, |
217494e5 DC |
247 | .bootime = 2, |
248 | }, | |
7885a8ce LJ |
249 | { |
250 | .wai = ST_PRESS_LPS001WP_WAI_EXP, | |
bc27381e | 251 | .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, |
7885a8ce LJ |
252 | .sensors_supported = { |
253 | [0] = LPS001WP_PRESS_DEV_NAME, | |
254 | }, | |
255 | .ch = (struct iio_chan_spec *)st_press_lps001wp_channels, | |
256 | .num_ch = ARRAY_SIZE(st_press_lps001wp_channels), | |
257 | .odr = { | |
258 | .addr = ST_PRESS_LPS001WP_ODR_ADDR, | |
259 | .mask = ST_PRESS_LPS001WP_ODR_MASK, | |
260 | .odr_avl = { | |
261 | { 1, ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL, }, | |
262 | { 7, ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL, }, | |
263 | { 13, ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL, }, | |
264 | }, | |
265 | }, | |
266 | .pw = { | |
267 | .addr = ST_PRESS_LPS001WP_PW_ADDR, | |
268 | .mask = ST_PRESS_LPS001WP_PW_MASK, | |
269 | .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, | |
270 | .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, | |
271 | }, | |
272 | .fs = { | |
d43a4115 GB |
273 | .fs_avl = { |
274 | /* | |
275 | * Pressure and temperature resolution values | |
276 | * as defined in table 3 of LPS001WP datasheet. | |
277 | */ | |
278 | [0] = { | |
279 | .num = ST_PRESS_FS_AVL_1100MB, | |
280 | .gain = ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN, | |
281 | .gain2 = ST_PRESS_LPS001WP_LSB_PER_CELSIUS, | |
282 | }, | |
283 | }, | |
7885a8ce LJ |
284 | }, |
285 | .bdu = { | |
286 | .addr = ST_PRESS_LPS001WP_BDU_ADDR, | |
287 | .mask = ST_PRESS_LPS001WP_BDU_MASK, | |
288 | }, | |
289 | .drdy_irq = { | |
290 | .addr = 0, | |
291 | }, | |
292 | .multi_read_bit = ST_PRESS_LPS001WP_MULTIREAD_BIT, | |
293 | .bootime = 2, | |
294 | }, | |
93187840 DC |
295 | { |
296 | .wai = ST_PRESS_LPS25H_WAI_EXP, | |
bc27381e | 297 | .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, |
93187840 DC |
298 | .sensors_supported = { |
299 | [0] = LPS25H_PRESS_DEV_NAME, | |
300 | }, | |
301 | .ch = (struct iio_chan_spec *)st_press_1_channels, | |
302 | .num_ch = ARRAY_SIZE(st_press_1_channels), | |
303 | .odr = { | |
304 | .addr = ST_PRESS_LPS25H_ODR_ADDR, | |
305 | .mask = ST_PRESS_LPS25H_ODR_MASK, | |
306 | .odr_avl = { | |
307 | { 1, ST_PRESS_LPS25H_ODR_AVL_1HZ_VAL, }, | |
308 | { 7, ST_PRESS_LPS25H_ODR_AVL_7HZ_VAL, }, | |
309 | { 13, ST_PRESS_LPS25H_ODR_AVL_13HZ_VAL, }, | |
310 | { 25, ST_PRESS_LPS25H_ODR_AVL_25HZ_VAL, }, | |
311 | }, | |
312 | }, | |
313 | .pw = { | |
314 | .addr = ST_PRESS_LPS25H_PW_ADDR, | |
315 | .mask = ST_PRESS_LPS25H_PW_MASK, | |
316 | .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, | |
317 | .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, | |
318 | }, | |
319 | .fs = { | |
93187840 | 320 | .fs_avl = { |
d43a4115 GB |
321 | /* |
322 | * Pressure and temperature sensitivity values | |
323 | * as defined in table 3 of LPS25H datasheet. | |
324 | */ | |
93187840 DC |
325 | [0] = { |
326 | .num = ST_PRESS_FS_AVL_1260MB, | |
d43a4115 GB |
327 | .gain = ST_PRESS_KPASCAL_NANO_SCALE, |
328 | .gain2 = ST_PRESS_LSB_PER_CELSIUS, | |
93187840 DC |
329 | }, |
330 | }, | |
331 | }, | |
332 | .bdu = { | |
333 | .addr = ST_PRESS_LPS25H_BDU_ADDR, | |
334 | .mask = ST_PRESS_LPS25H_BDU_MASK, | |
335 | }, | |
336 | .drdy_irq = { | |
337 | .addr = ST_PRESS_LPS25H_DRDY_IRQ_ADDR, | |
338 | .mask_int1 = ST_PRESS_LPS25H_DRDY_IRQ_INT1_MASK, | |
339 | .mask_int2 = ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK, | |
a9fd053b LW |
340 | .addr_ihl = ST_PRESS_LPS25H_IHL_IRQ_ADDR, |
341 | .mask_ihl = ST_PRESS_LPS25H_IHL_IRQ_MASK, | |
0e6f6871 LW |
342 | .addr_od = ST_PRESS_LPS25H_OD_IRQ_ADDR, |
343 | .mask_od = ST_PRESS_LPS25H_OD_IRQ_MASK, | |
97865fe4 | 344 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, |
93187840 DC |
345 | }, |
346 | .multi_read_bit = ST_PRESS_LPS25H_MULTIREAD_BIT, | |
347 | .bootime = 2, | |
348 | }, | |
217494e5 DC |
349 | }; |
350 | ||
2d239c9e JC |
351 | static int st_press_write_raw(struct iio_dev *indio_dev, |
352 | struct iio_chan_spec const *ch, | |
353 | int val, | |
354 | int val2, | |
355 | long mask) | |
356 | { | |
357 | int err; | |
358 | ||
359 | switch (mask) { | |
360 | case IIO_CHAN_INFO_SAMP_FREQ: | |
361 | if (val2) | |
362 | return -EINVAL; | |
363 | mutex_lock(&indio_dev->mlock); | |
364 | err = st_sensors_set_odr(indio_dev, val); | |
365 | mutex_unlock(&indio_dev->mlock); | |
366 | return err; | |
367 | default: | |
368 | return -EINVAL; | |
369 | } | |
370 | } | |
371 | ||
217494e5 DC |
372 | static int st_press_read_raw(struct iio_dev *indio_dev, |
373 | struct iio_chan_spec const *ch, int *val, | |
374 | int *val2, long mask) | |
375 | { | |
376 | int err; | |
a1dcf429 | 377 | struct st_sensor_data *press_data = iio_priv(indio_dev); |
217494e5 DC |
378 | |
379 | switch (mask) { | |
380 | case IIO_CHAN_INFO_RAW: | |
381 | err = st_sensors_read_info_raw(indio_dev, ch, val); | |
382 | if (err < 0) | |
383 | goto read_error; | |
384 | ||
385 | return IIO_VAL_INT; | |
386 | case IIO_CHAN_INFO_SCALE: | |
217494e5 DC |
387 | switch (ch->type) { |
388 | case IIO_PRESSURE: | |
d43a4115 | 389 | *val = 0; |
a1dcf429 | 390 | *val2 = press_data->current_fullscale->gain; |
d43a4115 | 391 | return IIO_VAL_INT_PLUS_NANO; |
217494e5 | 392 | case IIO_TEMP: |
d43a4115 | 393 | *val = MCELSIUS_PER_CELSIUS; |
a1dcf429 | 394 | *val2 = press_data->current_fullscale->gain2; |
d43a4115 | 395 | return IIO_VAL_FRACTIONAL; |
217494e5 DC |
396 | default: |
397 | err = -EINVAL; | |
398 | goto read_error; | |
399 | } | |
400 | ||
217494e5 DC |
401 | case IIO_CHAN_INFO_OFFSET: |
402 | switch (ch->type) { | |
403 | case IIO_TEMP: | |
d43a4115 GB |
404 | *val = ST_PRESS_MILLI_CELSIUS_OFFSET * |
405 | press_data->current_fullscale->gain2; | |
406 | *val2 = MCELSIUS_PER_CELSIUS; | |
217494e5 DC |
407 | break; |
408 | default: | |
409 | err = -EINVAL; | |
410 | goto read_error; | |
411 | } | |
412 | ||
413 | return IIO_VAL_FRACTIONAL; | |
2d239c9e | 414 | case IIO_CHAN_INFO_SAMP_FREQ: |
a1dcf429 | 415 | *val = press_data->odr; |
2d239c9e | 416 | return IIO_VAL_INT; |
217494e5 DC |
417 | default: |
418 | return -EINVAL; | |
419 | } | |
420 | ||
421 | read_error: | |
422 | return err; | |
423 | } | |
424 | ||
217494e5 DC |
425 | static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); |
426 | ||
427 | static struct attribute *st_press_attributes[] = { | |
428 | &iio_dev_attr_sampling_frequency_available.dev_attr.attr, | |
217494e5 DC |
429 | NULL, |
430 | }; | |
431 | ||
432 | static const struct attribute_group st_press_attribute_group = { | |
433 | .attrs = st_press_attributes, | |
434 | }; | |
435 | ||
436 | static const struct iio_info press_info = { | |
437 | .driver_module = THIS_MODULE, | |
438 | .attrs = &st_press_attribute_group, | |
439 | .read_raw = &st_press_read_raw, | |
2d239c9e | 440 | .write_raw = &st_press_write_raw, |
a0175b9c | 441 | .debugfs_reg_access = &st_sensors_debugfs_reg_access, |
217494e5 DC |
442 | }; |
443 | ||
444 | #ifdef CONFIG_IIO_TRIGGER | |
445 | static const struct iio_trigger_ops st_press_trigger_ops = { | |
446 | .owner = THIS_MODULE, | |
447 | .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE, | |
65925b65 | 448 | .validate_device = st_sensors_validate_device, |
217494e5 DC |
449 | }; |
450 | #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops) | |
451 | #else | |
452 | #define ST_PRESS_TRIGGER_OPS NULL | |
453 | #endif | |
454 | ||
0baa3fc1 | 455 | int st_press_common_probe(struct iio_dev *indio_dev) |
217494e5 | 456 | { |
a1dcf429 DC |
457 | struct st_sensor_data *press_data = iio_priv(indio_dev); |
458 | int irq = press_data->get_irq_data_ready(indio_dev); | |
a6cc5b25 | 459 | int err; |
217494e5 DC |
460 | |
461 | indio_dev->modes = INDIO_DIRECT_MODE; | |
462 | indio_dev->info = &press_info; | |
8e71c04f | 463 | mutex_init(&press_data->tb.buf_lock); |
217494e5 | 464 | |
ea7e586b | 465 | st_sensors_power_enable(indio_dev); |
77448761 | 466 | |
217494e5 | 467 | err = st_sensors_check_device_support(indio_dev, |
a7ee8839 DC |
468 | ARRAY_SIZE(st_press_sensors_settings), |
469 | st_press_sensors_settings); | |
217494e5 | 470 | if (err < 0) |
a6cc5b25 | 471 | return err; |
217494e5 | 472 | |
a1dcf429 DC |
473 | press_data->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS; |
474 | press_data->multiread_bit = press_data->sensor_settings->multi_read_bit; | |
475 | indio_dev->channels = press_data->sensor_settings->ch; | |
476 | indio_dev->num_channels = press_data->sensor_settings->num_ch; | |
217494e5 | 477 | |
a1dcf429 DC |
478 | if (press_data->sensor_settings->fs.addr != 0) |
479 | press_data->current_fullscale = | |
480 | (struct st_sensor_fullscale_avl *) | |
481 | &press_data->sensor_settings->fs.fs_avl[0]; | |
362f2f86 | 482 | |
a1dcf429 | 483 | press_data->odr = press_data->sensor_settings->odr.odr_avl[0].hz; |
217494e5 | 484 | |
38d1c6a9 | 485 | /* Some devices don't support a data ready pin. */ |
a1dcf429 DC |
486 | if (!press_data->dev->platform_data && |
487 | press_data->sensor_settings->drdy_irq.addr) | |
488 | press_data->dev->platform_data = | |
23cde4d6 DC |
489 | (struct st_sensors_platform_data *)&default_press_pdata; |
490 | ||
a1dcf429 | 491 | err = st_sensors_init_sensor(indio_dev, press_data->dev->platform_data); |
217494e5 | 492 | if (err < 0) |
a6cc5b25 | 493 | return err; |
217494e5 | 494 | |
7a137c9c DC |
495 | err = st_press_allocate_ring(indio_dev); |
496 | if (err < 0) | |
497 | return err; | |
217494e5 | 498 | |
7a137c9c | 499 | if (irq > 0) { |
217494e5 | 500 | err = st_sensors_allocate_trigger(indio_dev, |
a6cc5b25 | 501 | ST_PRESS_TRIGGER_OPS); |
217494e5 DC |
502 | if (err < 0) |
503 | goto st_press_probe_trigger_error; | |
504 | } | |
505 | ||
506 | err = iio_device_register(indio_dev); | |
507 | if (err) | |
508 | goto st_press_device_register_error; | |
509 | ||
4f544ced LW |
510 | dev_info(&indio_dev->dev, "registered pressure sensor %s\n", |
511 | indio_dev->name); | |
512 | ||
217494e5 DC |
513 | return err; |
514 | ||
515 | st_press_device_register_error: | |
a6cc5b25 | 516 | if (irq > 0) |
217494e5 DC |
517 | st_sensors_deallocate_trigger(indio_dev); |
518 | st_press_probe_trigger_error: | |
7a137c9c | 519 | st_press_deallocate_ring(indio_dev); |
a6cc5b25 | 520 | |
217494e5 DC |
521 | return err; |
522 | } | |
523 | EXPORT_SYMBOL(st_press_common_probe); | |
524 | ||
525 | void st_press_common_remove(struct iio_dev *indio_dev) | |
526 | { | |
a1dcf429 | 527 | struct st_sensor_data *press_data = iio_priv(indio_dev); |
217494e5 | 528 | |
ea7e586b | 529 | st_sensors_power_disable(indio_dev); |
77448761 | 530 | |
217494e5 | 531 | iio_device_unregister(indio_dev); |
a1dcf429 | 532 | if (press_data->get_irq_data_ready(indio_dev) > 0) |
217494e5 | 533 | st_sensors_deallocate_trigger(indio_dev); |
7a137c9c DC |
534 | |
535 | st_press_deallocate_ring(indio_dev); | |
217494e5 DC |
536 | } |
537 | EXPORT_SYMBOL(st_press_common_remove); | |
538 | ||
539 | MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); | |
540 | MODULE_DESCRIPTION("STMicroelectronics pressures driver"); | |
541 | MODULE_LICENSE("GPL v2"); |