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"
31 #define MCELSIUS_PER_CELSIUS 1000
33 /* Default pressure sensitivity */
34 #define ST_PRESS_LSB_PER_MBAR 4096UL
35 #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \
36 ST_PRESS_LSB_PER_MBAR)
38 /* Default temperature sensitivity */
39 #define ST_PRESS_LSB_PER_CELSIUS 480UL
40 #define ST_PRESS_MILLI_CELSIUS_OFFSET 42500UL
42 #define ST_PRESS_NUMBER_DATA_CHANNELS 1
45 #define ST_PRESS_FS_AVL_1100MB 1100
46 #define ST_PRESS_FS_AVL_1260MB 1260
48 #define ST_PRESS_1_OUT_XL_ADDR 0x28
49 #define ST_TEMP_1_OUT_L_ADDR 0x2b
51 /* CUSTOM VALUES FOR LPS331AP SENSOR */
52 #define ST_PRESS_LPS331AP_WAI_EXP 0xbb
53 #define ST_PRESS_LPS331AP_ODR_ADDR 0x20
54 #define ST_PRESS_LPS331AP_ODR_MASK 0x70
55 #define ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL 0x01
56 #define ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL 0x05
57 #define ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL 0x06
58 #define ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL 0x07
59 #define ST_PRESS_LPS331AP_PW_ADDR 0x20
60 #define ST_PRESS_LPS331AP_PW_MASK 0x80
61 #define ST_PRESS_LPS331AP_FS_ADDR 0x23
62 #define ST_PRESS_LPS331AP_FS_MASK 0x30
63 #define ST_PRESS_LPS331AP_BDU_ADDR 0x20
64 #define ST_PRESS_LPS331AP_BDU_MASK 0x04
65 #define ST_PRESS_LPS331AP_DRDY_IRQ_ADDR 0x22
66 #define ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK 0x04
67 #define ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK 0x20
68 #define ST_PRESS_LPS331AP_IHL_IRQ_ADDR 0x22
69 #define ST_PRESS_LPS331AP_IHL_IRQ_MASK 0x80
70 #define ST_PRESS_LPS331AP_OD_IRQ_ADDR 0x22
71 #define ST_PRESS_LPS331AP_OD_IRQ_MASK 0x40
72 #define ST_PRESS_LPS331AP_MULTIREAD_BIT true
74 /* CUSTOM VALUES FOR LPS001WP SENSOR */
76 /* LPS001WP pressure resolution */
77 #define ST_PRESS_LPS001WP_LSB_PER_MBAR 16UL
78 /* LPS001WP temperature resolution */
79 #define ST_PRESS_LPS001WP_LSB_PER_CELSIUS 64UL
81 #define ST_PRESS_LPS001WP_WAI_EXP 0xba
82 #define ST_PRESS_LPS001WP_ODR_ADDR 0x20
83 #define ST_PRESS_LPS001WP_ODR_MASK 0x30
84 #define ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL 0x01
85 #define ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL 0x02
86 #define ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL 0x03
87 #define ST_PRESS_LPS001WP_PW_ADDR 0x20
88 #define ST_PRESS_LPS001WP_PW_MASK 0x40
89 #define ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN \
90 (100000000UL / ST_PRESS_LPS001WP_LSB_PER_MBAR)
91 #define ST_PRESS_LPS001WP_BDU_ADDR 0x20
92 #define ST_PRESS_LPS001WP_BDU_MASK 0x04
93 #define ST_PRESS_LPS001WP_MULTIREAD_BIT true
94 #define ST_PRESS_LPS001WP_OUT_L_ADDR 0x28
95 #define ST_TEMP_LPS001WP_OUT_L_ADDR 0x2a
97 /* CUSTOM VALUES FOR LPS25H SENSOR */
98 #define ST_PRESS_LPS25H_WAI_EXP 0xbd
99 #define ST_PRESS_LPS25H_ODR_ADDR 0x20
100 #define ST_PRESS_LPS25H_ODR_MASK 0x70
101 #define ST_PRESS_LPS25H_ODR_AVL_1HZ_VAL 0x01
102 #define ST_PRESS_LPS25H_ODR_AVL_7HZ_VAL 0x02
103 #define ST_PRESS_LPS25H_ODR_AVL_13HZ_VAL 0x03
104 #define ST_PRESS_LPS25H_ODR_AVL_25HZ_VAL 0x04
105 #define ST_PRESS_LPS25H_PW_ADDR 0x20
106 #define ST_PRESS_LPS25H_PW_MASK 0x80
107 #define ST_PRESS_LPS25H_BDU_ADDR 0x20
108 #define ST_PRESS_LPS25H_BDU_MASK 0x04
109 #define ST_PRESS_LPS25H_DRDY_IRQ_ADDR 0x23
110 #define ST_PRESS_LPS25H_DRDY_IRQ_INT1_MASK 0x01
111 #define ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK 0x10
112 #define ST_PRESS_LPS25H_IHL_IRQ_ADDR 0x22
113 #define ST_PRESS_LPS25H_IHL_IRQ_MASK 0x80
114 #define ST_PRESS_LPS25H_OD_IRQ_ADDR 0x22
115 #define ST_PRESS_LPS25H_OD_IRQ_MASK 0x40
116 #define ST_PRESS_LPS25H_MULTIREAD_BIT true
117 #define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28
118 #define ST_TEMP_LPS25H_OUT_L_ADDR 0x2b
120 static const struct iio_chan_spec st_press_1_channels
[] = {
122 .type
= IIO_PRESSURE
,
123 .channel2
= IIO_NO_MOD
,
124 .address
= ST_PRESS_1_OUT_XL_ADDR
,
125 .scan_index
= ST_SENSORS_SCAN_X
,
130 .endianness
= IIO_LE
,
132 .info_mask_separate
=
133 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
138 .channel2
= IIO_NO_MOD
,
139 .address
= ST_TEMP_1_OUT_L_ADDR
,
145 .endianness
= IIO_LE
,
147 .info_mask_separate
=
148 BIT(IIO_CHAN_INFO_RAW
) |
149 BIT(IIO_CHAN_INFO_SCALE
) |
150 BIT(IIO_CHAN_INFO_OFFSET
),
153 IIO_CHAN_SOFT_TIMESTAMP(1)
156 static const struct iio_chan_spec st_press_lps001wp_channels
[] = {
158 .type
= IIO_PRESSURE
,
159 .channel2
= IIO_NO_MOD
,
160 .address
= ST_PRESS_LPS001WP_OUT_L_ADDR
,
161 .scan_index
= ST_SENSORS_SCAN_X
,
166 .endianness
= IIO_LE
,
168 .info_mask_separate
=
169 BIT(IIO_CHAN_INFO_RAW
) |
170 BIT(IIO_CHAN_INFO_SCALE
),
175 .channel2
= IIO_NO_MOD
,
176 .address
= ST_TEMP_LPS001WP_OUT_L_ADDR
,
182 .endianness
= IIO_LE
,
184 .info_mask_separate
=
185 BIT(IIO_CHAN_INFO_RAW
) |
186 BIT(IIO_CHAN_INFO_SCALE
),
189 IIO_CHAN_SOFT_TIMESTAMP(1)
192 static const struct st_sensor_settings st_press_sensors_settings
[] = {
194 .wai
= ST_PRESS_LPS331AP_WAI_EXP
,
195 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
196 .sensors_supported
= {
197 [0] = LPS331AP_PRESS_DEV_NAME
,
199 .ch
= (struct iio_chan_spec
*)st_press_1_channels
,
200 .num_ch
= ARRAY_SIZE(st_press_1_channels
),
202 .addr
= ST_PRESS_LPS331AP_ODR_ADDR
,
203 .mask
= ST_PRESS_LPS331AP_ODR_MASK
,
205 { 1, ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL
, },
206 { 7, ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL
, },
207 { 13, ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL
, },
208 { 25, ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL
, },
212 .addr
= ST_PRESS_LPS331AP_PW_ADDR
,
213 .mask
= ST_PRESS_LPS331AP_PW_MASK
,
214 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
215 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
218 .addr
= ST_PRESS_LPS331AP_FS_ADDR
,
219 .mask
= ST_PRESS_LPS331AP_FS_MASK
,
222 * Pressure and temperature sensitivity values
223 * as defined in table 3 of LPS331AP datasheet.
226 .num
= ST_PRESS_FS_AVL_1260MB
,
227 .gain
= ST_PRESS_KPASCAL_NANO_SCALE
,
228 .gain2
= ST_PRESS_LSB_PER_CELSIUS
,
233 .addr
= ST_PRESS_LPS331AP_BDU_ADDR
,
234 .mask
= ST_PRESS_LPS331AP_BDU_MASK
,
237 .addr
= ST_PRESS_LPS331AP_DRDY_IRQ_ADDR
,
238 .mask_int1
= ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK
,
239 .mask_int2
= ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK
,
240 .addr_ihl
= ST_PRESS_LPS331AP_IHL_IRQ_ADDR
,
241 .mask_ihl
= ST_PRESS_LPS331AP_IHL_IRQ_MASK
,
242 .addr_od
= ST_PRESS_LPS331AP_OD_IRQ_ADDR
,
243 .mask_od
= ST_PRESS_LPS331AP_OD_IRQ_MASK
,
244 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
246 .multi_read_bit
= ST_PRESS_LPS331AP_MULTIREAD_BIT
,
250 .wai
= ST_PRESS_LPS001WP_WAI_EXP
,
251 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
252 .sensors_supported
= {
253 [0] = LPS001WP_PRESS_DEV_NAME
,
255 .ch
= (struct iio_chan_spec
*)st_press_lps001wp_channels
,
256 .num_ch
= ARRAY_SIZE(st_press_lps001wp_channels
),
258 .addr
= ST_PRESS_LPS001WP_ODR_ADDR
,
259 .mask
= ST_PRESS_LPS001WP_ODR_MASK
,
261 { 1, ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL
, },
262 { 7, ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL
, },
263 { 13, ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL
, },
267 .addr
= ST_PRESS_LPS001WP_PW_ADDR
,
268 .mask
= ST_PRESS_LPS001WP_PW_MASK
,
269 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
270 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
275 * Pressure and temperature resolution values
276 * as defined in table 3 of LPS001WP datasheet.
279 .num
= ST_PRESS_FS_AVL_1100MB
,
280 .gain
= ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN
,
281 .gain2
= ST_PRESS_LPS001WP_LSB_PER_CELSIUS
,
286 .addr
= ST_PRESS_LPS001WP_BDU_ADDR
,
287 .mask
= ST_PRESS_LPS001WP_BDU_MASK
,
292 .multi_read_bit
= ST_PRESS_LPS001WP_MULTIREAD_BIT
,
296 .wai
= ST_PRESS_LPS25H_WAI_EXP
,
297 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
298 .sensors_supported
= {
299 [0] = LPS25H_PRESS_DEV_NAME
,
301 .ch
= (struct iio_chan_spec
*)st_press_1_channels
,
302 .num_ch
= ARRAY_SIZE(st_press_1_channels
),
304 .addr
= ST_PRESS_LPS25H_ODR_ADDR
,
305 .mask
= ST_PRESS_LPS25H_ODR_MASK
,
307 { 1, ST_PRESS_LPS25H_ODR_AVL_1HZ_VAL
, },
308 { 7, ST_PRESS_LPS25H_ODR_AVL_7HZ_VAL
, },
309 { 13, ST_PRESS_LPS25H_ODR_AVL_13HZ_VAL
, },
310 { 25, ST_PRESS_LPS25H_ODR_AVL_25HZ_VAL
, },
314 .addr
= ST_PRESS_LPS25H_PW_ADDR
,
315 .mask
= ST_PRESS_LPS25H_PW_MASK
,
316 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
317 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
322 * Pressure and temperature sensitivity values
323 * as defined in table 3 of LPS25H datasheet.
326 .num
= ST_PRESS_FS_AVL_1260MB
,
327 .gain
= ST_PRESS_KPASCAL_NANO_SCALE
,
328 .gain2
= ST_PRESS_LSB_PER_CELSIUS
,
333 .addr
= ST_PRESS_LPS25H_BDU_ADDR
,
334 .mask
= ST_PRESS_LPS25H_BDU_MASK
,
337 .addr
= ST_PRESS_LPS25H_DRDY_IRQ_ADDR
,
338 .mask_int1
= ST_PRESS_LPS25H_DRDY_IRQ_INT1_MASK
,
339 .mask_int2
= ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK
,
340 .addr_ihl
= ST_PRESS_LPS25H_IHL_IRQ_ADDR
,
341 .mask_ihl
= ST_PRESS_LPS25H_IHL_IRQ_MASK
,
342 .addr_od
= ST_PRESS_LPS25H_OD_IRQ_ADDR
,
343 .mask_od
= ST_PRESS_LPS25H_OD_IRQ_MASK
,
344 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
346 .multi_read_bit
= ST_PRESS_LPS25H_MULTIREAD_BIT
,
351 static int st_press_write_raw(struct iio_dev
*indio_dev
,
352 struct iio_chan_spec
const *ch
,
360 case IIO_CHAN_INFO_SAMP_FREQ
:
363 mutex_lock(&indio_dev
->mlock
);
364 err
= st_sensors_set_odr(indio_dev
, val
);
365 mutex_unlock(&indio_dev
->mlock
);
372 static int st_press_read_raw(struct iio_dev
*indio_dev
,
373 struct iio_chan_spec
const *ch
, int *val
,
374 int *val2
, long mask
)
377 struct st_sensor_data
*press_data
= iio_priv(indio_dev
);
380 case IIO_CHAN_INFO_RAW
:
381 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
386 case IIO_CHAN_INFO_SCALE
:
390 *val2
= press_data
->current_fullscale
->gain
;
391 return IIO_VAL_INT_PLUS_NANO
;
393 *val
= MCELSIUS_PER_CELSIUS
;
394 *val2
= press_data
->current_fullscale
->gain2
;
395 return IIO_VAL_FRACTIONAL
;
401 case IIO_CHAN_INFO_OFFSET
:
404 *val
= ST_PRESS_MILLI_CELSIUS_OFFSET
*
405 press_data
->current_fullscale
->gain2
;
406 *val2
= MCELSIUS_PER_CELSIUS
;
413 return IIO_VAL_FRACTIONAL
;
414 case IIO_CHAN_INFO_SAMP_FREQ
:
415 *val
= press_data
->odr
;
425 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
427 static struct attribute
*st_press_attributes
[] = {
428 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
432 static const struct attribute_group st_press_attribute_group
= {
433 .attrs
= st_press_attributes
,
436 static const struct iio_info press_info
= {
437 .driver_module
= THIS_MODULE
,
438 .attrs
= &st_press_attribute_group
,
439 .read_raw
= &st_press_read_raw
,
440 .write_raw
= &st_press_write_raw
,
441 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
444 #ifdef CONFIG_IIO_TRIGGER
445 static const struct iio_trigger_ops st_press_trigger_ops
= {
446 .owner
= THIS_MODULE
,
447 .set_trigger_state
= ST_PRESS_TRIGGER_SET_STATE
,
448 .validate_device
= st_sensors_validate_device
,
450 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
452 #define ST_PRESS_TRIGGER_OPS NULL
455 int st_press_common_probe(struct iio_dev
*indio_dev
)
457 struct st_sensor_data
*press_data
= iio_priv(indio_dev
);
458 int irq
= press_data
->get_irq_data_ready(indio_dev
);
461 indio_dev
->modes
= INDIO_DIRECT_MODE
;
462 indio_dev
->info
= &press_info
;
463 mutex_init(&press_data
->tb
.buf_lock
);
465 st_sensors_power_enable(indio_dev
);
467 err
= st_sensors_check_device_support(indio_dev
,
468 ARRAY_SIZE(st_press_sensors_settings
),
469 st_press_sensors_settings
);
473 press_data
->num_data_channels
= ST_PRESS_NUMBER_DATA_CHANNELS
;
474 press_data
->multiread_bit
= press_data
->sensor_settings
->multi_read_bit
;
475 indio_dev
->channels
= press_data
->sensor_settings
->ch
;
476 indio_dev
->num_channels
= press_data
->sensor_settings
->num_ch
;
478 if (press_data
->sensor_settings
->fs
.addr
!= 0)
479 press_data
->current_fullscale
=
480 (struct st_sensor_fullscale_avl
*)
481 &press_data
->sensor_settings
->fs
.fs_avl
[0];
483 press_data
->odr
= press_data
->sensor_settings
->odr
.odr_avl
[0].hz
;
485 /* Some devices don't support a data ready pin. */
486 if (!press_data
->dev
->platform_data
&&
487 press_data
->sensor_settings
->drdy_irq
.addr
)
488 press_data
->dev
->platform_data
=
489 (struct st_sensors_platform_data
*)&default_press_pdata
;
491 err
= st_sensors_init_sensor(indio_dev
, press_data
->dev
->platform_data
);
495 err
= st_press_allocate_ring(indio_dev
);
500 err
= st_sensors_allocate_trigger(indio_dev
,
501 ST_PRESS_TRIGGER_OPS
);
503 goto st_press_probe_trigger_error
;
506 err
= iio_device_register(indio_dev
);
508 goto st_press_device_register_error
;
510 dev_info(&indio_dev
->dev
, "registered pressure sensor %s\n",
515 st_press_device_register_error
:
517 st_sensors_deallocate_trigger(indio_dev
);
518 st_press_probe_trigger_error
:
519 st_press_deallocate_ring(indio_dev
);
523 EXPORT_SYMBOL(st_press_common_probe
);
525 void st_press_common_remove(struct iio_dev
*indio_dev
)
527 struct st_sensor_data
*press_data
= iio_priv(indio_dev
);
529 st_sensors_power_disable(indio_dev
);
531 iio_device_unregister(indio_dev
);
532 if (press_data
->get_irq_data_ready(indio_dev
) > 0)
533 st_sensors_deallocate_trigger(indio_dev
);
535 st_press_deallocate_ring(indio_dev
);
537 EXPORT_SYMBOL(st_press_common_remove
);
539 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
540 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
541 MODULE_LICENSE("GPL v2");