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 <linux/regulator/consumer.h>
27 #include <asm/unaligned.h>
29 #include <linux/iio/common/st_sensors.h>
30 #include "st_pressure.h"
32 #define ST_PRESS_LSB_PER_MBAR 4096UL
33 #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \
34 ST_PRESS_LSB_PER_MBAR)
35 #define ST_PRESS_LSB_PER_CELSIUS 480UL
36 #define ST_PRESS_CELSIUS_NANO_SCALE (1000000000UL / \
37 ST_PRESS_LSB_PER_CELSIUS)
38 #define ST_PRESS_NUMBER_DATA_CHANNELS 1
41 #define ST_PRESS_FS_AVL_1260MB 1260
43 /* CUSTOM VALUES FOR LPS331AP SENSOR */
44 #define ST_PRESS_LPS331AP_WAI_EXP 0xbb
45 #define ST_PRESS_LPS331AP_ODR_ADDR 0x20
46 #define ST_PRESS_LPS331AP_ODR_MASK 0x70
47 #define ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL 0x01
48 #define ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL 0x05
49 #define ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL 0x06
50 #define ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL 0x07
51 #define ST_PRESS_LPS331AP_PW_ADDR 0x20
52 #define ST_PRESS_LPS331AP_PW_MASK 0x80
53 #define ST_PRESS_LPS331AP_FS_ADDR 0x23
54 #define ST_PRESS_LPS331AP_FS_MASK 0x30
55 #define ST_PRESS_LPS331AP_FS_AVL_1260_VAL 0x00
56 #define ST_PRESS_LPS331AP_FS_AVL_1260_GAIN ST_PRESS_KPASCAL_NANO_SCALE
57 #define ST_PRESS_LPS331AP_FS_AVL_TEMP_GAIN ST_PRESS_CELSIUS_NANO_SCALE
58 #define ST_PRESS_LPS331AP_BDU_ADDR 0x20
59 #define ST_PRESS_LPS331AP_BDU_MASK 0x04
60 #define ST_PRESS_LPS331AP_DRDY_IRQ_ADDR 0x22
61 #define ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK 0x04
62 #define ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK 0x20
63 #define ST_PRESS_LPS331AP_MULTIREAD_BIT true
64 #define ST_PRESS_LPS331AP_TEMP_OFFSET 42500
65 #define ST_PRESS_LPS331AP_OUT_XL_ADDR 0x28
66 #define ST_TEMP_LPS331AP_OUT_L_ADDR 0x2b
68 /* CUSTOM VALUES FOR LPS001WP SENSOR */
69 #define ST_PRESS_LPS001WP_WAI_EXP 0xba
70 #define ST_PRESS_LPS001WP_ODR_ADDR 0x20
71 #define ST_PRESS_LPS001WP_ODR_MASK 0x30
72 #define ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL 0x01
73 #define ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL 0x02
74 #define ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL 0x03
75 #define ST_PRESS_LPS001WP_PW_ADDR 0x20
76 #define ST_PRESS_LPS001WP_PW_MASK 0x40
77 #define ST_PRESS_LPS001WP_BDU_ADDR 0x20
78 #define ST_PRESS_LPS001WP_BDU_MASK 0x04
79 #define ST_PRESS_LPS001WP_MULTIREAD_BIT true
80 #define ST_PRESS_LPS001WP_OUT_L_ADDR 0x28
81 #define ST_TEMP_LPS001WP_OUT_L_ADDR 0x2a
83 static const struct iio_chan_spec st_press_lps331ap_channels
[] = {
86 .channel2
= IIO_NO_MOD
,
87 .address
= ST_PRESS_LPS331AP_OUT_XL_ADDR
,
88 .scan_index
= ST_SENSORS_SCAN_X
,
96 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
101 .channel2
= IIO_NO_MOD
,
102 .address
= ST_TEMP_LPS331AP_OUT_L_ADDR
,
108 .endianness
= IIO_LE
,
110 .info_mask_separate
=
111 BIT(IIO_CHAN_INFO_RAW
) |
112 BIT(IIO_CHAN_INFO_SCALE
) |
113 BIT(IIO_CHAN_INFO_OFFSET
),
116 IIO_CHAN_SOFT_TIMESTAMP(1)
119 static const struct iio_chan_spec st_press_lps001wp_channels
[] = {
121 .type
= IIO_PRESSURE
,
122 .channel2
= IIO_NO_MOD
,
123 .address
= ST_PRESS_LPS001WP_OUT_L_ADDR
,
124 .scan_index
= ST_SENSORS_SCAN_X
,
129 .endianness
= IIO_LE
,
131 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
136 .channel2
= IIO_NO_MOD
,
137 .address
= ST_TEMP_LPS001WP_OUT_L_ADDR
,
143 .endianness
= IIO_LE
,
145 .info_mask_separate
=
146 BIT(IIO_CHAN_INFO_RAW
) |
147 BIT(IIO_CHAN_INFO_OFFSET
),
150 IIO_CHAN_SOFT_TIMESTAMP(1)
153 static const struct st_sensors st_press_sensors
[] = {
155 .wai
= ST_PRESS_LPS331AP_WAI_EXP
,
156 .sensors_supported
= {
157 [0] = LPS331AP_PRESS_DEV_NAME
,
159 .ch
= (struct iio_chan_spec
*)st_press_lps331ap_channels
,
160 .num_ch
= ARRAY_SIZE(st_press_lps331ap_channels
),
162 .addr
= ST_PRESS_LPS331AP_ODR_ADDR
,
163 .mask
= ST_PRESS_LPS331AP_ODR_MASK
,
165 { 1, ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL
, },
166 { 7, ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL
, },
167 { 13, ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL
, },
168 { 25, ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL
, },
172 .addr
= ST_PRESS_LPS331AP_PW_ADDR
,
173 .mask
= ST_PRESS_LPS331AP_PW_MASK
,
174 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
175 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
178 .addr
= ST_PRESS_LPS331AP_FS_ADDR
,
179 .mask
= ST_PRESS_LPS331AP_FS_MASK
,
182 .num
= ST_PRESS_FS_AVL_1260MB
,
183 .value
= ST_PRESS_LPS331AP_FS_AVL_1260_VAL
,
184 .gain
= ST_PRESS_LPS331AP_FS_AVL_1260_GAIN
,
185 .gain2
= ST_PRESS_LPS331AP_FS_AVL_TEMP_GAIN
,
190 .addr
= ST_PRESS_LPS331AP_BDU_ADDR
,
191 .mask
= ST_PRESS_LPS331AP_BDU_MASK
,
194 .addr
= ST_PRESS_LPS331AP_DRDY_IRQ_ADDR
,
195 .mask_int1
= ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK
,
196 .mask_int2
= ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK
,
198 .multi_read_bit
= ST_PRESS_LPS331AP_MULTIREAD_BIT
,
202 .wai
= ST_PRESS_LPS001WP_WAI_EXP
,
203 .sensors_supported
= {
204 [0] = LPS001WP_PRESS_DEV_NAME
,
206 .ch
= (struct iio_chan_spec
*)st_press_lps001wp_channels
,
207 .num_ch
= ARRAY_SIZE(st_press_lps001wp_channels
),
209 .addr
= ST_PRESS_LPS001WP_ODR_ADDR
,
210 .mask
= ST_PRESS_LPS001WP_ODR_MASK
,
212 { 1, ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL
, },
213 { 7, ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL
, },
214 { 13, ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL
, },
218 .addr
= ST_PRESS_LPS001WP_PW_ADDR
,
219 .mask
= ST_PRESS_LPS001WP_PW_MASK
,
220 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
221 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
227 .addr
= ST_PRESS_LPS001WP_BDU_ADDR
,
228 .mask
= ST_PRESS_LPS001WP_BDU_MASK
,
233 .multi_read_bit
= ST_PRESS_LPS001WP_MULTIREAD_BIT
,
238 static int st_press_read_raw(struct iio_dev
*indio_dev
,
239 struct iio_chan_spec
const *ch
, int *val
,
240 int *val2
, long mask
)
243 struct st_sensor_data
*pdata
= iio_priv(indio_dev
);
246 case IIO_CHAN_INFO_RAW
:
247 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
252 case IIO_CHAN_INFO_SCALE
:
257 *val2
= pdata
->current_fullscale
->gain
;
260 *val2
= pdata
->current_fullscale
->gain2
;
267 return IIO_VAL_INT_PLUS_NANO
;
268 case IIO_CHAN_INFO_OFFSET
:
279 return IIO_VAL_FRACTIONAL
;
288 static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
289 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
291 static struct attribute
*st_press_attributes
[] = {
292 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
293 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
297 static const struct attribute_group st_press_attribute_group
= {
298 .attrs
= st_press_attributes
,
301 static const struct iio_info press_info
= {
302 .driver_module
= THIS_MODULE
,
303 .attrs
= &st_press_attribute_group
,
304 .read_raw
= &st_press_read_raw
,
307 #ifdef CONFIG_IIO_TRIGGER
308 static const struct iio_trigger_ops st_press_trigger_ops
= {
309 .owner
= THIS_MODULE
,
310 .set_trigger_state
= ST_PRESS_TRIGGER_SET_STATE
,
312 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
314 #define ST_PRESS_TRIGGER_OPS NULL
317 static void st_press_power_enable(struct iio_dev
*indio_dev
)
319 struct st_sensor_data
*pdata
= iio_priv(indio_dev
);
322 /* Regulators not mandatory, but if requested we should enable them. */
323 pdata
->vdd
= devm_regulator_get_optional(&indio_dev
->dev
, "vdd");
324 if (!IS_ERR(pdata
->vdd
)) {
325 err
= regulator_enable(pdata
->vdd
);
327 dev_warn(&indio_dev
->dev
,
328 "Failed to enable specified Vdd supply\n");
331 pdata
->vdd_io
= devm_regulator_get_optional(&indio_dev
->dev
, "vddio");
332 if (!IS_ERR(pdata
->vdd_io
)) {
333 err
= regulator_enable(pdata
->vdd_io
);
335 dev_warn(&indio_dev
->dev
,
336 "Failed to enable specified Vdd_IO supply\n");
340 static void st_press_power_disable(struct iio_dev
*indio_dev
)
342 struct st_sensor_data
*pdata
= iio_priv(indio_dev
);
344 if (!IS_ERR(pdata
->vdd
))
345 regulator_disable(pdata
->vdd
);
347 if (!IS_ERR(pdata
->vdd_io
))
348 regulator_disable(pdata
->vdd_io
);
351 int st_press_common_probe(struct iio_dev
*indio_dev
,
352 struct st_sensors_platform_data
*plat_data
)
354 struct st_sensor_data
*pdata
= iio_priv(indio_dev
);
355 int irq
= pdata
->get_irq_data_ready(indio_dev
);
358 indio_dev
->modes
= INDIO_DIRECT_MODE
;
359 indio_dev
->info
= &press_info
;
361 st_press_power_enable(indio_dev
);
363 err
= st_sensors_check_device_support(indio_dev
,
364 ARRAY_SIZE(st_press_sensors
),
369 pdata
->num_data_channels
= ST_PRESS_NUMBER_DATA_CHANNELS
;
370 pdata
->multiread_bit
= pdata
->sensor
->multi_read_bit
;
371 indio_dev
->channels
= pdata
->sensor
->ch
;
372 indio_dev
->num_channels
= pdata
->sensor
->num_ch
;
374 if (pdata
->sensor
->fs
.addr
!= 0)
375 pdata
->current_fullscale
= (struct st_sensor_fullscale_avl
*)
376 &pdata
->sensor
->fs
.fs_avl
[0];
378 pdata
->odr
= pdata
->sensor
->odr
.odr_avl
[0].hz
;
380 /* Some devices don't support a data ready pin. */
381 if (!plat_data
&& pdata
->sensor
->drdy_irq
.addr
)
383 (struct st_sensors_platform_data
*)&default_press_pdata
;
385 err
= st_sensors_init_sensor(indio_dev
, plat_data
);
389 err
= st_press_allocate_ring(indio_dev
);
394 err
= st_sensors_allocate_trigger(indio_dev
,
395 ST_PRESS_TRIGGER_OPS
);
397 goto st_press_probe_trigger_error
;
400 err
= iio_device_register(indio_dev
);
402 goto st_press_device_register_error
;
406 st_press_device_register_error
:
408 st_sensors_deallocate_trigger(indio_dev
);
409 st_press_probe_trigger_error
:
410 st_press_deallocate_ring(indio_dev
);
414 EXPORT_SYMBOL(st_press_common_probe
);
416 void st_press_common_remove(struct iio_dev
*indio_dev
)
418 struct st_sensor_data
*pdata
= iio_priv(indio_dev
);
420 st_press_power_disable(indio_dev
);
422 iio_device_unregister(indio_dev
);
423 if (pdata
->get_irq_data_ready(indio_dev
) > 0)
424 st_sensors_deallocate_trigger(indio_dev
);
426 st_press_deallocate_ring(indio_dev
);
428 EXPORT_SYMBOL(st_press_common_remove
);
430 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
431 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
432 MODULE_LICENSE("GPL v2");