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_MBAR_TO_KPASCAL(x) (x * 10)
32 #define ST_PRESS_NUMBER_DATA_CHANNELS 1
34 /* DEFAULT VALUE FOR SENSORS */
35 #define ST_PRESS_DEFAULT_OUT_XL_ADDR 0x28
36 #define ST_TEMP_DEFAULT_OUT_L_ADDR 0x2b
39 #define ST_PRESS_FS_AVL_1260MB 1260
41 /* CUSTOM VALUES FOR SENSOR 1 */
42 #define ST_PRESS_1_WAI_EXP 0xbb
43 #define ST_PRESS_1_ODR_ADDR 0x20
44 #define ST_PRESS_1_ODR_MASK 0x70
45 #define ST_PRESS_1_ODR_AVL_1HZ_VAL 0x01
46 #define ST_PRESS_1_ODR_AVL_7HZ_VAL 0x05
47 #define ST_PRESS_1_ODR_AVL_13HZ_VAL 0x06
48 #define ST_PRESS_1_ODR_AVL_25HZ_VAL 0x07
49 #define ST_PRESS_1_PW_ADDR 0x20
50 #define ST_PRESS_1_PW_MASK 0x80
51 #define ST_PRESS_1_FS_ADDR 0x23
52 #define ST_PRESS_1_FS_MASK 0x30
53 #define ST_PRESS_1_FS_AVL_1260_VAL 0x00
54 #define ST_PRESS_1_FS_AVL_1260_GAIN ST_PRESS_MBAR_TO_KPASCAL(244141)
55 #define ST_PRESS_1_FS_AVL_TEMP_GAIN 2083000
56 #define ST_PRESS_1_BDU_ADDR 0x20
57 #define ST_PRESS_1_BDU_MASK 0x04
58 #define ST_PRESS_1_DRDY_IRQ_ADDR 0x22
59 #define ST_PRESS_1_DRDY_IRQ_MASK 0x04
60 #define ST_PRESS_1_MULTIREAD_BIT true
61 #define ST_PRESS_1_TEMP_OFFSET 42500
63 static const struct iio_chan_spec st_press_channels[] = {
64 ST_SENSORS_LSM_CHANNELS(IIO_PRESSURE,
65 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
66 ST_SENSORS_SCAN_X, 0, IIO_NO_MOD, 'u', IIO_LE, 24, 24,
67 ST_PRESS_DEFAULT_OUT_XL_ADDR),
68 ST_SENSORS_LSM_CHANNELS(IIO_TEMP,
69 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) |
70 BIT(IIO_CHAN_INFO_OFFSET),
71 -1, 0, IIO_NO_MOD, 's', IIO_LE, 16, 16,
72 ST_TEMP_DEFAULT_OUT_L_ADDR),
73 IIO_CHAN_SOFT_TIMESTAMP(1)
76 static const struct st_sensors st_press_sensors[] = {
78 .wai = ST_PRESS_1_WAI_EXP,
79 .sensors_supported = {
80 [0] = LPS331AP_PRESS_DEV_NAME,
82 .ch = (struct iio_chan_spec *)st_press_channels,
84 .addr = ST_PRESS_1_ODR_ADDR,
85 .mask = ST_PRESS_1_ODR_MASK,
87 { 1, ST_PRESS_1_ODR_AVL_1HZ_VAL, },
88 { 7, ST_PRESS_1_ODR_AVL_7HZ_VAL, },
89 { 13, ST_PRESS_1_ODR_AVL_13HZ_VAL, },
90 { 25, ST_PRESS_1_ODR_AVL_25HZ_VAL, },
94 .addr = ST_PRESS_1_PW_ADDR,
95 .mask = ST_PRESS_1_PW_MASK,
96 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
97 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
100 .addr = ST_PRESS_1_FS_ADDR,
101 .mask = ST_PRESS_1_FS_MASK,
104 .num = ST_PRESS_FS_AVL_1260MB,
105 .value = ST_PRESS_1_FS_AVL_1260_VAL,
106 .gain = ST_PRESS_1_FS_AVL_1260_GAIN,
107 .gain2 = ST_PRESS_1_FS_AVL_TEMP_GAIN,
112 .addr = ST_PRESS_1_BDU_ADDR,
113 .mask = ST_PRESS_1_BDU_MASK,
116 .addr = ST_PRESS_1_DRDY_IRQ_ADDR,
117 .mask = ST_PRESS_1_DRDY_IRQ_MASK,
119 .multi_read_bit = ST_PRESS_1_MULTIREAD_BIT,
124 static int st_press_read_raw(struct iio_dev *indio_dev,
125 struct iio_chan_spec const *ch, int *val,
126 int *val2, long mask)
129 struct st_sensor_data *pdata = iio_priv(indio_dev);
132 case IIO_CHAN_INFO_RAW:
133 err = st_sensors_read_info_raw(indio_dev, ch, val);
138 case IIO_CHAN_INFO_SCALE:
143 *val2 = pdata->current_fullscale->gain;
146 *val2 = pdata->current_fullscale->gain2;
153 return IIO_VAL_INT_PLUS_NANO;
154 case IIO_CHAN_INFO_OFFSET:
165 return IIO_VAL_FRACTIONAL;
174 static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
175 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
177 static struct attribute *st_press_attributes[] = {
178 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
179 &iio_dev_attr_sampling_frequency.dev_attr.attr,
183 static const struct attribute_group st_press_attribute_group = {
184 .attrs = st_press_attributes,
187 static const struct iio_info press_info = {
188 .driver_module = THIS_MODULE,
189 .attrs = &st_press_attribute_group,
190 .read_raw = &st_press_read_raw,
193 #ifdef CONFIG_IIO_TRIGGER
194 static const struct iio_trigger_ops st_press_trigger_ops = {
195 .owner = THIS_MODULE,
196 .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
198 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
200 #define ST_PRESS_TRIGGER_OPS NULL
203 int st_press_common_probe(struct iio_dev *indio_dev)
206 struct st_sensor_data *pdata = iio_priv(indio_dev);
208 indio_dev->modes = INDIO_DIRECT_MODE;
209 indio_dev->info = &press_info;
211 err = st_sensors_check_device_support(indio_dev,
212 ARRAY_SIZE(st_press_sensors), st_press_sensors);
214 goto st_press_common_probe_error;
216 pdata->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS;
217 pdata->multiread_bit = pdata->sensor->multi_read_bit;
218 indio_dev->channels = pdata->sensor->ch;
219 indio_dev->num_channels = ARRAY_SIZE(st_press_channels);
221 pdata->current_fullscale = (struct st_sensor_fullscale_avl *)
222 &pdata->sensor->fs.fs_avl[0];
223 pdata->odr = pdata->sensor->odr.odr_avl[0].hz;
225 err = st_sensors_init_sensor(indio_dev);
227 goto st_press_common_probe_error;
229 if (pdata->get_irq_data_ready(indio_dev) > 0) {
230 err = st_press_allocate_ring(indio_dev);
232 goto st_press_common_probe_error;
234 err = st_sensors_allocate_trigger(indio_dev,
235 ST_PRESS_TRIGGER_OPS);
237 goto st_press_probe_trigger_error;
240 err = iio_device_register(indio_dev);
242 goto st_press_device_register_error;
246 st_press_device_register_error:
247 if (pdata->get_irq_data_ready(indio_dev) > 0)
248 st_sensors_deallocate_trigger(indio_dev);
249 st_press_probe_trigger_error:
250 if (pdata->get_irq_data_ready(indio_dev) > 0)
251 st_press_deallocate_ring(indio_dev);
252 st_press_common_probe_error:
255 EXPORT_SYMBOL(st_press_common_probe);
257 void st_press_common_remove(struct iio_dev *indio_dev)
259 struct st_sensor_data *pdata = iio_priv(indio_dev);
261 iio_device_unregister(indio_dev);
262 if (pdata->get_irq_data_ready(indio_dev) > 0) {
263 st_sensors_deallocate_trigger(indio_dev);
264 st_press_deallocate_ring(indio_dev);
266 iio_device_free(indio_dev);
268 EXPORT_SYMBOL(st_press_common_remove);
270 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
271 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
272 MODULE_LICENSE("GPL v2");