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_NUMBER_DATA_CHANNELS 1
36 /* DEFAULT VALUE FOR SENSORS */
37 #define ST_PRESS_DEFAULT_OUT_XL_ADDR 0x28
38 #define ST_TEMP_DEFAULT_OUT_L_ADDR 0x2b
41 #define ST_PRESS_FS_AVL_1260MB 1260
43 /* CUSTOM VALUES FOR SENSOR 1 */
44 #define ST_PRESS_1_WAI_EXP 0xbb
45 #define ST_PRESS_1_ODR_ADDR 0x20
46 #define ST_PRESS_1_ODR_MASK 0x70
47 #define ST_PRESS_1_ODR_AVL_1HZ_VAL 0x01
48 #define ST_PRESS_1_ODR_AVL_7HZ_VAL 0x05
49 #define ST_PRESS_1_ODR_AVL_13HZ_VAL 0x06
50 #define ST_PRESS_1_ODR_AVL_25HZ_VAL 0x07
51 #define ST_PRESS_1_PW_ADDR 0x20
52 #define ST_PRESS_1_PW_MASK 0x80
53 #define ST_PRESS_1_FS_ADDR 0x23
54 #define ST_PRESS_1_FS_MASK 0x30
55 #define ST_PRESS_1_FS_AVL_1260_VAL 0x00
56 #define ST_PRESS_1_FS_AVL_TEMP_GAIN 2083000
57 #define ST_PRESS_1_FS_AVL_1260_GAIN ST_PRESS_KPASCAL_NANO_SCALE
58 #define ST_PRESS_1_BDU_ADDR 0x20
59 #define ST_PRESS_1_BDU_MASK 0x04
60 #define ST_PRESS_1_DRDY_IRQ_ADDR 0x22
61 #define ST_PRESS_1_DRDY_IRQ_MASK 0x04
62 #define ST_PRESS_1_MULTIREAD_BIT true
63 #define ST_PRESS_1_TEMP_OFFSET 42500
65 static const struct iio_chan_spec st_press_channels[] = {
66 ST_SENSORS_LSM_CHANNELS(IIO_PRESSURE,
67 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
68 ST_SENSORS_SCAN_X, 0, IIO_NO_MOD, 'u', IIO_LE, 24, 24,
69 ST_PRESS_DEFAULT_OUT_XL_ADDR),
70 ST_SENSORS_LSM_CHANNELS(IIO_TEMP,
71 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) |
72 BIT(IIO_CHAN_INFO_OFFSET),
73 -1, 0, IIO_NO_MOD, 's', IIO_LE, 16, 16,
74 ST_TEMP_DEFAULT_OUT_L_ADDR),
75 IIO_CHAN_SOFT_TIMESTAMP(1)
78 static const struct st_sensors st_press_sensors[] = {
80 .wai = ST_PRESS_1_WAI_EXP,
81 .sensors_supported = {
82 [0] = LPS331AP_PRESS_DEV_NAME,
84 .ch = (struct iio_chan_spec *)st_press_channels,
86 .addr = ST_PRESS_1_ODR_ADDR,
87 .mask = ST_PRESS_1_ODR_MASK,
89 { 1, ST_PRESS_1_ODR_AVL_1HZ_VAL, },
90 { 7, ST_PRESS_1_ODR_AVL_7HZ_VAL, },
91 { 13, ST_PRESS_1_ODR_AVL_13HZ_VAL, },
92 { 25, ST_PRESS_1_ODR_AVL_25HZ_VAL, },
96 .addr = ST_PRESS_1_PW_ADDR,
97 .mask = ST_PRESS_1_PW_MASK,
98 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
99 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
102 .addr = ST_PRESS_1_FS_ADDR,
103 .mask = ST_PRESS_1_FS_MASK,
106 .num = ST_PRESS_FS_AVL_1260MB,
107 .value = ST_PRESS_1_FS_AVL_1260_VAL,
108 .gain = ST_PRESS_1_FS_AVL_1260_GAIN,
109 .gain2 = ST_PRESS_1_FS_AVL_TEMP_GAIN,
114 .addr = ST_PRESS_1_BDU_ADDR,
115 .mask = ST_PRESS_1_BDU_MASK,
118 .addr = ST_PRESS_1_DRDY_IRQ_ADDR,
119 .mask = ST_PRESS_1_DRDY_IRQ_MASK,
121 .multi_read_bit = ST_PRESS_1_MULTIREAD_BIT,
126 static int st_press_read_raw(struct iio_dev *indio_dev,
127 struct iio_chan_spec const *ch, int *val,
128 int *val2, long mask)
131 struct st_sensor_data *pdata = iio_priv(indio_dev);
134 case IIO_CHAN_INFO_RAW:
135 err = st_sensors_read_info_raw(indio_dev, ch, val);
140 case IIO_CHAN_INFO_SCALE:
145 *val2 = pdata->current_fullscale->gain;
148 *val2 = pdata->current_fullscale->gain2;
155 return IIO_VAL_INT_PLUS_NANO;
156 case IIO_CHAN_INFO_OFFSET:
167 return IIO_VAL_FRACTIONAL;
176 static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
177 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
179 static struct attribute *st_press_attributes[] = {
180 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
181 &iio_dev_attr_sampling_frequency.dev_attr.attr,
185 static const struct attribute_group st_press_attribute_group = {
186 .attrs = st_press_attributes,
189 static const struct iio_info press_info = {
190 .driver_module = THIS_MODULE,
191 .attrs = &st_press_attribute_group,
192 .read_raw = &st_press_read_raw,
195 #ifdef CONFIG_IIO_TRIGGER
196 static const struct iio_trigger_ops st_press_trigger_ops = {
197 .owner = THIS_MODULE,
198 .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
200 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
202 #define ST_PRESS_TRIGGER_OPS NULL
205 int st_press_common_probe(struct iio_dev *indio_dev)
208 struct st_sensor_data *pdata = iio_priv(indio_dev);
210 indio_dev->modes = INDIO_DIRECT_MODE;
211 indio_dev->info = &press_info;
213 err = st_sensors_check_device_support(indio_dev,
214 ARRAY_SIZE(st_press_sensors), st_press_sensors);
216 goto st_press_common_probe_error;
218 pdata->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS;
219 pdata->multiread_bit = pdata->sensor->multi_read_bit;
220 indio_dev->channels = pdata->sensor->ch;
221 indio_dev->num_channels = ARRAY_SIZE(st_press_channels);
223 pdata->current_fullscale = (struct st_sensor_fullscale_avl *)
224 &pdata->sensor->fs.fs_avl[0];
225 pdata->odr = pdata->sensor->odr.odr_avl[0].hz;
227 err = st_sensors_init_sensor(indio_dev);
229 goto st_press_common_probe_error;
231 if (pdata->get_irq_data_ready(indio_dev) > 0) {
232 err = st_press_allocate_ring(indio_dev);
234 goto st_press_common_probe_error;
236 err = st_sensors_allocate_trigger(indio_dev,
237 ST_PRESS_TRIGGER_OPS);
239 goto st_press_probe_trigger_error;
242 err = iio_device_register(indio_dev);
244 goto st_press_device_register_error;
248 st_press_device_register_error:
249 if (pdata->get_irq_data_ready(indio_dev) > 0)
250 st_sensors_deallocate_trigger(indio_dev);
251 st_press_probe_trigger_error:
252 if (pdata->get_irq_data_ready(indio_dev) > 0)
253 st_press_deallocate_ring(indio_dev);
254 st_press_common_probe_error:
257 EXPORT_SYMBOL(st_press_common_probe);
259 void st_press_common_remove(struct iio_dev *indio_dev)
261 struct st_sensor_data *pdata = iio_priv(indio_dev);
263 iio_device_unregister(indio_dev);
264 if (pdata->get_irq_data_ready(indio_dev) > 0) {
265 st_sensors_deallocate_trigger(indio_dev);
266 st_press_deallocate_ring(indio_dev);
268 iio_device_free(indio_dev);
270 EXPORT_SYMBOL(st_press_common_remove);
272 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
273 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
274 MODULE_LICENSE("GPL v2");