1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics pressures driver
4  *
5  * Copyright 2013 STMicroelectronics Inc.
6  *
7  * Denis Ciocca <denis.ciocca@st.com>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/errno.h>
14 #include <linux/types.h>
15 #include <linux/mutex.h>
16 #include <linux/interrupt.h>
17 #include <linux/i2c.h>
18 #include <linux/gpio.h>
19 #include <linux/irq.h>
20 #include <linux/delay.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/trigger.h>
24 #include <linux/iio/buffer.h>
25 #include <asm/unaligned.h>
26 
27 #include <linux/iio/common/st_sensors.h>
28 #include "st_pressure.h"
29 
30 /*
31  * About determining pressure scaling factors
32  * ------------------------------------------
33  *
34  * Datasheets specify typical pressure sensitivity so that pressure is computed
35  * according to the following equation :
36  *     pressure[mBar] = raw / sensitivity
37  * where :
38  *     raw          the 24 bits long raw sampled pressure
39  *     sensitivity  a scaling factor specified by the datasheet in LSB/mBar
40  *
41  * IIO ABI expects pressure to be expressed as kPascal, hence pressure should be
42  * computed according to :
43  *     pressure[kPascal] = pressure[mBar] / 10
44  *                       = raw / (sensitivity * 10)                          (1)
45  *
46  * Finally, st_press_read_raw() returns pressure scaling factor as an
47  * IIO_VAL_INT_PLUS_NANO with a zero integral part and "gain" as decimal part.
48  * Therefore, from (1), "gain" becomes :
49  *     gain = 10^9 / (sensitivity * 10)
50  *          = 10^8 / sensitivity
51  *
52  * About determining temperature scaling factors and offsets
53  * ---------------------------------------------------------
54  *
55  * Datasheets specify typical temperature sensitivity and offset so that
56  * temperature is computed according to the following equation :
57  *     temp[Celsius] = offset[Celsius] + (raw / sensitivity)
58  * where :
59  *     raw          the 16 bits long raw sampled temperature
60  *     offset       a constant specified by the datasheet in degree Celsius
61  *                  (sometimes zero)
62  *     sensitivity  a scaling factor specified by the datasheet in LSB/Celsius
63  *
64  * IIO ABI expects temperature to be expressed as milli degree Celsius such as
65  * user space should compute temperature according to :
66  *     temp[mCelsius] = temp[Celsius] * 10^3
67  *                    = (offset[Celsius] + (raw / sensitivity)) * 10^3
68  *                    = ((offset[Celsius] * sensitivity) + raw) *
69  *                      (10^3 / sensitivity)                                 (2)
70  *
71  * IIO ABI expects user space to apply offset and scaling factors to raw samples
72  * according to :
73  *     temp[mCelsius] = (OFFSET + raw) * SCALE
74  * where :
75  *     OFFSET an arbitrary constant exposed by device
76  *     SCALE  an arbitrary scaling factor exposed by device
77  *
78  * Matching OFFSET and SCALE with members of (2) gives :
79  *     OFFSET = offset[Celsius] * sensitivity                                (3)
80  *     SCALE  = 10^3 / sensitivity                                           (4)
81  *
82  * st_press_read_raw() returns temperature scaling factor as an
83  * IIO_VAL_FRACTIONAL with a 10^3 numerator and "gain2" as denominator.
84  * Therefore, from (3), "gain2" becomes :
85  *     gain2 = sensitivity
86  *
87  * When declared within channel, i.e. for a non zero specified offset,
88  * st_press_read_raw() will return the latter as an IIO_VAL_FRACTIONAL such as :
89  *     numerator = OFFSET * 10^3
90  *     denominator = 10^3
91  * giving from (4):
92  *     numerator = offset[Celsius] * 10^3 * sensitivity
93  *               = offset[mCelsius] * gain2
94  */
95 
96 #define MCELSIUS_PER_CELSIUS			1000
97 
98 /* Default pressure sensitivity */
99 #define ST_PRESS_LSB_PER_MBAR			4096UL
100 #define ST_PRESS_KPASCAL_NANO_SCALE		(100000000UL / \
101 						 ST_PRESS_LSB_PER_MBAR)
102 
103 /* Default temperature sensitivity */
104 #define ST_PRESS_LSB_PER_CELSIUS		480UL
105 #define ST_PRESS_MILLI_CELSIUS_OFFSET		42500UL
106 
107 /* FULLSCALE */
108 #define ST_PRESS_FS_AVL_1100MB			1100
109 #define ST_PRESS_FS_AVL_1260MB			1260
110 
111 #define ST_PRESS_1_OUT_XL_ADDR			0x28
112 #define ST_TEMP_1_OUT_L_ADDR			0x2b
113 
114 /* LPS001WP pressure resolution */
115 #define ST_PRESS_LPS001WP_LSB_PER_MBAR		16UL
116 /* LPS001WP temperature resolution */
117 #define ST_PRESS_LPS001WP_LSB_PER_CELSIUS	64UL
118 /* LPS001WP pressure gain */
119 #define ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN \
120 	(100000000UL / ST_PRESS_LPS001WP_LSB_PER_MBAR)
121 /* LPS001WP pressure and temp L addresses */
122 #define ST_PRESS_LPS001WP_OUT_L_ADDR		0x28
123 #define ST_TEMP_LPS001WP_OUT_L_ADDR		0x2a
124 
125 /* LPS25H pressure and temp L addresses */
126 #define ST_PRESS_LPS25H_OUT_XL_ADDR		0x28
127 #define ST_TEMP_LPS25H_OUT_L_ADDR		0x2b
128 
129 /* LPS22HB temperature sensitivity */
130 #define ST_PRESS_LPS22HB_LSB_PER_CELSIUS	100UL
131 
132 static const struct iio_chan_spec st_press_1_channels[] = {
133 	{
134 		.type = IIO_PRESSURE,
135 		.address = ST_PRESS_1_OUT_XL_ADDR,
136 		.scan_index = 0,
137 		.scan_type = {
138 			.sign = 's',
139 			.realbits = 24,
140 			.storagebits = 32,
141 			.endianness = IIO_LE,
142 		},
143 		.info_mask_separate =
144 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
145 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
146 	},
147 	{
148 		.type = IIO_TEMP,
149 		.address = ST_TEMP_1_OUT_L_ADDR,
150 		.scan_index = 1,
151 		.scan_type = {
152 			.sign = 's',
153 			.realbits = 16,
154 			.storagebits = 16,
155 			.endianness = IIO_LE,
156 		},
157 		.info_mask_separate =
158 			BIT(IIO_CHAN_INFO_RAW) |
159 			BIT(IIO_CHAN_INFO_SCALE) |
160 			BIT(IIO_CHAN_INFO_OFFSET),
161 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
162 	},
163 	IIO_CHAN_SOFT_TIMESTAMP(2)
164 };
165 
166 static const struct iio_chan_spec st_press_lps001wp_channels[] = {
167 	{
168 		.type = IIO_PRESSURE,
169 		.address = ST_PRESS_LPS001WP_OUT_L_ADDR,
170 		.scan_index = 0,
171 		.scan_type = {
172 			.sign = 's',
173 			.realbits = 16,
174 			.storagebits = 16,
175 			.endianness = IIO_LE,
176 		},
177 		.info_mask_separate =
178 			BIT(IIO_CHAN_INFO_RAW) |
179 			BIT(IIO_CHAN_INFO_SCALE),
180 	},
181 	{
182 		.type = IIO_TEMP,
183 		.address = ST_TEMP_LPS001WP_OUT_L_ADDR,
184 		.scan_index = 1,
185 		.scan_type = {
186 			.sign = 's',
187 			.realbits = 16,
188 			.storagebits = 16,
189 			.endianness = IIO_LE,
190 		},
191 		.info_mask_separate =
192 			BIT(IIO_CHAN_INFO_RAW) |
193 			BIT(IIO_CHAN_INFO_SCALE),
194 	},
195 	IIO_CHAN_SOFT_TIMESTAMP(2)
196 };
197 
198 static const struct iio_chan_spec st_press_lps22hb_channels[] = {
199 	{
200 		.type = IIO_PRESSURE,
201 		.address = ST_PRESS_1_OUT_XL_ADDR,
202 		.scan_index = 0,
203 		.scan_type = {
204 			.sign = 's',
205 			.realbits = 24,
206 			.storagebits = 32,
207 			.endianness = IIO_LE,
208 		},
209 		.info_mask_separate =
210 			BIT(IIO_CHAN_INFO_RAW) |
211 			BIT(IIO_CHAN_INFO_SCALE),
212 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
213 	},
214 	{
215 		.type = IIO_TEMP,
216 		.address = ST_TEMP_1_OUT_L_ADDR,
217 		.scan_index = 1,
218 		.scan_type = {
219 			.sign = 's',
220 			.realbits = 16,
221 			.storagebits = 16,
222 			.endianness = IIO_LE,
223 		},
224 		.info_mask_separate =
225 			BIT(IIO_CHAN_INFO_RAW) |
226 			BIT(IIO_CHAN_INFO_SCALE),
227 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
228 	},
229 	IIO_CHAN_SOFT_TIMESTAMP(2)
230 };
231 
232 static const struct st_sensor_settings st_press_sensors_settings[] = {
233 	{
234 		/*
235 		 * CUSTOM VALUES FOR LPS331AP SENSOR
236 		 * See LPS331AP datasheet:
237 		 * http://www2.st.com/resource/en/datasheet/lps331ap.pdf
238 		 */
239 		.wai = 0xbb,
240 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
241 		.sensors_supported = {
242 			[0] = LPS331AP_PRESS_DEV_NAME,
243 		},
244 		.ch = (struct iio_chan_spec *)st_press_1_channels,
245 		.num_ch = ARRAY_SIZE(st_press_1_channels),
246 		.odr = {
247 			.addr = 0x20,
248 			.mask = 0x70,
249 			.odr_avl = {
250 				{ .hz = 1, .value = 0x01 },
251 				{ .hz = 7, .value = 0x05 },
252 				{ .hz = 13, .value = 0x06 },
253 				{ .hz = 25, .value = 0x07 },
254 			},
255 		},
256 		.pw = {
257 			.addr = 0x20,
258 			.mask = 0x80,
259 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
260 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
261 		},
262 		.fs = {
263 			.addr = 0x23,
264 			.mask = 0x30,
265 			.fs_avl = {
266 				/*
267 				 * Pressure and temperature sensitivity values
268 				 * as defined in table 3 of LPS331AP datasheet.
269 				 */
270 				[0] = {
271 					.num = ST_PRESS_FS_AVL_1260MB,
272 					.gain = ST_PRESS_KPASCAL_NANO_SCALE,
273 					.gain2 = ST_PRESS_LSB_PER_CELSIUS,
274 				},
275 			},
276 		},
277 		.bdu = {
278 			.addr = 0x20,
279 			.mask = 0x04,
280 		},
281 		.drdy_irq = {
282 			.int1 = {
283 				.addr = 0x22,
284 				.mask = 0x04,
285 				.addr_od = 0x22,
286 				.mask_od = 0x40,
287 			},
288 			.int2 = {
289 				.addr = 0x22,
290 				.mask = 0x20,
291 				.addr_od = 0x22,
292 				.mask_od = 0x40,
293 			},
294 			.addr_ihl = 0x22,
295 			.mask_ihl = 0x80,
296 			.stat_drdy = {
297 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
298 				.mask = 0x03,
299 			},
300 		},
301 		.sim = {
302 			.addr = 0x20,
303 			.value = BIT(0),
304 		},
305 		.multi_read_bit = true,
306 		.bootime = 2,
307 	},
308 	{
309 		/*
310 		 * CUSTOM VALUES FOR LPS001WP SENSOR
311 		 */
312 		.wai = 0xba,
313 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
314 		.sensors_supported = {
315 			[0] = LPS001WP_PRESS_DEV_NAME,
316 		},
317 		.ch = (struct iio_chan_spec *)st_press_lps001wp_channels,
318 		.num_ch = ARRAY_SIZE(st_press_lps001wp_channels),
319 		.odr = {
320 			.addr = 0x20,
321 			.mask = 0x30,
322 			.odr_avl = {
323 				{ .hz = 1, .value = 0x01 },
324 				{ .hz = 7, .value = 0x02 },
325 				{ .hz = 13, .value = 0x03 },
326 			},
327 		},
328 		.pw = {
329 			.addr = 0x20,
330 			.mask = 0x40,
331 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
332 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
333 		},
334 		.fs = {
335 			.fs_avl = {
336 				/*
337 				 * Pressure and temperature resolution values
338 				 * as defined in table 3 of LPS001WP datasheet.
339 				 */
340 				[0] = {
341 					.num = ST_PRESS_FS_AVL_1100MB,
342 					.gain = ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN,
343 					.gain2 = ST_PRESS_LPS001WP_LSB_PER_CELSIUS,
344 				},
345 			},
346 		},
347 		.bdu = {
348 			.addr = 0x20,
349 			.mask = 0x04,
350 		},
351 		.sim = {
352 			.addr = 0x20,
353 			.value = BIT(0),
354 		},
355 		.multi_read_bit = true,
356 		.bootime = 2,
357 	},
358 	{
359 		/*
360 		 * CUSTOM VALUES FOR LPS25H SENSOR
361 		 * See LPS25H datasheet:
362 		 * http://www2.st.com/resource/en/datasheet/lps25h.pdf
363 		 */
364 		.wai = 0xbd,
365 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
366 		.sensors_supported = {
367 			[0] = LPS25H_PRESS_DEV_NAME,
368 		},
369 		.ch = (struct iio_chan_spec *)st_press_1_channels,
370 		.num_ch = ARRAY_SIZE(st_press_1_channels),
371 		.odr = {
372 			.addr = 0x20,
373 			.mask = 0x70,
374 			.odr_avl = {
375 				{ .hz = 1, .value = 0x01 },
376 				{ .hz = 7, .value = 0x02 },
377 				{ .hz = 13, .value = 0x03 },
378 				{ .hz = 25, .value = 0x04 },
379 			},
380 		},
381 		.pw = {
382 			.addr = 0x20,
383 			.mask = 0x80,
384 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
385 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
386 		},
387 		.fs = {
388 			.fs_avl = {
389 				/*
390 				 * Pressure and temperature sensitivity values
391 				 * as defined in table 3 of LPS25H datasheet.
392 				 */
393 				[0] = {
394 					.num = ST_PRESS_FS_AVL_1260MB,
395 					.gain = ST_PRESS_KPASCAL_NANO_SCALE,
396 					.gain2 = ST_PRESS_LSB_PER_CELSIUS,
397 				},
398 			},
399 		},
400 		.bdu = {
401 			.addr = 0x20,
402 			.mask = 0x04,
403 		},
404 		.drdy_irq = {
405 			.int1 = {
406 				.addr = 0x23,
407 				.mask = 0x01,
408 				.addr_od = 0x22,
409 				.mask_od = 0x40,
410 			},
411 			.addr_ihl = 0x22,
412 			.mask_ihl = 0x80,
413 			.stat_drdy = {
414 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
415 				.mask = 0x03,
416 			},
417 		},
418 		.sim = {
419 			.addr = 0x20,
420 			.value = BIT(0),
421 		},
422 		.multi_read_bit = true,
423 		.bootime = 2,
424 	},
425 	{
426 		/*
427 		 * CUSTOM VALUES FOR LPS22HB SENSOR
428 		 * See LPS22HB datasheet:
429 		 * http://www2.st.com/resource/en/datasheet/lps22hb.pdf
430 		 */
431 		.wai = 0xb1,
432 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
433 		.sensors_supported = {
434 			[0] = LPS22HB_PRESS_DEV_NAME,
435 			[1] = LPS33HW_PRESS_DEV_NAME,
436 			[2] = LPS35HW_PRESS_DEV_NAME,
437 		},
438 		.ch = (struct iio_chan_spec *)st_press_lps22hb_channels,
439 		.num_ch = ARRAY_SIZE(st_press_lps22hb_channels),
440 		.odr = {
441 			.addr = 0x10,
442 			.mask = 0x70,
443 			.odr_avl = {
444 				{ .hz = 1, .value = 0x01 },
445 				{ .hz = 10, .value = 0x02 },
446 				{ .hz = 25, .value = 0x03 },
447 				{ .hz = 50, .value = 0x04 },
448 				{ .hz = 75, .value = 0x05 },
449 			},
450 		},
451 		.pw = {
452 			.addr = 0x10,
453 			.mask = 0x70,
454 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
455 		},
456 		.fs = {
457 			.fs_avl = {
458 				/*
459 				 * Pressure and temperature sensitivity values
460 				 * as defined in table 3 of LPS22HB datasheet.
461 				 */
462 				[0] = {
463 					.num = ST_PRESS_FS_AVL_1260MB,
464 					.gain = ST_PRESS_KPASCAL_NANO_SCALE,
465 					.gain2 = ST_PRESS_LPS22HB_LSB_PER_CELSIUS,
466 				},
467 			},
468 		},
469 		.bdu = {
470 			.addr = 0x10,
471 			.mask = 0x02,
472 		},
473 		.drdy_irq = {
474 			.int1 = {
475 				.addr = 0x12,
476 				.mask = 0x04,
477 				.addr_od = 0x12,
478 				.mask_od = 0x40,
479 			},
480 			.addr_ihl = 0x12,
481 			.mask_ihl = 0x80,
482 			.stat_drdy = {
483 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
484 				.mask = 0x03,
485 			},
486 		},
487 		.sim = {
488 			.addr = 0x10,
489 			.value = BIT(0),
490 		},
491 		.multi_read_bit = false,
492 		.bootime = 2,
493 	},
494 	{
495 		/*
496 		 * CUSTOM VALUES FOR LPS22HH SENSOR
497 		 * See LPS22HH datasheet:
498 		 * http://www2.st.com/resource/en/datasheet/lps22hh.pdf
499 		 */
500 		.wai = 0xb3,
501 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
502 		.sensors_supported = {
503 			[0] = LPS22HH_PRESS_DEV_NAME,
504 		},
505 		.ch = (struct iio_chan_spec *)st_press_lps22hb_channels,
506 		.num_ch = ARRAY_SIZE(st_press_lps22hb_channels),
507 		.odr = {
508 			.addr = 0x10,
509 			.mask = 0x70,
510 			.odr_avl = {
511 				{ .hz = 1, .value = 0x01 },
512 				{ .hz = 10, .value = 0x02 },
513 				{ .hz = 25, .value = 0x03 },
514 				{ .hz = 50, .value = 0x04 },
515 				{ .hz = 75, .value = 0x05 },
516 				{ .hz = 100, .value = 0x06 },
517 				{ .hz = 200, .value = 0x07 },
518 			},
519 		},
520 		.pw = {
521 			.addr = 0x10,
522 			.mask = 0x70,
523 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
524 		},
525 		.fs = {
526 			.fs_avl = {
527 				/*
528 				 * Pressure and temperature sensitivity values
529 				 * as defined in table 3 of LPS22HH datasheet.
530 				 */
531 				[0] = {
532 					.num = ST_PRESS_FS_AVL_1260MB,
533 					.gain = ST_PRESS_KPASCAL_NANO_SCALE,
534 					.gain2 = ST_PRESS_LPS22HB_LSB_PER_CELSIUS,
535 				},
536 			},
537 		},
538 		.bdu = {
539 			.addr = 0x10,
540 			.mask = BIT(1),
541 		},
542 		.drdy_irq = {
543 			.int1 = {
544 				.addr = 0x12,
545 				.mask = BIT(2),
546 				.addr_od = 0x11,
547 				.mask_od = BIT(5),
548 			},
549 			.addr_ihl = 0x11,
550 			.mask_ihl = BIT(6),
551 			.stat_drdy = {
552 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
553 				.mask = 0x03,
554 			},
555 		},
556 		.sim = {
557 			.addr = 0x10,
558 			.value = BIT(0),
559 		},
560 		.multi_read_bit = false,
561 		.bootime = 2,
562 	},
563 };
564 
565 static int st_press_write_raw(struct iio_dev *indio_dev,
566 			      struct iio_chan_spec const *ch,
567 			      int val,
568 			      int val2,
569 			      long mask)
570 {
571 	int err;
572 
573 	switch (mask) {
574 	case IIO_CHAN_INFO_SAMP_FREQ:
575 		if (val2)
576 			return -EINVAL;
577 		mutex_lock(&indio_dev->mlock);
578 		err = st_sensors_set_odr(indio_dev, val);
579 		mutex_unlock(&indio_dev->mlock);
580 		return err;
581 	default:
582 		return -EINVAL;
583 	}
584 }
585 
586 static int st_press_read_raw(struct iio_dev *indio_dev,
587 			struct iio_chan_spec const *ch, int *val,
588 							int *val2, long mask)
589 {
590 	int err;
591 	struct st_sensor_data *press_data = iio_priv(indio_dev);
592 
593 	switch (mask) {
594 	case IIO_CHAN_INFO_RAW:
595 		err = st_sensors_read_info_raw(indio_dev, ch, val);
596 		if (err < 0)
597 			goto read_error;
598 
599 		return IIO_VAL_INT;
600 	case IIO_CHAN_INFO_SCALE:
601 		switch (ch->type) {
602 		case IIO_PRESSURE:
603 			*val = 0;
604 			*val2 = press_data->current_fullscale->gain;
605 			return IIO_VAL_INT_PLUS_NANO;
606 		case IIO_TEMP:
607 			*val = MCELSIUS_PER_CELSIUS;
608 			*val2 = press_data->current_fullscale->gain2;
609 			return IIO_VAL_FRACTIONAL;
610 		default:
611 			err = -EINVAL;
612 			goto read_error;
613 		}
614 
615 	case IIO_CHAN_INFO_OFFSET:
616 		switch (ch->type) {
617 		case IIO_TEMP:
618 			*val = ST_PRESS_MILLI_CELSIUS_OFFSET *
619 			       press_data->current_fullscale->gain2;
620 			*val2 = MCELSIUS_PER_CELSIUS;
621 			break;
622 		default:
623 			err = -EINVAL;
624 			goto read_error;
625 		}
626 
627 		return IIO_VAL_FRACTIONAL;
628 	case IIO_CHAN_INFO_SAMP_FREQ:
629 		*val = press_data->odr;
630 		return IIO_VAL_INT;
631 	default:
632 		return -EINVAL;
633 	}
634 
635 read_error:
636 	return err;
637 }
638 
639 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
640 
641 static struct attribute *st_press_attributes[] = {
642 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
643 	NULL,
644 };
645 
646 static const struct attribute_group st_press_attribute_group = {
647 	.attrs = st_press_attributes,
648 };
649 
650 static const struct iio_info press_info = {
651 	.attrs = &st_press_attribute_group,
652 	.read_raw = &st_press_read_raw,
653 	.write_raw = &st_press_write_raw,
654 	.debugfs_reg_access = &st_sensors_debugfs_reg_access,
655 };
656 
657 #ifdef CONFIG_IIO_TRIGGER
658 static const struct iio_trigger_ops st_press_trigger_ops = {
659 	.set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
660 	.validate_device = st_sensors_validate_device,
661 };
662 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
663 #else
664 #define ST_PRESS_TRIGGER_OPS NULL
665 #endif
666 
667 int st_press_common_probe(struct iio_dev *indio_dev)
668 {
669 	struct st_sensor_data *press_data = iio_priv(indio_dev);
670 	struct st_sensors_platform_data *pdata =
671 		(struct st_sensors_platform_data *)press_data->dev->platform_data;
672 	int irq = press_data->get_irq_data_ready(indio_dev);
673 	int err;
674 
675 	indio_dev->modes = INDIO_DIRECT_MODE;
676 	indio_dev->info = &press_info;
677 	mutex_init(&press_data->tb.buf_lock);
678 
679 	err = st_sensors_power_enable(indio_dev);
680 	if (err)
681 		return err;
682 
683 	err = st_sensors_check_device_support(indio_dev,
684 					ARRAY_SIZE(st_press_sensors_settings),
685 					st_press_sensors_settings);
686 	if (err < 0)
687 		goto st_press_power_off;
688 
689 	/*
690 	 * Skip timestamping channel while declaring available channels to
691 	 * common st_sensor layer. Look at st_sensors_get_buffer_element() to
692 	 * see how timestamps are explicitly pushed as last samples block
693 	 * element.
694 	 */
695 	press_data->num_data_channels = press_data->sensor_settings->num_ch - 1;
696 	press_data->multiread_bit = press_data->sensor_settings->multi_read_bit;
697 	indio_dev->channels = press_data->sensor_settings->ch;
698 	indio_dev->num_channels = press_data->sensor_settings->num_ch;
699 
700 	press_data->current_fullscale =
701 		(struct st_sensor_fullscale_avl *)
702 			&press_data->sensor_settings->fs.fs_avl[0];
703 
704 	press_data->odr = press_data->sensor_settings->odr.odr_avl[0].hz;
705 
706 	/* Some devices don't support a data ready pin. */
707 	if (!pdata && (press_data->sensor_settings->drdy_irq.int1.addr ||
708 		       press_data->sensor_settings->drdy_irq.int2.addr))
709 		pdata =	(struct st_sensors_platform_data *)&default_press_pdata;
710 
711 	err = st_sensors_init_sensor(indio_dev, pdata);
712 	if (err < 0)
713 		goto st_press_power_off;
714 
715 	err = st_press_allocate_ring(indio_dev);
716 	if (err < 0)
717 		goto st_press_power_off;
718 
719 	if (irq > 0) {
720 		err = st_sensors_allocate_trigger(indio_dev,
721 						  ST_PRESS_TRIGGER_OPS);
722 		if (err < 0)
723 			goto st_press_probe_trigger_error;
724 	}
725 
726 	err = iio_device_register(indio_dev);
727 	if (err)
728 		goto st_press_device_register_error;
729 
730 	dev_info(&indio_dev->dev, "registered pressure sensor %s\n",
731 		 indio_dev->name);
732 
733 	return err;
734 
735 st_press_device_register_error:
736 	if (irq > 0)
737 		st_sensors_deallocate_trigger(indio_dev);
738 st_press_probe_trigger_error:
739 	st_press_deallocate_ring(indio_dev);
740 st_press_power_off:
741 	st_sensors_power_disable(indio_dev);
742 
743 	return err;
744 }
745 EXPORT_SYMBOL(st_press_common_probe);
746 
747 void st_press_common_remove(struct iio_dev *indio_dev)
748 {
749 	struct st_sensor_data *press_data = iio_priv(indio_dev);
750 
751 	st_sensors_power_disable(indio_dev);
752 
753 	iio_device_unregister(indio_dev);
754 	if (press_data->get_irq_data_ready(indio_dev) > 0)
755 		st_sensors_deallocate_trigger(indio_dev);
756 
757 	st_press_deallocate_ring(indio_dev);
758 }
759 EXPORT_SYMBOL(st_press_common_remove);
760 
761 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
762 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
763 MODULE_LICENSE("GPL v2");
764