]>
Commit | Line | Data |
---|---|---|
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 | |
7885a8ce LJ |
67 | /* CUSTOM VALUES FOR LPS001WP SENSOR */ |
68 | #define ST_PRESS_LPS001WP_WAI_EXP 0xba | |
69 | #define ST_PRESS_LPS001WP_ODR_ADDR 0x20 | |
70 | #define ST_PRESS_LPS001WP_ODR_MASK 0x30 | |
71 | #define ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL 0x01 | |
72 | #define ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL 0x02 | |
73 | #define ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL 0x03 | |
74 | #define ST_PRESS_LPS001WP_PW_ADDR 0x20 | |
75 | #define ST_PRESS_LPS001WP_PW_MASK 0x40 | |
76 | #define ST_PRESS_LPS001WP_BDU_ADDR 0x20 | |
77 | #define ST_PRESS_LPS001WP_BDU_MASK 0x04 | |
78 | #define ST_PRESS_LPS001WP_MULTIREAD_BIT true | |
79 | #define ST_PRESS_LPS001WP_OUT_L_ADDR 0x28 | |
80 | #define ST_TEMP_LPS001WP_OUT_L_ADDR 0x2a | |
81 | ||
2f5effcb LJ |
82 | static const struct iio_chan_spec st_press_lps331ap_channels[] = { |
83 | { | |
84 | .type = IIO_PRESSURE, | |
85 | .channel2 = IIO_NO_MOD, | |
86 | .address = ST_PRESS_LPS331AP_OUT_XL_ADDR, | |
87 | .scan_index = ST_SENSORS_SCAN_X, | |
88 | .scan_type = { | |
89 | .sign = 'u', | |
90 | .realbits = 24, | |
91 | .storagebits = 24, | |
92 | .endianness = IIO_LE, | |
93 | }, | |
94 | .info_mask_separate = | |
217494e5 | 95 | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), |
2f5effcb LJ |
96 | .modified = 0, |
97 | }, | |
98 | { | |
99 | .type = IIO_TEMP, | |
100 | .channel2 = IIO_NO_MOD, | |
101 | .address = ST_TEMP_LPS331AP_OUT_L_ADDR, | |
102 | .scan_index = -1, | |
103 | .scan_type = { | |
104 | .sign = 'u', | |
105 | .realbits = 16, | |
106 | .storagebits = 16, | |
107 | .endianness = IIO_LE, | |
108 | }, | |
109 | .info_mask_separate = | |
110 | BIT(IIO_CHAN_INFO_RAW) | | |
111 | BIT(IIO_CHAN_INFO_SCALE) | | |
112 | BIT(IIO_CHAN_INFO_OFFSET), | |
113 | .modified = 0, | |
114 | }, | |
217494e5 DC |
115 | IIO_CHAN_SOFT_TIMESTAMP(1) |
116 | }; | |
117 | ||
7885a8ce LJ |
118 | static const struct iio_chan_spec st_press_lps001wp_channels[] = { |
119 | { | |
120 | .type = IIO_PRESSURE, | |
121 | .channel2 = IIO_NO_MOD, | |
122 | .address = ST_PRESS_LPS001WP_OUT_L_ADDR, | |
123 | .scan_index = ST_SENSORS_SCAN_X, | |
124 | .scan_type = { | |
125 | .sign = 'u', | |
126 | .realbits = 16, | |
127 | .storagebits = 16, | |
128 | .endianness = IIO_LE, | |
129 | }, | |
130 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), | |
131 | .modified = 0, | |
132 | }, | |
133 | { | |
134 | .type = IIO_TEMP, | |
135 | .channel2 = IIO_NO_MOD, | |
136 | .address = ST_TEMP_LPS001WP_OUT_L_ADDR, | |
137 | .scan_index = -1, | |
138 | .scan_type = { | |
139 | .sign = 'u', | |
140 | .realbits = 16, | |
141 | .storagebits = 16, | |
142 | .endianness = IIO_LE, | |
143 | }, | |
144 | .info_mask_separate = | |
145 | BIT(IIO_CHAN_INFO_RAW) | | |
146 | BIT(IIO_CHAN_INFO_OFFSET), | |
147 | .modified = 0, | |
148 | }, | |
149 | IIO_CHAN_SOFT_TIMESTAMP(1) | |
150 | }; | |
151 | ||
217494e5 DC |
152 | static const struct st_sensors st_press_sensors[] = { |
153 | { | |
302fbd50 | 154 | .wai = ST_PRESS_LPS331AP_WAI_EXP, |
217494e5 DC |
155 | .sensors_supported = { |
156 | [0] = LPS331AP_PRESS_DEV_NAME, | |
157 | }, | |
2f5effcb | 158 | .ch = (struct iio_chan_spec *)st_press_lps331ap_channels, |
ea01f2c1 | 159 | .num_ch = ARRAY_SIZE(st_press_lps331ap_channels), |
217494e5 | 160 | .odr = { |
302fbd50 LJ |
161 | .addr = ST_PRESS_LPS331AP_ODR_ADDR, |
162 | .mask = ST_PRESS_LPS331AP_ODR_MASK, | |
217494e5 | 163 | .odr_avl = { |
302fbd50 LJ |
164 | { 1, ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL, }, |
165 | { 7, ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL, }, | |
166 | { 13, ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL, }, | |
167 | { 25, ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL, }, | |
217494e5 DC |
168 | }, |
169 | }, | |
170 | .pw = { | |
302fbd50 LJ |
171 | .addr = ST_PRESS_LPS331AP_PW_ADDR, |
172 | .mask = ST_PRESS_LPS331AP_PW_MASK, | |
217494e5 DC |
173 | .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, |
174 | .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, | |
175 | }, | |
176 | .fs = { | |
302fbd50 LJ |
177 | .addr = ST_PRESS_LPS331AP_FS_ADDR, |
178 | .mask = ST_PRESS_LPS331AP_FS_MASK, | |
217494e5 DC |
179 | .fs_avl = { |
180 | [0] = { | |
181 | .num = ST_PRESS_FS_AVL_1260MB, | |
302fbd50 LJ |
182 | .value = ST_PRESS_LPS331AP_FS_AVL_1260_VAL, |
183 | .gain = ST_PRESS_LPS331AP_FS_AVL_1260_GAIN, | |
184 | .gain2 = ST_PRESS_LPS331AP_FS_AVL_TEMP_GAIN, | |
217494e5 DC |
185 | }, |
186 | }, | |
187 | }, | |
188 | .bdu = { | |
302fbd50 LJ |
189 | .addr = ST_PRESS_LPS331AP_BDU_ADDR, |
190 | .mask = ST_PRESS_LPS331AP_BDU_MASK, | |
217494e5 DC |
191 | }, |
192 | .drdy_irq = { | |
302fbd50 LJ |
193 | .addr = ST_PRESS_LPS331AP_DRDY_IRQ_ADDR, |
194 | .mask_int1 = ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK, | |
195 | .mask_int2 = ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK, | |
217494e5 | 196 | }, |
302fbd50 | 197 | .multi_read_bit = ST_PRESS_LPS331AP_MULTIREAD_BIT, |
217494e5 DC |
198 | .bootime = 2, |
199 | }, | |
7885a8ce LJ |
200 | { |
201 | .wai = ST_PRESS_LPS001WP_WAI_EXP, | |
202 | .sensors_supported = { | |
203 | [0] = LPS001WP_PRESS_DEV_NAME, | |
204 | }, | |
205 | .ch = (struct iio_chan_spec *)st_press_lps001wp_channels, | |
206 | .num_ch = ARRAY_SIZE(st_press_lps001wp_channels), | |
207 | .odr = { | |
208 | .addr = ST_PRESS_LPS001WP_ODR_ADDR, | |
209 | .mask = ST_PRESS_LPS001WP_ODR_MASK, | |
210 | .odr_avl = { | |
211 | { 1, ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL, }, | |
212 | { 7, ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL, }, | |
213 | { 13, ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL, }, | |
214 | }, | |
215 | }, | |
216 | .pw = { | |
217 | .addr = ST_PRESS_LPS001WP_PW_ADDR, | |
218 | .mask = ST_PRESS_LPS001WP_PW_MASK, | |
219 | .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, | |
220 | .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, | |
221 | }, | |
222 | .fs = { | |
223 | .addr = 0, | |
224 | }, | |
225 | .bdu = { | |
226 | .addr = ST_PRESS_LPS001WP_BDU_ADDR, | |
227 | .mask = ST_PRESS_LPS001WP_BDU_MASK, | |
228 | }, | |
229 | .drdy_irq = { | |
230 | .addr = 0, | |
231 | }, | |
232 | .multi_read_bit = ST_PRESS_LPS001WP_MULTIREAD_BIT, | |
233 | .bootime = 2, | |
234 | }, | |
217494e5 DC |
235 | }; |
236 | ||
237 | static int st_press_read_raw(struct iio_dev *indio_dev, | |
238 | struct iio_chan_spec const *ch, int *val, | |
239 | int *val2, long mask) | |
240 | { | |
241 | int err; | |
242 | struct st_sensor_data *pdata = iio_priv(indio_dev); | |
243 | ||
244 | switch (mask) { | |
245 | case IIO_CHAN_INFO_RAW: | |
246 | err = st_sensors_read_info_raw(indio_dev, ch, val); | |
247 | if (err < 0) | |
248 | goto read_error; | |
249 | ||
250 | return IIO_VAL_INT; | |
251 | case IIO_CHAN_INFO_SCALE: | |
252 | *val = 0; | |
253 | ||
254 | switch (ch->type) { | |
255 | case IIO_PRESSURE: | |
256 | *val2 = pdata->current_fullscale->gain; | |
257 | break; | |
258 | case IIO_TEMP: | |
259 | *val2 = pdata->current_fullscale->gain2; | |
260 | break; | |
261 | default: | |
262 | err = -EINVAL; | |
263 | goto read_error; | |
264 | } | |
265 | ||
266 | return IIO_VAL_INT_PLUS_NANO; | |
267 | case IIO_CHAN_INFO_OFFSET: | |
268 | switch (ch->type) { | |
269 | case IIO_TEMP: | |
270 | *val = 425; | |
271 | *val2 = 10; | |
272 | break; | |
273 | default: | |
274 | err = -EINVAL; | |
275 | goto read_error; | |
276 | } | |
277 | ||
278 | return IIO_VAL_FRACTIONAL; | |
279 | default: | |
280 | return -EINVAL; | |
281 | } | |
282 | ||
283 | read_error: | |
284 | return err; | |
285 | } | |
286 | ||
287 | static ST_SENSOR_DEV_ATTR_SAMP_FREQ(); | |
288 | static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); | |
289 | ||
290 | static struct attribute *st_press_attributes[] = { | |
291 | &iio_dev_attr_sampling_frequency_available.dev_attr.attr, | |
292 | &iio_dev_attr_sampling_frequency.dev_attr.attr, | |
293 | NULL, | |
294 | }; | |
295 | ||
296 | static const struct attribute_group st_press_attribute_group = { | |
297 | .attrs = st_press_attributes, | |
298 | }; | |
299 | ||
300 | static const struct iio_info press_info = { | |
301 | .driver_module = THIS_MODULE, | |
302 | .attrs = &st_press_attribute_group, | |
303 | .read_raw = &st_press_read_raw, | |
304 | }; | |
305 | ||
306 | #ifdef CONFIG_IIO_TRIGGER | |
307 | static const struct iio_trigger_ops st_press_trigger_ops = { | |
308 | .owner = THIS_MODULE, | |
309 | .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE, | |
310 | }; | |
311 | #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops) | |
312 | #else | |
313 | #define ST_PRESS_TRIGGER_OPS NULL | |
314 | #endif | |
315 | ||
23cde4d6 DC |
316 | int st_press_common_probe(struct iio_dev *indio_dev, |
317 | struct st_sensors_platform_data *plat_data) | |
217494e5 | 318 | { |
217494e5 | 319 | struct st_sensor_data *pdata = iio_priv(indio_dev); |
a6cc5b25 LJ |
320 | int irq = pdata->get_irq_data_ready(indio_dev); |
321 | int err; | |
217494e5 DC |
322 | |
323 | indio_dev->modes = INDIO_DIRECT_MODE; | |
324 | indio_dev->info = &press_info; | |
325 | ||
326 | err = st_sensors_check_device_support(indio_dev, | |
a6cc5b25 LJ |
327 | ARRAY_SIZE(st_press_sensors), |
328 | st_press_sensors); | |
217494e5 | 329 | if (err < 0) |
a6cc5b25 | 330 | return err; |
217494e5 DC |
331 | |
332 | pdata->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS; | |
a6cc5b25 LJ |
333 | pdata->multiread_bit = pdata->sensor->multi_read_bit; |
334 | indio_dev->channels = pdata->sensor->ch; | |
335 | indio_dev->num_channels = pdata->sensor->num_ch; | |
217494e5 | 336 | |
362f2f86 LJ |
337 | if (pdata->sensor->fs.addr != 0) |
338 | pdata->current_fullscale = (struct st_sensor_fullscale_avl *) | |
339 | &pdata->sensor->fs.fs_avl[0]; | |
340 | ||
217494e5 DC |
341 | pdata->odr = pdata->sensor->odr.odr_avl[0].hz; |
342 | ||
38d1c6a9 LJ |
343 | /* Some devices don't support a data ready pin. */ |
344 | if (!plat_data && pdata->sensor->drdy_irq.addr) | |
23cde4d6 DC |
345 | plat_data = |
346 | (struct st_sensors_platform_data *)&default_press_pdata; | |
347 | ||
348 | err = st_sensors_init_sensor(indio_dev, plat_data); | |
217494e5 | 349 | if (err < 0) |
a6cc5b25 | 350 | return err; |
217494e5 | 351 | |
7a137c9c DC |
352 | err = st_press_allocate_ring(indio_dev); |
353 | if (err < 0) | |
354 | return err; | |
217494e5 | 355 | |
7a137c9c | 356 | if (irq > 0) { |
217494e5 | 357 | err = st_sensors_allocate_trigger(indio_dev, |
a6cc5b25 | 358 | ST_PRESS_TRIGGER_OPS); |
217494e5 DC |
359 | if (err < 0) |
360 | goto st_press_probe_trigger_error; | |
361 | } | |
362 | ||
363 | err = iio_device_register(indio_dev); | |
364 | if (err) | |
365 | goto st_press_device_register_error; | |
366 | ||
367 | return err; | |
368 | ||
369 | st_press_device_register_error: | |
a6cc5b25 | 370 | if (irq > 0) |
217494e5 DC |
371 | st_sensors_deallocate_trigger(indio_dev); |
372 | st_press_probe_trigger_error: | |
7a137c9c | 373 | st_press_deallocate_ring(indio_dev); |
a6cc5b25 | 374 | |
217494e5 DC |
375 | return err; |
376 | } | |
377 | EXPORT_SYMBOL(st_press_common_probe); | |
378 | ||
379 | void st_press_common_remove(struct iio_dev *indio_dev) | |
380 | { | |
381 | struct st_sensor_data *pdata = iio_priv(indio_dev); | |
382 | ||
383 | iio_device_unregister(indio_dev); | |
7a137c9c | 384 | if (pdata->get_irq_data_ready(indio_dev) > 0) |
217494e5 | 385 | st_sensors_deallocate_trigger(indio_dev); |
7a137c9c DC |
386 | |
387 | st_press_deallocate_ring(indio_dev); | |
217494e5 DC |
388 | } |
389 | EXPORT_SYMBOL(st_press_common_remove); | |
390 | ||
391 | MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); | |
392 | MODULE_DESCRIPTION("STMicroelectronics pressures driver"); | |
393 | MODULE_LICENSE("GPL v2"); |