]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/iio/pressure/st_pressure_core.c
iio: pressure-core: st: Clean-up probe() function
[mirror_ubuntu-artful-kernel.git] / drivers / iio / pressure / st_pressure_core.c
CommitLineData
217494e5
DC
1/*
2 * STMicroelectronics pressures driver
3 *
4 * Copyright 2013 STMicroelectronics Inc.
5 *
6 * Denis Ciocca <denis.ciocca@st.com>
7 *
8 * Licensed under the GPL-2.
9 */
10
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>
27
28#include <linux/iio/common/st_sensors.h>
29#include "st_pressure.h"
30
67dbf54a
JA
31#define ST_PRESS_LSB_PER_MBAR 4096UL
32#define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \
33 ST_PRESS_LSB_PER_MBAR)
1003eb67
JA
34#define ST_PRESS_LSB_PER_CELSIUS 480UL
35#define ST_PRESS_CELSIUS_NANO_SCALE (1000000000UL / \
36 ST_PRESS_LSB_PER_CELSIUS)
217494e5
DC
37#define ST_PRESS_NUMBER_DATA_CHANNELS 1
38
217494e5
DC
39/* FULLSCALE */
40#define ST_PRESS_FS_AVL_1260MB 1260
41
302fbd50
LJ
42/* CUSTOM VALUES FOR LPS331AP SENSOR */
43#define ST_PRESS_LPS331AP_WAI_EXP 0xbb
44#define ST_PRESS_LPS331AP_ODR_ADDR 0x20
45#define ST_PRESS_LPS331AP_ODR_MASK 0x70
46#define ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL 0x01
47#define ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL 0x05
48#define ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL 0x06
49#define ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL 0x07
50#define ST_PRESS_LPS331AP_PW_ADDR 0x20
51#define ST_PRESS_LPS331AP_PW_MASK 0x80
52#define ST_PRESS_LPS331AP_FS_ADDR 0x23
53#define ST_PRESS_LPS331AP_FS_MASK 0x30
54#define ST_PRESS_LPS331AP_FS_AVL_1260_VAL 0x00
55#define ST_PRESS_LPS331AP_FS_AVL_1260_GAIN ST_PRESS_KPASCAL_NANO_SCALE
56#define ST_PRESS_LPS331AP_FS_AVL_TEMP_GAIN ST_PRESS_CELSIUS_NANO_SCALE
57#define ST_PRESS_LPS331AP_BDU_ADDR 0x20
58#define ST_PRESS_LPS331AP_BDU_MASK 0x04
59#define ST_PRESS_LPS331AP_DRDY_IRQ_ADDR 0x22
60#define ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK 0x04
61#define ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK 0x20
62#define ST_PRESS_LPS331AP_MULTIREAD_BIT true
63#define ST_PRESS_LPS331AP_TEMP_OFFSET 42500
64#define ST_PRESS_LPS331AP_OUT_XL_ADDR 0x28
65#define ST_TEMP_LPS331AP_OUT_L_ADDR 0x2b
217494e5 66
2f5effcb
LJ
67static const struct iio_chan_spec st_press_lps331ap_channels[] = {
68 {
69 .type = IIO_PRESSURE,
70 .channel2 = IIO_NO_MOD,
71 .address = ST_PRESS_LPS331AP_OUT_XL_ADDR,
72 .scan_index = ST_SENSORS_SCAN_X,
73 .scan_type = {
74 .sign = 'u',
75 .realbits = 24,
76 .storagebits = 24,
77 .endianness = IIO_LE,
78 },
79 .info_mask_separate =
217494e5 80 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
2f5effcb
LJ
81 .modified = 0,
82 },
83 {
84 .type = IIO_TEMP,
85 .channel2 = IIO_NO_MOD,
86 .address = ST_TEMP_LPS331AP_OUT_L_ADDR,
87 .scan_index = -1,
88 .scan_type = {
89 .sign = 'u',
90 .realbits = 16,
91 .storagebits = 16,
92 .endianness = IIO_LE,
93 },
94 .info_mask_separate =
95 BIT(IIO_CHAN_INFO_RAW) |
96 BIT(IIO_CHAN_INFO_SCALE) |
97 BIT(IIO_CHAN_INFO_OFFSET),
98 .modified = 0,
99 },
217494e5
DC
100 IIO_CHAN_SOFT_TIMESTAMP(1)
101};
102
103static const struct st_sensors st_press_sensors[] = {
104 {
302fbd50 105 .wai = ST_PRESS_LPS331AP_WAI_EXP,
217494e5
DC
106 .sensors_supported = {
107 [0] = LPS331AP_PRESS_DEV_NAME,
108 },
2f5effcb 109 .ch = (struct iio_chan_spec *)st_press_lps331ap_channels,
ea01f2c1 110 .num_ch = ARRAY_SIZE(st_press_lps331ap_channels),
217494e5 111 .odr = {
302fbd50
LJ
112 .addr = ST_PRESS_LPS331AP_ODR_ADDR,
113 .mask = ST_PRESS_LPS331AP_ODR_MASK,
217494e5 114 .odr_avl = {
302fbd50
LJ
115 { 1, ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL, },
116 { 7, ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL, },
117 { 13, ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL, },
118 { 25, ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL, },
217494e5
DC
119 },
120 },
121 .pw = {
302fbd50
LJ
122 .addr = ST_PRESS_LPS331AP_PW_ADDR,
123 .mask = ST_PRESS_LPS331AP_PW_MASK,
217494e5
DC
124 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
125 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
126 },
127 .fs = {
302fbd50
LJ
128 .addr = ST_PRESS_LPS331AP_FS_ADDR,
129 .mask = ST_PRESS_LPS331AP_FS_MASK,
217494e5
DC
130 .fs_avl = {
131 [0] = {
132 .num = ST_PRESS_FS_AVL_1260MB,
302fbd50
LJ
133 .value = ST_PRESS_LPS331AP_FS_AVL_1260_VAL,
134 .gain = ST_PRESS_LPS331AP_FS_AVL_1260_GAIN,
135 .gain2 = ST_PRESS_LPS331AP_FS_AVL_TEMP_GAIN,
217494e5
DC
136 },
137 },
138 },
139 .bdu = {
302fbd50
LJ
140 .addr = ST_PRESS_LPS331AP_BDU_ADDR,
141 .mask = ST_PRESS_LPS331AP_BDU_MASK,
217494e5
DC
142 },
143 .drdy_irq = {
302fbd50
LJ
144 .addr = ST_PRESS_LPS331AP_DRDY_IRQ_ADDR,
145 .mask_int1 = ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK,
146 .mask_int2 = ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK,
217494e5 147 },
302fbd50 148 .multi_read_bit = ST_PRESS_LPS331AP_MULTIREAD_BIT,
217494e5
DC
149 .bootime = 2,
150 },
151};
152
153static int st_press_read_raw(struct iio_dev *indio_dev,
154 struct iio_chan_spec const *ch, int *val,
155 int *val2, long mask)
156{
157 int err;
158 struct st_sensor_data *pdata = iio_priv(indio_dev);
159
160 switch (mask) {
161 case IIO_CHAN_INFO_RAW:
162 err = st_sensors_read_info_raw(indio_dev, ch, val);
163 if (err < 0)
164 goto read_error;
165
166 return IIO_VAL_INT;
167 case IIO_CHAN_INFO_SCALE:
168 *val = 0;
169
170 switch (ch->type) {
171 case IIO_PRESSURE:
172 *val2 = pdata->current_fullscale->gain;
173 break;
174 case IIO_TEMP:
175 *val2 = pdata->current_fullscale->gain2;
176 break;
177 default:
178 err = -EINVAL;
179 goto read_error;
180 }
181
182 return IIO_VAL_INT_PLUS_NANO;
183 case IIO_CHAN_INFO_OFFSET:
184 switch (ch->type) {
185 case IIO_TEMP:
186 *val = 425;
187 *val2 = 10;
188 break;
189 default:
190 err = -EINVAL;
191 goto read_error;
192 }
193
194 return IIO_VAL_FRACTIONAL;
195 default:
196 return -EINVAL;
197 }
198
199read_error:
200 return err;
201}
202
203static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
204static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
205
206static struct attribute *st_press_attributes[] = {
207 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
208 &iio_dev_attr_sampling_frequency.dev_attr.attr,
209 NULL,
210};
211
212static const struct attribute_group st_press_attribute_group = {
213 .attrs = st_press_attributes,
214};
215
216static const struct iio_info press_info = {
217 .driver_module = THIS_MODULE,
218 .attrs = &st_press_attribute_group,
219 .read_raw = &st_press_read_raw,
220};
221
222#ifdef CONFIG_IIO_TRIGGER
223static const struct iio_trigger_ops st_press_trigger_ops = {
224 .owner = THIS_MODULE,
225 .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
226};
227#define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
228#else
229#define ST_PRESS_TRIGGER_OPS NULL
230#endif
231
23cde4d6
DC
232int st_press_common_probe(struct iio_dev *indio_dev,
233 struct st_sensors_platform_data *plat_data)
217494e5 234{
217494e5 235 struct st_sensor_data *pdata = iio_priv(indio_dev);
a6cc5b25
LJ
236 int irq = pdata->get_irq_data_ready(indio_dev);
237 int err;
217494e5
DC
238
239 indio_dev->modes = INDIO_DIRECT_MODE;
240 indio_dev->info = &press_info;
241
242 err = st_sensors_check_device_support(indio_dev,
a6cc5b25
LJ
243 ARRAY_SIZE(st_press_sensors),
244 st_press_sensors);
217494e5 245 if (err < 0)
a6cc5b25 246 return err;
217494e5
DC
247
248 pdata->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS;
a6cc5b25
LJ
249 pdata->multiread_bit = pdata->sensor->multi_read_bit;
250 indio_dev->channels = pdata->sensor->ch;
251 indio_dev->num_channels = pdata->sensor->num_ch;
217494e5 252
362f2f86
LJ
253 if (pdata->sensor->fs.addr != 0)
254 pdata->current_fullscale = (struct st_sensor_fullscale_avl *)
255 &pdata->sensor->fs.fs_avl[0];
256
217494e5
DC
257 pdata->odr = pdata->sensor->odr.odr_avl[0].hz;
258
38d1c6a9
LJ
259 /* Some devices don't support a data ready pin. */
260 if (!plat_data && pdata->sensor->drdy_irq.addr)
23cde4d6
DC
261 plat_data =
262 (struct st_sensors_platform_data *)&default_press_pdata;
263
264 err = st_sensors_init_sensor(indio_dev, plat_data);
217494e5 265 if (err < 0)
a6cc5b25 266 return err;
217494e5 267
a6cc5b25 268 if (irq > 0) {
217494e5
DC
269 err = st_press_allocate_ring(indio_dev);
270 if (err < 0)
a6cc5b25 271 return err;
217494e5
DC
272
273 err = st_sensors_allocate_trigger(indio_dev,
a6cc5b25 274 ST_PRESS_TRIGGER_OPS);
217494e5
DC
275 if (err < 0)
276 goto st_press_probe_trigger_error;
277 }
278
279 err = iio_device_register(indio_dev);
280 if (err)
281 goto st_press_device_register_error;
282
283 return err;
284
285st_press_device_register_error:
a6cc5b25 286 if (irq > 0)
217494e5
DC
287 st_sensors_deallocate_trigger(indio_dev);
288st_press_probe_trigger_error:
a6cc5b25 289 if (irq > 0)
217494e5 290 st_press_deallocate_ring(indio_dev);
a6cc5b25 291
217494e5
DC
292 return err;
293}
294EXPORT_SYMBOL(st_press_common_probe);
295
296void st_press_common_remove(struct iio_dev *indio_dev)
297{
298 struct st_sensor_data *pdata = iio_priv(indio_dev);
299
300 iio_device_unregister(indio_dev);
301 if (pdata->get_irq_data_ready(indio_dev) > 0) {
302 st_sensors_deallocate_trigger(indio_dev);
303 st_press_deallocate_ring(indio_dev);
304 }
217494e5
DC
305}
306EXPORT_SYMBOL(st_press_common_remove);
307
308MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
309MODULE_DESCRIPTION("STMicroelectronics pressures driver");
310MODULE_LICENSE("GPL v2");