]>
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 | ||
19b7b8a8 GB |
31 | /* |
32 | * About determining pressure scaling factors | |
33 | * ------------------------------------------ | |
34 | * | |
35 | * Datasheets specify typical pressure sensitivity so that pressure is computed | |
36 | * according to the following equation : | |
37 | * pressure[mBar] = raw / sensitivity | |
38 | * where : | |
39 | * raw the 24 bits long raw sampled pressure | |
40 | * sensitivity a scaling factor specified by the datasheet in LSB/mBar | |
41 | * | |
42 | * IIO ABI expects pressure to be expressed as kPascal, hence pressure should be | |
43 | * computed according to : | |
44 | * pressure[kPascal] = pressure[mBar] / 10 | |
45 | * = raw / (sensitivity * 10) (1) | |
46 | * | |
47 | * Finally, st_press_read_raw() returns pressure scaling factor as an | |
48 | * IIO_VAL_INT_PLUS_NANO with a zero integral part and "gain" as decimal part. | |
49 | * Therefore, from (1), "gain" becomes : | |
50 | * gain = 10^9 / (sensitivity * 10) | |
51 | * = 10^8 / sensitivity | |
52 | * | |
53 | * About determining temperature scaling factors and offsets | |
54 | * --------------------------------------------------------- | |
55 | * | |
56 | * Datasheets specify typical temperature sensitivity and offset so that | |
57 | * temperature is computed according to the following equation : | |
58 | * temp[Celsius] = offset[Celsius] + (raw / sensitivity) | |
59 | * where : | |
60 | * raw the 16 bits long raw sampled temperature | |
61 | * offset a constant specified by the datasheet in degree Celsius | |
62 | * (sometimes zero) | |
63 | * sensitivity a scaling factor specified by the datasheet in LSB/Celsius | |
64 | * | |
65 | * IIO ABI expects temperature to be expressed as milli degree Celsius such as | |
66 | * user space should compute temperature according to : | |
67 | * temp[mCelsius] = temp[Celsius] * 10^3 | |
68 | * = (offset[Celsius] + (raw / sensitivity)) * 10^3 | |
69 | * = ((offset[Celsius] * sensitivity) + raw) * | |
70 | * (10^3 / sensitivity) (2) | |
71 | * | |
72 | * IIO ABI expects user space to apply offset and scaling factors to raw samples | |
73 | * according to : | |
74 | * temp[mCelsius] = (OFFSET + raw) * SCALE | |
75 | * where : | |
76 | * OFFSET an arbitrary constant exposed by device | |
77 | * SCALE an arbitrary scaling factor exposed by device | |
78 | * | |
79 | * Matching OFFSET and SCALE with members of (2) gives : | |
80 | * OFFSET = offset[Celsius] * sensitivity (3) | |
81 | * SCALE = 10^3 / sensitivity (4) | |
82 | * | |
83 | * st_press_read_raw() returns temperature scaling factor as an | |
84 | * IIO_VAL_FRACTIONAL with a 10^3 numerator and "gain2" as denominator. | |
85 | * Therefore, from (3), "gain2" becomes : | |
86 | * gain2 = sensitivity | |
87 | * | |
88 | * When declared within channel, i.e. for a non zero specified offset, | |
89 | * st_press_read_raw() will return the latter as an IIO_VAL_FRACTIONAL such as : | |
90 | * numerator = OFFSET * 10^3 | |
91 | * denominator = 10^3 | |
92 | * giving from (4): | |
93 | * numerator = offset[Celsius] * 10^3 * sensitivity | |
94 | * = offset[mCelsius] * gain2 | |
95 | */ | |
96 | ||
d43a4115 GB |
97 | #define MCELSIUS_PER_CELSIUS 1000 |
98 | ||
99 | /* Default pressure sensitivity */ | |
67dbf54a JA |
100 | #define ST_PRESS_LSB_PER_MBAR 4096UL |
101 | #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \ | |
102 | ST_PRESS_LSB_PER_MBAR) | |
d43a4115 GB |
103 | |
104 | /* Default temperature sensitivity */ | |
1003eb67 | 105 | #define ST_PRESS_LSB_PER_CELSIUS 480UL |
d43a4115 GB |
106 | #define ST_PRESS_MILLI_CELSIUS_OFFSET 42500UL |
107 | ||
217494e5 | 108 | /* FULLSCALE */ |
d43a4115 | 109 | #define ST_PRESS_FS_AVL_1100MB 1100 |
217494e5 DC |
110 | #define ST_PRESS_FS_AVL_1260MB 1260 |
111 | ||
93187840 DC |
112 | #define ST_PRESS_1_OUT_XL_ADDR 0x28 |
113 | #define ST_TEMP_1_OUT_L_ADDR 0x2b | |
114 | ||
d43a4115 GB |
115 | /* LPS001WP pressure resolution */ |
116 | #define ST_PRESS_LPS001WP_LSB_PER_MBAR 16UL | |
117 | /* LPS001WP temperature resolution */ | |
118 | #define ST_PRESS_LPS001WP_LSB_PER_CELSIUS 64UL | |
91a86a3b | 119 | /* LPS001WP pressure gain */ |
d43a4115 GB |
120 | #define ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN \ |
121 | (100000000UL / ST_PRESS_LPS001WP_LSB_PER_MBAR) | |
91a86a3b | 122 | /* LPS001WP pressure and temp L addresses */ |
7885a8ce LJ |
123 | #define ST_PRESS_LPS001WP_OUT_L_ADDR 0x28 |
124 | #define ST_TEMP_LPS001WP_OUT_L_ADDR 0x2a | |
125 | ||
91a86a3b | 126 | /* LPS25H pressure and temp L addresses */ |
93187840 DC |
127 | #define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28 |
128 | #define ST_TEMP_LPS25H_OUT_L_ADDR 0x2b | |
129 | ||
85d79136 GB |
130 | /* LPS22HB temperature sensitivity */ |
131 | #define ST_PRESS_LPS22HB_LSB_PER_CELSIUS 100UL | |
132 | ||
93187840 | 133 | static const struct iio_chan_spec st_press_1_channels[] = { |
2f5effcb LJ |
134 | { |
135 | .type = IIO_PRESSURE, | |
93187840 | 136 | .address = ST_PRESS_1_OUT_XL_ADDR, |
b4701fd6 | 137 | .scan_index = 0, |
2f5effcb | 138 | .scan_type = { |
1b211d48 | 139 | .sign = 's', |
2f5effcb | 140 | .realbits = 24, |
c9d5e5b9 | 141 | .storagebits = 32, |
2f5effcb LJ |
142 | .endianness = IIO_LE, |
143 | }, | |
144 | .info_mask_separate = | |
217494e5 | 145 | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), |
7f0d8740 | 146 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), |
2f5effcb LJ |
147 | }, |
148 | { | |
149 | .type = IIO_TEMP, | |
93187840 | 150 | .address = ST_TEMP_1_OUT_L_ADDR, |
b4701fd6 | 151 | .scan_index = 1, |
2f5effcb | 152 | .scan_type = { |
1b211d48 | 153 | .sign = 's', |
2f5effcb LJ |
154 | .realbits = 16, |
155 | .storagebits = 16, | |
156 | .endianness = IIO_LE, | |
157 | }, | |
158 | .info_mask_separate = | |
159 | BIT(IIO_CHAN_INFO_RAW) | | |
160 | BIT(IIO_CHAN_INFO_SCALE) | | |
161 | BIT(IIO_CHAN_INFO_OFFSET), | |
7f0d8740 | 162 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), |
2f5effcb | 163 | }, |
b4701fd6 | 164 | IIO_CHAN_SOFT_TIMESTAMP(2) |
217494e5 DC |
165 | }; |
166 | ||
7885a8ce LJ |
167 | static const struct iio_chan_spec st_press_lps001wp_channels[] = { |
168 | { | |
169 | .type = IIO_PRESSURE, | |
7885a8ce | 170 | .address = ST_PRESS_LPS001WP_OUT_L_ADDR, |
b4701fd6 | 171 | .scan_index = 0, |
7885a8ce | 172 | .scan_type = { |
1b211d48 | 173 | .sign = 's', |
7885a8ce LJ |
174 | .realbits = 16, |
175 | .storagebits = 16, | |
176 | .endianness = IIO_LE, | |
177 | }, | |
d43a4115 GB |
178 | .info_mask_separate = |
179 | BIT(IIO_CHAN_INFO_RAW) | | |
180 | BIT(IIO_CHAN_INFO_SCALE), | |
7885a8ce LJ |
181 | }, |
182 | { | |
183 | .type = IIO_TEMP, | |
7885a8ce | 184 | .address = ST_TEMP_LPS001WP_OUT_L_ADDR, |
b4701fd6 | 185 | .scan_index = 1, |
7885a8ce | 186 | .scan_type = { |
1b211d48 | 187 | .sign = 's', |
7885a8ce LJ |
188 | .realbits = 16, |
189 | .storagebits = 16, | |
190 | .endianness = IIO_LE, | |
191 | }, | |
192 | .info_mask_separate = | |
193 | BIT(IIO_CHAN_INFO_RAW) | | |
d43a4115 | 194 | BIT(IIO_CHAN_INFO_SCALE), |
7885a8ce | 195 | }, |
b4701fd6 | 196 | IIO_CHAN_SOFT_TIMESTAMP(2) |
7885a8ce LJ |
197 | }; |
198 | ||
e039e2f5 GB |
199 | static const struct iio_chan_spec st_press_lps22hb_channels[] = { |
200 | { | |
201 | .type = IIO_PRESSURE, | |
e039e2f5 GB |
202 | .address = ST_PRESS_1_OUT_XL_ADDR, |
203 | .scan_index = 0, | |
204 | .scan_type = { | |
1b211d48 | 205 | .sign = 's', |
e039e2f5 | 206 | .realbits = 24, |
c9d5e5b9 | 207 | .storagebits = 32, |
e039e2f5 GB |
208 | .endianness = IIO_LE, |
209 | }, | |
210 | .info_mask_separate = | |
211 | BIT(IIO_CHAN_INFO_RAW) | | |
212 | BIT(IIO_CHAN_INFO_SCALE), | |
213 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), | |
e039e2f5 | 214 | }, |
85d79136 GB |
215 | { |
216 | .type = IIO_TEMP, | |
217 | .address = ST_TEMP_1_OUT_L_ADDR, | |
218 | .scan_index = 1, | |
219 | .scan_type = { | |
220 | .sign = 's', | |
221 | .realbits = 16, | |
222 | .storagebits = 16, | |
223 | .endianness = IIO_LE, | |
224 | }, | |
225 | .info_mask_separate = | |
226 | BIT(IIO_CHAN_INFO_RAW) | | |
227 | BIT(IIO_CHAN_INFO_SCALE), | |
228 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), | |
229 | }, | |
230 | IIO_CHAN_SOFT_TIMESTAMP(2) | |
e039e2f5 GB |
231 | }; |
232 | ||
a7ee8839 | 233 | static const struct st_sensor_settings st_press_sensors_settings[] = { |
217494e5 | 234 | { |
91a86a3b LW |
235 | /* |
236 | * CUSTOM VALUES FOR LPS331AP SENSOR | |
237 | * See LPS331AP datasheet: | |
238 | * http://www2.st.com/resource/en/datasheet/lps331ap.pdf | |
239 | */ | |
240 | .wai = 0xbb, | |
bc27381e | 241 | .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, |
217494e5 DC |
242 | .sensors_supported = { |
243 | [0] = LPS331AP_PRESS_DEV_NAME, | |
244 | }, | |
93187840 DC |
245 | .ch = (struct iio_chan_spec *)st_press_1_channels, |
246 | .num_ch = ARRAY_SIZE(st_press_1_channels), | |
217494e5 | 247 | .odr = { |
91a86a3b LW |
248 | .addr = 0x20, |
249 | .mask = 0x70, | |
217494e5 | 250 | .odr_avl = { |
91a86a3b LW |
251 | { .hz = 1, .value = 0x01 }, |
252 | { .hz = 7, .value = 0x05 }, | |
253 | { .hz = 13, .value = 0x06 }, | |
254 | { .hz = 25, .value = 0x07 }, | |
217494e5 DC |
255 | }, |
256 | }, | |
257 | .pw = { | |
91a86a3b LW |
258 | .addr = 0x20, |
259 | .mask = 0x80, | |
217494e5 DC |
260 | .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, |
261 | .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, | |
262 | }, | |
263 | .fs = { | |
91a86a3b LW |
264 | .addr = 0x23, |
265 | .mask = 0x30, | |
217494e5 | 266 | .fs_avl = { |
d43a4115 GB |
267 | /* |
268 | * Pressure and temperature sensitivity values | |
269 | * as defined in table 3 of LPS331AP datasheet. | |
270 | */ | |
217494e5 DC |
271 | [0] = { |
272 | .num = ST_PRESS_FS_AVL_1260MB, | |
d43a4115 GB |
273 | .gain = ST_PRESS_KPASCAL_NANO_SCALE, |
274 | .gain2 = ST_PRESS_LSB_PER_CELSIUS, | |
217494e5 DC |
275 | }, |
276 | }, | |
277 | }, | |
278 | .bdu = { | |
91a86a3b LW |
279 | .addr = 0x20, |
280 | .mask = 0x04, | |
217494e5 DC |
281 | }, |
282 | .drdy_irq = { | |
91a86a3b LW |
283 | .addr = 0x22, |
284 | .mask_int1 = 0x04, | |
285 | .mask_int2 = 0x20, | |
286 | .addr_ihl = 0x22, | |
287 | .mask_ihl = 0x80, | |
288 | .addr_od = 0x22, | |
289 | .mask_od = 0x40, | |
97865fe4 | 290 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, |
217494e5 | 291 | }, |
91a86a3b | 292 | .multi_read_bit = true, |
217494e5 DC |
293 | .bootime = 2, |
294 | }, | |
7885a8ce | 295 | { |
91a86a3b LW |
296 | /* |
297 | * CUSTOM VALUES FOR LPS001WP SENSOR | |
298 | */ | |
299 | .wai = 0xba, | |
bc27381e | 300 | .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, |
7885a8ce LJ |
301 | .sensors_supported = { |
302 | [0] = LPS001WP_PRESS_DEV_NAME, | |
303 | }, | |
304 | .ch = (struct iio_chan_spec *)st_press_lps001wp_channels, | |
305 | .num_ch = ARRAY_SIZE(st_press_lps001wp_channels), | |
306 | .odr = { | |
91a86a3b LW |
307 | .addr = 0x20, |
308 | .mask = 0x30, | |
7885a8ce | 309 | .odr_avl = { |
91a86a3b LW |
310 | { .hz = 1, .value = 0x01 }, |
311 | { .hz = 7, .value = 0x02 }, | |
312 | { .hz = 13, .value = 0x03 }, | |
7885a8ce LJ |
313 | }, |
314 | }, | |
315 | .pw = { | |
91a86a3b LW |
316 | .addr = 0x20, |
317 | .mask = 0x40, | |
7885a8ce LJ |
318 | .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, |
319 | .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, | |
320 | }, | |
321 | .fs = { | |
d43a4115 GB |
322 | .fs_avl = { |
323 | /* | |
324 | * Pressure and temperature resolution values | |
325 | * as defined in table 3 of LPS001WP datasheet. | |
326 | */ | |
327 | [0] = { | |
328 | .num = ST_PRESS_FS_AVL_1100MB, | |
329 | .gain = ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN, | |
330 | .gain2 = ST_PRESS_LPS001WP_LSB_PER_CELSIUS, | |
331 | }, | |
332 | }, | |
7885a8ce LJ |
333 | }, |
334 | .bdu = { | |
91a86a3b LW |
335 | .addr = 0x20, |
336 | .mask = 0x04, | |
7885a8ce LJ |
337 | }, |
338 | .drdy_irq = { | |
339 | .addr = 0, | |
340 | }, | |
91a86a3b | 341 | .multi_read_bit = true, |
7885a8ce LJ |
342 | .bootime = 2, |
343 | }, | |
93187840 | 344 | { |
91a86a3b LW |
345 | /* |
346 | * CUSTOM VALUES FOR LPS25H SENSOR | |
347 | * See LPS25H datasheet: | |
348 | * http://www2.st.com/resource/en/datasheet/lps25h.pdf | |
349 | */ | |
350 | .wai = 0xbd, | |
bc27381e | 351 | .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, |
93187840 DC |
352 | .sensors_supported = { |
353 | [0] = LPS25H_PRESS_DEV_NAME, | |
354 | }, | |
355 | .ch = (struct iio_chan_spec *)st_press_1_channels, | |
356 | .num_ch = ARRAY_SIZE(st_press_1_channels), | |
357 | .odr = { | |
91a86a3b LW |
358 | .addr = 0x20, |
359 | .mask = 0x70, | |
93187840 | 360 | .odr_avl = { |
91a86a3b LW |
361 | { .hz = 1, .value = 0x01 }, |
362 | { .hz = 7, .value = 0x02 }, | |
363 | { .hz = 13, .value = 0x03 }, | |
364 | { .hz = 25, .value = 0x04 }, | |
93187840 DC |
365 | }, |
366 | }, | |
367 | .pw = { | |
91a86a3b LW |
368 | .addr = 0x20, |
369 | .mask = 0x80, | |
93187840 DC |
370 | .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, |
371 | .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, | |
372 | }, | |
373 | .fs = { | |
93187840 | 374 | .fs_avl = { |
d43a4115 GB |
375 | /* |
376 | * Pressure and temperature sensitivity values | |
377 | * as defined in table 3 of LPS25H datasheet. | |
378 | */ | |
93187840 DC |
379 | [0] = { |
380 | .num = ST_PRESS_FS_AVL_1260MB, | |
d43a4115 GB |
381 | .gain = ST_PRESS_KPASCAL_NANO_SCALE, |
382 | .gain2 = ST_PRESS_LSB_PER_CELSIUS, | |
93187840 DC |
383 | }, |
384 | }, | |
385 | }, | |
386 | .bdu = { | |
91a86a3b LW |
387 | .addr = 0x20, |
388 | .mask = 0x04, | |
93187840 DC |
389 | }, |
390 | .drdy_irq = { | |
91a86a3b LW |
391 | .addr = 0x23, |
392 | .mask_int1 = 0x01, | |
393 | .mask_int2 = 0x10, | |
394 | .addr_ihl = 0x22, | |
395 | .mask_ihl = 0x80, | |
396 | .addr_od = 0x22, | |
397 | .mask_od = 0x40, | |
97865fe4 | 398 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, |
93187840 | 399 | }, |
91a86a3b | 400 | .multi_read_bit = true, |
93187840 DC |
401 | .bootime = 2, |
402 | }, | |
e039e2f5 | 403 | { |
91a86a3b LW |
404 | /* |
405 | * CUSTOM VALUES FOR LPS22HB SENSOR | |
406 | * See LPS22HB datasheet: | |
407 | * http://www2.st.com/resource/en/datasheet/lps22hb.pdf | |
408 | */ | |
409 | .wai = 0xb1, | |
e039e2f5 GB |
410 | .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, |
411 | .sensors_supported = { | |
412 | [0] = LPS22HB_PRESS_DEV_NAME, | |
413 | }, | |
414 | .ch = (struct iio_chan_spec *)st_press_lps22hb_channels, | |
415 | .num_ch = ARRAY_SIZE(st_press_lps22hb_channels), | |
416 | .odr = { | |
91a86a3b LW |
417 | .addr = 0x10, |
418 | .mask = 0x70, | |
e039e2f5 | 419 | .odr_avl = { |
91a86a3b LW |
420 | { .hz = 1, .value = 0x01 }, |
421 | { .hz = 10, .value = 0x02 }, | |
422 | { .hz = 25, .value = 0x03 }, | |
423 | { .hz = 50, .value = 0x04 }, | |
424 | { .hz = 75, .value = 0x05 }, | |
e039e2f5 GB |
425 | }, |
426 | }, | |
427 | .pw = { | |
91a86a3b LW |
428 | .addr = 0x10, |
429 | .mask = 0x70, | |
e039e2f5 GB |
430 | .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, |
431 | }, | |
432 | .fs = { | |
433 | .fs_avl = { | |
19b7b8a8 | 434 | /* |
85d79136 GB |
435 | * Pressure and temperature sensitivity values |
436 | * as defined in table 3 of LPS22HB datasheet. | |
19b7b8a8 | 437 | */ |
e039e2f5 GB |
438 | [0] = { |
439 | .num = ST_PRESS_FS_AVL_1260MB, | |
440 | .gain = ST_PRESS_KPASCAL_NANO_SCALE, | |
85d79136 | 441 | .gain2 = ST_PRESS_LPS22HB_LSB_PER_CELSIUS, |
e039e2f5 GB |
442 | }, |
443 | }, | |
444 | }, | |
445 | .bdu = { | |
91a86a3b LW |
446 | .addr = 0x10, |
447 | .mask = 0x02, | |
e039e2f5 GB |
448 | }, |
449 | .drdy_irq = { | |
91a86a3b LW |
450 | .addr = 0x12, |
451 | .mask_int1 = 0x04, | |
452 | .mask_int2 = 0x08, | |
453 | .addr_ihl = 0x12, | |
454 | .mask_ihl = 0x80, | |
455 | .addr_od = 0x12, | |
456 | .mask_od = 0x40, | |
05167cdc | 457 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, |
e039e2f5 | 458 | }, |
add6e6ab | 459 | .multi_read_bit = false, |
51f528a1 | 460 | .bootime = 2, |
e039e2f5 | 461 | }, |
217494e5 DC |
462 | }; |
463 | ||
2d239c9e JC |
464 | static int st_press_write_raw(struct iio_dev *indio_dev, |
465 | struct iio_chan_spec const *ch, | |
466 | int val, | |
467 | int val2, | |
468 | long mask) | |
469 | { | |
470 | int err; | |
471 | ||
472 | switch (mask) { | |
473 | case IIO_CHAN_INFO_SAMP_FREQ: | |
474 | if (val2) | |
475 | return -EINVAL; | |
476 | mutex_lock(&indio_dev->mlock); | |
477 | err = st_sensors_set_odr(indio_dev, val); | |
478 | mutex_unlock(&indio_dev->mlock); | |
479 | return err; | |
480 | default: | |
481 | return -EINVAL; | |
482 | } | |
483 | } | |
484 | ||
217494e5 DC |
485 | static int st_press_read_raw(struct iio_dev *indio_dev, |
486 | struct iio_chan_spec const *ch, int *val, | |
487 | int *val2, long mask) | |
488 | { | |
489 | int err; | |
a1dcf429 | 490 | struct st_sensor_data *press_data = iio_priv(indio_dev); |
217494e5 DC |
491 | |
492 | switch (mask) { | |
493 | case IIO_CHAN_INFO_RAW: | |
494 | err = st_sensors_read_info_raw(indio_dev, ch, val); | |
495 | if (err < 0) | |
496 | goto read_error; | |
497 | ||
498 | return IIO_VAL_INT; | |
499 | case IIO_CHAN_INFO_SCALE: | |
217494e5 DC |
500 | switch (ch->type) { |
501 | case IIO_PRESSURE: | |
d43a4115 | 502 | *val = 0; |
a1dcf429 | 503 | *val2 = press_data->current_fullscale->gain; |
d43a4115 | 504 | return IIO_VAL_INT_PLUS_NANO; |
217494e5 | 505 | case IIO_TEMP: |
d43a4115 | 506 | *val = MCELSIUS_PER_CELSIUS; |
a1dcf429 | 507 | *val2 = press_data->current_fullscale->gain2; |
d43a4115 | 508 | return IIO_VAL_FRACTIONAL; |
217494e5 DC |
509 | default: |
510 | err = -EINVAL; | |
511 | goto read_error; | |
512 | } | |
513 | ||
217494e5 DC |
514 | case IIO_CHAN_INFO_OFFSET: |
515 | switch (ch->type) { | |
516 | case IIO_TEMP: | |
d43a4115 GB |
517 | *val = ST_PRESS_MILLI_CELSIUS_OFFSET * |
518 | press_data->current_fullscale->gain2; | |
519 | *val2 = MCELSIUS_PER_CELSIUS; | |
217494e5 DC |
520 | break; |
521 | default: | |
522 | err = -EINVAL; | |
523 | goto read_error; | |
524 | } | |
525 | ||
526 | return IIO_VAL_FRACTIONAL; | |
2d239c9e | 527 | case IIO_CHAN_INFO_SAMP_FREQ: |
a1dcf429 | 528 | *val = press_data->odr; |
2d239c9e | 529 | return IIO_VAL_INT; |
217494e5 DC |
530 | default: |
531 | return -EINVAL; | |
532 | } | |
533 | ||
534 | read_error: | |
535 | return err; | |
536 | } | |
537 | ||
217494e5 DC |
538 | static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); |
539 | ||
540 | static struct attribute *st_press_attributes[] = { | |
541 | &iio_dev_attr_sampling_frequency_available.dev_attr.attr, | |
217494e5 DC |
542 | NULL, |
543 | }; | |
544 | ||
545 | static const struct attribute_group st_press_attribute_group = { | |
546 | .attrs = st_press_attributes, | |
547 | }; | |
548 | ||
549 | static const struct iio_info press_info = { | |
550 | .driver_module = THIS_MODULE, | |
551 | .attrs = &st_press_attribute_group, | |
552 | .read_raw = &st_press_read_raw, | |
2d239c9e | 553 | .write_raw = &st_press_write_raw, |
a0175b9c | 554 | .debugfs_reg_access = &st_sensors_debugfs_reg_access, |
217494e5 DC |
555 | }; |
556 | ||
557 | #ifdef CONFIG_IIO_TRIGGER | |
558 | static const struct iio_trigger_ops st_press_trigger_ops = { | |
559 | .owner = THIS_MODULE, | |
560 | .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE, | |
65925b65 | 561 | .validate_device = st_sensors_validate_device, |
217494e5 DC |
562 | }; |
563 | #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops) | |
564 | #else | |
565 | #define ST_PRESS_TRIGGER_OPS NULL | |
566 | #endif | |
567 | ||
0baa3fc1 | 568 | int st_press_common_probe(struct iio_dev *indio_dev) |
217494e5 | 569 | { |
a1dcf429 | 570 | struct st_sensor_data *press_data = iio_priv(indio_dev); |
7383d44b SB |
571 | struct st_sensors_platform_data *pdata = |
572 | (struct st_sensors_platform_data *)press_data->dev->platform_data; | |
a1dcf429 | 573 | int irq = press_data->get_irq_data_ready(indio_dev); |
a6cc5b25 | 574 | int err; |
217494e5 DC |
575 | |
576 | indio_dev->modes = INDIO_DIRECT_MODE; | |
577 | indio_dev->info = &press_info; | |
8e71c04f | 578 | mutex_init(&press_data->tb.buf_lock); |
217494e5 | 579 | |
14f295c8 GB |
580 | err = st_sensors_power_enable(indio_dev); |
581 | if (err) | |
582 | return err; | |
77448761 | 583 | |
217494e5 | 584 | err = st_sensors_check_device_support(indio_dev, |
a7ee8839 DC |
585 | ARRAY_SIZE(st_press_sensors_settings), |
586 | st_press_sensors_settings); | |
217494e5 | 587 | if (err < 0) |
14f295c8 | 588 | goto st_press_power_off; |
217494e5 | 589 | |
b4701fd6 GB |
590 | /* |
591 | * Skip timestamping channel while declaring available channels to | |
592 | * common st_sensor layer. Look at st_sensors_get_buffer_element() to | |
593 | * see how timestamps are explicitly pushed as last samples block | |
594 | * element. | |
595 | */ | |
596 | press_data->num_data_channels = press_data->sensor_settings->num_ch - 1; | |
a1dcf429 DC |
597 | press_data->multiread_bit = press_data->sensor_settings->multi_read_bit; |
598 | indio_dev->channels = press_data->sensor_settings->ch; | |
599 | indio_dev->num_channels = press_data->sensor_settings->num_ch; | |
217494e5 | 600 | |
e039e2f5 GB |
601 | press_data->current_fullscale = |
602 | (struct st_sensor_fullscale_avl *) | |
603 | &press_data->sensor_settings->fs.fs_avl[0]; | |
362f2f86 | 604 | |
a1dcf429 | 605 | press_data->odr = press_data->sensor_settings->odr.odr_avl[0].hz; |
217494e5 | 606 | |
38d1c6a9 | 607 | /* Some devices don't support a data ready pin. */ |
7383d44b SB |
608 | if (!pdata && press_data->sensor_settings->drdy_irq.addr) |
609 | pdata = (struct st_sensors_platform_data *)&default_press_pdata; | |
23cde4d6 | 610 | |
a1dcf429 | 611 | err = st_sensors_init_sensor(indio_dev, press_data->dev->platform_data); |
217494e5 | 612 | if (err < 0) |
14f295c8 | 613 | goto st_press_power_off; |
217494e5 | 614 | |
7a137c9c DC |
615 | err = st_press_allocate_ring(indio_dev); |
616 | if (err < 0) | |
14f295c8 | 617 | goto st_press_power_off; |
217494e5 | 618 | |
7a137c9c | 619 | if (irq > 0) { |
217494e5 | 620 | err = st_sensors_allocate_trigger(indio_dev, |
a6cc5b25 | 621 | ST_PRESS_TRIGGER_OPS); |
217494e5 DC |
622 | if (err < 0) |
623 | goto st_press_probe_trigger_error; | |
624 | } | |
625 | ||
626 | err = iio_device_register(indio_dev); | |
627 | if (err) | |
628 | goto st_press_device_register_error; | |
629 | ||
4f544ced LW |
630 | dev_info(&indio_dev->dev, "registered pressure sensor %s\n", |
631 | indio_dev->name); | |
632 | ||
217494e5 DC |
633 | return err; |
634 | ||
635 | st_press_device_register_error: | |
a6cc5b25 | 636 | if (irq > 0) |
217494e5 DC |
637 | st_sensors_deallocate_trigger(indio_dev); |
638 | st_press_probe_trigger_error: | |
7a137c9c | 639 | st_press_deallocate_ring(indio_dev); |
14f295c8 GB |
640 | st_press_power_off: |
641 | st_sensors_power_disable(indio_dev); | |
a6cc5b25 | 642 | |
217494e5 DC |
643 | return err; |
644 | } | |
645 | EXPORT_SYMBOL(st_press_common_probe); | |
646 | ||
647 | void st_press_common_remove(struct iio_dev *indio_dev) | |
648 | { | |
a1dcf429 | 649 | struct st_sensor_data *press_data = iio_priv(indio_dev); |
217494e5 | 650 | |
ea7e586b | 651 | st_sensors_power_disable(indio_dev); |
77448761 | 652 | |
217494e5 | 653 | iio_device_unregister(indio_dev); |
a1dcf429 | 654 | if (press_data->get_irq_data_ready(indio_dev) > 0) |
217494e5 | 655 | st_sensors_deallocate_trigger(indio_dev); |
7a137c9c DC |
656 | |
657 | st_press_deallocate_ring(indio_dev); | |
217494e5 DC |
658 | } |
659 | EXPORT_SYMBOL(st_press_common_remove); | |
660 | ||
661 | MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); | |
662 | MODULE_DESCRIPTION("STMicroelectronics pressures driver"); | |
663 | MODULE_LICENSE("GPL v2"); |