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