x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / drivers / iio / pressure / st_pressure_core.c
blobec5ca03529b57b4206f1edb876463e8a2e4ebf2c
1 /*
2 * STMicroelectronics pressures driver
4 * Copyright 2013 STMicroelectronics Inc.
6 * Denis Ciocca <denis.ciocca@st.com>
8 * Licensed under the GPL-2.
9 */
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>
28 #include <linux/iio/common/st_sensors.h>
29 #include "st_pressure.h"
32 * About determining pressure scaling factors
33 * ------------------------------------------
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
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)
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
53 * About determining temperature scaling factors and offsets
54 * ---------------------------------------------------------
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
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)
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
79 * Matching OFFSET and SCALE with members of (2) gives :
80 * OFFSET = offset[Celsius] * sensitivity (3)
81 * SCALE = 10^3 / sensitivity (4)
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
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
97 #define MCELSIUS_PER_CELSIUS 1000
99 /* Default pressure sensitivity */
100 #define ST_PRESS_LSB_PER_MBAR 4096UL
101 #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \
102 ST_PRESS_LSB_PER_MBAR)
104 /* Default temperature sensitivity */
105 #define ST_PRESS_LSB_PER_CELSIUS 480UL
106 #define ST_PRESS_MILLI_CELSIUS_OFFSET 42500UL
108 /* FULLSCALE */
109 #define ST_PRESS_FS_AVL_1100MB 1100
110 #define ST_PRESS_FS_AVL_1260MB 1260
112 #define ST_PRESS_1_OUT_XL_ADDR 0x28
113 #define ST_TEMP_1_OUT_L_ADDR 0x2b
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
119 /* LPS001WP pressure gain */
120 #define ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN \
121 (100000000UL / ST_PRESS_LPS001WP_LSB_PER_MBAR)
122 /* LPS001WP pressure and temp L addresses */
123 #define ST_PRESS_LPS001WP_OUT_L_ADDR 0x28
124 #define ST_TEMP_LPS001WP_OUT_L_ADDR 0x2a
126 /* LPS25H pressure and temp L addresses */
127 #define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28
128 #define ST_TEMP_LPS25H_OUT_L_ADDR 0x2b
130 /* LPS22HB temperature sensitivity */
131 #define ST_PRESS_LPS22HB_LSB_PER_CELSIUS 100UL
133 static const struct iio_chan_spec st_press_1_channels[] = {
135 .type = IIO_PRESSURE,
136 .address = ST_PRESS_1_OUT_XL_ADDR,
137 .scan_index = 0,
138 .scan_type = {
139 .sign = 's',
140 .realbits = 24,
141 .storagebits = 32,
142 .endianness = IIO_LE,
144 .info_mask_separate =
145 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
146 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
149 .type = IIO_TEMP,
150 .address = ST_TEMP_1_OUT_L_ADDR,
151 .scan_index = 1,
152 .scan_type = {
153 .sign = 's',
154 .realbits = 16,
155 .storagebits = 16,
156 .endianness = IIO_LE,
158 .info_mask_separate =
159 BIT(IIO_CHAN_INFO_RAW) |
160 BIT(IIO_CHAN_INFO_SCALE) |
161 BIT(IIO_CHAN_INFO_OFFSET),
162 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
164 IIO_CHAN_SOFT_TIMESTAMP(2)
167 static const struct iio_chan_spec st_press_lps001wp_channels[] = {
169 .type = IIO_PRESSURE,
170 .address = ST_PRESS_LPS001WP_OUT_L_ADDR,
171 .scan_index = 0,
172 .scan_type = {
173 .sign = 's',
174 .realbits = 16,
175 .storagebits = 16,
176 .endianness = IIO_LE,
178 .info_mask_separate =
179 BIT(IIO_CHAN_INFO_RAW) |
180 BIT(IIO_CHAN_INFO_SCALE),
183 .type = IIO_TEMP,
184 .address = ST_TEMP_LPS001WP_OUT_L_ADDR,
185 .scan_index = 1,
186 .scan_type = {
187 .sign = 's',
188 .realbits = 16,
189 .storagebits = 16,
190 .endianness = IIO_LE,
192 .info_mask_separate =
193 BIT(IIO_CHAN_INFO_RAW) |
194 BIT(IIO_CHAN_INFO_SCALE),
196 IIO_CHAN_SOFT_TIMESTAMP(2)
199 static const struct iio_chan_spec st_press_lps22hb_channels[] = {
201 .type = IIO_PRESSURE,
202 .address = ST_PRESS_1_OUT_XL_ADDR,
203 .scan_index = 0,
204 .scan_type = {
205 .sign = 's',
206 .realbits = 24,
207 .storagebits = 32,
208 .endianness = IIO_LE,
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),
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,
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),
230 IIO_CHAN_SOFT_TIMESTAMP(2)
233 static const struct st_sensor_settings st_press_sensors_settings[] = {
236 * CUSTOM VALUES FOR LPS331AP SENSOR
237 * See LPS331AP datasheet:
238 * http://www2.st.com/resource/en/datasheet/lps331ap.pdf
240 .wai = 0xbb,
241 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
242 .sensors_supported = {
243 [0] = LPS331AP_PRESS_DEV_NAME,
245 .ch = (struct iio_chan_spec *)st_press_1_channels,
246 .num_ch = ARRAY_SIZE(st_press_1_channels),
247 .odr = {
248 .addr = 0x20,
249 .mask = 0x70,
250 .odr_avl = {
251 { .hz = 1, .value = 0x01 },
252 { .hz = 7, .value = 0x05 },
253 { .hz = 13, .value = 0x06 },
254 { .hz = 25, .value = 0x07 },
257 .pw = {
258 .addr = 0x20,
259 .mask = 0x80,
260 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
261 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
263 .fs = {
264 .addr = 0x23,
265 .mask = 0x30,
266 .fs_avl = {
268 * Pressure and temperature sensitivity values
269 * as defined in table 3 of LPS331AP datasheet.
271 [0] = {
272 .num = ST_PRESS_FS_AVL_1260MB,
273 .gain = ST_PRESS_KPASCAL_NANO_SCALE,
274 .gain2 = ST_PRESS_LSB_PER_CELSIUS,
278 .bdu = {
279 .addr = 0x20,
280 .mask = 0x04,
282 .drdy_irq = {
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,
290 .stat_drdy = {
291 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
292 .mask = 0x03,
295 .multi_read_bit = true,
296 .bootime = 2,
300 * CUSTOM VALUES FOR LPS001WP SENSOR
302 .wai = 0xba,
303 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
304 .sensors_supported = {
305 [0] = LPS001WP_PRESS_DEV_NAME,
307 .ch = (struct iio_chan_spec *)st_press_lps001wp_channels,
308 .num_ch = ARRAY_SIZE(st_press_lps001wp_channels),
309 .odr = {
310 .addr = 0x20,
311 .mask = 0x30,
312 .odr_avl = {
313 { .hz = 1, .value = 0x01 },
314 { .hz = 7, .value = 0x02 },
315 { .hz = 13, .value = 0x03 },
318 .pw = {
319 .addr = 0x20,
320 .mask = 0x40,
321 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
322 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
324 .fs = {
325 .fs_avl = {
327 * Pressure and temperature resolution values
328 * as defined in table 3 of LPS001WP datasheet.
330 [0] = {
331 .num = ST_PRESS_FS_AVL_1100MB,
332 .gain = ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN,
333 .gain2 = ST_PRESS_LPS001WP_LSB_PER_CELSIUS,
337 .bdu = {
338 .addr = 0x20,
339 .mask = 0x04,
341 .drdy_irq = {
342 .addr = 0,
344 .multi_read_bit = true,
345 .bootime = 2,
349 * CUSTOM VALUES FOR LPS25H SENSOR
350 * See LPS25H datasheet:
351 * http://www2.st.com/resource/en/datasheet/lps25h.pdf
353 .wai = 0xbd,
354 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
355 .sensors_supported = {
356 [0] = LPS25H_PRESS_DEV_NAME,
358 .ch = (struct iio_chan_spec *)st_press_1_channels,
359 .num_ch = ARRAY_SIZE(st_press_1_channels),
360 .odr = {
361 .addr = 0x20,
362 .mask = 0x70,
363 .odr_avl = {
364 { .hz = 1, .value = 0x01 },
365 { .hz = 7, .value = 0x02 },
366 { .hz = 13, .value = 0x03 },
367 { .hz = 25, .value = 0x04 },
370 .pw = {
371 .addr = 0x20,
372 .mask = 0x80,
373 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
374 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
376 .fs = {
377 .fs_avl = {
379 * Pressure and temperature sensitivity values
380 * as defined in table 3 of LPS25H datasheet.
382 [0] = {
383 .num = ST_PRESS_FS_AVL_1260MB,
384 .gain = ST_PRESS_KPASCAL_NANO_SCALE,
385 .gain2 = ST_PRESS_LSB_PER_CELSIUS,
389 .bdu = {
390 .addr = 0x20,
391 .mask = 0x04,
393 .drdy_irq = {
394 .addr = 0x23,
395 .mask_int1 = 0x01,
396 .mask_int2 = 0x00,
397 .addr_ihl = 0x22,
398 .mask_ihl = 0x80,
399 .addr_od = 0x22,
400 .mask_od = 0x40,
401 .stat_drdy = {
402 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
403 .mask = 0x03,
406 .multi_read_bit = true,
407 .bootime = 2,
411 * CUSTOM VALUES FOR LPS22HB SENSOR
412 * See LPS22HB datasheet:
413 * http://www2.st.com/resource/en/datasheet/lps22hb.pdf
415 .wai = 0xb1,
416 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
417 .sensors_supported = {
418 [0] = LPS22HB_PRESS_DEV_NAME,
420 .ch = (struct iio_chan_spec *)st_press_lps22hb_channels,
421 .num_ch = ARRAY_SIZE(st_press_lps22hb_channels),
422 .odr = {
423 .addr = 0x10,
424 .mask = 0x70,
425 .odr_avl = {
426 { .hz = 1, .value = 0x01 },
427 { .hz = 10, .value = 0x02 },
428 { .hz = 25, .value = 0x03 },
429 { .hz = 50, .value = 0x04 },
430 { .hz = 75, .value = 0x05 },
433 .pw = {
434 .addr = 0x10,
435 .mask = 0x70,
436 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
438 .fs = {
439 .fs_avl = {
441 * Pressure and temperature sensitivity values
442 * as defined in table 3 of LPS22HB datasheet.
444 [0] = {
445 .num = ST_PRESS_FS_AVL_1260MB,
446 .gain = ST_PRESS_KPASCAL_NANO_SCALE,
447 .gain2 = ST_PRESS_LPS22HB_LSB_PER_CELSIUS,
451 .bdu = {
452 .addr = 0x10,
453 .mask = 0x02,
455 .drdy_irq = {
456 .addr = 0x12,
457 .mask_int1 = 0x04,
458 .mask_int2 = 0x00,
459 .addr_ihl = 0x12,
460 .mask_ihl = 0x80,
461 .addr_od = 0x12,
462 .mask_od = 0x40,
463 .stat_drdy = {
464 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
465 .mask = 0x03,
468 .multi_read_bit = false,
469 .bootime = 2,
473 static int st_press_write_raw(struct iio_dev *indio_dev,
474 struct iio_chan_spec const *ch,
475 int val,
476 int val2,
477 long mask)
479 int err;
481 switch (mask) {
482 case IIO_CHAN_INFO_SAMP_FREQ:
483 if (val2)
484 return -EINVAL;
485 mutex_lock(&indio_dev->mlock);
486 err = st_sensors_set_odr(indio_dev, val);
487 mutex_unlock(&indio_dev->mlock);
488 return err;
489 default:
490 return -EINVAL;
494 static int st_press_read_raw(struct iio_dev *indio_dev,
495 struct iio_chan_spec const *ch, int *val,
496 int *val2, long mask)
498 int err;
499 struct st_sensor_data *press_data = iio_priv(indio_dev);
501 switch (mask) {
502 case IIO_CHAN_INFO_RAW:
503 err = st_sensors_read_info_raw(indio_dev, ch, val);
504 if (err < 0)
505 goto read_error;
507 return IIO_VAL_INT;
508 case IIO_CHAN_INFO_SCALE:
509 switch (ch->type) {
510 case IIO_PRESSURE:
511 *val = 0;
512 *val2 = press_data->current_fullscale->gain;
513 return IIO_VAL_INT_PLUS_NANO;
514 case IIO_TEMP:
515 *val = MCELSIUS_PER_CELSIUS;
516 *val2 = press_data->current_fullscale->gain2;
517 return IIO_VAL_FRACTIONAL;
518 default:
519 err = -EINVAL;
520 goto read_error;
523 case IIO_CHAN_INFO_OFFSET:
524 switch (ch->type) {
525 case IIO_TEMP:
526 *val = ST_PRESS_MILLI_CELSIUS_OFFSET *
527 press_data->current_fullscale->gain2;
528 *val2 = MCELSIUS_PER_CELSIUS;
529 break;
530 default:
531 err = -EINVAL;
532 goto read_error;
535 return IIO_VAL_FRACTIONAL;
536 case IIO_CHAN_INFO_SAMP_FREQ:
537 *val = press_data->odr;
538 return IIO_VAL_INT;
539 default:
540 return -EINVAL;
543 read_error:
544 return err;
547 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
549 static struct attribute *st_press_attributes[] = {
550 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
551 NULL,
554 static const struct attribute_group st_press_attribute_group = {
555 .attrs = st_press_attributes,
558 static const struct iio_info press_info = {
559 .driver_module = THIS_MODULE,
560 .attrs = &st_press_attribute_group,
561 .read_raw = &st_press_read_raw,
562 .write_raw = &st_press_write_raw,
563 .debugfs_reg_access = &st_sensors_debugfs_reg_access,
566 #ifdef CONFIG_IIO_TRIGGER
567 static const struct iio_trigger_ops st_press_trigger_ops = {
568 .owner = THIS_MODULE,
569 .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
570 .validate_device = st_sensors_validate_device,
572 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
573 #else
574 #define ST_PRESS_TRIGGER_OPS NULL
575 #endif
577 int st_press_common_probe(struct iio_dev *indio_dev)
579 struct st_sensor_data *press_data = iio_priv(indio_dev);
580 struct st_sensors_platform_data *pdata =
581 (struct st_sensors_platform_data *)press_data->dev->platform_data;
582 int irq = press_data->get_irq_data_ready(indio_dev);
583 int err;
585 indio_dev->modes = INDIO_DIRECT_MODE;
586 indio_dev->info = &press_info;
587 mutex_init(&press_data->tb.buf_lock);
589 err = st_sensors_power_enable(indio_dev);
590 if (err)
591 return err;
593 err = st_sensors_check_device_support(indio_dev,
594 ARRAY_SIZE(st_press_sensors_settings),
595 st_press_sensors_settings);
596 if (err < 0)
597 goto st_press_power_off;
600 * Skip timestamping channel while declaring available channels to
601 * common st_sensor layer. Look at st_sensors_get_buffer_element() to
602 * see how timestamps are explicitly pushed as last samples block
603 * element.
605 press_data->num_data_channels = press_data->sensor_settings->num_ch - 1;
606 press_data->multiread_bit = press_data->sensor_settings->multi_read_bit;
607 indio_dev->channels = press_data->sensor_settings->ch;
608 indio_dev->num_channels = press_data->sensor_settings->num_ch;
610 press_data->current_fullscale =
611 (struct st_sensor_fullscale_avl *)
612 &press_data->sensor_settings->fs.fs_avl[0];
614 press_data->odr = press_data->sensor_settings->odr.odr_avl[0].hz;
616 /* Some devices don't support a data ready pin. */
617 if (!pdata && press_data->sensor_settings->drdy_irq.addr)
618 pdata = (struct st_sensors_platform_data *)&default_press_pdata;
620 err = st_sensors_init_sensor(indio_dev, pdata);
621 if (err < 0)
622 goto st_press_power_off;
624 err = st_press_allocate_ring(indio_dev);
625 if (err < 0)
626 goto st_press_power_off;
628 if (irq > 0) {
629 err = st_sensors_allocate_trigger(indio_dev,
630 ST_PRESS_TRIGGER_OPS);
631 if (err < 0)
632 goto st_press_probe_trigger_error;
635 err = iio_device_register(indio_dev);
636 if (err)
637 goto st_press_device_register_error;
639 dev_info(&indio_dev->dev, "registered pressure sensor %s\n",
640 indio_dev->name);
642 return err;
644 st_press_device_register_error:
645 if (irq > 0)
646 st_sensors_deallocate_trigger(indio_dev);
647 st_press_probe_trigger_error:
648 st_press_deallocate_ring(indio_dev);
649 st_press_power_off:
650 st_sensors_power_disable(indio_dev);
652 return err;
654 EXPORT_SYMBOL(st_press_common_probe);
656 void st_press_common_remove(struct iio_dev *indio_dev)
658 struct st_sensor_data *press_data = iio_priv(indio_dev);
660 st_sensors_power_disable(indio_dev);
662 iio_device_unregister(indio_dev);
663 if (press_data->get_irq_data_ready(indio_dev) > 0)
664 st_sensors_deallocate_trigger(indio_dev);
666 st_press_deallocate_ring(indio_dev);
668 EXPORT_SYMBOL(st_press_common_remove);
670 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
671 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
672 MODULE_LICENSE("GPL v2");