2 * STMicroelectronics pressures driver
4 * Copyright 2013 STMicroelectronics Inc.
6 * Denis Ciocca <denis.ciocca@st.com>
8 * Licensed under the GPL-2.
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
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)
60 * raw the 16 bits long raw sampled temperature
61 * offset a constant specified by the datasheet in degree Celsius
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
74 * temp[mCelsius] = (OFFSET + raw) * SCALE
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 :
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
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
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
,
142 .endianness
= IIO_LE
,
144 .info_mask_separate
=
145 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
149 .address
= ST_TEMP_1_OUT_L_ADDR
,
155 .endianness
= IIO_LE
,
157 .info_mask_separate
=
158 BIT(IIO_CHAN_INFO_RAW
) |
159 BIT(IIO_CHAN_INFO_SCALE
) |
160 BIT(IIO_CHAN_INFO_OFFSET
),
162 IIO_CHAN_SOFT_TIMESTAMP(2)
165 static const struct iio_chan_spec st_press_lps001wp_channels
[] = {
167 .type
= IIO_PRESSURE
,
168 .address
= ST_PRESS_LPS001WP_OUT_L_ADDR
,
174 .endianness
= IIO_LE
,
176 .info_mask_separate
=
177 BIT(IIO_CHAN_INFO_RAW
) |
178 BIT(IIO_CHAN_INFO_SCALE
),
182 .address
= ST_TEMP_LPS001WP_OUT_L_ADDR
,
188 .endianness
= IIO_LE
,
190 .info_mask_separate
=
191 BIT(IIO_CHAN_INFO_RAW
) |
192 BIT(IIO_CHAN_INFO_SCALE
),
194 IIO_CHAN_SOFT_TIMESTAMP(2)
197 static const struct iio_chan_spec st_press_lps22hb_channels
[] = {
199 .type
= IIO_PRESSURE
,
200 .address
= ST_PRESS_1_OUT_XL_ADDR
,
206 .endianness
= IIO_LE
,
208 .info_mask_separate
=
209 BIT(IIO_CHAN_INFO_RAW
) |
210 BIT(IIO_CHAN_INFO_SCALE
),
211 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
215 .address
= ST_TEMP_1_OUT_L_ADDR
,
221 .endianness
= IIO_LE
,
223 .info_mask_separate
=
224 BIT(IIO_CHAN_INFO_RAW
) |
225 BIT(IIO_CHAN_INFO_SCALE
),
226 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
228 IIO_CHAN_SOFT_TIMESTAMP(2)
231 static const struct st_sensor_settings st_press_sensors_settings
[] = {
234 * CUSTOM VALUES FOR LPS331AP SENSOR
235 * See LPS331AP datasheet:
236 * http://www2.st.com/resource/en/datasheet/lps331ap.pdf
239 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
240 .sensors_supported
= {
241 [0] = LPS331AP_PRESS_DEV_NAME
,
243 .ch
= (struct iio_chan_spec
*)st_press_1_channels
,
244 .num_ch
= ARRAY_SIZE(st_press_1_channels
),
249 { .hz
= 1, .value
= 0x01 },
250 { .hz
= 7, .value
= 0x05 },
251 { .hz
= 13, .value
= 0x06 },
252 { .hz
= 25, .value
= 0x07 },
258 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
259 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
266 * Pressure and temperature sensitivity values
267 * as defined in table 3 of LPS331AP datasheet.
270 .num
= ST_PRESS_FS_AVL_1260MB
,
271 .gain
= ST_PRESS_KPASCAL_NANO_SCALE
,
272 .gain2
= ST_PRESS_LSB_PER_CELSIUS
,
288 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
290 .multi_read_bit
= true,
295 * CUSTOM VALUES FOR LPS001WP SENSOR
298 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
299 .sensors_supported
= {
300 [0] = LPS001WP_PRESS_DEV_NAME
,
302 .ch
= (struct iio_chan_spec
*)st_press_lps001wp_channels
,
303 .num_ch
= ARRAY_SIZE(st_press_lps001wp_channels
),
308 { .hz
= 1, .value
= 0x01 },
309 { .hz
= 7, .value
= 0x02 },
310 { .hz
= 13, .value
= 0x03 },
316 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
317 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
322 * Pressure and temperature resolution values
323 * as defined in table 3 of LPS001WP datasheet.
326 .num
= ST_PRESS_FS_AVL_1100MB
,
327 .gain
= ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN
,
328 .gain2
= ST_PRESS_LPS001WP_LSB_PER_CELSIUS
,
339 .multi_read_bit
= true,
344 * CUSTOM VALUES FOR LPS25H SENSOR
345 * See LPS25H datasheet:
346 * http://www2.st.com/resource/en/datasheet/lps25h.pdf
349 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
350 .sensors_supported
= {
351 [0] = LPS25H_PRESS_DEV_NAME
,
353 .ch
= (struct iio_chan_spec
*)st_press_1_channels
,
354 .num_ch
= ARRAY_SIZE(st_press_1_channels
),
359 { .hz
= 1, .value
= 0x01 },
360 { .hz
= 7, .value
= 0x02 },
361 { .hz
= 13, .value
= 0x03 },
362 { .hz
= 25, .value
= 0x04 },
368 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
369 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
374 * Pressure and temperature sensitivity values
375 * as defined in table 3 of LPS25H datasheet.
378 .num
= ST_PRESS_FS_AVL_1260MB
,
379 .gain
= ST_PRESS_KPASCAL_NANO_SCALE
,
380 .gain2
= ST_PRESS_LSB_PER_CELSIUS
,
396 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
398 .multi_read_bit
= true,
403 * CUSTOM VALUES FOR LPS22HB SENSOR
404 * See LPS22HB datasheet:
405 * http://www2.st.com/resource/en/datasheet/lps22hb.pdf
408 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
409 .sensors_supported
= {
410 [0] = LPS22HB_PRESS_DEV_NAME
,
412 .ch
= (struct iio_chan_spec
*)st_press_lps22hb_channels
,
413 .num_ch
= ARRAY_SIZE(st_press_lps22hb_channels
),
418 { .hz
= 1, .value
= 0x01 },
419 { .hz
= 10, .value
= 0x02 },
420 { .hz
= 25, .value
= 0x03 },
421 { .hz
= 50, .value
= 0x04 },
422 { .hz
= 75, .value
= 0x05 },
428 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
433 * Pressure and temperature sensitivity values
434 * as defined in table 3 of LPS22HB datasheet.
437 .num
= ST_PRESS_FS_AVL_1260MB
,
438 .gain
= ST_PRESS_KPASCAL_NANO_SCALE
,
439 .gain2
= ST_PRESS_LPS22HB_LSB_PER_CELSIUS
,
455 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
457 .multi_read_bit
= true,
461 static int st_press_write_raw(struct iio_dev
*indio_dev
,
462 struct iio_chan_spec
const *ch
,
470 case IIO_CHAN_INFO_SAMP_FREQ
:
473 mutex_lock(&indio_dev
->mlock
);
474 err
= st_sensors_set_odr(indio_dev
, val
);
475 mutex_unlock(&indio_dev
->mlock
);
482 static int st_press_read_raw(struct iio_dev
*indio_dev
,
483 struct iio_chan_spec
const *ch
, int *val
,
484 int *val2
, long mask
)
487 struct st_sensor_data
*press_data
= iio_priv(indio_dev
);
490 case IIO_CHAN_INFO_RAW
:
491 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
496 case IIO_CHAN_INFO_SCALE
:
500 *val2
= press_data
->current_fullscale
->gain
;
501 return IIO_VAL_INT_PLUS_NANO
;
503 *val
= MCELSIUS_PER_CELSIUS
;
504 *val2
= press_data
->current_fullscale
->gain2
;
505 return IIO_VAL_FRACTIONAL
;
511 case IIO_CHAN_INFO_OFFSET
:
514 *val
= ST_PRESS_MILLI_CELSIUS_OFFSET
*
515 press_data
->current_fullscale
->gain2
;
516 *val2
= MCELSIUS_PER_CELSIUS
;
523 return IIO_VAL_FRACTIONAL
;
524 case IIO_CHAN_INFO_SAMP_FREQ
:
525 *val
= press_data
->odr
;
535 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
537 static struct attribute
*st_press_attributes
[] = {
538 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
542 static const struct attribute_group st_press_attribute_group
= {
543 .attrs
= st_press_attributes
,
546 static const struct iio_info press_info
= {
547 .driver_module
= THIS_MODULE
,
548 .attrs
= &st_press_attribute_group
,
549 .read_raw
= &st_press_read_raw
,
550 .write_raw
= &st_press_write_raw
,
551 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
554 #ifdef CONFIG_IIO_TRIGGER
555 static const struct iio_trigger_ops st_press_trigger_ops
= {
556 .owner
= THIS_MODULE
,
557 .set_trigger_state
= ST_PRESS_TRIGGER_SET_STATE
,
558 .validate_device
= st_sensors_validate_device
,
560 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
562 #define ST_PRESS_TRIGGER_OPS NULL
565 int st_press_common_probe(struct iio_dev
*indio_dev
)
567 struct st_sensor_data
*press_data
= iio_priv(indio_dev
);
568 int irq
= press_data
->get_irq_data_ready(indio_dev
);
571 indio_dev
->modes
= INDIO_DIRECT_MODE
;
572 indio_dev
->info
= &press_info
;
573 mutex_init(&press_data
->tb
.buf_lock
);
575 err
= st_sensors_power_enable(indio_dev
);
579 err
= st_sensors_check_device_support(indio_dev
,
580 ARRAY_SIZE(st_press_sensors_settings
),
581 st_press_sensors_settings
);
583 goto st_press_power_off
;
586 * Skip timestamping channel while declaring available channels to
587 * common st_sensor layer. Look at st_sensors_get_buffer_element() to
588 * see how timestamps are explicitly pushed as last samples block
591 press_data
->num_data_channels
= press_data
->sensor_settings
->num_ch
- 1;
592 press_data
->multiread_bit
= press_data
->sensor_settings
->multi_read_bit
;
593 indio_dev
->channels
= press_data
->sensor_settings
->ch
;
594 indio_dev
->num_channels
= press_data
->sensor_settings
->num_ch
;
596 press_data
->current_fullscale
=
597 (struct st_sensor_fullscale_avl
*)
598 &press_data
->sensor_settings
->fs
.fs_avl
[0];
600 press_data
->odr
= press_data
->sensor_settings
->odr
.odr_avl
[0].hz
;
602 /* Some devices don't support a data ready pin. */
603 if (!press_data
->dev
->platform_data
&&
604 press_data
->sensor_settings
->drdy_irq
.addr
)
605 press_data
->dev
->platform_data
=
606 (struct st_sensors_platform_data
*)&default_press_pdata
;
608 err
= st_sensors_init_sensor(indio_dev
, press_data
->dev
->platform_data
);
610 goto st_press_power_off
;
612 err
= st_press_allocate_ring(indio_dev
);
614 goto st_press_power_off
;
617 err
= st_sensors_allocate_trigger(indio_dev
,
618 ST_PRESS_TRIGGER_OPS
);
620 goto st_press_probe_trigger_error
;
623 err
= iio_device_register(indio_dev
);
625 goto st_press_device_register_error
;
627 dev_info(&indio_dev
->dev
, "registered pressure sensor %s\n",
632 st_press_device_register_error
:
634 st_sensors_deallocate_trigger(indio_dev
);
635 st_press_probe_trigger_error
:
636 st_press_deallocate_ring(indio_dev
);
638 st_sensors_power_disable(indio_dev
);
642 EXPORT_SYMBOL(st_press_common_probe
);
644 void st_press_common_remove(struct iio_dev
*indio_dev
)
646 struct st_sensor_data
*press_data
= iio_priv(indio_dev
);
648 st_sensors_power_disable(indio_dev
);
650 iio_device_unregister(indio_dev
);
651 if (press_data
->get_irq_data_ready(indio_dev
) > 0)
652 st_sensors_deallocate_trigger(indio_dev
);
654 st_press_deallocate_ring(indio_dev
);
656 EXPORT_SYMBOL(st_press_common_remove
);
658 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
659 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
660 MODULE_LICENSE("GPL v2");