1 // SPDX-License-Identifier: GPL-2.0-only
3 * STMicroelectronics pressures driver
5 * Copyright 2013 STMicroelectronics Inc.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/sysfs.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/trigger.h>
17 #include <asm/unaligned.h>
19 #include <linux/iio/common/st_sensors.h>
20 #include "st_pressure.h"
23 * About determining pressure scaling factors
24 * ------------------------------------------
26 * Datasheets specify typical pressure sensitivity so that pressure is computed
27 * according to the following equation :
28 * pressure[mBar] = raw / sensitivity
30 * raw the 24 bits long raw sampled pressure
31 * sensitivity a scaling factor specified by the datasheet in LSB/mBar
33 * IIO ABI expects pressure to be expressed as kPascal, hence pressure should be
34 * computed according to :
35 * pressure[kPascal] = pressure[mBar] / 10
36 * = raw / (sensitivity * 10) (1)
38 * Finally, st_press_read_raw() returns pressure scaling factor as an
39 * IIO_VAL_INT_PLUS_NANO with a zero integral part and "gain" as decimal part.
40 * Therefore, from (1), "gain" becomes :
41 * gain = 10^9 / (sensitivity * 10)
42 * = 10^8 / sensitivity
44 * About determining temperature scaling factors and offsets
45 * ---------------------------------------------------------
47 * Datasheets specify typical temperature sensitivity and offset so that
48 * temperature is computed according to the following equation :
49 * temp[Celsius] = offset[Celsius] + (raw / sensitivity)
51 * raw the 16 bits long raw sampled temperature
52 * offset a constant specified by the datasheet in degree Celsius
54 * sensitivity a scaling factor specified by the datasheet in LSB/Celsius
56 * IIO ABI expects temperature to be expressed as milli degree Celsius such as
57 * user space should compute temperature according to :
58 * temp[mCelsius] = temp[Celsius] * 10^3
59 * = (offset[Celsius] + (raw / sensitivity)) * 10^3
60 * = ((offset[Celsius] * sensitivity) + raw) *
61 * (10^3 / sensitivity) (2)
63 * IIO ABI expects user space to apply offset and scaling factors to raw samples
65 * temp[mCelsius] = (OFFSET + raw) * SCALE
67 * OFFSET an arbitrary constant exposed by device
68 * SCALE an arbitrary scaling factor exposed by device
70 * Matching OFFSET and SCALE with members of (2) gives :
71 * OFFSET = offset[Celsius] * sensitivity (3)
72 * SCALE = 10^3 / sensitivity (4)
74 * st_press_read_raw() returns temperature scaling factor as an
75 * IIO_VAL_FRACTIONAL with a 10^3 numerator and "gain2" as denominator.
76 * Therefore, from (3), "gain2" becomes :
79 * When declared within channel, i.e. for a non zero specified offset,
80 * st_press_read_raw() will return the latter as an IIO_VAL_FRACTIONAL such as :
81 * numerator = OFFSET * 10^3
84 * numerator = offset[Celsius] * 10^3 * sensitivity
85 * = offset[mCelsius] * gain2
88 #define MCELSIUS_PER_CELSIUS 1000
90 /* Default pressure sensitivity */
91 #define ST_PRESS_LSB_PER_MBAR 4096UL
92 #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \
93 ST_PRESS_LSB_PER_MBAR)
95 /* Default temperature sensitivity */
96 #define ST_PRESS_LSB_PER_CELSIUS 480UL
97 #define ST_PRESS_MILLI_CELSIUS_OFFSET 42500UL
100 #define ST_PRESS_FS_AVL_1100MB 1100
101 #define ST_PRESS_FS_AVL_1260MB 1260
103 #define ST_PRESS_1_OUT_XL_ADDR 0x28
104 #define ST_TEMP_1_OUT_L_ADDR 0x2b
106 /* LPS001WP pressure resolution */
107 #define ST_PRESS_LPS001WP_LSB_PER_MBAR 16UL
108 /* LPS001WP temperature resolution */
109 #define ST_PRESS_LPS001WP_LSB_PER_CELSIUS 64UL
110 /* LPS001WP pressure gain */
111 #define ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN \
112 (100000000UL / ST_PRESS_LPS001WP_LSB_PER_MBAR)
113 /* LPS001WP pressure and temp L addresses */
114 #define ST_PRESS_LPS001WP_OUT_L_ADDR 0x28
115 #define ST_TEMP_LPS001WP_OUT_L_ADDR 0x2a
117 /* LPS25H pressure and temp L addresses */
118 #define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28
119 #define ST_TEMP_LPS25H_OUT_L_ADDR 0x2b
121 /* LPS22HB temperature sensitivity */
122 #define ST_PRESS_LPS22HB_LSB_PER_CELSIUS 100UL
124 static const struct iio_chan_spec st_press_1_channels[] = {
126 .type = IIO_PRESSURE,
127 .address = ST_PRESS_1_OUT_XL_ADDR,
133 .endianness = IIO_LE,
135 .info_mask_separate =
136 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
137 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
141 .address = ST_TEMP_1_OUT_L_ADDR,
147 .endianness = IIO_LE,
149 .info_mask_separate =
150 BIT(IIO_CHAN_INFO_RAW) |
151 BIT(IIO_CHAN_INFO_SCALE) |
152 BIT(IIO_CHAN_INFO_OFFSET),
153 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
155 IIO_CHAN_SOFT_TIMESTAMP(2)
158 static const struct iio_chan_spec st_press_lps001wp_channels[] = {
160 .type = IIO_PRESSURE,
161 .address = ST_PRESS_LPS001WP_OUT_L_ADDR,
167 .endianness = IIO_LE,
169 .info_mask_separate =
170 BIT(IIO_CHAN_INFO_RAW) |
171 BIT(IIO_CHAN_INFO_SCALE),
175 .address = ST_TEMP_LPS001WP_OUT_L_ADDR,
181 .endianness = IIO_LE,
183 .info_mask_separate =
184 BIT(IIO_CHAN_INFO_RAW) |
185 BIT(IIO_CHAN_INFO_SCALE),
187 IIO_CHAN_SOFT_TIMESTAMP(2)
190 static const struct iio_chan_spec st_press_lps22hb_channels[] = {
192 .type = IIO_PRESSURE,
193 .address = ST_PRESS_1_OUT_XL_ADDR,
199 .endianness = IIO_LE,
201 .info_mask_separate =
202 BIT(IIO_CHAN_INFO_RAW) |
203 BIT(IIO_CHAN_INFO_SCALE),
204 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
208 .address = ST_TEMP_1_OUT_L_ADDR,
214 .endianness = IIO_LE,
216 .info_mask_separate =
217 BIT(IIO_CHAN_INFO_RAW) |
218 BIT(IIO_CHAN_INFO_SCALE),
219 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
221 IIO_CHAN_SOFT_TIMESTAMP(2)
224 static const struct st_sensor_settings st_press_sensors_settings[] = {
227 * CUSTOM VALUES FOR LPS331AP SENSOR
228 * See LPS331AP datasheet:
229 * http://www2.st.com/resource/en/datasheet/lps331ap.pdf
232 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
233 .sensors_supported = {
234 [0] = LPS331AP_PRESS_DEV_NAME,
236 .ch = (struct iio_chan_spec *)st_press_1_channels,
237 .num_ch = ARRAY_SIZE(st_press_1_channels),
242 { .hz = 1, .value = 0x01 },
243 { .hz = 7, .value = 0x05 },
244 { .hz = 13, .value = 0x06 },
245 { .hz = 25, .value = 0x07 },
251 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
252 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
259 * Pressure and temperature sensitivity values
260 * as defined in table 3 of LPS331AP datasheet.
263 .num = ST_PRESS_FS_AVL_1260MB,
264 .gain = ST_PRESS_KPASCAL_NANO_SCALE,
265 .gain2 = ST_PRESS_LSB_PER_CELSIUS,
289 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
297 .multi_read_bit = true,
302 * CUSTOM VALUES FOR LPS001WP SENSOR
305 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
306 .sensors_supported = {
307 [0] = LPS001WP_PRESS_DEV_NAME,
309 .ch = (struct iio_chan_spec *)st_press_lps001wp_channels,
310 .num_ch = ARRAY_SIZE(st_press_lps001wp_channels),
315 { .hz = 1, .value = 0x01 },
316 { .hz = 7, .value = 0x02 },
317 { .hz = 13, .value = 0x03 },
323 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
324 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
329 * Pressure and temperature resolution values
330 * as defined in table 3 of LPS001WP datasheet.
333 .num = ST_PRESS_FS_AVL_1100MB,
334 .gain = ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN,
335 .gain2 = ST_PRESS_LPS001WP_LSB_PER_CELSIUS,
347 .multi_read_bit = true,
352 * CUSTOM VALUES FOR LPS25H SENSOR
353 * See LPS25H datasheet:
354 * http://www2.st.com/resource/en/datasheet/lps25h.pdf
357 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
358 .sensors_supported = {
359 [0] = LPS25H_PRESS_DEV_NAME,
361 .ch = (struct iio_chan_spec *)st_press_1_channels,
362 .num_ch = ARRAY_SIZE(st_press_1_channels),
367 { .hz = 1, .value = 0x01 },
368 { .hz = 7, .value = 0x02 },
369 { .hz = 13, .value = 0x03 },
370 { .hz = 25, .value = 0x04 },
376 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
377 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
382 * Pressure and temperature sensitivity values
383 * as defined in table 3 of LPS25H datasheet.
386 .num = ST_PRESS_FS_AVL_1260MB,
387 .gain = ST_PRESS_KPASCAL_NANO_SCALE,
388 .gain2 = ST_PRESS_LSB_PER_CELSIUS,
406 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
414 .multi_read_bit = true,
419 * CUSTOM VALUES FOR LPS22HB SENSOR
420 * See LPS22HB datasheet:
421 * http://www2.st.com/resource/en/datasheet/lps22hb.pdf
424 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
425 .sensors_supported = {
426 [0] = LPS22HB_PRESS_DEV_NAME,
427 [1] = LPS33HW_PRESS_DEV_NAME,
428 [2] = LPS35HW_PRESS_DEV_NAME,
430 .ch = (struct iio_chan_spec *)st_press_lps22hb_channels,
431 .num_ch = ARRAY_SIZE(st_press_lps22hb_channels),
436 { .hz = 1, .value = 0x01 },
437 { .hz = 10, .value = 0x02 },
438 { .hz = 25, .value = 0x03 },
439 { .hz = 50, .value = 0x04 },
440 { .hz = 75, .value = 0x05 },
446 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
451 * Pressure and temperature sensitivity values
452 * as defined in table 3 of LPS22HB datasheet.
455 .num = ST_PRESS_FS_AVL_1260MB,
456 .gain = ST_PRESS_KPASCAL_NANO_SCALE,
457 .gain2 = ST_PRESS_LPS22HB_LSB_PER_CELSIUS,
475 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
483 .multi_read_bit = false,
488 * CUSTOM VALUES FOR LPS22HH SENSOR
489 * See LPS22HH datasheet:
490 * http://www2.st.com/resource/en/datasheet/lps22hh.pdf
493 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
494 .sensors_supported = {
495 [0] = LPS22HH_PRESS_DEV_NAME,
497 .ch = (struct iio_chan_spec *)st_press_lps22hb_channels,
498 .num_ch = ARRAY_SIZE(st_press_lps22hb_channels),
503 { .hz = 1, .value = 0x01 },
504 { .hz = 10, .value = 0x02 },
505 { .hz = 25, .value = 0x03 },
506 { .hz = 50, .value = 0x04 },
507 { .hz = 75, .value = 0x05 },
508 { .hz = 100, .value = 0x06 },
509 { .hz = 200, .value = 0x07 },
515 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
520 * Pressure and temperature sensitivity values
521 * as defined in table 3 of LPS22HH datasheet.
524 .num = ST_PRESS_FS_AVL_1260MB,
525 .gain = ST_PRESS_KPASCAL_NANO_SCALE,
526 .gain2 = ST_PRESS_LPS22HB_LSB_PER_CELSIUS,
544 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
552 .multi_read_bit = false,
557 * CUSTOM VALUES FOR LPS22DF SENSOR
558 * See LPS22DF datasheet:
559 * http://www.st.com/resource/en/datasheet/lps22df.pdf
562 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
563 .sensors_supported = {
564 [0] = LPS22DF_PRESS_DEV_NAME,
566 .ch = (struct iio_chan_spec *)st_press_lps22hb_channels,
567 .num_ch = ARRAY_SIZE(st_press_lps22hb_channels),
572 { .hz = 1, .value = 0x01 },
573 { .hz = 4, .value = 0x02 },
574 { .hz = 10, .value = 0x03 },
575 { .hz = 25, .value = 0x04 },
576 { .hz = 50, .value = 0x05 },
577 { .hz = 75, .value = 0x06 },
578 { .hz = 100, .value = 0x07 },
579 { .hz = 200, .value = 0x08 },
585 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
590 * Pressure and temperature sensitivity values
591 * as defined in table 2 of LPS22DF datasheet.
594 .num = ST_PRESS_FS_AVL_1260MB,
595 .gain = ST_PRESS_KPASCAL_NANO_SCALE,
596 .gain2 = ST_PRESS_LPS22HB_LSB_PER_CELSIUS,
614 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
622 .multi_read_bit = false,
627 static int st_press_write_raw(struct iio_dev *indio_dev,
628 struct iio_chan_spec const *ch,
634 case IIO_CHAN_INFO_SAMP_FREQ:
638 return st_sensors_set_odr(indio_dev, val);
644 static int st_press_read_raw(struct iio_dev *indio_dev,
645 struct iio_chan_spec const *ch, int *val,
646 int *val2, long mask)
649 struct st_sensor_data *press_data = iio_priv(indio_dev);
652 case IIO_CHAN_INFO_RAW:
653 err = st_sensors_read_info_raw(indio_dev, ch, val);
658 case IIO_CHAN_INFO_SCALE:
662 *val2 = press_data->current_fullscale->gain;
663 return IIO_VAL_INT_PLUS_NANO;
665 *val = MCELSIUS_PER_CELSIUS;
666 *val2 = press_data->current_fullscale->gain2;
667 return IIO_VAL_FRACTIONAL;
673 case IIO_CHAN_INFO_OFFSET:
676 *val = ST_PRESS_MILLI_CELSIUS_OFFSET *
677 press_data->current_fullscale->gain2;
678 *val2 = MCELSIUS_PER_CELSIUS;
685 return IIO_VAL_FRACTIONAL;
686 case IIO_CHAN_INFO_SAMP_FREQ:
687 *val = press_data->odr;
697 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
699 static struct attribute *st_press_attributes[] = {
700 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
704 static const struct attribute_group st_press_attribute_group = {
705 .attrs = st_press_attributes,
708 static const struct iio_info press_info = {
709 .attrs = &st_press_attribute_group,
710 .read_raw = &st_press_read_raw,
711 .write_raw = &st_press_write_raw,
712 .debugfs_reg_access = &st_sensors_debugfs_reg_access,
715 #ifdef CONFIG_IIO_TRIGGER
716 static const struct iio_trigger_ops st_press_trigger_ops = {
717 .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
718 .validate_device = st_sensors_validate_device,
720 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
722 #define ST_PRESS_TRIGGER_OPS NULL
726 * st_press_get_settings() - get sensor settings from device name
727 * @name: device name buffer reference.
729 * Return: valid reference on success, NULL otherwise.
731 const struct st_sensor_settings *st_press_get_settings(const char *name)
733 int index = st_sensors_get_settings_index(name,
734 st_press_sensors_settings,
735 ARRAY_SIZE(st_press_sensors_settings));
739 return &st_press_sensors_settings[index];
741 EXPORT_SYMBOL_NS(st_press_get_settings, IIO_ST_SENSORS);
743 int st_press_common_probe(struct iio_dev *indio_dev)
745 struct st_sensor_data *press_data = iio_priv(indio_dev);
746 struct device *parent = indio_dev->dev.parent;
747 struct st_sensors_platform_data *pdata = dev_get_platdata(parent);
750 indio_dev->modes = INDIO_DIRECT_MODE;
751 indio_dev->info = &press_info;
753 err = st_sensors_verify_id(indio_dev);
758 * Skip timestamping channel while declaring available channels to
759 * common st_sensor layer. Look at st_sensors_get_buffer_element() to
760 * see how timestamps are explicitly pushed as last samples block
763 press_data->num_data_channels = press_data->sensor_settings->num_ch - 1;
764 indio_dev->channels = press_data->sensor_settings->ch;
765 indio_dev->num_channels = press_data->sensor_settings->num_ch;
767 press_data->current_fullscale = &press_data->sensor_settings->fs.fs_avl[0];
769 press_data->odr = press_data->sensor_settings->odr.odr_avl[0].hz;
771 /* Some devices don't support a data ready pin. */
772 if (!pdata && (press_data->sensor_settings->drdy_irq.int1.addr ||
773 press_data->sensor_settings->drdy_irq.int2.addr))
774 pdata = (struct st_sensors_platform_data *)&default_press_pdata;
776 err = st_sensors_init_sensor(indio_dev, pdata);
780 err = st_press_allocate_ring(indio_dev);
784 if (press_data->irq > 0) {
785 err = st_sensors_allocate_trigger(indio_dev,
786 ST_PRESS_TRIGGER_OPS);
791 return devm_iio_device_register(parent, indio_dev);
793 EXPORT_SYMBOL_NS(st_press_common_probe, IIO_ST_SENSORS);
796 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
797 MODULE_LICENSE("GPL v2");
798 MODULE_IMPORT_NS(IIO_ST_SENSORS);