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
),
146 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
150 .address
= ST_TEMP_1_OUT_L_ADDR
,
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
,
176 .endianness
= IIO_LE
,
178 .info_mask_separate
=
179 BIT(IIO_CHAN_INFO_RAW
) |
180 BIT(IIO_CHAN_INFO_SCALE
),
184 .address
= ST_TEMP_LPS001WP_OUT_L_ADDR
,
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
,
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
),
217 .address
= ST_TEMP_1_OUT_L_ADDR
,
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
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
),
251 { .hz
= 1, .value
= 0x01 },
252 { .hz
= 7, .value
= 0x05 },
253 { .hz
= 13, .value
= 0x06 },
254 { .hz
= 25, .value
= 0x07 },
260 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
261 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
268 * Pressure and temperature sensitivity values
269 * as defined in table 3 of LPS331AP datasheet.
272 .num
= ST_PRESS_FS_AVL_1260MB
,
273 .gain
= ST_PRESS_KPASCAL_NANO_SCALE
,
274 .gain2
= ST_PRESS_LSB_PER_CELSIUS
,
298 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
306 .multi_read_bit
= true,
311 * CUSTOM VALUES FOR LPS001WP SENSOR
314 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
315 .sensors_supported
= {
316 [0] = LPS001WP_PRESS_DEV_NAME
,
318 .ch
= (struct iio_chan_spec
*)st_press_lps001wp_channels
,
319 .num_ch
= ARRAY_SIZE(st_press_lps001wp_channels
),
324 { .hz
= 1, .value
= 0x01 },
325 { .hz
= 7, .value
= 0x02 },
326 { .hz
= 13, .value
= 0x03 },
332 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
333 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
338 * Pressure and temperature resolution values
339 * as defined in table 3 of LPS001WP datasheet.
342 .num
= ST_PRESS_FS_AVL_1100MB
,
343 .gain
= ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN
,
344 .gain2
= ST_PRESS_LPS001WP_LSB_PER_CELSIUS
,
356 .multi_read_bit
= true,
361 * CUSTOM VALUES FOR LPS25H SENSOR
362 * See LPS25H datasheet:
363 * http://www2.st.com/resource/en/datasheet/lps25h.pdf
366 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
367 .sensors_supported
= {
368 [0] = LPS25H_PRESS_DEV_NAME
,
370 .ch
= (struct iio_chan_spec
*)st_press_1_channels
,
371 .num_ch
= ARRAY_SIZE(st_press_1_channels
),
376 { .hz
= 1, .value
= 0x01 },
377 { .hz
= 7, .value
= 0x02 },
378 { .hz
= 13, .value
= 0x03 },
379 { .hz
= 25, .value
= 0x04 },
385 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
386 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
391 * Pressure and temperature sensitivity values
392 * as defined in table 3 of LPS25H datasheet.
395 .num
= ST_PRESS_FS_AVL_1260MB
,
396 .gain
= ST_PRESS_KPASCAL_NANO_SCALE
,
397 .gain2
= ST_PRESS_LSB_PER_CELSIUS
,
415 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
423 .multi_read_bit
= true,
428 * CUSTOM VALUES FOR LPS22HB SENSOR
429 * See LPS22HB datasheet:
430 * http://www2.st.com/resource/en/datasheet/lps22hb.pdf
433 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
434 .sensors_supported
= {
435 [0] = LPS22HB_PRESS_DEV_NAME
,
436 [1] = LPS33HW_PRESS_DEV_NAME
,
437 [2] = LPS35HW_PRESS_DEV_NAME
,
439 .ch
= (struct iio_chan_spec
*)st_press_lps22hb_channels
,
440 .num_ch
= ARRAY_SIZE(st_press_lps22hb_channels
),
445 { .hz
= 1, .value
= 0x01 },
446 { .hz
= 10, .value
= 0x02 },
447 { .hz
= 25, .value
= 0x03 },
448 { .hz
= 50, .value
= 0x04 },
449 { .hz
= 75, .value
= 0x05 },
455 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
460 * Pressure and temperature sensitivity values
461 * as defined in table 3 of LPS22HB datasheet.
464 .num
= ST_PRESS_FS_AVL_1260MB
,
465 .gain
= ST_PRESS_KPASCAL_NANO_SCALE
,
466 .gain2
= ST_PRESS_LPS22HB_LSB_PER_CELSIUS
,
484 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
492 .multi_read_bit
= false,
497 static int st_press_write_raw(struct iio_dev
*indio_dev
,
498 struct iio_chan_spec
const *ch
,
506 case IIO_CHAN_INFO_SAMP_FREQ
:
509 mutex_lock(&indio_dev
->mlock
);
510 err
= st_sensors_set_odr(indio_dev
, val
);
511 mutex_unlock(&indio_dev
->mlock
);
518 static int st_press_read_raw(struct iio_dev
*indio_dev
,
519 struct iio_chan_spec
const *ch
, int *val
,
520 int *val2
, long mask
)
523 struct st_sensor_data
*press_data
= iio_priv(indio_dev
);
526 case IIO_CHAN_INFO_RAW
:
527 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
532 case IIO_CHAN_INFO_SCALE
:
536 *val2
= press_data
->current_fullscale
->gain
;
537 return IIO_VAL_INT_PLUS_NANO
;
539 *val
= MCELSIUS_PER_CELSIUS
;
540 *val2
= press_data
->current_fullscale
->gain2
;
541 return IIO_VAL_FRACTIONAL
;
547 case IIO_CHAN_INFO_OFFSET
:
550 *val
= ST_PRESS_MILLI_CELSIUS_OFFSET
*
551 press_data
->current_fullscale
->gain2
;
552 *val2
= MCELSIUS_PER_CELSIUS
;
559 return IIO_VAL_FRACTIONAL
;
560 case IIO_CHAN_INFO_SAMP_FREQ
:
561 *val
= press_data
->odr
;
571 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
573 static struct attribute
*st_press_attributes
[] = {
574 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
578 static const struct attribute_group st_press_attribute_group
= {
579 .attrs
= st_press_attributes
,
582 static const struct iio_info press_info
= {
583 .attrs
= &st_press_attribute_group
,
584 .read_raw
= &st_press_read_raw
,
585 .write_raw
= &st_press_write_raw
,
586 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
589 #ifdef CONFIG_IIO_TRIGGER
590 static const struct iio_trigger_ops st_press_trigger_ops
= {
591 .set_trigger_state
= ST_PRESS_TRIGGER_SET_STATE
,
592 .validate_device
= st_sensors_validate_device
,
594 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
596 #define ST_PRESS_TRIGGER_OPS NULL
599 int st_press_common_probe(struct iio_dev
*indio_dev
)
601 struct st_sensor_data
*press_data
= iio_priv(indio_dev
);
602 struct st_sensors_platform_data
*pdata
=
603 (struct st_sensors_platform_data
*)press_data
->dev
->platform_data
;
604 int irq
= press_data
->get_irq_data_ready(indio_dev
);
607 indio_dev
->modes
= INDIO_DIRECT_MODE
;
608 indio_dev
->info
= &press_info
;
609 mutex_init(&press_data
->tb
.buf_lock
);
611 err
= st_sensors_power_enable(indio_dev
);
615 err
= st_sensors_check_device_support(indio_dev
,
616 ARRAY_SIZE(st_press_sensors_settings
),
617 st_press_sensors_settings
);
619 goto st_press_power_off
;
622 * Skip timestamping channel while declaring available channels to
623 * common st_sensor layer. Look at st_sensors_get_buffer_element() to
624 * see how timestamps are explicitly pushed as last samples block
627 press_data
->num_data_channels
= press_data
->sensor_settings
->num_ch
- 1;
628 press_data
->multiread_bit
= press_data
->sensor_settings
->multi_read_bit
;
629 indio_dev
->channels
= press_data
->sensor_settings
->ch
;
630 indio_dev
->num_channels
= press_data
->sensor_settings
->num_ch
;
632 press_data
->current_fullscale
=
633 (struct st_sensor_fullscale_avl
*)
634 &press_data
->sensor_settings
->fs
.fs_avl
[0];
636 press_data
->odr
= press_data
->sensor_settings
->odr
.odr_avl
[0].hz
;
638 /* Some devices don't support a data ready pin. */
639 if (!pdata
&& (press_data
->sensor_settings
->drdy_irq
.int1
.addr
||
640 press_data
->sensor_settings
->drdy_irq
.int2
.addr
))
641 pdata
= (struct st_sensors_platform_data
*)&default_press_pdata
;
643 err
= st_sensors_init_sensor(indio_dev
, pdata
);
645 goto st_press_power_off
;
647 err
= st_press_allocate_ring(indio_dev
);
649 goto st_press_power_off
;
652 err
= st_sensors_allocate_trigger(indio_dev
,
653 ST_PRESS_TRIGGER_OPS
);
655 goto st_press_probe_trigger_error
;
658 err
= iio_device_register(indio_dev
);
660 goto st_press_device_register_error
;
662 dev_info(&indio_dev
->dev
, "registered pressure sensor %s\n",
667 st_press_device_register_error
:
669 st_sensors_deallocate_trigger(indio_dev
);
670 st_press_probe_trigger_error
:
671 st_press_deallocate_ring(indio_dev
);
673 st_sensors_power_disable(indio_dev
);
677 EXPORT_SYMBOL(st_press_common_probe
);
679 void st_press_common_remove(struct iio_dev
*indio_dev
)
681 struct st_sensor_data
*press_data
= iio_priv(indio_dev
);
683 st_sensors_power_disable(indio_dev
);
685 iio_device_unregister(indio_dev
);
686 if (press_data
->get_irq_data_ready(indio_dev
) > 0)
687 st_sensors_deallocate_trigger(indio_dev
);
689 st_press_deallocate_ring(indio_dev
);
691 EXPORT_SYMBOL(st_press_common_remove
);
693 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
694 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
695 MODULE_LICENSE("GPL v2");