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 ST_PRESS_LSB_PER_MBAR 4096UL
32 #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \
33 ST_PRESS_LSB_PER_MBAR)
34 #define ST_PRESS_LSB_PER_CELSIUS 480UL
35 #define ST_PRESS_CELSIUS_NANO_SCALE (1000000000UL / \
36 ST_PRESS_LSB_PER_CELSIUS)
37 #define ST_PRESS_NUMBER_DATA_CHANNELS 1
39 /* DEFAULT VALUE FOR SENSORS */
40 #define ST_PRESS_DEFAULT_OUT_XL_ADDR 0x28
41 #define ST_TEMP_DEFAULT_OUT_L_ADDR 0x2b
44 #define ST_PRESS_FS_AVL_1260MB 1260
46 /* CUSTOM VALUES FOR SENSOR 1 */
47 #define ST_PRESS_1_WAI_EXP 0xbb
48 #define ST_PRESS_1_ODR_ADDR 0x20
49 #define ST_PRESS_1_ODR_MASK 0x70
50 #define ST_PRESS_1_ODR_AVL_1HZ_VAL 0x01
51 #define ST_PRESS_1_ODR_AVL_7HZ_VAL 0x05
52 #define ST_PRESS_1_ODR_AVL_13HZ_VAL 0x06
53 #define ST_PRESS_1_ODR_AVL_25HZ_VAL 0x07
54 #define ST_PRESS_1_PW_ADDR 0x20
55 #define ST_PRESS_1_PW_MASK 0x80
56 #define ST_PRESS_1_FS_ADDR 0x23
57 #define ST_PRESS_1_FS_MASK 0x30
58 #define ST_PRESS_1_FS_AVL_1260_VAL 0x00
59 #define ST_PRESS_1_FS_AVL_1260_GAIN ST_PRESS_KPASCAL_NANO_SCALE
60 #define ST_PRESS_1_FS_AVL_TEMP_GAIN ST_PRESS_CELSIUS_NANO_SCALE
61 #define ST_PRESS_1_BDU_ADDR 0x20
62 #define ST_PRESS_1_BDU_MASK 0x04
63 #define ST_PRESS_1_DRDY_IRQ_ADDR 0x22
64 #define ST_PRESS_1_DRDY_IRQ_INT1_MASK 0x04
65 #define ST_PRESS_1_DRDY_IRQ_INT2_MASK 0x20
66 #define ST_PRESS_1_MULTIREAD_BIT true
67 #define ST_PRESS_1_TEMP_OFFSET 42500
69 static const struct iio_chan_spec st_press_channels
[] = {
70 ST_SENSORS_LSM_CHANNELS(IIO_PRESSURE
,
71 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
72 ST_SENSORS_SCAN_X
, 0, IIO_NO_MOD
, 'u', IIO_LE
, 24, 24,
73 ST_PRESS_DEFAULT_OUT_XL_ADDR
),
74 ST_SENSORS_LSM_CHANNELS(IIO_TEMP
,
75 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
) |
76 BIT(IIO_CHAN_INFO_OFFSET
),
77 -1, 0, IIO_NO_MOD
, 's', IIO_LE
, 16, 16,
78 ST_TEMP_DEFAULT_OUT_L_ADDR
),
79 IIO_CHAN_SOFT_TIMESTAMP(1)
82 static const struct st_sensors st_press_sensors
[] = {
84 .wai
= ST_PRESS_1_WAI_EXP
,
85 .sensors_supported
= {
86 [0] = LPS331AP_PRESS_DEV_NAME
,
88 .ch
= (struct iio_chan_spec
*)st_press_channels
,
90 .addr
= ST_PRESS_1_ODR_ADDR
,
91 .mask
= ST_PRESS_1_ODR_MASK
,
93 { 1, ST_PRESS_1_ODR_AVL_1HZ_VAL
, },
94 { 7, ST_PRESS_1_ODR_AVL_7HZ_VAL
, },
95 { 13, ST_PRESS_1_ODR_AVL_13HZ_VAL
, },
96 { 25, ST_PRESS_1_ODR_AVL_25HZ_VAL
, },
100 .addr
= ST_PRESS_1_PW_ADDR
,
101 .mask
= ST_PRESS_1_PW_MASK
,
102 .value_on
= ST_SENSORS_DEFAULT_POWER_ON_VALUE
,
103 .value_off
= ST_SENSORS_DEFAULT_POWER_OFF_VALUE
,
106 .addr
= ST_PRESS_1_FS_ADDR
,
107 .mask
= ST_PRESS_1_FS_MASK
,
110 .num
= ST_PRESS_FS_AVL_1260MB
,
111 .value
= ST_PRESS_1_FS_AVL_1260_VAL
,
112 .gain
= ST_PRESS_1_FS_AVL_1260_GAIN
,
113 .gain2
= ST_PRESS_1_FS_AVL_TEMP_GAIN
,
118 .addr
= ST_PRESS_1_BDU_ADDR
,
119 .mask
= ST_PRESS_1_BDU_MASK
,
122 .addr
= ST_PRESS_1_DRDY_IRQ_ADDR
,
123 .mask_int1
= ST_PRESS_1_DRDY_IRQ_INT1_MASK
,
124 .mask_int2
= ST_PRESS_1_DRDY_IRQ_INT2_MASK
,
126 .multi_read_bit
= ST_PRESS_1_MULTIREAD_BIT
,
131 static int st_press_read_raw(struct iio_dev
*indio_dev
,
132 struct iio_chan_spec
const *ch
, int *val
,
133 int *val2
, long mask
)
136 struct st_sensor_data
*pdata
= iio_priv(indio_dev
);
139 case IIO_CHAN_INFO_RAW
:
140 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
145 case IIO_CHAN_INFO_SCALE
:
150 *val2
= pdata
->current_fullscale
->gain
;
153 *val2
= pdata
->current_fullscale
->gain2
;
160 return IIO_VAL_INT_PLUS_NANO
;
161 case IIO_CHAN_INFO_OFFSET
:
172 return IIO_VAL_FRACTIONAL
;
181 static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
182 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
184 static struct attribute
*st_press_attributes
[] = {
185 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
186 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
190 static const struct attribute_group st_press_attribute_group
= {
191 .attrs
= st_press_attributes
,
194 static const struct iio_info press_info
= {
195 .driver_module
= THIS_MODULE
,
196 .attrs
= &st_press_attribute_group
,
197 .read_raw
= &st_press_read_raw
,
200 #ifdef CONFIG_IIO_TRIGGER
201 static const struct iio_trigger_ops st_press_trigger_ops
= {
202 .owner
= THIS_MODULE
,
203 .set_trigger_state
= ST_PRESS_TRIGGER_SET_STATE
,
205 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
207 #define ST_PRESS_TRIGGER_OPS NULL
210 int st_press_common_probe(struct iio_dev
*indio_dev
,
211 struct st_sensors_platform_data
*plat_data
)
214 struct st_sensor_data
*pdata
= iio_priv(indio_dev
);
216 indio_dev
->modes
= INDIO_DIRECT_MODE
;
217 indio_dev
->info
= &press_info
;
219 err
= st_sensors_check_device_support(indio_dev
,
220 ARRAY_SIZE(st_press_sensors
), st_press_sensors
);
222 goto st_press_common_probe_error
;
224 pdata
->num_data_channels
= ST_PRESS_NUMBER_DATA_CHANNELS
;
225 pdata
->multiread_bit
= pdata
->sensor
->multi_read_bit
;
226 indio_dev
->channels
= pdata
->sensor
->ch
;
227 indio_dev
->num_channels
= ARRAY_SIZE(st_press_channels
);
229 pdata
->current_fullscale
= (struct st_sensor_fullscale_avl
*)
230 &pdata
->sensor
->fs
.fs_avl
[0];
231 pdata
->odr
= pdata
->sensor
->odr
.odr_avl
[0].hz
;
235 (struct st_sensors_platform_data
*)&default_press_pdata
;
237 err
= st_sensors_init_sensor(indio_dev
, plat_data
);
239 goto st_press_common_probe_error
;
241 if (pdata
->get_irq_data_ready(indio_dev
) > 0) {
242 err
= st_press_allocate_ring(indio_dev
);
244 goto st_press_common_probe_error
;
246 err
= st_sensors_allocate_trigger(indio_dev
,
247 ST_PRESS_TRIGGER_OPS
);
249 goto st_press_probe_trigger_error
;
252 err
= iio_device_register(indio_dev
);
254 goto st_press_device_register_error
;
258 st_press_device_register_error
:
259 if (pdata
->get_irq_data_ready(indio_dev
) > 0)
260 st_sensors_deallocate_trigger(indio_dev
);
261 st_press_probe_trigger_error
:
262 if (pdata
->get_irq_data_ready(indio_dev
) > 0)
263 st_press_deallocate_ring(indio_dev
);
264 st_press_common_probe_error
:
267 EXPORT_SYMBOL(st_press_common_probe
);
269 void st_press_common_remove(struct iio_dev
*indio_dev
)
271 struct st_sensor_data
*pdata
= iio_priv(indio_dev
);
273 iio_device_unregister(indio_dev
);
274 if (pdata
->get_irq_data_ready(indio_dev
) > 0) {
275 st_sensors_deallocate_trigger(indio_dev
);
276 st_press_deallocate_ring(indio_dev
);
279 EXPORT_SYMBOL(st_press_common_remove
);
281 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
282 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
283 MODULE_LICENSE("GPL v2");