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
31 #define MCELSIUS_PER_CELSIUS 1000
32
33 /* Default pressure sensitivity */
34 #define ST_PRESS_LSB_PER_MBAR 4096UL
35 #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \
36 ST_PRESS_LSB_PER_MBAR)
37
38 /* Default temperature sensitivity */
39 #define ST_PRESS_LSB_PER_CELSIUS 480UL
40 #define ST_PRESS_MILLI_CELSIUS_OFFSET 42500UL
41
42 #define ST_PRESS_NUMBER_DATA_CHANNELS 1
43
44 /* FULLSCALE */
45 #define ST_PRESS_FS_AVL_1100MB 1100
46 #define ST_PRESS_FS_AVL_1260MB 1260
47
48 #define ST_PRESS_1_OUT_XL_ADDR 0x28
49 #define ST_TEMP_1_OUT_L_ADDR 0x2b
50
51 /* CUSTOM VALUES FOR LPS331AP SENSOR */
52 #define ST_PRESS_LPS331AP_WAI_EXP 0xbb
53 #define ST_PRESS_LPS331AP_ODR_ADDR 0x20
54 #define ST_PRESS_LPS331AP_ODR_MASK 0x70
55 #define ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL 0x01
56 #define ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL 0x05
57 #define ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL 0x06
58 #define ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL 0x07
59 #define ST_PRESS_LPS331AP_PW_ADDR 0x20
60 #define ST_PRESS_LPS331AP_PW_MASK 0x80
61 #define ST_PRESS_LPS331AP_FS_ADDR 0x23
62 #define ST_PRESS_LPS331AP_FS_MASK 0x30
63 #define ST_PRESS_LPS331AP_BDU_ADDR 0x20
64 #define ST_PRESS_LPS331AP_BDU_MASK 0x04
65 #define ST_PRESS_LPS331AP_DRDY_IRQ_ADDR 0x22
66 #define ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK 0x04
67 #define ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK 0x20
68 #define ST_PRESS_LPS331AP_MULTIREAD_BIT true
69
70 /* CUSTOM VALUES FOR LPS001WP SENSOR */
71
72 /* LPS001WP pressure resolution */
73 #define ST_PRESS_LPS001WP_LSB_PER_MBAR 16UL
74 /* LPS001WP temperature resolution */
75 #define ST_PRESS_LPS001WP_LSB_PER_CELSIUS 64UL
76
77 #define ST_PRESS_LPS001WP_WAI_EXP 0xba
78 #define ST_PRESS_LPS001WP_ODR_ADDR 0x20
79 #define ST_PRESS_LPS001WP_ODR_MASK 0x30
80 #define ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL 0x01
81 #define ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL 0x02
82 #define ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL 0x03
83 #define ST_PRESS_LPS001WP_PW_ADDR 0x20
84 #define ST_PRESS_LPS001WP_PW_MASK 0x40
85 #define ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN \
86 (100000000UL / ST_PRESS_LPS001WP_LSB_PER_MBAR)
87 #define ST_PRESS_LPS001WP_BDU_ADDR 0x20
88 #define ST_PRESS_LPS001WP_BDU_MASK 0x04
89 #define ST_PRESS_LPS001WP_MULTIREAD_BIT true
90 #define ST_PRESS_LPS001WP_OUT_L_ADDR 0x28
91 #define ST_TEMP_LPS001WP_OUT_L_ADDR 0x2a
92
93 /* CUSTOM VALUES FOR LPS25H SENSOR */
94 #define ST_PRESS_LPS25H_WAI_EXP 0xbd
95 #define ST_PRESS_LPS25H_ODR_ADDR 0x20
96 #define ST_PRESS_LPS25H_ODR_MASK 0x70
97 #define ST_PRESS_LPS25H_ODR_AVL_1HZ_VAL 0x01
98 #define ST_PRESS_LPS25H_ODR_AVL_7HZ_VAL 0x02
99 #define ST_PRESS_LPS25H_ODR_AVL_13HZ_VAL 0x03
100 #define ST_PRESS_LPS25H_ODR_AVL_25HZ_VAL 0x04
101 #define ST_PRESS_LPS25H_PW_ADDR 0x20
102 #define ST_PRESS_LPS25H_PW_MASK 0x80
103 #define ST_PRESS_LPS25H_BDU_ADDR 0x20
104 #define ST_PRESS_LPS25H_BDU_MASK 0x04
105 #define ST_PRESS_LPS25H_DRDY_IRQ_ADDR 0x23
106 #define ST_PRESS_LPS25H_DRDY_IRQ_INT1_MASK 0x01
107 #define ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK 0x10
108 #define ST_PRESS_LPS25H_MULTIREAD_BIT true
109 #define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28
110 #define ST_TEMP_LPS25H_OUT_L_ADDR 0x2b
111
112 static const struct iio_chan_spec st_press_1_channels[] = {
113 {
114 .type = IIO_PRESSURE,
115 .channel2 = IIO_NO_MOD,
116 .address = ST_PRESS_1_OUT_XL_ADDR,
117 .scan_index = ST_SENSORS_SCAN_X,
118 .scan_type = {
119 .sign = 'u',
120 .realbits = 24,
121 .storagebits = 24,
122 .endianness = IIO_LE,
123 },
124 .info_mask_separate =
125 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
126 .modified = 0,
127 },
128 {
129 .type = IIO_TEMP,
130 .channel2 = IIO_NO_MOD,
131 .address = ST_TEMP_1_OUT_L_ADDR,
132 .scan_index = -1,
133 .scan_type = {
134 .sign = 'u',
135 .realbits = 16,
136 .storagebits = 16,
137 .endianness = IIO_LE,
138 },
139 .info_mask_separate =
140 BIT(IIO_CHAN_INFO_RAW) |
141 BIT(IIO_CHAN_INFO_SCALE) |
142 BIT(IIO_CHAN_INFO_OFFSET),
143 .modified = 0,
144 },
145 IIO_CHAN_SOFT_TIMESTAMP(1)
146 };
147
148 static const struct iio_chan_spec st_press_lps001wp_channels[] = {
149 {
150 .type = IIO_PRESSURE,
151 .channel2 = IIO_NO_MOD,
152 .address = ST_PRESS_LPS001WP_OUT_L_ADDR,
153 .scan_index = ST_SENSORS_SCAN_X,
154 .scan_type = {
155 .sign = 'u',
156 .realbits = 16,
157 .storagebits = 16,
158 .endianness = IIO_LE,
159 },
160 .info_mask_separate =
161 BIT(IIO_CHAN_INFO_RAW) |
162 BIT(IIO_CHAN_INFO_SCALE),
163 .modified = 0,
164 },
165 {
166 .type = IIO_TEMP,
167 .channel2 = IIO_NO_MOD,
168 .address = ST_TEMP_LPS001WP_OUT_L_ADDR,
169 .scan_index = -1,
170 .scan_type = {
171 .sign = 'u',
172 .realbits = 16,
173 .storagebits = 16,
174 .endianness = IIO_LE,
175 },
176 .info_mask_separate =
177 BIT(IIO_CHAN_INFO_RAW) |
178 BIT(IIO_CHAN_INFO_SCALE),
179 .modified = 0,
180 },
181 IIO_CHAN_SOFT_TIMESTAMP(1)
182 };
183
184 static const struct st_sensor_settings st_press_sensors_settings[] = {
185 {
186 .wai = ST_PRESS_LPS331AP_WAI_EXP,
187 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
188 .sensors_supported = {
189 [0] = LPS331AP_PRESS_DEV_NAME,
190 },
191 .ch = (struct iio_chan_spec *)st_press_1_channels,
192 .num_ch = ARRAY_SIZE(st_press_1_channels),
193 .odr = {
194 .addr = ST_PRESS_LPS331AP_ODR_ADDR,
195 .mask = ST_PRESS_LPS331AP_ODR_MASK,
196 .odr_avl = {
197 { 1, ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL, },
198 { 7, ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL, },
199 { 13, ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL, },
200 { 25, ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL, },
201 },
202 },
203 .pw = {
204 .addr = ST_PRESS_LPS331AP_PW_ADDR,
205 .mask = ST_PRESS_LPS331AP_PW_MASK,
206 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
207 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
208 },
209 .fs = {
210 .addr = ST_PRESS_LPS331AP_FS_ADDR,
211 .mask = ST_PRESS_LPS331AP_FS_MASK,
212 .fs_avl = {
213 /*
214 * Pressure and temperature sensitivity values
215 * as defined in table 3 of LPS331AP datasheet.
216 */
217 [0] = {
218 .num = ST_PRESS_FS_AVL_1260MB,
219 .gain = ST_PRESS_KPASCAL_NANO_SCALE,
220 .gain2 = ST_PRESS_LSB_PER_CELSIUS,
221 },
222 },
223 },
224 .bdu = {
225 .addr = ST_PRESS_LPS331AP_BDU_ADDR,
226 .mask = ST_PRESS_LPS331AP_BDU_MASK,
227 },
228 .drdy_irq = {
229 .addr = ST_PRESS_LPS331AP_DRDY_IRQ_ADDR,
230 .mask_int1 = ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK,
231 .mask_int2 = ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK,
232 },
233 .multi_read_bit = ST_PRESS_LPS331AP_MULTIREAD_BIT,
234 .bootime = 2,
235 },
236 {
237 .wai = ST_PRESS_LPS001WP_WAI_EXP,
238 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
239 .sensors_supported = {
240 [0] = LPS001WP_PRESS_DEV_NAME,
241 },
242 .ch = (struct iio_chan_spec *)st_press_lps001wp_channels,
243 .num_ch = ARRAY_SIZE(st_press_lps001wp_channels),
244 .odr = {
245 .addr = ST_PRESS_LPS001WP_ODR_ADDR,
246 .mask = ST_PRESS_LPS001WP_ODR_MASK,
247 .odr_avl = {
248 { 1, ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL, },
249 { 7, ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL, },
250 { 13, ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL, },
251 },
252 },
253 .pw = {
254 .addr = ST_PRESS_LPS001WP_PW_ADDR,
255 .mask = ST_PRESS_LPS001WP_PW_MASK,
256 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
257 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
258 },
259 .fs = {
260 .fs_avl = {
261 /*
262 * Pressure and temperature resolution values
263 * as defined in table 3 of LPS001WP datasheet.
264 */
265 [0] = {
266 .num = ST_PRESS_FS_AVL_1100MB,
267 .gain = ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN,
268 .gain2 = ST_PRESS_LPS001WP_LSB_PER_CELSIUS,
269 },
270 },
271 },
272 .bdu = {
273 .addr = ST_PRESS_LPS001WP_BDU_ADDR,
274 .mask = ST_PRESS_LPS001WP_BDU_MASK,
275 },
276 .drdy_irq = {
277 .addr = 0,
278 },
279 .multi_read_bit = ST_PRESS_LPS001WP_MULTIREAD_BIT,
280 .bootime = 2,
281 },
282 {
283 .wai = ST_PRESS_LPS25H_WAI_EXP,
284 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
285 .sensors_supported = {
286 [0] = LPS25H_PRESS_DEV_NAME,
287 },
288 .ch = (struct iio_chan_spec *)st_press_1_channels,
289 .num_ch = ARRAY_SIZE(st_press_1_channels),
290 .odr = {
291 .addr = ST_PRESS_LPS25H_ODR_ADDR,
292 .mask = ST_PRESS_LPS25H_ODR_MASK,
293 .odr_avl = {
294 { 1, ST_PRESS_LPS25H_ODR_AVL_1HZ_VAL, },
295 { 7, ST_PRESS_LPS25H_ODR_AVL_7HZ_VAL, },
296 { 13, ST_PRESS_LPS25H_ODR_AVL_13HZ_VAL, },
297 { 25, ST_PRESS_LPS25H_ODR_AVL_25HZ_VAL, },
298 },
299 },
300 .pw = {
301 .addr = ST_PRESS_LPS25H_PW_ADDR,
302 .mask = ST_PRESS_LPS25H_PW_MASK,
303 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
304 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
305 },
306 .fs = {
307 .fs_avl = {
308 /*
309 * Pressure and temperature sensitivity values
310 * as defined in table 3 of LPS25H datasheet.
311 */
312 [0] = {
313 .num = ST_PRESS_FS_AVL_1260MB,
314 .gain = ST_PRESS_KPASCAL_NANO_SCALE,
315 .gain2 = ST_PRESS_LSB_PER_CELSIUS,
316 },
317 },
318 },
319 .bdu = {
320 .addr = ST_PRESS_LPS25H_BDU_ADDR,
321 .mask = ST_PRESS_LPS25H_BDU_MASK,
322 },
323 .drdy_irq = {
324 .addr = ST_PRESS_LPS25H_DRDY_IRQ_ADDR,
325 .mask_int1 = ST_PRESS_LPS25H_DRDY_IRQ_INT1_MASK,
326 .mask_int2 = ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK,
327 },
328 .multi_read_bit = ST_PRESS_LPS25H_MULTIREAD_BIT,
329 .bootime = 2,
330 },
331 };
332
st_press_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * ch,int val,int val2,long mask)333 static int st_press_write_raw(struct iio_dev *indio_dev,
334 struct iio_chan_spec const *ch,
335 int val,
336 int val2,
337 long mask)
338 {
339 int err;
340
341 switch (mask) {
342 case IIO_CHAN_INFO_SAMP_FREQ:
343 if (val2)
344 return -EINVAL;
345 mutex_lock(&indio_dev->mlock);
346 err = st_sensors_set_odr(indio_dev, val);
347 mutex_unlock(&indio_dev->mlock);
348 return err;
349 default:
350 return -EINVAL;
351 }
352 }
353
st_press_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * ch,int * val,int * val2,long mask)354 static int st_press_read_raw(struct iio_dev *indio_dev,
355 struct iio_chan_spec const *ch, int *val,
356 int *val2, long mask)
357 {
358 int err;
359 struct st_sensor_data *press_data = iio_priv(indio_dev);
360
361 switch (mask) {
362 case IIO_CHAN_INFO_RAW:
363 err = st_sensors_read_info_raw(indio_dev, ch, val);
364 if (err < 0)
365 goto read_error;
366
367 return IIO_VAL_INT;
368 case IIO_CHAN_INFO_SCALE:
369 switch (ch->type) {
370 case IIO_PRESSURE:
371 *val = 0;
372 *val2 = press_data->current_fullscale->gain;
373 return IIO_VAL_INT_PLUS_NANO;
374 case IIO_TEMP:
375 *val = MCELSIUS_PER_CELSIUS;
376 *val2 = press_data->current_fullscale->gain2;
377 return IIO_VAL_FRACTIONAL;
378 default:
379 err = -EINVAL;
380 goto read_error;
381 }
382
383 case IIO_CHAN_INFO_OFFSET:
384 switch (ch->type) {
385 case IIO_TEMP:
386 *val = ST_PRESS_MILLI_CELSIUS_OFFSET *
387 press_data->current_fullscale->gain2;
388 *val2 = MCELSIUS_PER_CELSIUS;
389 break;
390 default:
391 err = -EINVAL;
392 goto read_error;
393 }
394
395 return IIO_VAL_FRACTIONAL;
396 case IIO_CHAN_INFO_SAMP_FREQ:
397 *val = press_data->odr;
398 return IIO_VAL_INT;
399 default:
400 return -EINVAL;
401 }
402
403 read_error:
404 return err;
405 }
406
407 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
408
409 static struct attribute *st_press_attributes[] = {
410 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
411 NULL,
412 };
413
414 static const struct attribute_group st_press_attribute_group = {
415 .attrs = st_press_attributes,
416 };
417
418 static const struct iio_info press_info = {
419 .driver_module = THIS_MODULE,
420 .attrs = &st_press_attribute_group,
421 .read_raw = &st_press_read_raw,
422 .write_raw = &st_press_write_raw,
423 .debugfs_reg_access = &st_sensors_debugfs_reg_access,
424 };
425
426 #ifdef CONFIG_IIO_TRIGGER
427 static const struct iio_trigger_ops st_press_trigger_ops = {
428 .owner = THIS_MODULE,
429 .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
430 };
431 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
432 #else
433 #define ST_PRESS_TRIGGER_OPS NULL
434 #endif
435
st_press_common_probe(struct iio_dev * indio_dev)436 int st_press_common_probe(struct iio_dev *indio_dev)
437 {
438 struct st_sensor_data *press_data = iio_priv(indio_dev);
439 struct st_sensors_platform_data *pdata =
440 (struct st_sensors_platform_data *)press_data->dev->platform_data;
441 int irq = press_data->get_irq_data_ready(indio_dev);
442 int err;
443
444 indio_dev->modes = INDIO_DIRECT_MODE;
445 indio_dev->info = &press_info;
446 mutex_init(&press_data->tb.buf_lock);
447
448 st_sensors_power_enable(indio_dev);
449
450 err = st_sensors_check_device_support(indio_dev,
451 ARRAY_SIZE(st_press_sensors_settings),
452 st_press_sensors_settings);
453 if (err < 0)
454 return err;
455
456 press_data->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS;
457 press_data->multiread_bit = press_data->sensor_settings->multi_read_bit;
458 indio_dev->channels = press_data->sensor_settings->ch;
459 indio_dev->num_channels = press_data->sensor_settings->num_ch;
460
461 if (press_data->sensor_settings->fs.addr != 0)
462 press_data->current_fullscale =
463 (struct st_sensor_fullscale_avl *)
464 &press_data->sensor_settings->fs.fs_avl[0];
465
466 press_data->odr = press_data->sensor_settings->odr.odr_avl[0].hz;
467
468 /* Some devices don't support a data ready pin. */
469 if (!pdata && press_data->sensor_settings->drdy_irq.addr)
470 pdata = (struct st_sensors_platform_data *)&default_press_pdata;
471
472 err = st_sensors_init_sensor(indio_dev, pdata);
473 if (err < 0)
474 return err;
475
476 err = st_press_allocate_ring(indio_dev);
477 if (err < 0)
478 return err;
479
480 if (irq > 0) {
481 err = st_sensors_allocate_trigger(indio_dev,
482 ST_PRESS_TRIGGER_OPS);
483 if (err < 0)
484 goto st_press_probe_trigger_error;
485 }
486
487 err = iio_device_register(indio_dev);
488 if (err)
489 goto st_press_device_register_error;
490
491 dev_info(&indio_dev->dev, "registered pressure sensor %s\n",
492 indio_dev->name);
493
494 return err;
495
496 st_press_device_register_error:
497 if (irq > 0)
498 st_sensors_deallocate_trigger(indio_dev);
499 st_press_probe_trigger_error:
500 st_press_deallocate_ring(indio_dev);
501
502 return err;
503 }
504 EXPORT_SYMBOL(st_press_common_probe);
505
st_press_common_remove(struct iio_dev * indio_dev)506 void st_press_common_remove(struct iio_dev *indio_dev)
507 {
508 struct st_sensor_data *press_data = iio_priv(indio_dev);
509
510 st_sensors_power_disable(indio_dev);
511
512 iio_device_unregister(indio_dev);
513 if (press_data->get_irq_data_ready(indio_dev) > 0)
514 st_sensors_deallocate_trigger(indio_dev);
515
516 st_press_deallocate_ring(indio_dev);
517 }
518 EXPORT_SYMBOL(st_press_common_remove);
519
520 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
521 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
522 MODULE_LICENSE("GPL v2");
523