1 // SPDX-License-Identifier: GPL-2.0-only
3 * STMicroelectronics pressures driver
5 * Copyright 2013 STMicroelectronics Inc.
7 * Denis Ciocca <denis.ciocca@st.com>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/errno.h>
14 #include <linux/types.h>
15 #include <linux/interrupt.h>
16 #include <linux/i2c.h>
17 #include <linux/irq.h>
18 #include <linux/delay.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/buffer.h>
23 #include <asm/unaligned.h>
25 #include <linux/iio/common/st_sensors.h>
26 #include "st_pressure.h"
29 * About determining pressure scaling factors
30 * ------------------------------------------
32 * Datasheets specify typical pressure sensitivity so that pressure is computed
33 * according to the following equation :
34 * pressure[mBar] = raw / sensitivity
36 * raw the 24 bits long raw sampled pressure
37 * sensitivity a scaling factor specified by the datasheet in LSB/mBar
39 * IIO ABI expects pressure to be expressed as kPascal, hence pressure should be
40 * computed according to :
41 * pressure[kPascal] = pressure[mBar] / 10
42 * = raw / (sensitivity * 10) (1)
44 * Finally, st_press_read_raw() returns pressure scaling factor as an
45 * IIO_VAL_INT_PLUS_NANO with a zero integral part and "gain" as decimal part.
46 * Therefore, from (1), "gain" becomes :
47 * gain = 10^9 / (sensitivity * 10)
48 * = 10^8 / sensitivity
50 * About determining temperature scaling factors and offsets
51 * ---------------------------------------------------------
53 * Datasheets specify typical temperature sensitivity and offset so that
54 * temperature is computed according to the following equation :
55 * temp[Celsius] = offset[Celsius] + (raw / sensitivity)
57 * raw the 16 bits long raw sampled temperature
58 * offset a constant specified by the datasheet in degree Celsius
60 * sensitivity a scaling factor specified by the datasheet in LSB/Celsius
62 * IIO ABI expects temperature to be expressed as milli degree Celsius such as
63 * user space should compute temperature according to :
64 * temp[mCelsius] = temp[Celsius] * 10^3
65 * = (offset[Celsius] + (raw / sensitivity)) * 10^3
66 * = ((offset[Celsius] * sensitivity) + raw) *
67 * (10^3 / sensitivity) (2)
69 * IIO ABI expects user space to apply offset and scaling factors to raw samples
71 * temp[mCelsius] = (OFFSET + raw) * SCALE
73 * OFFSET an arbitrary constant exposed by device
74 * SCALE an arbitrary scaling factor exposed by device
76 * Matching OFFSET and SCALE with members of (2) gives :
77 * OFFSET = offset[Celsius] * sensitivity (3)
78 * SCALE = 10^3 / sensitivity (4)
80 * st_press_read_raw() returns temperature scaling factor as an
81 * IIO_VAL_FRACTIONAL with a 10^3 numerator and "gain2" as denominator.
82 * Therefore, from (3), "gain2" becomes :
85 * When declared within channel, i.e. for a non zero specified offset,
86 * st_press_read_raw() will return the latter as an IIO_VAL_FRACTIONAL such as :
87 * numerator = OFFSET * 10^3
90 * numerator = offset[Celsius] * 10^3 * sensitivity
91 * = offset[mCelsius] * gain2
94 #define MCELSIUS_PER_CELSIUS 1000
96 /* Default pressure sensitivity */
97 #define ST_PRESS_LSB_PER_MBAR 4096UL
98 #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \
99 ST_PRESS_LSB_PER_MBAR)
101 /* Default temperature sensitivity */
102 #define ST_PRESS_LSB_PER_CELSIUS 480UL
103 #define ST_PRESS_MILLI_CELSIUS_OFFSET 42500UL
106 #define ST_PRESS_FS_AVL_1100MB 1100
107 #define ST_PRESS_FS_AVL_1260MB 1260
109 #define ST_PRESS_1_OUT_XL_ADDR 0x28
110 #define ST_TEMP_1_OUT_L_ADDR 0x2b
112 /* LPS001WP pressure resolution */
113 #define ST_PRESS_LPS001WP_LSB_PER_MBAR 16UL
114 /* LPS001WP temperature resolution */
115 #define ST_PRESS_LPS001WP_LSB_PER_CELSIUS 64UL
116 /* LPS001WP pressure gain */
117 #define ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN \
118 (100000000UL / ST_PRESS_LPS001WP_LSB_PER_MBAR)
119 /* LPS001WP pressure and temp L addresses */
120 #define ST_PRESS_LPS001WP_OUT_L_ADDR 0x28
121 #define ST_TEMP_LPS001WP_OUT_L_ADDR 0x2a
123 /* LPS25H pressure and temp L addresses */
124 #define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28
125 #define ST_TEMP_LPS25H_OUT_L_ADDR 0x2b
127 /* LPS22HB temperature sensitivity */
128 #define ST_PRESS_LPS22HB_LSB_PER_CELSIUS 100UL
130 static const struct iio_chan_spec st_press_1_channels
[] = {
132 .type
= IIO_PRESSURE
,
133 .address
= ST_PRESS_1_OUT_XL_ADDR
,
139 .endianness
= IIO_LE
,
141 .info_mask_separate
=
142 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
143 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
147 .address
= ST_TEMP_1_OUT_L_ADDR
,
153 .endianness
= IIO_LE
,
155 .info_mask_separate
=
156 BIT(IIO_CHAN_INFO_RAW
) |
157 BIT(IIO_CHAN_INFO_SCALE
) |
158 BIT(IIO_CHAN_INFO_OFFSET
),
159 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
161 IIO_CHAN_SOFT_TIMESTAMP(2)
164 static const struct iio_chan_spec st_press_lps001wp_channels
[] = {
166 .type
= IIO_PRESSURE
,
167 .address
= ST_PRESS_LPS001WP_OUT_L_ADDR
,
173 .endianness
= IIO_LE
,
175 .info_mask_separate
=
176 BIT(IIO_CHAN_INFO_RAW
) |
177 BIT(IIO_CHAN_INFO_SCALE
),
181 .address
= ST_TEMP_LPS001WP_OUT_L_ADDR
,
187 .endianness
= IIO_LE
,
189 .info_mask_separate
=
190 BIT(IIO_CHAN_INFO_RAW
) |
191 BIT(IIO_CHAN_INFO_SCALE
),
193 IIO_CHAN_SOFT_TIMESTAMP(2)
196 static const struct iio_chan_spec st_press_lps22hb_channels
[] = {
198 .type
= IIO_PRESSURE
,
199 .address
= ST_PRESS_1_OUT_XL_ADDR
,
205 .endianness
= IIO_LE
,
207 .info_mask_separate
=
208 BIT(IIO_CHAN_INFO_RAW
) |
209 BIT(IIO_CHAN_INFO_SCALE
),
210 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
214 .address
= ST_TEMP_1_OUT_L_ADDR
,
220 .endianness
= IIO_LE
,
222 .info_mask_separate
=
223 BIT(IIO_CHAN_INFO_RAW
) |
224 BIT(IIO_CHAN_INFO_SCALE
),
225 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
227 IIO_CHAN_SOFT_TIMESTAMP(2)
230 static const struct st_sensor_settings st_press_sensors_settings
[] = {
233 * CUSTOM VALUES FOR LPS331AP SENSOR
234 * See LPS331AP datasheet:
235 * http://www2.st.com/resource/en/datasheet/lps331ap.pdf
238 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
239 .sensors_supported
= {
240 [0] = LPS331AP_PRESS_DEV_NAME
,
242 .ch
= (struct iio_chan_spec
*)st_press_1_channels
,
243 .num_ch
= ARRAY_SIZE(st_press_1_channels
),
248 { .hz
= 1, .value
= 0x01 },
249 { .hz
= 7, .value
= 0x05 },
250 { .hz
= 13, .value
= 0x06 },
251 { .hz
= 25, .value
= 0x07 },
257 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
258 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
265 * Pressure and temperature sensitivity values
266 * as defined in table 3 of LPS331AP datasheet.
269 .num
= ST_PRESS_FS_AVL_1260MB
,
270 .gain
= ST_PRESS_KPASCAL_NANO_SCALE
,
271 .gain2
= ST_PRESS_LSB_PER_CELSIUS
,
295 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
303 .multi_read_bit
= true,
308 * CUSTOM VALUES FOR LPS001WP SENSOR
311 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
312 .sensors_supported
= {
313 [0] = LPS001WP_PRESS_DEV_NAME
,
315 .ch
= (struct iio_chan_spec
*)st_press_lps001wp_channels
,
316 .num_ch
= ARRAY_SIZE(st_press_lps001wp_channels
),
321 { .hz
= 1, .value
= 0x01 },
322 { .hz
= 7, .value
= 0x02 },
323 { .hz
= 13, .value
= 0x03 },
329 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
330 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
335 * Pressure and temperature resolution values
336 * as defined in table 3 of LPS001WP datasheet.
339 .num
= ST_PRESS_FS_AVL_1100MB
,
340 .gain
= ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN
,
341 .gain2
= ST_PRESS_LPS001WP_LSB_PER_CELSIUS
,
353 .multi_read_bit
= true,
358 * CUSTOM VALUES FOR LPS25H SENSOR
359 * See LPS25H datasheet:
360 * http://www2.st.com/resource/en/datasheet/lps25h.pdf
363 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
364 .sensors_supported
= {
365 [0] = LPS25H_PRESS_DEV_NAME
,
367 .ch
= (struct iio_chan_spec
*)st_press_1_channels
,
368 .num_ch
= ARRAY_SIZE(st_press_1_channels
),
373 { .hz
= 1, .value
= 0x01 },
374 { .hz
= 7, .value
= 0x02 },
375 { .hz
= 13, .value
= 0x03 },
376 { .hz
= 25, .value
= 0x04 },
382 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
383 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
388 * Pressure and temperature sensitivity values
389 * as defined in table 3 of LPS25H datasheet.
392 .num
= ST_PRESS_FS_AVL_1260MB
,
393 .gain
= ST_PRESS_KPASCAL_NANO_SCALE
,
394 .gain2
= ST_PRESS_LSB_PER_CELSIUS
,
412 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
420 .multi_read_bit
= true,
425 * CUSTOM VALUES FOR LPS22HB SENSOR
426 * See LPS22HB datasheet:
427 * http://www2.st.com/resource/en/datasheet/lps22hb.pdf
430 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
431 .sensors_supported
= {
432 [0] = LPS22HB_PRESS_DEV_NAME
,
433 [1] = LPS33HW_PRESS_DEV_NAME
,
434 [2] = LPS35HW_PRESS_DEV_NAME
,
436 .ch
= (struct iio_chan_spec
*)st_press_lps22hb_channels
,
437 .num_ch
= ARRAY_SIZE(st_press_lps22hb_channels
),
442 { .hz
= 1, .value
= 0x01 },
443 { .hz
= 10, .value
= 0x02 },
444 { .hz
= 25, .value
= 0x03 },
445 { .hz
= 50, .value
= 0x04 },
446 { .hz
= 75, .value
= 0x05 },
452 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
457 * Pressure and temperature sensitivity values
458 * as defined in table 3 of LPS22HB datasheet.
461 .num
= ST_PRESS_FS_AVL_1260MB
,
462 .gain
= ST_PRESS_KPASCAL_NANO_SCALE
,
463 .gain2
= ST_PRESS_LPS22HB_LSB_PER_CELSIUS
,
481 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
489 .multi_read_bit
= false,
494 * CUSTOM VALUES FOR LPS22HH SENSOR
495 * See LPS22HH datasheet:
496 * http://www2.st.com/resource/en/datasheet/lps22hh.pdf
499 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
500 .sensors_supported
= {
501 [0] = LPS22HH_PRESS_DEV_NAME
,
503 .ch
= (struct iio_chan_spec
*)st_press_lps22hb_channels
,
504 .num_ch
= ARRAY_SIZE(st_press_lps22hb_channels
),
509 { .hz
= 1, .value
= 0x01 },
510 { .hz
= 10, .value
= 0x02 },
511 { .hz
= 25, .value
= 0x03 },
512 { .hz
= 50, .value
= 0x04 },
513 { .hz
= 75, .value
= 0x05 },
514 { .hz
= 100, .value
= 0x06 },
515 { .hz
= 200, .value
= 0x07 },
521 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
526 * Pressure and temperature sensitivity values
527 * as defined in table 3 of LPS22HH datasheet.
530 .num
= ST_PRESS_FS_AVL_1260MB
,
531 .gain
= ST_PRESS_KPASCAL_NANO_SCALE
,
532 .gain2
= ST_PRESS_LPS22HB_LSB_PER_CELSIUS
,
550 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
558 .multi_read_bit
= false,
563 static int st_press_write_raw(struct iio_dev
*indio_dev
,
564 struct iio_chan_spec
const *ch
,
572 case IIO_CHAN_INFO_SAMP_FREQ
:
575 mutex_lock(&indio_dev
->mlock
);
576 err
= st_sensors_set_odr(indio_dev
, val
);
577 mutex_unlock(&indio_dev
->mlock
);
584 static int st_press_read_raw(struct iio_dev
*indio_dev
,
585 struct iio_chan_spec
const *ch
, int *val
,
586 int *val2
, long mask
)
589 struct st_sensor_data
*press_data
= iio_priv(indio_dev
);
592 case IIO_CHAN_INFO_RAW
:
593 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
598 case IIO_CHAN_INFO_SCALE
:
602 *val2
= press_data
->current_fullscale
->gain
;
603 return IIO_VAL_INT_PLUS_NANO
;
605 *val
= MCELSIUS_PER_CELSIUS
;
606 *val2
= press_data
->current_fullscale
->gain2
;
607 return IIO_VAL_FRACTIONAL
;
613 case IIO_CHAN_INFO_OFFSET
:
616 *val
= ST_PRESS_MILLI_CELSIUS_OFFSET
*
617 press_data
->current_fullscale
->gain2
;
618 *val2
= MCELSIUS_PER_CELSIUS
;
625 return IIO_VAL_FRACTIONAL
;
626 case IIO_CHAN_INFO_SAMP_FREQ
:
627 *val
= press_data
->odr
;
637 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
639 static struct attribute
*st_press_attributes
[] = {
640 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
644 static const struct attribute_group st_press_attribute_group
= {
645 .attrs
= st_press_attributes
,
648 static const struct iio_info press_info
= {
649 .attrs
= &st_press_attribute_group
,
650 .read_raw
= &st_press_read_raw
,
651 .write_raw
= &st_press_write_raw
,
652 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
655 #ifdef CONFIG_IIO_TRIGGER
656 static const struct iio_trigger_ops st_press_trigger_ops
= {
657 .set_trigger_state
= ST_PRESS_TRIGGER_SET_STATE
,
658 .validate_device
= st_sensors_validate_device
,
660 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
662 #define ST_PRESS_TRIGGER_OPS NULL
666 * st_press_get_settings() - get sensor settings from device name
667 * @name: device name buffer reference.
669 * Return: valid reference on success, NULL otherwise.
671 const struct st_sensor_settings
*st_press_get_settings(const char *name
)
673 int index
= st_sensors_get_settings_index(name
,
674 st_press_sensors_settings
,
675 ARRAY_SIZE(st_press_sensors_settings
));
679 return &st_press_sensors_settings
[index
];
681 EXPORT_SYMBOL(st_press_get_settings
);
683 int st_press_common_probe(struct iio_dev
*indio_dev
)
685 struct st_sensor_data
*press_data
= iio_priv(indio_dev
);
686 struct st_sensors_platform_data
*pdata
= dev_get_platdata(press_data
->dev
);
689 indio_dev
->modes
= INDIO_DIRECT_MODE
;
690 indio_dev
->info
= &press_info
;
692 err
= st_sensors_power_enable(indio_dev
);
696 err
= st_sensors_verify_id(indio_dev
);
698 goto st_press_power_off
;
701 * Skip timestamping channel while declaring available channels to
702 * common st_sensor layer. Look at st_sensors_get_buffer_element() to
703 * see how timestamps are explicitly pushed as last samples block
706 press_data
->num_data_channels
= press_data
->sensor_settings
->num_ch
- 1;
707 indio_dev
->channels
= press_data
->sensor_settings
->ch
;
708 indio_dev
->num_channels
= press_data
->sensor_settings
->num_ch
;
710 press_data
->current_fullscale
= &press_data
->sensor_settings
->fs
.fs_avl
[0];
712 press_data
->odr
= press_data
->sensor_settings
->odr
.odr_avl
[0].hz
;
714 /* Some devices don't support a data ready pin. */
715 if (!pdata
&& (press_data
->sensor_settings
->drdy_irq
.int1
.addr
||
716 press_data
->sensor_settings
->drdy_irq
.int2
.addr
))
717 pdata
= (struct st_sensors_platform_data
*)&default_press_pdata
;
719 err
= st_sensors_init_sensor(indio_dev
, pdata
);
721 goto st_press_power_off
;
723 err
= st_press_allocate_ring(indio_dev
);
725 goto st_press_power_off
;
727 if (press_data
->irq
> 0) {
728 err
= st_sensors_allocate_trigger(indio_dev
,
729 ST_PRESS_TRIGGER_OPS
);
731 goto st_press_probe_trigger_error
;
734 err
= iio_device_register(indio_dev
);
736 goto st_press_device_register_error
;
738 dev_info(&indio_dev
->dev
, "registered pressure sensor %s\n",
743 st_press_device_register_error
:
744 if (press_data
->irq
> 0)
745 st_sensors_deallocate_trigger(indio_dev
);
746 st_press_probe_trigger_error
:
747 st_press_deallocate_ring(indio_dev
);
749 st_sensors_power_disable(indio_dev
);
753 EXPORT_SYMBOL(st_press_common_probe
);
755 void st_press_common_remove(struct iio_dev
*indio_dev
)
757 struct st_sensor_data
*press_data
= iio_priv(indio_dev
);
759 st_sensors_power_disable(indio_dev
);
761 iio_device_unregister(indio_dev
);
762 if (press_data
->irq
> 0)
763 st_sensors_deallocate_trigger(indio_dev
);
765 st_press_deallocate_ring(indio_dev
);
767 EXPORT_SYMBOL(st_press_common_remove
);
769 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
770 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
771 MODULE_LICENSE("GPL v2");